glossary_repository.class.php 9.4 KB

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