link_category_repository.class.php 6.7 KB

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