notebook_repository.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. $name = Database::escape_string($name);
  145. return $this->find_one("n.c_id = $c_id AND n.title = '$title'");
  146. }
  147. /**
  148. * Returns the list of notes belonging to a specific course and
  149. * session.
  150. *
  151. * @param object $course
  152. * @return Array
  153. */
  154. public function find_by_course($course, $orderby = '', $limit = null)
  155. {
  156. $c_id = (int) $course->c_id;
  157. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  158. if (empty($c_id)) {
  159. return array();
  160. }
  161. $condition_session = api_get_session_condition($session_id);
  162. $where = "n.c_id = $c_id $condition_session";
  163. return $this->find($where, $orderby, $limit);
  164. }
  165. /**
  166. * Returns the list of notes belonging to a specific course and
  167. * session.
  168. *
  169. * @param object $course
  170. * @return Array
  171. */
  172. public function find_by_course_and_user($course, $user, $orderby = '', $limit = null)
  173. {
  174. $user_id = is_object($user) ? (int)$user->user_id : (int)$user;
  175. $c_id = (int) $course->c_id;
  176. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  177. if (empty($c_id)) {
  178. return array();
  179. }
  180. $condition_session = api_get_session_condition($session_id);
  181. $where = "user_id = $user_id AND n.c_id = $c_id $condition_session";
  182. return $this->find($where, $orderby, $limit);
  183. }
  184. public function count_by_course($course)
  185. {
  186. $c_id = (int) $course->c_id;
  187. $session_id = isset($course->session_id) ? (int) $course->session_id : 0;
  188. if (empty($c_id)) {
  189. return 0;
  190. }
  191. $condition_session = api_get_session_condition($session_id);
  192. $where = "n.c_id = $c_id $condition_session";
  193. return $this->count($where);
  194. }
  195. /**
  196. *
  197. * @param object \Notebook\Notebook
  198. * @return bool
  199. */
  200. public function save($item)
  201. {
  202. $id = $item->id;
  203. if (empty($id)) {
  204. return $this->insert($item);
  205. } else {
  206. return $this->update($item);
  207. }
  208. }
  209. /**
  210. *
  211. * @param \Notebook\Notebook $item
  212. * @return bool
  213. */
  214. public function insert($item)
  215. {
  216. $c_id = (int) $item->c_id;
  217. $user_id = (int)$item->user_id;
  218. $user_id = $user_id ? $user_id : api_get_user_id();
  219. $_course = api_get_course_info_by_id($c_id);
  220. $course = $_course['code'];
  221. $session_id = (int) $item->session_id;
  222. $session_id = $session_id ? $session_id : api_get_session_id();
  223. $title = trim($item->title);
  224. $title = Database::escape_string($title);
  225. $description = trim($item->description);
  226. $description = Database::escape_string($description);
  227. $now = time();
  228. $creation_date = date('Y-m-d H:i:s', $now);
  229. $update_date = $creation_date;
  230. $status = (int)$item->status;
  231. $status = $status ? $status : '0';
  232. $table = $this->get_table();
  233. $sql = "INSERT INTO $table
  234. (c_id, user_id, course, session_id, title, description, creation_date, update_date, status)
  235. VALUES
  236. ($c_id, $user_id, '$course', $session_id, '$title', '$description', '$creation_date', '$update_date', $status)";
  237. $result = (bool) Database :: query($sql);
  238. if ($result) {
  239. $id = Database::insert_id();
  240. $item->id = $id;
  241. $item->creation_date = $creation_date;
  242. $item->update_date = $update_date;
  243. $item->status = (int)$status;
  244. $item->course = $course;
  245. $item->session_id = $session_id;
  246. $item->user_id = $user_id;
  247. //$_course = api_get_course_info_by_id($c_id);
  248. $tool = $this->get_tool();
  249. //$user_id = api_get_user_id();
  250. api_item_property_update($_course, $tool, $id, 'NotebookAdded', $user_id);
  251. }
  252. return $result;
  253. }
  254. /**
  255. *
  256. * @param \Notebook\Notebook $item
  257. * @return bool
  258. */
  259. function update($item)
  260. {
  261. $c_id = (int) $item->c_id;
  262. $id = (int) $item->id;
  263. $user_id = (int)$item->user_id;
  264. $user_id = $user_id ? $user_id : api_get_user_id();
  265. $title = trim($item->title);
  266. $title = Database::escape_string($title);
  267. $description = trim($item->description);
  268. $description = Database::escape_string($description);
  269. $session_id = (int) $item->session_id;
  270. $session_id = $session_id ? $session_id : '0';
  271. $creation_date = $item->creation_date;
  272. $creation_date = is_string($creation_date) ? strtotime($creation_date) : $creation_date;
  273. $creation_date = date('Y-m-d H:i:s', $creation_date);
  274. $now = time();
  275. $creation_date = date('Y-m-d H:i:s', $now);
  276. $update_date = $creation_date;
  277. $status = (int)$item->status;
  278. $status = $status ? $status : '0';
  279. $table = $this->get_table();
  280. $sql = "UPDATE $table SET
  281. user_id = $user_id,
  282. session_id = $session_id,
  283. title = '$title',
  284. description = '$description',
  285. update_date = '$update_date',
  286. status = $status
  287. WHERE
  288. c_id = $c_id AND
  289. notebook_id = $id";
  290. $result = (bool) Database :: query($sql);
  291. if ($result) {
  292. $item->update_date = $update_date;
  293. $_course = api_get_course_info_by_id($c_id);
  294. $tool = $this->get_tool();
  295. //$user_id = api_get_user_id();
  296. api_item_property_update($_course, $tool, $id, 'NotebookUpdated', $user_id);
  297. }
  298. return $result;
  299. }
  300. /**
  301. *
  302. * @param object $item
  303. * @return boolean
  304. */
  305. public function remove($item)
  306. {
  307. $table = $this->get_table();
  308. $c_id = (int) $item->c_id;
  309. $id = (int) $item->id;
  310. if (empty($c_id) || empty($id)) {
  311. return false;
  312. }
  313. $sql = "DELETE FROM $table WHERE c_id=$c_id AND notebook_id=$id";
  314. $result = Database :: query($sql);
  315. if ($result) {
  316. $tool = $this->get_tool();
  317. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  318. $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
  319. Database :: query($sql);
  320. }
  321. return (bool) $result;
  322. }
  323. /**
  324. *
  325. * @param object $course
  326. * @return int
  327. */
  328. public function remove_by_course($course)
  329. {
  330. $items = $this->find_by_course($course);
  331. foreach ($items as $item) {
  332. $success = $this->remove($item);
  333. if ($success) {
  334. $result++;
  335. }
  336. }
  337. return $result;
  338. }
  339. }