notebook_repository.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace Notebook;
  3. use Database;
  4. /**
  5. * Notebook repository. Interface with the database.
  6. *
  7. * @author Laurent Opprecht <laurent@opprecht.info> for the University of Geneva
  8. * @licence /license.txt
  9. */
  10. class NotebookRepository
  11. {
  12. /**
  13. * Return the instance of the repository.
  14. *
  15. * @return \Notebook\NotebookRepository
  16. */
  17. public static function instance()
  18. {
  19. static $result = null;
  20. if (empty($result)) {
  21. $result = new self();
  22. }
  23. return $result;
  24. }
  25. /**
  26. *
  27. * @return string
  28. */
  29. public function get_table()
  30. {
  31. return Database :: get_course_table(TABLE_NOTEBOOK);
  32. }
  33. /**
  34. *
  35. * @return string
  36. */
  37. public function get_tool()
  38. {
  39. return TOOL_NOTEBOOK;
  40. }
  41. /**
  42. *
  43. *
  44. * @param string $where Where filter to apply
  45. * @return array
  46. */
  47. public function find($where, $orderby = '', $limit = null)
  48. {
  49. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  50. $table = $this->get_table();
  51. $tool = $this->get_tool();
  52. $sql = "SELECT n.*,
  53. prop.id AS property_id,
  54. prop.tool,
  55. prop.insert_user_id,
  56. prop.insert_date,
  57. prop.lastedit_date,
  58. prop.ref,
  59. prop.lastedit_type,
  60. prop.lastedit_user_id,
  61. prop.to_group_id,
  62. prop.to_user_id,
  63. prop.visibility,
  64. prop.start_visible,
  65. prop.end_visible,
  66. prop.id_session
  67. FROM
  68. $table AS n,
  69. $table_item_property AS prop
  70. WHERE
  71. (n.notebook_id = prop.ref AND
  72. n.c_id = prop.c_id AND
  73. prop.tool = '$tool')";
  74. $sql .= $where ? "AND ($where)" : '';
  75. $sql .= ' ORDER BY ';
  76. $sql .= $orderby ? $orderby : 'creation_date ASC';
  77. if ($limit) {
  78. $from = (int) $limit->from;
  79. $count = (int) $limit->count;
  80. $sql .= " LIMIT $from, $count";
  81. }
  82. $rs = Database :: query($sql);
  83. while ($data = Database::fetch_object($rs)) {
  84. $result[] = Notebook::create($data);
  85. }
  86. return $result;
  87. }
  88. public function count($where)
  89. {
  90. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  91. $table = $this->get_table();
  92. $tool = $this->get_tool();
  93. $sql = "SELECT count(*) AS val
  94. FROM
  95. $table AS n,
  96. $table_item_property AS prop
  97. WHERE
  98. (n.notebook_id = prop.ref AND
  99. n.c_id = prop.c_id AND
  100. prop.tool = '$tool')";
  101. $sql .= $where ? "AND ($where)" : '';
  102. $sql .= " ORDER BY name ASC";
  103. $rs = Database :: query($sql);
  104. while ($data = Database::fetch_object($rs)) {
  105. return $data->val;
  106. }
  107. return 0;
  108. }
  109. /**
  110. *
  111. * @param string $where
  112. * @return \Notebook\notebook_id
  113. */
  114. public function find_one($where)
  115. {
  116. $items = $this->find($where);
  117. foreach ($items as $item) {
  118. return $item;
  119. }
  120. return null;
  121. }
  122. /**
  123. * Retrieve one course description from its ids.
  124. *
  125. * @param int|Course $c_id
  126. * @param int $id
  127. * @return \notebook_id\notebook_id
  128. */
  129. public function find_one_by_id($c_id, $id)
  130. {
  131. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  132. return $this->find_one("n.c_id = $c_id AND n.notebook_id = $id");
  133. }
  134. /**
  135. * Retrieve one course description from its ids.
  136. *
  137. * @param int|Course $c_id
  138. * @param string name
  139. * @return \Notebook\Notebook
  140. */
  141. public function find_one_by_course_and_title($c_id, $title)
  142. {
  143. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  144. return $this->find_one("n.c_id = $c_id AND n.title = '$title'");
  145. }
  146. /**
  147. * Returns the list of notes belonging to a specific course and
  148. * session.
  149. *
  150. * @param object $course
  151. * @return Array
  152. */
  153. public function find_by_course($course, $orderby = '', $limit = null)
  154. {
  155. $c_id = (int) $course->c_id;
  156. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  157. if (empty($c_id)) {
  158. return array();
  159. }
  160. $condition_session = api_get_session_condition($session_id);
  161. $where = "n.c_id = $c_id $condition_session";
  162. return $this->find($where, $orderby, $limit);
  163. }
  164. /**
  165. * Returns the list of notes belonging to a specific course and
  166. * session.
  167. *
  168. * @param object $course
  169. * @return Array
  170. */
  171. public function find_by_course_and_user($course, $user, $orderby = '', $limit = null)
  172. {
  173. $user_id = is_object($user) ? (int)$user->user_id : (int)$user;
  174. $c_id = (int) $course->c_id;
  175. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  176. if (empty($c_id)) {
  177. return array();
  178. }
  179. $condition_session = api_get_session_condition($session_id);
  180. $where = "user_id = $user_id AND n.c_id = $c_id $condition_session";
  181. return $this->find($where, $orderby, $limit);
  182. }
  183. public function count_by_course($course)
  184. {
  185. $c_id = (int) $course->c_id;
  186. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  187. if (empty($c_id)) {
  188. return 0;
  189. }
  190. $condition_session = api_get_session_condition($session_id);
  191. $where = "n.c_id = $c_id $condition_session";
  192. return $this->count($where);
  193. }
  194. /**
  195. *
  196. * @param object \Notebook\Notebook
  197. * @return bool
  198. */
  199. public function save($item)
  200. {
  201. $id = $item->id;
  202. if (empty($id)) {
  203. return $this->insert($item);
  204. } else {
  205. return $this->update($item);
  206. }
  207. }
  208. /**
  209. *
  210. * @param \Notebook\Notebook $item
  211. * @return bool
  212. */
  213. public function insert($item)
  214. {
  215. $c_id = (int) $item->c_id;
  216. $user_id = (int)$item->user_id;
  217. $user_id = $user_id ? $user_id : api_get_user_id();
  218. $_course = api_get_course_info_by_id($c_id);
  219. $course = $_course['code'];
  220. $session_id = (int) $item->session_id;
  221. $session_id = $session_id ? $session_id : api_get_session_id();
  222. $title = trim($item->title);
  223. $title = Database::escape_string($title);
  224. $description = trim($item->description);
  225. $description = Database::escape_string($description);
  226. $now = time();
  227. $creation_date = date('Y-m-d H:i:s', $now);
  228. $update_date = $creation_date;
  229. $status = (int)$item->status;
  230. $status = $status ? $status : '0';
  231. $table = $this->get_table();
  232. $sql = "INSERT INTO $table
  233. (c_id, user_id, course, session_id, title, description, creation_date, update_date, status)
  234. VALUES
  235. ($c_id, $user_id, '$course', $session_id, '$title', '$description', '$creation_date', '$update_date', $status)";
  236. $result = (bool) Database :: query($sql);
  237. if ($result) {
  238. $id = Database::insert_id();
  239. $item->id = $id;
  240. $item->creation_date = $creation_date;
  241. $item->update_date = $update_date;
  242. $item->status = (int)$status;
  243. $item->course = $course;
  244. $item->session_id = $session_id;
  245. $item->user_id = $user_id;
  246. //$_course = api_get_course_info_by_id($c_id);
  247. $tool = $this->get_tool();
  248. //$user_id = api_get_user_id();
  249. api_item_property_update($_course, $tool, $id, 'NotebookAdded', $user_id);
  250. }
  251. return $result;
  252. }
  253. /**
  254. *
  255. * @param \Notebook\Notebook $item
  256. * @return bool
  257. */
  258. function update($item)
  259. {
  260. $c_id = (int) $item->c_id;
  261. $id = (int) $item->id;
  262. $user_id = (int)$item->user_id;
  263. $user_id = $user_id ? $user_id : api_get_user_id();
  264. $title = trim($item->title);
  265. $title = Database::escape_string($title);
  266. $description = trim($item->description);
  267. $description = Database::escape_string($description);
  268. $session_id = (int) $item->session_id;
  269. $session_id = $session_id ? $session_id : '0';
  270. $creation_date = $item->creation_date;
  271. $creation_date = is_string($creation_date) ? strtotime($creation_date) : $creation_date;
  272. $creation_date = date('Y-m-d H:i:s', $creation_date);
  273. $now = time();
  274. $creation_date = date('Y-m-d H:i:s', $now);
  275. $update_date = $creation_date;
  276. $status = (int)$item->status;
  277. $status = $status ? $status : '0';
  278. $table = $this->get_table();
  279. $sql = "UPDATE $table SET
  280. user_id = $user_id,
  281. session_id = $session_id,
  282. title = '$title',
  283. description = '$description',
  284. update_date = '$update_date',
  285. status = $status
  286. WHERE
  287. c_id = $c_id AND
  288. notebook_id = $id";
  289. $result = (bool) Database :: query($sql);
  290. if ($result) {
  291. $item->update_date = $update_date;
  292. $_course = api_get_course_info_by_id($c_id);
  293. $tool = $this->get_tool();
  294. //$user_id = api_get_user_id();
  295. api_item_property_update($_course, $tool, $id, 'NotebookUpdated', $user_id);
  296. }
  297. return $result;
  298. }
  299. /**
  300. *
  301. * @param object $item
  302. * @return boolean
  303. */
  304. public function remove($item)
  305. {
  306. $table = $this->get_table();
  307. $c_id = (int) $item->c_id;
  308. $id = (int) $item->id;
  309. if (empty($c_id) || empty($id)) {
  310. return false;
  311. }
  312. $sql = "DELETE FROM $table WHERE c_id=$c_id AND notebook_id=$id";
  313. $result = Database :: query($sql);
  314. if ($result) {
  315. $tool = $this->get_tool();
  316. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  317. $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
  318. Database :: query($sql);
  319. }
  320. return (bool) $result;
  321. }
  322. /**
  323. *
  324. * @param object $course
  325. * @return int
  326. */
  327. public function remove_by_course($course)
  328. {
  329. $items = $this->find_by_course($course);
  330. foreach ($items as $item) {
  331. $success = $this->remove($item);
  332. if ($success) {
  333. $result++;
  334. }
  335. }
  336. return $result;
  337. }
  338. }