link_repository.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace Link;
  3. use Database;
  4. /**
  5. * Database interface for Link
  6. *
  7. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  8. * @license /license.txt
  9. */
  10. class LinkRepository
  11. {
  12. /**
  13. *
  14. * @return \Link\LinkRepository
  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($link)
  25. {
  26. $id = $link->id;
  27. if (empty($id)) {
  28. return $this->insert($link);
  29. } else {
  30. return $this->update($link);
  31. }
  32. }
  33. /**
  34. *
  35. * @param \Link\Link $link
  36. * @return bool
  37. */
  38. public function insert($link)
  39. {
  40. $c_id = (int) $link->c_id;
  41. $session_id = (int) $link->session_id;
  42. $session_id = $session_id ? $session_id : '0';
  43. $url = trim($link->url);
  44. $url = Database::escape_string($url);
  45. $title = trim($link->title);
  46. $title = Database::escape_string($title);
  47. $description = trim($link->description);
  48. $description = Database::escape_string($description);
  49. $category_id = intval($link->category_id);
  50. $category_id = $category_id ? $category_id : '0';
  51. $display_order = $this->next_display_order($c_id);
  52. $on_homepage = $link->on_homepage;
  53. $on_homepage = $on_homepage ? '1' : '0';
  54. $target = $link->target;
  55. $target = Database::escape_string($target);
  56. $table = Database :: get_course_table(TABLE_LINK);
  57. $sql = "INSERT INTO $table
  58. (c_id, url, title, description, category_id, display_order, on_homepage, target, session_id)
  59. VALUES
  60. ($c_id , '$url', '$title', '$description', $category_id, $display_order, '$on_homepage', '$target', $session_id)";
  61. $result = (bool) Database :: query($sql);
  62. if ($result) {
  63. $id = Database::insert_id();
  64. $link->id = $id;
  65. $link->display_order = $display_order;
  66. $_course = api_get_course_info_by_id($c_id);
  67. $tool = TOOL_LINK;
  68. $user_id = api_get_user_id();
  69. api_item_property_update($_course, $tool, $id, 'LinkAdded', $user_id);
  70. }
  71. return $result;
  72. }
  73. function update($link)
  74. {
  75. $c_id = (int) $link->c_id;
  76. $id = (int) $link->id;
  77. $session_id = (int) $link->session_id;
  78. $session_id = $session_id ? $session_id : '0';
  79. $url = trim($link->url);
  80. $url = Database::escape_string($url);
  81. $title = trim($link->title);
  82. $title = Database::escape_string($title);
  83. $description = trim($link->description);
  84. $description = Database::escape_string($description);
  85. $category_id = intval($link->category_id);
  86. $category_id = $category_id ? $category_id : '0';
  87. $display_order = (int) $link->display_order;
  88. $on_homepage = $link->on_homepage;
  89. $on_homepage = $on_homepage ? '1' : '0';
  90. $target = $link->target;
  91. $target = Database::escape_string($target);
  92. $table = Database :: get_course_table(TABLE_LINK);
  93. $sql = "UPDATE $table SET
  94. url = '$url',
  95. title = '$title',
  96. description = '$description',
  97. category_id = $category_id,
  98. display_order = $display_order,
  99. on_homepage = '$on_homepage',
  100. target = '$target',
  101. session_id = $session_id
  102. WHERE
  103. c_id = $c_id AND
  104. id = $id";
  105. $result = (bool) Database :: query($sql);
  106. if ($result) {
  107. $_course = api_get_course_info_by_id($c_id);
  108. $tool = TOOL_LINK;
  109. $user_id = api_get_user_id();
  110. api_item_property_update($_course, $tool, $id, 'LinkUpdated', $user_id);
  111. }
  112. return $result;
  113. }
  114. public function remove_by_category($category)
  115. {
  116. $result = true;
  117. $c_id = (int) $category->c_id;
  118. $id = (int) $category->id;
  119. $where = "l.c_id=$c_id AND l.category_id=$id";
  120. $links = $links = $this->find($where);
  121. foreach ($links as $link) {
  122. $success = $this->remove($link);
  123. if (!$success) {
  124. $result = false;
  125. }
  126. }
  127. return $result;
  128. }
  129. public function remove_by_course($c_id, $session_id = 0)
  130. {
  131. $result = true;
  132. $session_where = api_get_session_condition($session_id, true, true);
  133. $where = "l.c_id=$c_id $session_where";
  134. $links = $links = $this->find($where);
  135. foreach ($links as $link) {
  136. $success = $this->remove($link);
  137. if (!$success) {
  138. $result = false;
  139. }
  140. }
  141. return $result;
  142. }
  143. /**
  144. *
  145. * Note:
  146. *
  147. * Note as ids are reused when objects are deleted it is either we delete
  148. * everything or nothing.
  149. *
  150. * @param \Link\Link|object $link
  151. * @return bool
  152. */
  153. public function remove($link)
  154. {
  155. $table = Database :: get_course_table(TABLE_LINK);
  156. $c_id = (int) $link->c_id;
  157. $id = (int) $link->id;
  158. $sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
  159. $result = Database :: query($sql);
  160. if ($result) {
  161. $tool = TOOL_LINK;
  162. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  163. $sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
  164. Database :: query($sql);
  165. $id = "Link.$id";
  166. $table = Database :: get_course_table(TABLE_METADATA);
  167. $sql = "DELETE FROM $table WHERE c_id=$c_id AND eid='$id'";
  168. Database :: query($sql);
  169. }
  170. return (bool) $result;
  171. }
  172. /**
  173. *
  174. * @param string $where
  175. * @return \Link\Link
  176. */
  177. public function find_one($where)
  178. {
  179. $items = $this->find($where);
  180. foreach ($items as $item) {
  181. return $item;
  182. }
  183. return null;
  184. }
  185. /**
  186. *
  187. * @param int $c_id
  188. * @param int $id
  189. * @return \Link\Link
  190. */
  191. public function find_one_by_id($c_id, $id)
  192. {
  193. $where = "l.c_id = $c_id AND l.id = $id";
  194. return $this->find_one($where);
  195. }
  196. /**
  197. *
  198. * @param int $c_id
  199. * @param int $session_id
  200. * @param string $url
  201. * @return \Link\Link
  202. */
  203. public function find_one_by_course_and_url($c_id, $session_id, $url)
  204. {
  205. $c_id = (int)$c_id;
  206. $session_id = (int)$session_id;
  207. $url = Database::escape_string($url);
  208. $session_where =api_get_session_condition($session_id, true, true);
  209. $where = "l.c_id = $c_id $session_where AND l.url = '$url'";
  210. return $this->find_one($where);
  211. }
  212. /**
  213. *
  214. * @param string $where
  215. * @return array
  216. */
  217. public function find($where = '')
  218. {
  219. $result = array();
  220. $tbl_link = Database :: get_course_table(TABLE_LINK);
  221. $tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  222. $tool = TOOL_LINK;
  223. $where = $where ? " AND ($where)" : '';
  224. $sql = "SELECT
  225. l.*,
  226. p.visibility
  227. FROM
  228. $tbl_link AS l,
  229. $tbl_property AS p
  230. WHERE
  231. p.tool='$tool' AND
  232. l.id = p.ref AND
  233. l.c_id = p.c_id
  234. $where
  235. ORDER BY
  236. l.display_order DESC";
  237. $rs = Database :: query($sql);
  238. while ($data = Database::fetch_object($rs)) {
  239. $result[] = Link::create($data);
  240. }
  241. return $result;
  242. }
  243. /**
  244. *
  245. * @param \Link\LinkCategory $category
  246. */
  247. public function find_by_category($category, $visible_only = false)
  248. {
  249. $session_id = $category->session_id;
  250. $id = $category->id;
  251. $where = $visible_only ? 'p.visibility=1' : '(p.visibility=0 OR p.visibility=1)';
  252. $where .=api_get_session_condition($session_id, true, true);
  253. $where .= "AND l.category_id = $id";
  254. return $this->find($where);
  255. }
  256. public function make_visible($c_id, $id)
  257. {
  258. $_course = api_get_course_info_by_id($c_id);
  259. $user_id = api_get_user_id();
  260. $result = api_item_property_update($_course, TOOL_LINK, $id, 'visible', $user_id);
  261. return $result;
  262. }
  263. public function make_invisible($c_id, $id)
  264. {
  265. $_course = api_get_course_info_by_id($c_id);
  266. $user_id = api_get_user_id();
  267. $result = api_item_property_update($_course, TOOL_LINK, $id, 'invisible', $user_id);
  268. return $result;
  269. }
  270. function next_display_order($c_id)
  271. {
  272. $table = Database :: get_course_table(TABLE_LINK);
  273. $sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
  274. $rs = Database :: query($sql);
  275. list ($result) = Database :: fetch_row($rs);
  276. $result = $result + 1;
  277. $result = intval($result);
  278. return $result;
  279. }
  280. /**
  281. * Ensure $ids are sorted in the order given from greater to lower
  282. *
  283. * Simple algorithm, works only if all ids are given at the same time.
  284. * This would not work if pagination were used and only a subset were sorted.
  285. * On the other hand the algorithm is sturdy, it will work even if current
  286. * weights/display orders are not correctly set up in the db.
  287. *
  288. * @param int $c_id
  289. * @param array $ids
  290. */
  291. public function order($c_id, $ids)
  292. {
  293. $result = true;
  294. $ids = array_map('intval', $ids);
  295. $table = Database :: get_course_table(TABLE_LINK);
  296. $counter = 0;
  297. $weight = count($ids) + 1;
  298. foreach ($ids as $id) {
  299. $sql = "UPDATE $table SET display_order = $weight WHERE c_id = $c_id AND id = $id";
  300. $success = Database::query($sql);
  301. if (!$success) {
  302. $result = false;
  303. }
  304. --$weight;
  305. }
  306. return $result;
  307. }
  308. }