glossary_repository.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace Glossary;
  3. use Database;
  4. /**
  5. * Glossary entry 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 GlossaryRepository
  11. {
  12. /**
  13. * Return the instance of the repository.
  14. *
  15. * @return \Glossary\GlossaryRepository
  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. *
  28. * @param string $where Where filter to apply
  29. * @return array
  30. */
  31. public function find($where, $orderby = '', $limit = null)
  32. {
  33. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  34. $table = Database::get_course_table(TABLE_GLOSSARY);
  35. $tool = TOOL_GLOSSARY;
  36. $sql = "SELECT g.*,
  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 AS g,
  53. $table_item_property AS prop
  54. WHERE
  55. (g.glossary_id = prop.ref AND
  56. g.c_id = prop.c_id AND
  57. prop.tool = '$tool')";
  58. $sql .= $where ? "AND ($where)" : '';
  59. $sql .= ' ORDER BY ';
  60. $sql .= $orderby ? $orderby : 'name ASC';
  61. if($count){
  62. $from = (int)$limit->from;
  63. $count = (int)$limit->count;
  64. $sql .= " LIMIT $from, $count";
  65. }
  66. $rs = Database :: query($sql);
  67. while ($data = Database::fetch_object($rs)) {
  68. $result[] = Glossary::create($data);
  69. }
  70. return $result;
  71. }
  72. public function count($where)
  73. {
  74. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  75. $table = Database::get_course_table(TABLE_GLOSSARY);
  76. $tool = TOOL_GLOSSARY;
  77. $sql = "SELECT count(*) AS val
  78. FROM
  79. $table AS g,
  80. $table_item_property AS prop
  81. WHERE
  82. (g.glossary_id = prop.ref AND
  83. g.c_id = prop.c_id AND
  84. prop.tool = '$tool')";
  85. $sql .= $where ? "AND ($where)" : '';
  86. $sql .= " ORDER BY name ASC";
  87. $rs = Database :: query($sql);
  88. while ($data = Database::fetch_object($rs)) {
  89. return $data->val;
  90. }
  91. return 0;
  92. }
  93. /**
  94. *
  95. * @param string $where
  96. * @return \Glossary\Glossary
  97. */
  98. public function find_one($where)
  99. {
  100. $items = $this->find($where);
  101. foreach ($items as $item) {
  102. return $item;
  103. }
  104. return null;
  105. }
  106. /**
  107. * Retrieve one course description from its ids.
  108. *
  109. * @param int|Course $c_id
  110. * @param int $id
  111. * @return \Glossary\Glossary
  112. */
  113. public function find_one_by_id($c_id, $id)
  114. {
  115. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  116. return $this->find_one("g.c_id = $c_id AND g.glossary_id = $id");
  117. }
  118. /**
  119. * Retrieve one course description from its ids.
  120. *
  121. * @param int|Course $c_id
  122. * @param string name
  123. * @return \Glossary\Glossary
  124. */
  125. public function find_one_by_course_and_name($c_id, $name)
  126. {
  127. $c_id = is_object($c_id) ? $c_id->get_id() : (int) $c_id;
  128. $name = Database::escape_string($name);
  129. return $this->find_one("g.c_id = $c_id AND g.name = '$name'");
  130. }
  131. /**
  132. * Returns the list of course descriptions belonging to a specific course and
  133. * session.
  134. *
  135. * @param object $course
  136. * @return Array
  137. */
  138. public function find_by_course($course, $orderby = '', $limit = null)
  139. {
  140. $c_id = (int)$course->c_id;
  141. $session_id = isset($course->session_id) ? (int)$course->session_id : 0;
  142. if (empty($c_id)) {
  143. return array();
  144. }
  145. $condition_session = api_get_session_condition($session_id, true, true);
  146. $where = "g.c_id = $c_id $condition_session";
  147. return $this->find($where, $orderby, $limit);
  148. }
  149. public function count_by_course($course)
  150. {
  151. $c_id = (int)$course->c_id;
  152. $session_id = isset($course->session_id) ? (int)$course->session_id : 0;
  153. if (empty($c_id)) {
  154. return 0;
  155. }
  156. $condition_session = api_get_session_condition($session_id, true, true);
  157. $where = "g.c_id = $c_id $condition_session";
  158. return $this->count($where);
  159. }
  160. /**
  161. *
  162. * @param object $glossary
  163. * @return bool
  164. */
  165. public function save($glossary)
  166. {
  167. $id = $glossary->id;
  168. if (empty($id)) {
  169. return $this->insert($glossary);
  170. } else {
  171. return $this->update($glossary);
  172. }
  173. }
  174. function next_display_order($c_id)
  175. {
  176. $table = Database :: get_course_table(TABLE_GLOSSARY);
  177. $sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
  178. $rs = Database :: query($sql);
  179. list ($result) = Database :: fetch_row($rs);
  180. $result = intval($result) + 1;
  181. return $result;
  182. }
  183. /**
  184. *
  185. * @param \Glossary\Glossary $glossary
  186. * @return bool
  187. */
  188. public function insert($glossary)
  189. {
  190. $c_id = (int) $glossary->c_id;
  191. $name = trim($glossary->name);
  192. $name = Database::escape_string($name);
  193. $description = trim($glossary->description);
  194. $description = Database::escape_string($description);
  195. $session_id = (int) $glossary->session_id;
  196. $session_id = $session_id ? $session_id : '0';
  197. $display_order = $this->next_display_order($c_id);
  198. $table = Database :: get_course_table(TABLE_GLOSSARY);
  199. $sql = "INSERT INTO $table
  200. (c_id, name, description, display_order, session_id)
  201. VALUES
  202. ($c_id , '$name', '$description', $display_order, $session_id)";
  203. $result = (bool) Database :: query($sql);
  204. if ($result) {
  205. $id = Database::insert_id();
  206. $glossary->id = $id;
  207. $_course = api_get_course_info_by_id($c_id);
  208. $tool = TOOL_GLOSSARY;
  209. $user_id = api_get_user_id();
  210. api_item_property_update($_course, $tool, $id, 'GlossaryAdded', $user_id);
  211. }
  212. return $result;
  213. }
  214. /**
  215. *
  216. * @param \Glossary\Glossary $glossary
  217. * @return bool
  218. */
  219. function update($glossary)
  220. {
  221. $c_id = (int) $glossary->c_id;
  222. $id = (int) $glossary->id;
  223. $name = trim($glossary->name);
  224. $name = Database::escape_string($name);
  225. $description = trim($glossary->description);
  226. $description = Database::escape_string($description);
  227. $session_id = (int) $glossary->session_id;
  228. $session_id = $session_id ? $session_id : '0';
  229. $display_order = (int) $glossary->display_order;
  230. $table = Database :: get_course_table(TABLE_GLOSSARY);
  231. $sql = "UPDATE $table SET
  232. name = '$name',
  233. description = '$description',
  234. display_order = $display_order,
  235. session_id = $session_id
  236. WHERE
  237. c_id = $c_id AND
  238. glossary_id = $id";
  239. $result = (bool) Database :: query($sql);
  240. if ($result) {
  241. $_course = api_get_course_info_by_id($c_id);
  242. $tool = TOOL_GLOSSARY;
  243. $user_id = api_get_user_id();
  244. api_item_property_update($_course, $tool, $id, 'GlossaryUpdated', $user_id);
  245. }
  246. return $result;
  247. }
  248. /**
  249. *
  250. * @param object $glossary
  251. * @return boolean
  252. */
  253. public function remove($glossary)
  254. {
  255. $table = Database :: get_course_table(TABLE_GLOSSARY);
  256. $c_id = (int) $glossary->c_id;
  257. $id = (int) $glossary->id;
  258. if (empty($c_id) || empty($id)) {
  259. return false;
  260. }
  261. $sql = "DELETE FROM $table WHERE c_id=$c_id AND glossary_id=$id";
  262. $result = Database :: query($sql);
  263. if ($result) {
  264. $tool = TOOL_GLOSSARY;
  265. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  266. $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
  267. Database :: query($sql);
  268. }
  269. return (bool) $result;
  270. }
  271. /**
  272. *
  273. * @param object $course
  274. * @return int
  275. */
  276. public function remove_by_course($course)
  277. {
  278. $items = $this->find_by_course($course);
  279. foreach ($items as $item) {
  280. $success = $this->remove($item);
  281. if ($success) {
  282. $result++;
  283. }
  284. }
  285. return $result;
  286. }
  287. }