document.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace Model;
  3. use Database;
  4. use ResultSet;
  5. /**
  6. * Represent a database "document" object - i.e. a file or a folder.
  7. *
  8. * Note:
  9. *
  10. * Each database column is mapped to a property.
  11. *
  12. * The item_property table is available through its own property but is loaded
  13. * alongside document data.
  14. *
  15. * Some db query functions exists in this class and would need to be adapted
  16. * to Symphony once it is moved to production. Yet the object structure should
  17. * stay.
  18. *
  19. * @see \Model\ItemProperty
  20. * @see table c_document
  21. * @license see /license.txt
  22. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  23. */
  24. class Document
  25. {
  26. /**
  27. *
  28. * @param string $where
  29. * @return \ResultSet
  30. */
  31. public static function query($where)
  32. {
  33. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  34. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  35. $tool = TOOL_DOCUMENT;
  36. $sql = "SELECT doc.*,
  37. prop.id AS property_id,
  38. prop.tool,
  39. prop.insert_user_id,
  40. prop.insert_date,
  41. prop.lastedit_date,
  42. prop.ref,
  43. prop.lastedit_type,
  44. prop.lastedit_user_id,
  45. prop.to_group_id,
  46. prop.to_user_id,
  47. prop.visibility,
  48. prop.start_visible,
  49. prop.end_visible,
  50. prop.id_session
  51. FROM
  52. $table_document AS doc,
  53. $table_item_property AS prop
  54. WHERE
  55. (doc.id = prop.ref AND
  56. doc.c_id = prop.c_id AND
  57. prop.tool = '$tool')";
  58. $sql .= $where ? "AND ($where)" : '';
  59. $result = new ResultSet($sql);
  60. return $result->return_type(__CLASS__);
  61. }
  62. /**
  63. *
  64. * @param int|Course $c_id
  65. * @param int $id
  66. * @return \Model\Document
  67. */
  68. public static function get_by_id($c_id, $id)
  69. {
  70. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  71. return self::query("doc.c_id = $c_id AND doc.id = $id")->first();
  72. }
  73. /**
  74. *
  75. * @param int|Course $c_id
  76. * @param int $id
  77. * @return \Model\Document
  78. */
  79. public static function get_by_path($c_id, $path)
  80. {
  81. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  82. return self::query("doc.c_id = $c_id AND doc.path = '$path'")->first();
  83. }
  84. protected $c_id = 0;
  85. protected $id = 0;
  86. protected $path = '';
  87. protected $comment = '';
  88. protected $title = '';
  89. protected $filetype = '';
  90. protected $size = 0;
  91. protected $readonly = false;
  92. protected $session_id = 0;
  93. protected $course = null;
  94. protected $item_property = null;
  95. public function __construct($data)
  96. {
  97. $data = (object) $data;
  98. $this->c_id = (int) $data->c_id;
  99. $this->id = (int) $data->id;
  100. $this->path = $data->path;
  101. $this->comment = $data->comment;
  102. $this->title = $data->title;
  103. $this->filetype = $data->filetype;
  104. $this->size = (int) $data->size;
  105. $this->readonly = (bool) $data->readonly;
  106. $this->session_id = (int) $data->session_id;
  107. $this->course = null;
  108. if (isset($data->property_id)) {
  109. $property = (array) $data;
  110. $property = (object) $property;
  111. $property->id = $property->property_id;
  112. $this->item_property = ItemProperty::create($property);
  113. } else {
  114. $this->item_property = null;
  115. }
  116. }
  117. public function get_c_id()
  118. {
  119. return $this->c_id;
  120. }
  121. public function set_c_id($value)
  122. {
  123. $this->c_id = (int) $value;
  124. $this->course = null;
  125. $this->item_property = null;
  126. }
  127. public function get_id()
  128. {
  129. return $this->id;
  130. }
  131. public function set_id($value)
  132. {
  133. $this->id = (int) $value;
  134. $this->item_property = null;
  135. }
  136. public function get_path()
  137. {
  138. return $this->path;
  139. }
  140. public function set_path($value)
  141. {
  142. $this->path = $value;
  143. }
  144. public function get_comment()
  145. {
  146. return $this->comment;
  147. }
  148. public function set_comment($value)
  149. {
  150. $this->comment = $value;
  151. }
  152. public function get_title()
  153. {
  154. return $this->title;
  155. }
  156. public function set_title($value)
  157. {
  158. $this->title = $value;
  159. }
  160. public function get_filetype()
  161. {
  162. return $this->filetype;
  163. }
  164. public function set_filetype($value)
  165. {
  166. $this->filetype = $value;
  167. }
  168. public function get_size()
  169. {
  170. return $this->size;
  171. }
  172. public function set_size($value)
  173. {
  174. $this->size = (int) $value;
  175. }
  176. public function get_readonly()
  177. {
  178. return $this->readonly;
  179. }
  180. public function set_readonly($value)
  181. {
  182. $this->readonly = (bool) $value;
  183. }
  184. public function get_session_id()
  185. {
  186. return $this->session_id;
  187. }
  188. public function set_session_id($value)
  189. {
  190. $this->session_id = (int) $value;
  191. }
  192. public function is_folder()
  193. {
  194. return $this->filetype == 'folder';
  195. }
  196. public function is_file()
  197. {
  198. return $this->filetype == 'file';
  199. }
  200. public function is_visible()
  201. {
  202. $this->get_item_property()->get_visibility() == 1;
  203. }
  204. public function is_accessible()
  205. {
  206. return api_is_allowed_to_edit() || $this->is_visible();
  207. }
  208. /**
  209. *
  210. * @return \Model\Course
  211. */
  212. public function get_course()
  213. {
  214. if ($this->course && $this->course->get_id() == $this->c_id) {
  215. return $this->course;
  216. }
  217. $this->course = Course::get_by_id($this->c_id);
  218. return $this->course;
  219. }
  220. /**
  221. *
  222. * @return \Model\ItemProperty
  223. */
  224. public function get_item_property()
  225. {
  226. if ($this->item_property && $this->item_property->get_c_id() == $this->c_id && $this->item_property->get_ref() == $this->id) {
  227. return $this->item_property;
  228. }
  229. $this->item_property = ItemProperty::get_by_ref($this->id, TOOL_DOCUMENT);
  230. return $this->item_property;
  231. }
  232. public function get_absolute_path()
  233. {
  234. $course = $this->get_course();
  235. return $course->get_path() . 'document' . $this->path;
  236. }
  237. public function __toString()
  238. {
  239. return $this->get_absolute_path();
  240. }
  241. /**
  242. *
  243. * @param bool $all
  244. * @return ResultSet|array
  245. */
  246. public function get_children($all = false)
  247. {
  248. if (!$this->is_folder()) {
  249. return array();
  250. }
  251. $path = $this->path;
  252. $c_id = $this->c_id;
  253. if ($this->all) {
  254. $where = "doc.c_id = $c_id AND doc.path LIKE '$path/%'";
  255. } else {
  256. $where = "doc.c_id = $c_id AND doc.path LIKE '$path/%' AND doc.path NOT LIKE '$path/%/%'";
  257. }
  258. return self::query($where);
  259. }
  260. }