link_repository.class.php 10 KB

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