link_category_repository.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Link category repository class definition
  5. * @package chamilo.link
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace Link;
  11. use Database;
  12. /**
  13. * Database interface for link_category
  14. *
  15. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  16. * @license /license.txt
  17. */
  18. class LinkCategoryRepository
  19. {
  20. /**
  21. *
  22. * @return \Link\LinkCategoryRepository
  23. */
  24. public static function instance()
  25. {
  26. static $result = null;
  27. if (empty($result)) {
  28. $result = new self();
  29. }
  30. return $result;
  31. }
  32. public function save($category)
  33. {
  34. $id = $category->id;
  35. if (empty($id)) {
  36. return $this->insert($category);
  37. } else {
  38. return $this->update($category);
  39. }
  40. }
  41. public function insert($category)
  42. {
  43. $c_id = (int) $category->c_id;
  44. $session_id = (int) $category->session_id;
  45. $session_id = $session_id ? $session_id : '0';
  46. $category_title = trim($category->category_title);
  47. $category_title = Database::escape_string($category_title);
  48. $description = trim($category->description);
  49. $description = Database::escape_string($description);
  50. $display_order = $this->next_display_order($c_id);
  51. $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  52. $sql = "INSERT INTO $table
  53. (c_id, category_title, description, display_order, session_id)
  54. VALUES
  55. ($c_id , '$category_title', '$description', $display_order, $session_id)";
  56. $result = (bool) Database :: query($sql);
  57. if ($result) {
  58. $id = Database::insert_id();
  59. $category->id = $id;
  60. $category->display_order = $display_order;
  61. }
  62. return $result;
  63. }
  64. function update($category)
  65. {
  66. $c_id = (int) $category->c_id;
  67. $id = (int) $category->id;
  68. $session_id = (int) $category->session_id;
  69. $session_id = $session_id ? $session_id : '0';
  70. $category_title = trim($category->category_title);
  71. $category_title = Database::escape_string($category_title);
  72. $description = trim($category->description);
  73. $description = Database::escape_string($description);
  74. $display_order = (int) $category->display_order;
  75. $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  76. $sql = "UPDATE $table SET
  77. category_title = '$category_title',
  78. description = '$description',
  79. display_order = $display_order,
  80. session_id = $session_id
  81. WHERE
  82. c_id = $c_id AND
  83. id = $id";
  84. $result = (bool) Database :: query($sql);
  85. return $result;
  86. }
  87. function remove($category)
  88. {
  89. $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  90. $c_id = (int) $category->c_id;
  91. $id = (int) $category->id;
  92. $sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
  93. $success = (bool) Database :: query($sql);
  94. if ($success) {
  95. LinkRepository::instance()->remove_by_category($category);
  96. }
  97. return $success;
  98. }
  99. function remove_by_course($c_id, $session_id = 0)
  100. {
  101. $result = true;
  102. $categories = $this->find_by_course($c_id, $session_id);
  103. foreach($categories as $category){
  104. $success = $this->remove($category);
  105. if(!$success){
  106. $result = false;
  107. }
  108. }
  109. return $result;
  110. }
  111. function next_display_order($c_id)
  112. {
  113. $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  114. $sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
  115. $rs = Database :: query($sql);
  116. list ($result) = Database :: fetch_row($rs);
  117. $result = $result + 1;
  118. $result = intval($result);
  119. return $result;
  120. }
  121. /**
  122. *
  123. * @param string $where
  124. * @return array
  125. */
  126. public function find($where)
  127. {
  128. $result = array();
  129. $table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  130. $where = $where ? "WHERE $where" : '';
  131. $sql = "SELECT * FROM $table $where ORDER BY display_order DESC";
  132. $rs = Database :: query($sql);
  133. while ($data = Database::fetch_object($rs)) {
  134. $result[] = LinkCategory::create($data);
  135. }
  136. return $result;
  137. }
  138. /**
  139. *
  140. * @param string $where
  141. * @return \Link\LinkCategory
  142. */
  143. public function find_one($where)
  144. {
  145. $items = $this->find($where);
  146. foreach ($items as $item) {
  147. return $item;
  148. }
  149. return null;
  150. }
  151. /**
  152. *
  153. * @param int $c_id
  154. * @param int $id
  155. * @return \Link\LinkCategory
  156. */
  157. public function find_one_by_id($c_id, $id)
  158. {
  159. $where = "c_id = $c_id AND id = $id";
  160. return $this->find_one($where);
  161. }
  162. /**
  163. *
  164. * @param int $c_id
  165. * @param int $session_id
  166. * @param string $title
  167. * @return \Link\LinkCategory
  168. */
  169. public function find_one_by_course_and_name($c_id, $session_id = 0, $title)
  170. {
  171. $c_id = (int) $c_id;
  172. $session_id = (int) $session_id;
  173. $title = Database::escape_string($title);
  174. $condition_session = api_get_session_condition($session_id, true, true);
  175. $where = "c_id = $c_id $condition_session AND category_title = '$title'";
  176. return $this->find_one($where);
  177. }
  178. function find_by_course($c_id, $session_id = 0)
  179. {
  180. $c_id = (int) $c_id;
  181. $session_id = (int) $session_id;
  182. $condition_session = api_get_session_condition($session_id, true, true);
  183. $where = "c_id = $c_id $condition_session";
  184. return $this->find($where);
  185. }
  186. /**
  187. * Ensure $ids are sorted in the order given from greater to lower.
  188. *
  189. * Simple algorithm, works only if all ids are given at the same time.
  190. * This would not work if pagination were used and only a subset were sorted.
  191. * On the other hand the algorithm is sturdy, it will work even if current
  192. * weights/display orders are not correctly set up in the db.
  193. *
  194. * @param int $c_id
  195. * @param array $ids
  196. */
  197. public function order($c_id, $ids)
  198. {
  199. $result = true;
  200. $ids = array_map('intval', $ids);
  201. $table = Database::get_course_table(TABLE_LINK_CATEGORY);
  202. $counter = 0;
  203. $weight = count($ids) + 1;
  204. foreach ($ids as $id) {
  205. $sql = "UPDATE $table SET display_order = $weight WHERE c_id = $c_id AND id = $id";
  206. $success = Database::query($sql);
  207. if (!$success) {
  208. $result = false;
  209. }
  210. --$weight;
  211. }
  212. return $result;
  213. }
  214. }