link.lib.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Function library for the links tool.
  5. *
  6. * This is a complete remake of the original link tool.
  7. * New features:
  8. * - Organize links into categories;
  9. * - favorites/bookmarks interface;
  10. * - move links up/down within a category;
  11. * - move categories up/down;
  12. * - expand/collapse all categories;
  13. * - add link to 'root' category => category-less link is always visible.
  14. *
  15. * @author Patrick Cool, complete remake (December 2003 - January 2004)
  16. * @author René Haentjens, CSV file import (October 2004)
  17. * @package chamilo.link
  18. */
  19. class Link extends Model
  20. {
  21. public $table;
  22. public $is_course_model = true;
  23. public $columns = array(
  24. 'id',
  25. 'c_id',
  26. 'url',
  27. 'title',
  28. 'description',
  29. 'category_id',
  30. 'display_order',
  31. 'on_homepage',
  32. 'target',
  33. 'session_id'
  34. );
  35. public $required = array('url', 'title');
  36. private $course;
  37. /**
  38. *
  39. */
  40. public function __construct()
  41. {
  42. $this->table = Database::get_course_table(TABLE_LINK);
  43. }
  44. /**
  45. * @param array $course
  46. */
  47. public function setCourse($course)
  48. {
  49. $this->course = $course;
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getCourse()
  55. {
  56. return !empty($this->course) ? $this->course : api_get_course_info();
  57. }
  58. /**
  59. * Organize the saving of a link, using the parent's save method and
  60. * updating the item_property table
  61. * @param array $params
  62. * @param boolean $show_query Whether to show the query in logs when
  63. * calling parent's save method
  64. *
  65. * @return bool True if link could be saved, false otherwise
  66. */
  67. public function save($params, $show_query = null)
  68. {
  69. $course_info = $this->getCourse();
  70. $courseId = $course_info['real_id'];
  71. $params['session_id'] = api_get_session_id();
  72. $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
  73. $sql = "SELECT MAX(display_order)
  74. FROM ".$this->table."
  75. WHERE
  76. c_id = $courseId AND
  77. category_id = '" . intval($params['category_id'])."'";
  78. $result = Database:: query($sql);
  79. list ($orderMax) = Database:: fetch_row($result);
  80. $order = $orderMax + 1;
  81. $params['display_order'] = $order;
  82. $id = parent::save($params, $show_query);
  83. if (!empty($id)) {
  84. // iid
  85. $sql = "UPDATE ".$this->table." SET id = iid WHERE iid = $id";
  86. Database:: query($sql);
  87. api_set_default_visibility($id, TOOL_LINK);
  88. api_item_property_update(
  89. $course_info,
  90. TOOL_LINK,
  91. $id,
  92. 'LinkAdded',
  93. api_get_user_id()
  94. );
  95. }
  96. return $id;
  97. }
  98. /**
  99. * Update a link in the database
  100. * @param int $linkId The ID of the link to update
  101. * @param string $linkUrl The new URL to be saved
  102. * @param int Course ID
  103. * @param int Session ID
  104. * @return bool
  105. */
  106. public function updateLink(
  107. $linkId,
  108. $linkUrl,
  109. $courseId = null,
  110. $sessionId = null
  111. ) {
  112. $tblLink = Database:: get_course_table(TABLE_LINK);
  113. $linkUrl = Database::escape_string($linkUrl);
  114. $linkId = intval($linkId);
  115. if (is_null($courseId)) {
  116. $courseId = api_get_course_int_id();
  117. }
  118. $courseId = intval($courseId);
  119. if (is_null($sessionId)) {
  120. $sessionId = api_get_session_id();
  121. }
  122. $sessionId = intval($sessionId);
  123. if ($linkUrl != '') {
  124. $sql = "UPDATE $tblLink SET url = '$linkUrl'
  125. WHERE id = $linkId AND c_id = $courseId AND session_id = $sessionId";
  126. $resLink = Database::query($sql);
  127. return $resLink;
  128. }
  129. return false;
  130. }
  131. /**
  132. * Used to add a link or a category
  133. * @param string $type , "link" or "category"
  134. * @todo replace strings by constants
  135. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  136. * @return bool True on success, false on failure
  137. */
  138. public static function addlinkcategory($type)
  139. {
  140. global $catlinkstatus;
  141. global $msgErr;
  142. $ok = true;
  143. $_course = api_get_course_info();
  144. $course_id = $_course['real_id'];
  145. $session_id = api_get_session_id();
  146. if ($type == 'link') {
  147. $tbl_link = Database:: get_course_table(TABLE_LINK);
  148. $title = Security:: remove_XSS(stripslashes($_POST['title']));
  149. $urllink = Security:: remove_XSS($_POST['url']);
  150. $description = Security:: remove_XSS($_POST['description']);
  151. $selectcategory = Security:: remove_XSS($_POST['category_id']);
  152. if (!isset($_POST['on_homepage'])) {
  153. $onhomepage = 0;
  154. } else {
  155. $onhomepage = Security:: remove_XSS($_POST['on_homepage']);
  156. }
  157. if (empty($_POST['target'])) {
  158. $target = '_self'; // Default target.
  159. } else {
  160. $target = Security:: remove_XSS($_POST['target']);
  161. }
  162. $urllink = trim($urllink);
  163. $title = trim($title);
  164. $description = trim($description);
  165. // We ensure URL to be absolute.
  166. if (strpos($urllink, '://') === false) {
  167. $urllink = 'http://' . $urllink;
  168. }
  169. // If the title is empty, we use the URL as title.
  170. if ($title == '') {
  171. $title = $urllink;
  172. }
  173. // If the URL is invalid, an error occurs.
  174. if (!api_valid_url($urllink, true)) {
  175. // A check against an absolute URL
  176. Display::addFlash(Display::return_message(get_lang('GiveURL'), 'error'));
  177. return false;
  178. } else {
  179. // Looking for the largest order number for this category.
  180. $link = new Link();
  181. $params = [
  182. 'c_id' => $course_id,
  183. 'url' => $urllink,
  184. 'title' => $title,
  185. 'description' => $description ,
  186. 'category_id' => $selectcategory,
  187. 'on_homepage' => $onhomepage,
  188. 'target' => $target,
  189. 'session_id' => $session_id,
  190. ];
  191. $link_id = $link->save($params);
  192. $catlinkstatus = get_lang('LinkAdded');
  193. if ((api_get_setting('search_enabled') == 'true') &&
  194. $link_id && extension_loaded('xapian')
  195. ) {
  196. require_once api_get_path(
  197. LIBRARY_PATH
  198. ) . 'search/ChamiloIndexer.class.php';
  199. require_once api_get_path(
  200. LIBRARY_PATH
  201. ) . 'search/IndexableChunk.class.php';
  202. require_once api_get_path(
  203. LIBRARY_PATH
  204. ) . 'specific_fields_manager.lib.php';
  205. $course_int_id = $_course['real_id'];
  206. $courseCode = $_course['code'];
  207. $specific_fields = get_specific_field_list();
  208. $ic_slide = new IndexableChunk();
  209. // Add all terms to db.
  210. $all_specific_terms = '';
  211. foreach ($specific_fields as $specific_field) {
  212. if (isset ($_REQUEST[$specific_field['code']])) {
  213. $sterms = trim($_REQUEST[$specific_field['code']]);
  214. if (!empty ($sterms)) {
  215. $all_specific_terms .= ' ' . $sterms;
  216. $sterms = explode(',', $sterms);
  217. foreach ($sterms as $sterm) {
  218. $ic_slide->addTerm(
  219. trim($sterm),
  220. $specific_field['code']
  221. );
  222. add_specific_field_value(
  223. $specific_field['id'],
  224. $courseCode,
  225. TOOL_LINK,
  226. $link_id,
  227. $sterm
  228. );
  229. }
  230. }
  231. }
  232. }
  233. // Build the chunk to index.
  234. $ic_slide->addValue('title', $title);
  235. $ic_slide->addCourseId($courseCode);
  236. $ic_slide->addToolId(TOOL_LINK);
  237. $xapian_data = array(
  238. SE_COURSE_ID => $courseCode,
  239. SE_TOOL_ID => TOOL_LINK,
  240. SE_DATA => array(
  241. 'link_id' => (int)$link_id
  242. ),
  243. SE_USER => (int)api_get_user_id(),
  244. );
  245. $ic_slide->xapian_data = serialize($xapian_data);
  246. $description = $all_specific_terms . ' ' . $description;
  247. $ic_slide->addValue('content', $description);
  248. // Add category name if set.
  249. if (isset($selectcategory) && $selectcategory > 0) {
  250. $table_link_category = Database:: get_course_table(
  251. TABLE_LINK_CATEGORY
  252. );
  253. $sql_cat = 'SELECT * FROM %s WHERE id=%d AND c_id = %d LIMIT 1';
  254. $sql_cat = sprintf(
  255. $sql_cat,
  256. $table_link_category,
  257. (int)$selectcategory,
  258. $course_int_id
  259. );
  260. $result = Database:: query($sql_cat);
  261. if (Database:: num_rows($result) == 1) {
  262. $row = Database:: fetch_array($result);
  263. $ic_slide->addValue(
  264. 'category',
  265. $row['category_title']
  266. );
  267. }
  268. }
  269. $di = new ChamiloIndexer();
  270. isset ($_POST['language']) ? $lang = Database:: escape_string(
  271. $_POST['language']
  272. ) : $lang = 'english';
  273. $di->connectDb(null, null, $lang);
  274. $di->addChunk($ic_slide);
  275. // Index and return search engine document id.
  276. $did = $di->index();
  277. if ($did) {
  278. // Save it to db.
  279. $tbl_se_ref = Database:: get_main_table(
  280. TABLE_MAIN_SEARCH_ENGINE_REF
  281. );
  282. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  283. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  284. $sql = sprintf(
  285. $sql,
  286. $tbl_se_ref,
  287. $course_int_id,
  288. $courseCode,
  289. TOOL_LINK,
  290. $link_id,
  291. $did
  292. );
  293. Database:: query($sql);
  294. }
  295. }
  296. Display::addFlash(Display::return_message(get_lang('LinkAdded')));
  297. }
  298. } elseif ($type == 'category') {
  299. $tbl_categories = Database:: get_course_table(TABLE_LINK_CATEGORY);
  300. $category_title = trim($_POST['category_title']);
  301. $description = trim($_POST['description']);
  302. if (empty($category_title)) {
  303. $msgErr = get_lang('GiveCategoryName');
  304. Display:: display_error_message(get_lang('GiveCategoryName'));
  305. $ok = false;
  306. } else {
  307. // Looking for the largest order number for this category.
  308. $result = Database:: query(
  309. "SELECT MAX(display_order) FROM $tbl_categories
  310. WHERE c_id = $course_id "
  311. );
  312. list ($orderMax) = Database:: fetch_row($result);
  313. $order = $orderMax + 1;
  314. $order = intval($order);
  315. $session_id = api_get_session_id();
  316. $params = [
  317. 'c_id' => $course_id,
  318. 'category_title' => $category_title,
  319. 'description' => $description,
  320. 'display_order' => $order,
  321. 'session_id' => $session_id
  322. ];
  323. $linkId = Database::insert($tbl_categories, $params);
  324. if ($linkId) {
  325. // iid
  326. $sql = "UPDATE $tbl_categories SET id = iid WHERE iid = $linkId";
  327. Database:: query($sql);
  328. // add link_category visibility
  329. // course ID is taken from context in api_set_default_visibility
  330. api_set_default_visibility($linkId, TOOL_LINK_CATEGORY);
  331. }
  332. Display::addFlash(Display::return_message(get_lang('CategoryAdded')));
  333. }
  334. }
  335. return $ok;
  336. }
  337. /**
  338. * Used to delete a link or a category
  339. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  340. * @param int $id
  341. * @param string $type The type of item to delete
  342. * @return bool
  343. */
  344. public static function deletelinkcategory($id, $type)
  345. {
  346. $courseInfo = api_get_course_info();
  347. $tbl_link = Database:: get_course_table(TABLE_LINK);
  348. $tbl_categories = Database:: get_course_table(TABLE_LINK_CATEGORY);
  349. $course_id = $courseInfo['real_id'];
  350. $id = intval($id);
  351. if (empty($id)) {
  352. return false;
  353. }
  354. $result = false;
  355. switch ($type) {
  356. case 'link':
  357. // -> Items are no longer physically deleted,
  358. // but the visibility is set to 2 (in item_property).
  359. // This will make a restore function possible for the platform administrator.
  360. $sql = "UPDATE $tbl_link SET on_homepage='0'
  361. WHERE c_id = $course_id AND id='" . $id . "'";
  362. Database:: query($sql);
  363. api_item_property_update(
  364. $courseInfo,
  365. TOOL_LINK,
  366. $id,
  367. 'delete',
  368. api_get_user_id()
  369. );
  370. self::delete_link_from_search_engine(api_get_course_id(), $id);
  371. Display:: display_confirmation_message(get_lang('LinkDeleted'));
  372. $result = true;
  373. break;
  374. case 'category':
  375. // First we delete the category itself and afterwards all the links of this category.
  376. $sql = "DELETE FROM " . $tbl_categories . "
  377. WHERE c_id = $course_id AND id='" . $id . "'";
  378. Database:: query($sql);
  379. $sql = "DELETE FROM " . $tbl_link . "
  380. WHERE c_id = $course_id AND category_id='" . $id . "'";
  381. Database:: query($sql);
  382. api_item_property_update(
  383. $courseInfo,
  384. TOOL_LINK_CATEGORY,
  385. $id,
  386. 'delete',
  387. api_get_user_id()
  388. );
  389. Display::addFlash(Display::return_message(get_lang('CategoryDeleted')));
  390. $result = true;
  391. break;
  392. }
  393. return $result;
  394. }
  395. /**
  396. * Removes a link from search engine database
  397. * @param string $course_id Course code
  398. * @param int $link_id Document id to delete
  399. * @return void
  400. */
  401. public static function delete_link_from_search_engine($course_id, $link_id)
  402. {
  403. // Remove from search engine if enabled.
  404. if (api_get_setting('search_enabled') == 'true') {
  405. $tbl_se_ref = Database:: get_main_table(
  406. TABLE_MAIN_SEARCH_ENGINE_REF
  407. );
  408. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  409. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  410. $res = Database:: query($sql);
  411. if (Database:: num_rows($res) > 0) {
  412. $row = Database:: fetch_array($res);
  413. require_once api_get_path(
  414. LIBRARY_PATH
  415. ) . 'search/ChamiloIndexer.class.php';
  416. $di = new ChamiloIndexer();
  417. $di->remove_document((int)$row['search_did']);
  418. }
  419. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  420. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  421. Database:: query($sql);
  422. // Remove terms from db.
  423. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  424. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  425. }
  426. }
  427. /**
  428. *
  429. * Get link info
  430. * @param int link id
  431. * @return array link info
  432. *
  433. **/
  434. public static function get_link_info($id)
  435. {
  436. $tbl_link = Database:: get_course_table(TABLE_LINK);
  437. $course_id = api_get_course_int_id();
  438. $sql = "SELECT * FROM " . $tbl_link . "
  439. WHERE c_id = $course_id AND id='" . intval($id) . "' ";
  440. $result = Database::query($sql);
  441. $data = array();
  442. if (Database::num_rows($result)) {
  443. $data = Database::fetch_array($result);
  444. }
  445. return $data;
  446. }
  447. /**
  448. * @param int $id
  449. * @param array $values
  450. */
  451. public static function editLink($id, $values = array())
  452. {
  453. $tbl_link = Database:: get_course_table(TABLE_LINK);
  454. $_course = api_get_course_info();
  455. $course_id = $_course['real_id'];
  456. $values['url'] = trim($values['url']);
  457. $values['title'] = trim($values['title']);
  458. $values['description'] = trim($values['description']);
  459. $values['target'] = empty($values['target']) ? '_self' : $values['target'];
  460. $values['on_homepage'] = isset($values['on_homepage']) ? $values['on_homepage'] : '';
  461. $categoryId = intval($values['category_id']);
  462. // We ensure URL to be absolute.
  463. if (strpos($values['url'], '://') === false) {
  464. $values['url'] = 'http://' . $_POST['url'];
  465. }
  466. // If the title is empty, we use the URL as title.
  467. if ($values['title'] == '') {
  468. $values['title'] = $values['url'];
  469. }
  470. // If the URL is invalid, an error occurs.
  471. if (!api_valid_url($values['url'], true)) {
  472. Display::addFlash(
  473. Display::return_message(get_lang('GiveURL'), 'error')
  474. );
  475. return false;
  476. }
  477. // Finding the old category_id.
  478. $sql = "SELECT * FROM " . $tbl_link . "
  479. WHERE c_id = $course_id AND id='" . $id . "'";
  480. $result = Database:: query($sql);
  481. $row = Database:: fetch_array($result);
  482. $category_id = $row['category_id'];
  483. if ($category_id != $values['category_id']) {
  484. $sql = "SELECT MAX(display_order)
  485. FROM " . $tbl_link . "
  486. WHERE
  487. c_id = $course_id AND
  488. category_id='" . intval($values['category_id']) . "'";
  489. $result = Database:: query($sql);
  490. list ($max_display_order) = Database:: fetch_row($result);
  491. $max_display_order++;
  492. } else {
  493. $max_display_order = $row['display_order'];
  494. }
  495. $params = [
  496. 'url' => $values['url'],
  497. 'title' => $values['title'],
  498. 'description' => $values['description'],
  499. 'category_id' => $values['category_id'],
  500. 'display_order' => $max_display_order,
  501. 'on_homepage' => $values['on_homepage'],
  502. 'target' => $values['target'],
  503. 'category_id' => $values['category_id'],
  504. ];
  505. Database::update($tbl_link, $params, ['c_id = ? AND id = ?' => [$course_id, $id] ]);
  506. // Update search enchine and its values table if enabled.
  507. if (api_get_setting('search_enabled') == 'true') {
  508. $course_int_id = api_get_course_int_id();
  509. $course_id = api_get_course_id();
  510. $link_title = Database:: escape_string($values['title']);
  511. $link_description = Database:: escape_string($values['description']);
  512. // Actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one.
  513. // Get search_did.
  514. $tbl_se_ref = Database:: get_main_table(
  515. TABLE_MAIN_SEARCH_ENGINE_REF
  516. );
  517. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  518. $sql = sprintf(
  519. $sql,
  520. $tbl_se_ref,
  521. $course_id,
  522. TOOL_LINK,
  523. $id
  524. );
  525. $res = Database:: query($sql);
  526. if (Database:: num_rows($res) > 0) {
  527. require_once api_get_path(
  528. LIBRARY_PATH
  529. ).'search/ChamiloIndexer.class.php';
  530. require_once api_get_path(
  531. LIBRARY_PATH
  532. ).'search/IndexableChunk.class.php';
  533. require_once api_get_path(
  534. LIBRARY_PATH
  535. ).'specific_fields_manager.lib.php';
  536. $se_ref = Database:: fetch_array($res);
  537. $specific_fields = get_specific_field_list();
  538. $ic_slide = new IndexableChunk();
  539. $all_specific_terms = '';
  540. foreach ($specific_fields as $specific_field) {
  541. delete_all_specific_field_value(
  542. $course_id,
  543. $specific_field['id'],
  544. TOOL_LINK,
  545. $id
  546. );
  547. if (isset ($_REQUEST[$specific_field['code']])) {
  548. $sterms = trim(
  549. $_REQUEST[$specific_field['code']]
  550. );
  551. if (!empty ($sterms)) {
  552. $all_specific_terms .= ' ' . $sterms;
  553. $sterms = explode(',', $sterms);
  554. foreach ($sterms as $sterm) {
  555. $ic_slide->addTerm(
  556. trim($sterm),
  557. $specific_field['code']
  558. );
  559. add_specific_field_value(
  560. $specific_field['id'],
  561. $course_id,
  562. TOOL_LINK,
  563. $id,
  564. $sterm
  565. );
  566. }
  567. }
  568. }
  569. }
  570. // Build the chunk to index.
  571. $ic_slide->addValue("title", $link_title);
  572. $ic_slide->addCourseId($course_id);
  573. $ic_slide->addToolId(TOOL_LINK);
  574. $xapian_data = array(
  575. SE_COURSE_ID => $course_id,
  576. SE_TOOL_ID => TOOL_LINK,
  577. SE_DATA => array(
  578. 'link_id' => (int)$id
  579. ),
  580. SE_USER => (int)api_get_user_id(),
  581. );
  582. $ic_slide->xapian_data = serialize($xapian_data);
  583. $link_description = $all_specific_terms . ' ' . $link_description;
  584. $ic_slide->addValue('content', $link_description);
  585. // Add category name if set.
  586. if (isset ($categoryId) && $categoryId > 0) {
  587. $table_link_category = Database:: get_course_table(
  588. TABLE_LINK_CATEGORY
  589. );
  590. $sql_cat = 'SELECT * FROM %s WHERE id=%d and c_id = %d LIMIT 1';
  591. $sql_cat = sprintf(
  592. $sql_cat,
  593. $table_link_category,
  594. $categoryId,
  595. $course_int_id
  596. );
  597. $result = Database:: query($sql_cat);
  598. if (Database:: num_rows($result) == 1) {
  599. $row = Database:: fetch_array($result);
  600. $ic_slide->addValue(
  601. 'category',
  602. $row['category_title']
  603. );
  604. }
  605. }
  606. $di = new ChamiloIndexer();
  607. isset ($_POST['language']) ? $lang = Database:: escape_string($_POST['language']) : $lang = 'english';
  608. $di->connectDb(null, null, $lang);
  609. $di->remove_document((int)$se_ref['search_did']);
  610. $di->addChunk($ic_slide);
  611. // Index and return search engine document id.
  612. $did = $di->index();
  613. if ($did) {
  614. // Save it to db.
  615. $sql = 'DELETE FROM %s
  616. WHERE course_code=\'%s\'
  617. AND tool_id=\'%s\'
  618. AND ref_id_high_level=\'%s\'';
  619. $sql = sprintf(
  620. $sql,
  621. $tbl_se_ref,
  622. $course_id,
  623. TOOL_LINK,
  624. $id
  625. );
  626. Database:: query($sql);
  627. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  628. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  629. $sql = sprintf(
  630. $sql,
  631. $tbl_se_ref,
  632. $course_int_id,
  633. $course_id,
  634. TOOL_LINK,
  635. $id,
  636. $did
  637. );
  638. Database:: query($sql);
  639. }
  640. }
  641. }
  642. // "WHAT'S NEW" notification: update table last_toolEdit.
  643. api_item_property_update(
  644. $_course,
  645. TOOL_LINK,
  646. $id,
  647. 'LinkUpdated',
  648. api_get_user_id()
  649. );
  650. Display::addFlash(Display::return_message(get_lang('LinkModded')));
  651. }
  652. /**
  653. * @param int $id
  654. * @return bool
  655. */
  656. public static function editCategory($id, $values)
  657. {
  658. $tbl_categories = Database:: get_course_table(TABLE_LINK_CATEGORY);
  659. $course_id = api_get_course_int_id();
  660. $id = intval($id);
  661. // This is used to put the modified info of the category-form into the database.
  662. $params = [
  663. 'category_title' => $values['category_title'],
  664. 'description' => $values['description']
  665. ];
  666. Database::update($tbl_categories, $params, ['c_id = ? AND id = ?' => [$course_id, $id] ]);
  667. Display::addFlash(Display::return_message(get_lang('CategoryModded')));
  668. return true;
  669. }
  670. /**
  671. * Creates a correct $view for in the URL
  672. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  673. */
  674. public static function makedefaultviewcode($locatie)
  675. {
  676. global $aantalcategories, $view;
  677. for ($j = 0; $j <= $aantalcategories - 1; $j++) {
  678. $view[$j] = 0;
  679. }
  680. $view[intval($locatie)] = '1';
  681. }
  682. /**
  683. * Changes the visibility of a link
  684. * @todo add the changing of the visibility of a course
  685. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  686. */
  687. public static function change_visibility_link($id, $scope)
  688. {
  689. $_course = api_get_course_info();
  690. $_user = api_get_user_info();
  691. if ($scope == TOOL_LINK) {
  692. api_item_property_update(
  693. $_course,
  694. TOOL_LINK,
  695. $id,
  696. $_GET['action'],
  697. $_user['user_id']
  698. );
  699. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  700. } elseif ($scope == TOOL_LINK_CATEGORY) {
  701. api_item_property_update(
  702. $_course,
  703. TOOL_LINK_CATEGORY,
  704. $id,
  705. $_GET['action'],
  706. $_user['user_id']
  707. );
  708. Display::addFlash(Display::return_message(get_lang('VisibilityChanged')));
  709. }
  710. }
  711. /**
  712. * Generate SQL to select all the links categories in the current course and
  713. * session
  714. * @param int $courseId
  715. * @param int $sessionId
  716. * @return array
  717. */
  718. public static function getLinkCategories($courseId, $sessionId)
  719. {
  720. $tblLinkCategory = Database:: get_course_table(TABLE_LINK_CATEGORY);
  721. $tblItemProperty = Database:: get_course_table(TABLE_ITEM_PROPERTY);
  722. $courseId = intval($courseId);
  723. // Condition for the session.
  724. $sessionCondition = api_get_session_condition($sessionId, true, true, 'linkcat.session_id');
  725. // Getting links
  726. $sql = "SELECT *, linkcat.id
  727. FROM $tblLinkCategory linkcat
  728. WHERE
  729. linkcat.c_id = " . $courseId . "
  730. $sessionCondition
  731. ORDER BY linkcat.display_order DESC";
  732. $result = Database::query($sql);
  733. $categories = Database::store_result($result);
  734. $sql = "SELECT *, linkcat.id
  735. FROM $tblLinkCategory linkcat
  736. INNER JOIN $tblItemProperty itemproperties
  737. ON (linkcat.id = itemproperties.ref AND linkcat.c_id = itemproperties.c_id)
  738. WHERE
  739. itemproperties.tool = '" . TOOL_LINK_CATEGORY . "' AND
  740. (itemproperties.visibility = '0' OR itemproperties.visibility = '1')
  741. $sessionCondition AND
  742. linkcat.c_id = " . $courseId . "
  743. ORDER BY linkcat.display_order DESC";
  744. $result = Database::query($sql);
  745. $categoryInItemProperty = array();
  746. if (Database::num_rows($result)) {
  747. while ($row = Database::fetch_array($result, 'ASSOC')) {
  748. $categoryInItemProperty[$row['id']] = $row;
  749. }
  750. }
  751. foreach ($categories as & $category) {
  752. if (!isset($categoryInItemProperty[$category['id']])) {
  753. api_set_default_visibility($category['id'], TOOL_LINK_CATEGORY);
  754. }
  755. }
  756. $sql = "SELECT DISTINCT linkcat.*, visibility
  757. FROM $tblLinkCategory linkcat
  758. INNER JOIN $tblItemProperty itemproperties
  759. ON (linkcat.id = itemproperties.ref AND linkcat.c_id = itemproperties.c_id)
  760. WHERE
  761. itemproperties.tool = '" . TOOL_LINK_CATEGORY . "' AND
  762. (itemproperties.visibility = '0' OR itemproperties.visibility = '1')
  763. $sessionCondition AND
  764. linkcat.c_id = " . $courseId . "
  765. GROUP BY c_id, id
  766. ORDER BY linkcat.display_order DESC
  767. ";
  768. $result = Database::query($sql);
  769. return Database::store_result($result, 'ASSOC');
  770. }
  771. /**
  772. * Displays all the links of a given category.
  773. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  774. */
  775. public static function showlinksofcategory($catid)
  776. {
  777. global $urlview, $up, $down, $_user, $token;
  778. $tbl_link = Database:: get_course_table(TABLE_LINK);
  779. $TABLE_ITEM_PROPERTY = Database:: get_course_table(TABLE_ITEM_PROPERTY);
  780. // Condition for the session.
  781. $session_id = api_get_session_id();
  782. $condition_session = api_get_session_condition($session_id, true, true, 'link.session_id');
  783. $catid = intval($catid);
  784. $course_id = api_get_course_int_id();
  785. $sqlLinks = "SELECT *, link.id FROM $tbl_link link
  786. INNER JOIN $TABLE_ITEM_PROPERTY itemproperties
  787. ON (link.id=itemproperties.ref AND link.c_id = itemproperties.c_id )
  788. WHERE
  789. itemproperties.tool='" . TOOL_LINK . "' AND
  790. link.category_id='" . $catid . "' AND
  791. (itemproperties.visibility='0' OR itemproperties.visibility='1')
  792. $condition_session AND
  793. link.c_id = " . $course_id . " AND
  794. itemproperties.c_id = " . $course_id . "
  795. ORDER BY link.display_order DESC";
  796. $result = Database:: query($sqlLinks);
  797. $numberoflinks = Database:: num_rows($result);
  798. if ($numberoflinks > 0) {
  799. echo '<table class="data_table" width="100%">';
  800. $i = 1;
  801. while ($myrow = Database:: fetch_array($result)) {
  802. // Validation when belongs to a session.
  803. $session_img = api_get_session_image(
  804. $myrow['session_id'],
  805. $_user['status']
  806. );
  807. $css_class = $i % 2 == 0 ? $css_class = 'row_odd' : $css_class = 'row_even';
  808. $link_validator = '';
  809. if (api_is_allowed_to_edit(null, true)) {
  810. $link_validator = Display::url(
  811. Display::return_icon(
  812. 'preview_view.png',
  813. get_lang('CheckURL'),
  814. array(),
  815. 16
  816. ),
  817. '#',
  818. array(
  819. 'onclick' => "check_url('" . $myrow['id'] . "', '" . addslashes($myrow['url']) . "');"
  820. )
  821. );
  822. $link_validator .= Display::span(
  823. '',
  824. array('id' => 'url_id_' . $myrow['id'])
  825. );
  826. }
  827. if ($myrow['visibility'] == '1') {
  828. echo '<tr class="' . $css_class . '">';
  829. echo '<td align="center" valign="middle" width="5%">';
  830. echo '<a href="link_goto.php?' . api_get_cidreq() .
  831. '&link_id=' . $myrow['id'] .
  832. '&link_url=' . urlencode($myrow['url']) . '" target="_blank">
  833. <img src="../../main/img/link.gif" border="0" alt="' . get_lang('Link') . '"/></a>
  834. </td>
  835. <td width="80%" valign="top">
  836. <a href="link_goto.php?' . api_get_cidreq() .
  837. '&link_id=' . $myrow['id'] .
  838. '&link_url=' . urlencode($myrow['url']) . '" target="' . $myrow['target'] . '">';
  839. echo Security:: remove_XSS($myrow['title']);
  840. echo '</a>';
  841. echo $link_validator;
  842. echo $session_img;
  843. echo '<br />' . $myrow['description'];
  844. } else {
  845. if (api_is_allowed_to_edit(null, true)) {
  846. echo '<tr class="' . $css_class . '">';
  847. echo '<td align="center" valign="middle" width="5%">
  848. <a href="link_goto.php?' . api_get_cidreq() .
  849. '&link_id=' . $myrow['id'] . "
  850. &link_url=" . urlencode($myrow['url']) . '"
  851. target="_blank" class="invisible">';
  852. echo Display:: return_icon(
  853. 'link_na.gif',
  854. get_lang('Link')
  855. ), '</a>';
  856. echo '</td><td width="80%" valign="top">
  857. <a href="link_goto.php?', api_get_cidreq(), '&link_id=', $myrow['id'], '&link_url=', urlencode($myrow['url']), '" target="', $myrow['target'], '" class="invisible">';
  858. echo Security:: remove_XSS($myrow['title']);
  859. echo "</a>";
  860. echo $link_validator;
  861. echo $session_img, '<br />', $myrow['description'];
  862. }
  863. }
  864. if (api_is_allowed_to_edit(null, true)) {
  865. echo '<td style="text-align:center;">';
  866. if ($session_id == $myrow['session_id']) {
  867. $url = api_get_self() . '?' . api_get_cidreq() .
  868. '&action=editlink&category=' . (!empty ($category) ? $category : '') .
  869. '&id=' . $myrow['id'] .
  870. '&category_id=' . $myrow['id'];
  871. echo '<a href="' .$url .'">'.
  872. Display:: return_icon(
  873. 'edit.png',
  874. get_lang('Modify'),
  875. array(),
  876. ICON_SIZE_SMALL
  877. ) . '</a>';
  878. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  879. /* commented at least since 2014-10-11
  880. if ($i != 1) {
  881. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&sec_token='.$token.'&urlview=' . $urlview . '&up=', $myrow[0], '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  882. } else {
  883. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  884. }
  885. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  886. if ($i < $numberoflinks) {
  887. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&sec_token='.$token.'&urlview=' . $urlview . '&down=' . $myrow[0] . '" title="' . get_lang('Down') . '">' . Display :: return_icon('down.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  888. } else {
  889. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  890. }*/
  891. if ($myrow['visibility'] == '1') {
  892. echo '<a href="link.php?' . api_get_cidreq() .
  893. '&sec_token=' . $token .
  894. '&action=invisible&id=' . $myrow['id'] .
  895. '&scope=link&category_id=' . $myrow['category_id'] . '" title="' . get_lang('Hide') . '">' .
  896. Display:: return_icon(
  897. 'visible.png',
  898. get_lang('Hide'),
  899. array(),
  900. ICON_SIZE_SMALL
  901. ) . '</a>';
  902. }
  903. if ($myrow['visibility'] == '0') {
  904. echo ' <a href="link.php?' . api_get_cidreq() .
  905. '&sec_token=' . $token .
  906. '&action=visible&id=' . $myrow['id'] .
  907. '&scope=link&category_id=' . $myrow['category_id'] . '" title="' . get_lang(
  908. 'Show'
  909. ) . '">' .
  910. Display:: return_icon(
  911. 'invisible.png',
  912. get_lang('Show'),
  913. array(),
  914. ICON_SIZE_SMALL
  915. ) . '</a>';
  916. }
  917. echo ' <a href="' . api_get_self() . '?' . api_get_cidreq() .'&sec_token=' . $token .'&action=deletelink&id=' . $myrow['id'] .'&category_id=' . $myrow['category_id'] . "\"
  918. onclick=\"javascript: if(!confirm('" . get_lang('LinkDelconfirm') . "'))
  919. return false;\" title=\"" . get_lang('Delete') . '">' .
  920. Display:: return_icon(
  921. 'delete.png',
  922. get_lang('Delete'),
  923. array(),
  924. ICON_SIZE_SMALL
  925. ) . '</a>';
  926. } else {
  927. echo Display:: return_icon(
  928. 'edit_na.png',
  929. get_lang('EditionNotAvailableFromSession'),
  930. array(),
  931. ICON_SIZE_SMALL
  932. ); //get_lang('EditionNotAvailableFromSession');
  933. }
  934. echo '</td>';
  935. }
  936. echo '</tr>';
  937. $i++;
  938. }
  939. echo '</table>';
  940. }
  941. }
  942. /**
  943. * Displays the edit, delete and move icons
  944. * @param int Category ID
  945. * @return void
  946. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  947. */
  948. public static function showCategoryAdminTools($category, $currentCategory, $countCategories)
  949. {
  950. $categoryId = $category['id'];
  951. $token = null;
  952. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&sec_token=' . $token . '&action=editcategory&id=' . $categoryId . '&category_id=' . $categoryId . '" title=' . get_lang('Modify') . '">' .
  953. Display:: return_icon(
  954. 'edit.png',
  955. get_lang('Modify'),
  956. array(),
  957. ICON_SIZE_SMALL
  958. ) . '</a>';
  959. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  960. if ($currentCategory != 0) {
  961. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&sec_token=' . $token . '&action=up&up=', $categoryId, '&category_id=' . $categoryId . '" title="' . get_lang('Up') . '">' .
  962. Display:: return_icon(
  963. 'up.png',
  964. get_lang('Up'),
  965. array(),
  966. ICON_SIZE_SMALL
  967. ) . '</a>';
  968. } else {
  969. echo Display:: return_icon(
  970. 'up_na.png',
  971. get_lang('Up'),
  972. array(),
  973. ICON_SIZE_SMALL
  974. ) . '</a>';
  975. }
  976. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  977. if ($currentCategory < $countCategories-1) {
  978. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() .
  979. '&sec_token=' . $token .
  980. '&action=down&down=' . $categoryId .
  981. '&category_id=' . $categoryId . '">
  982. ' .
  983. Display:: return_icon(
  984. 'down.png',
  985. get_lang('Down'),
  986. array(),
  987. ICON_SIZE_SMALL
  988. ) . '</a>';
  989. } else {
  990. echo Display:: return_icon(
  991. 'down_na.png',
  992. get_lang('Down'),
  993. array(),
  994. ICON_SIZE_SMALL
  995. ) . '</a>';
  996. }
  997. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() .
  998. '&sec_token=' . $token .
  999. '&action=deletecategory&id=', $categoryId,
  1000. "&category_id=$categoryId\"
  1001. onclick=\"javascript: if(!confirm('" . get_lang('CategoryDelconfirm') . "')) return false;\">",
  1002. Display:: return_icon(
  1003. 'delete.png',
  1004. get_lang('Delete'),
  1005. array(),
  1006. ICON_SIZE_SMALL
  1007. ) . '</a>';
  1008. }
  1009. /**
  1010. * move a link or a linkcategory up or down
  1011. * @param int Category ID
  1012. * @param int Course ID
  1013. * @param int Session ID
  1014. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1015. * @todo support sessions
  1016. */
  1017. public static function movecatlink($action, $catlinkid, $courseId = null, $sessionId = null)
  1018. {
  1019. $tbl_link = Database:: get_course_table(TABLE_LINK);
  1020. $tbl_categories = Database:: get_course_table(TABLE_LINK_CATEGORY);
  1021. if (is_null($courseId)) {
  1022. $courseId = api_get_course_int_id();
  1023. }
  1024. $courseId = intval($courseId);
  1025. if (is_null($sessionId)) {
  1026. $sessionId = api_get_session_id();
  1027. }
  1028. $sessionId = intval($sessionId);
  1029. if ($action == 'down') {
  1030. $thiscatlinkId = intval($catlinkid);
  1031. $sortDirection = 'DESC';
  1032. }
  1033. if ($action == 'up') {
  1034. $thiscatlinkId = intval($catlinkid);
  1035. $sortDirection = 'ASC';
  1036. }
  1037. // We check if it is a category we are moving or a link.
  1038. // If it is a category, a querystring catmove = true is present in the url.
  1039. $catmove = 'true';
  1040. if ($catmove == 'true') {
  1041. $movetable = $tbl_categories;
  1042. $catid = $catlinkid;
  1043. } else {
  1044. $movetable = $tbl_link;
  1045. // Getting the category of the link.
  1046. if (!empty ($thiscatlinkId)) {
  1047. $sql = "SELECT category_id FROM " . $movetable . "
  1048. WHERE c_id = $courseId AND id='$thiscatlinkId'";
  1049. $result = Database:: query($sql);
  1050. $catid = Database:: fetch_array($result);
  1051. }
  1052. }
  1053. // This code is copied and modified from announcements.php.
  1054. if (!empty($sortDirection)) {
  1055. if (!in_array(trim(strtoupper($sortDirection)), array('ASC', 'DESC'))) {
  1056. $sortDirection = 'ASC';
  1057. }
  1058. if ($catmove == 'true') {
  1059. $sql = "SELECT id, display_order FROM $movetable
  1060. WHERE c_id = $courseId
  1061. ORDER BY display_order $sortDirection";
  1062. } else {
  1063. $sql = "SELECT id, display_order FROM $movetable
  1064. WHERE c_id = $courseId AND category_id='" . $catid[0] . "'
  1065. ORDER BY display_order $sortDirection";
  1066. }
  1067. $linkresult = Database:: query($sql);
  1068. $thislinkOrder = 1;
  1069. while ($sortrow = Database:: fetch_array($linkresult)) {
  1070. // STEP 2 : FOUND THE NEXT LINK ID AND ORDER, COMMIT SWAP
  1071. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  1072. // and we know the next iteration of the while loop is the next one. These should be swapped.
  1073. if (isset($thislinkFound) && $thislinkFound) {
  1074. $nextlinkId = $sortrow['id'];
  1075. $nextlinkOrder = $sortrow['display_order'];
  1076. Database:: query(
  1077. "UPDATE " . $movetable . "
  1078. SET display_order = '$nextlinkOrder'
  1079. WHERE c_id = $courseId AND id = '$thiscatlinkId'"
  1080. );
  1081. Database:: query(
  1082. "UPDATE " . $movetable . "
  1083. SET display_order = '$thislinkOrder'
  1084. WHERE c_id = $courseId AND id = '$nextlinkId'"
  1085. );
  1086. break;
  1087. }
  1088. if ($sortrow['id'] == $thiscatlinkId) {
  1089. $thislinkOrder = $sortrow['display_order'];
  1090. $thislinkFound = true;
  1091. }
  1092. }
  1093. }
  1094. Display::addFlash(Display::return_message(get_lang('LinkMoved')));
  1095. }
  1096. /**
  1097. * CSV file import functions
  1098. * @author René Haentjens , Ghent University
  1099. */
  1100. public static function get_cat($catname)
  1101. {
  1102. // Get category id (existing or make new).
  1103. $tbl_categories = Database:: get_course_table(TABLE_LINK_CATEGORY);
  1104. $course_id = api_get_course_int_id();
  1105. $result = Database:: query(
  1106. "SELECT id FROM " . $tbl_categories . "
  1107. WHERE c_id = $course_id AND category_title='" . Database::escape_string(
  1108. $catname
  1109. ) . "'"
  1110. );
  1111. if (Database:: num_rows($result) >= 1 && ($row = Database:: fetch_array(
  1112. $result
  1113. ))
  1114. ) {
  1115. return $row['id']; // Several categories with same name: take the first.
  1116. }
  1117. $result = Database:: query(
  1118. "SELECT MAX(display_order) FROM " . $tbl_categories . " WHERE c_id = $course_id "
  1119. );
  1120. list ($max_order) = Database:: fetch_row($result);
  1121. $params = [
  1122. 'c_id' => $course_id,
  1123. 'category_title' => $catname,
  1124. 'description' => '',
  1125. 'display_order' => $max_order + 1
  1126. ];
  1127. $id = Database::insert($tbl_categories, $params);
  1128. return $id;
  1129. }
  1130. /**
  1131. * CSV file import functions
  1132. * @author René Haentjens , Ghent University
  1133. */
  1134. public static function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
  1135. {
  1136. $tbl_link = Database:: get_course_table(TABLE_LINK);
  1137. $course_id = api_get_course_int_id();
  1138. $urleq = "url='" . Database:: escape_string($url) . "'";
  1139. $cateq = "category_id=" . intval($cat);
  1140. $result = Database:: query(
  1141. "SELECT id FROM $tbl_link
  1142. WHERE c_id = $course_id AND " . $urleq . ' AND ' . $cateq
  1143. );
  1144. if (Database:: num_rows($result) >= 1 && ($row = Database:: fetch_array(
  1145. $result
  1146. ))
  1147. ) {
  1148. Database:: query(
  1149. "UPDATE $tbl_link set title='" . Database:: escape_string(
  1150. $title
  1151. ) . "', description='" . Database:: escape_string(
  1152. $description
  1153. ) . "'
  1154. WHERE c_id = $course_id AND id='" . Database:: escape_string(
  1155. $row['id']
  1156. ) . "'"
  1157. );
  1158. $ipu = 'LinkUpdated';
  1159. $rv = 1; // 1 = upd
  1160. } else {
  1161. // Add new link
  1162. $result = Database:: query(
  1163. "SELECT MAX(display_order) FROM $tbl_link
  1164. WHERE c_id = $course_id AND category_id='" . intval($cat) . "'"
  1165. );
  1166. list ($max_order) = Database:: fetch_row($result);
  1167. Database:: query(
  1168. "INSERT INTO $tbl_link (c_id, url, title, description, category_id, display_order, on_homepage)
  1169. VALUES (" . api_get_course_int_id() . ",
  1170. '" . Database:: escape_string($url) . "',
  1171. '" . Database:: escape_string($title) . "',
  1172. '" . Database:: escape_string($description) . "',
  1173. '" . intval($cat) . "','" . (intval($max_order) + 1) . "',
  1174. '" . intval($on_homepage) .
  1175. "')"
  1176. );
  1177. $id = Database:: insert_id();
  1178. $ipu = 'LinkAdded';
  1179. $rv = 2; // 2 = new
  1180. }
  1181. global $_course, $_user;
  1182. api_item_property_update(
  1183. $_course,
  1184. TOOL_LINK,
  1185. $id,
  1186. $ipu,
  1187. $_user['user_id']
  1188. );
  1189. if ($hidden && $ipu == 'LinkAdded') {
  1190. api_item_property_update(
  1191. $_course,
  1192. TOOL_LINK,
  1193. $id,
  1194. 'invisible',
  1195. $_user['user_id']
  1196. );
  1197. }
  1198. return $rv;
  1199. }
  1200. /**
  1201. * CSV file import functions
  1202. * @author René Haentjens , Ghent University
  1203. */
  1204. public static function import_link($linkdata)
  1205. {
  1206. // url, category_id, title, description, ...
  1207. // Field names used in the uploaded file
  1208. $known_fields = array(
  1209. 'url',
  1210. 'category',
  1211. 'title',
  1212. 'description',
  1213. 'on_homepage',
  1214. 'hidden'
  1215. );
  1216. $hide_fields = array(
  1217. 'kw',
  1218. 'kwd',
  1219. 'kwds',
  1220. 'keyword',
  1221. 'keywords'
  1222. );
  1223. // All other fields are added to description, as "name:value".
  1224. // Only one hide_field is assumed to be present, <> is removed from value.
  1225. if (!($url = trim($linkdata['url'])) || !($title = trim(
  1226. $linkdata['title']
  1227. ))
  1228. ) {
  1229. return 0; // 0 = fail
  1230. }
  1231. $cat = ($catname = trim($linkdata['category'])) ? Link::get_cat($catname) : 0;
  1232. $regs = array(); // Will be passed to ereg()
  1233. $d = '';
  1234. foreach ($linkdata as $key => $value) {
  1235. if (!in_array($key, $known_fields)) {
  1236. if (in_array($key, $hide_fields) && ereg(
  1237. '^<?([^>]*)>?$',
  1238. $value,
  1239. $regs
  1240. )
  1241. ) { // possibly in <...>
  1242. if (($kwlist = trim($regs[1])) != '') {
  1243. $kw = '<i kw="' . htmlspecialchars($kwlist) . '">';
  1244. } else {
  1245. $kw = '';
  1246. }
  1247. // i.e. assume only one of the $hide_fields will be present
  1248. // and if found, hide the value as expando property of an <i> tag
  1249. } elseif (trim($value)) {
  1250. $d .= ', ' . $key . ':' . $value;
  1251. }
  1252. }
  1253. }
  1254. if (!empty($d)) {
  1255. $d = substr($d, 2) . ' - ';
  1256. }
  1257. return Link::put_link(
  1258. $url,
  1259. $cat,
  1260. $title,
  1261. $kw . ereg_replace(
  1262. '\[((/?(b|big|i|small|sub|sup|u))|br/)\]',
  1263. '<\\1>',
  1264. htmlspecialchars($d . $linkdata['description'])
  1265. ) . ($kw ? '</i>' : ''),
  1266. $linkdata['on_homepage'] ? '1' : '0',
  1267. $linkdata['hidden'] ? '1' : '0'
  1268. );
  1269. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  1270. }
  1271. /**
  1272. * This function checks if the url is a vimeo link
  1273. * @author Julio Montoya
  1274. * @version 1.0
  1275. */
  1276. public static function isVimeoLink($url)
  1277. {
  1278. $isLink = strrpos($url, "vimeo.com");
  1279. return $isLink;
  1280. }
  1281. /**
  1282. * Get vimeo id from URL
  1283. * @param string $url
  1284. * @return bool|mixed
  1285. */
  1286. public static function getVimeoLinkId($url)
  1287. {
  1288. $possibleUrls = array(
  1289. 'http://www.vimeo.com/',
  1290. 'http://vimeo.com/',
  1291. 'https://www.vimeo.com/',
  1292. 'https://vimeo.com/'
  1293. );
  1294. $url = str_replace($possibleUrls, '', $url);
  1295. if (is_numeric($url)) {
  1296. return $url;
  1297. }
  1298. return false;
  1299. }
  1300. /**
  1301. * This function checks if the url is a youtube link
  1302. * @author Jorge Frisancho
  1303. * @author Julio Montoya - Fixing code
  1304. * @version 1.0
  1305. */
  1306. public static function is_youtube_link($url)
  1307. {
  1308. $is_youtube_link = strrpos($url, "youtube") || strrpos(
  1309. $url,
  1310. "youtu.be"
  1311. );
  1312. return $is_youtube_link;
  1313. }
  1314. /**
  1315. * Get youtube id from an URL
  1316. * @param string $url
  1317. * @return string
  1318. */
  1319. public static function get_youtube_video_id($url)
  1320. {
  1321. // This is the length of YouTube's video IDs
  1322. $len = 11;
  1323. // The ID string starts after "v=", which is usually right after
  1324. // "youtube.com/watch?" in the URL
  1325. $pos = strpos($url, "v=");
  1326. $id = '';
  1327. //If false try other options
  1328. if ($pos === false) {
  1329. $url_parsed = parse_url($url);
  1330. //Youtube shortener
  1331. //http://youtu.be/ID
  1332. $pos = strpos($url, "youtu.be");
  1333. if ($pos == false) {
  1334. $id = '';
  1335. } else {
  1336. return substr($url_parsed['path'], 1);
  1337. }
  1338. //if empty try the youtube.com/embed/ID
  1339. if (empty($id)) {
  1340. $pos = strpos($url, "embed");
  1341. if ($pos === false) {
  1342. return '';
  1343. } else {
  1344. return substr($url_parsed['path'], 7);
  1345. }
  1346. }
  1347. } else {
  1348. // Offset the start location to match the beginning of the ID string
  1349. $pos += 2;
  1350. // Get the ID string and return it
  1351. $id = substr($url, $pos, $len);
  1352. return $id;
  1353. }
  1354. }
  1355. public static function listLinksAndCategories($course_id, $session_id, $categoryId, $show = 'none', $token = null)
  1356. {
  1357. $tbl_link = Database::get_course_table(TABLE_LINK);
  1358. $_user = api_get_user_info();
  1359. $categoryId = intval($categoryId);
  1360. /* Action Links */
  1361. echo '<div class="actions">';
  1362. if (api_is_allowed_to_edit(null, true)) {
  1363. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addlink&category_id='.$categoryId.'">'.
  1364. Display::return_icon('new_link.png', get_lang('LinkAdd'),'',ICON_SIZE_MEDIUM).'</a>';
  1365. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addcategory&category_id='.$categoryId.'">'.
  1366. Display::return_icon('new_folder.png', get_lang('CategoryAdd'),'',ICON_SIZE_MEDIUM).'</a>';
  1367. }
  1368. $categories = Link::getLinkCategories($course_id, $session_id);
  1369. $count = count($categories);
  1370. if (!empty($count)) {
  1371. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=none">';
  1372. echo Display::return_icon('view_remove.png', get_lang('shownone'), '', ICON_SIZE_MEDIUM).'</a>';
  1373. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=list&show=all">';
  1374. echo Display::return_icon('view_tree.png', get_lang('showall'), '', ICON_SIZE_MEDIUM).'</a>';
  1375. }
  1376. echo '</div>';
  1377. // Displaying the links which have no category (thus category = 0 or NULL),
  1378. // if none present this will not be displayed
  1379. $sql = "SELECT * FROM $tbl_link
  1380. WHERE c_id = $course_id AND category_id=0 OR category_id IS NULL";
  1381. $result = Database::query($sql);
  1382. $count = Database::num_rows($result);
  1383. if ($count !== 0) {
  1384. echo '<table class="data_table">';
  1385. echo '<tr><th style="font-weight: bold; text-align:left;padding-left: 10px;">'.get_lang('General').'</th></tr>';
  1386. echo '</table>';
  1387. self::showlinksofcategory(0);
  1388. }
  1389. $i = 0;
  1390. $view = '0';
  1391. $counter = 0;
  1392. foreach ($categories as $myrow) {
  1393. // Student don't see invisible categories.
  1394. if (!api_is_allowed_to_edit(null, true)) {
  1395. if ($myrow['visibility'] == 0) {
  1396. continue;
  1397. }
  1398. }
  1399. // Validation when belongs to a session
  1400. $showChildren = $categoryId == $myrow['id'] || $show == 'all';
  1401. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  1402. $myrow['description'] = text_filter($myrow['description']);
  1403. $strVisibility = '';
  1404. $visibilityClass = null;
  1405. if ($myrow['visibility'] == '1') {
  1406. $strVisibility = '<a href="link.php?' . api_get_cidreq() . '&sec_token='.$token.'&action=invisible&id=' . $myrow['id'] . '&scope=' . TOOL_LINK_CATEGORY . '" title="' . get_lang('Hide') . '">' .
  1407. Display :: return_icon('visible.png', get_lang('Hide'), array (), ICON_SIZE_SMALL) . '</a>';
  1408. } elseif ($myrow['visibility'] == '0') {
  1409. $visibilityClass = 'invisible';
  1410. $strVisibility = ' <a href="link.php?' . api_get_cidreq() . '&sec_token='.$token.'&action=visible&id=' . $myrow['id'] . '&scope=' . TOOL_LINK_CATEGORY . '" title="' . get_lang('Show') . '">' .
  1411. Display :: return_icon('invisible.png', get_lang('Show'), array (), ICON_SIZE_SMALL) . '</a>';
  1412. }
  1413. echo '<tr>';
  1414. echo '<table class="data_table">';
  1415. echo '<tr>';
  1416. echo '<th width="81%" style="font-weight: bold; text-align:left;padding-left: 5px;">';
  1417. if ($showChildren) {
  1418. echo '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id=">';
  1419. echo Display::return_icon('view_remove.png');
  1420. } else {
  1421. echo '<a class="'.$visibilityClass.'" href="'.api_get_self().'?'.api_get_cidreq().'&category_id='.$myrow['id'].'">';
  1422. echo Display::return_icon('view_tree.png');
  1423. }
  1424. echo '&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).'</a>
  1425. <br />&nbsp;&nbsp;&nbsp;'.$myrow['description'];
  1426. echo '</th>';
  1427. if (api_is_allowed_to_edit(null, true)) {
  1428. if ($session_id == $myrow['session_id']) {
  1429. echo '<th>';
  1430. echo $strVisibility;
  1431. Link::showCategoryAdminTools($myrow, $counter, count($categories));
  1432. echo '</th>';
  1433. } else {
  1434. echo '<th>'.get_lang('EditionNotAvailableFromSession');
  1435. }
  1436. }
  1437. echo '</tr>';
  1438. echo '</table>';
  1439. if ($showChildren) {
  1440. echo Link::showlinksofcategory($myrow['id']);
  1441. }
  1442. echo '</tr>';
  1443. echo '</th>';
  1444. echo '</tr>';
  1445. echo '</table>';
  1446. echo '</tr>';
  1447. $counter++;
  1448. /*
  1449. if ($myrow['visibility'] == '1') {
  1450. if (isset($urlview[$i]) && $urlview[$i] == '1') {
  1451. $newurlview = $urlview;
  1452. $newurlview[$i] = '0';
  1453. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview='.Security::remove_XSS($newurlview).'">';
  1454. echo '<img src="../img/icons/22/view_remove.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).'</a>
  1455. <br />&nbsp;&nbsp;&nbsp;'.$myrow['description'];
  1456. echo '</th>';
  1457. if (api_is_allowed_to_edit(null, true)) {
  1458. if ($session_id == $myrow['session_id']) {
  1459. echo '<th>';
  1460. echo $strVisibility;
  1461. Link::showcategoryadmintools($myrow['id']);
  1462. echo '</th>';
  1463. } else {
  1464. echo '<th>'.get_lang('EditionNotAvailableFromSession');
  1465. }
  1466. }
  1467. echo '</tr>';
  1468. echo '</table>';
  1469. echo Link::showlinksofcategory($myrow['id']);
  1470. echo '</tr>';
  1471. } else {
  1472. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
  1473. echo is_array($view) ? implode('', $view) : $view;
  1474. echo '"><img src="../img/icons/22/view_tree.png" />&nbsp;&nbsp;'.
  1475. Security::remove_XSS($myrow['category_title']).$session_img;
  1476. echo'</a><br />&nbsp;&nbsp;&nbsp;';
  1477. echo $myrow['description'];
  1478. if (api_is_allowed_to_edit(null, true)) {
  1479. if ($session_id == $myrow['session_id']) {
  1480. echo '<th style="text-align:center;">';
  1481. echo $strVisibility;
  1482. Link::showcategoryadmintools($myrow['id']);
  1483. echo '</th>';
  1484. }
  1485. }
  1486. echo '</th>';
  1487. echo '</tr>';
  1488. echo '</table>';
  1489. echo '</tr>';
  1490. }
  1491. } else {
  1492. // NO VISIBLE
  1493. if (api_is_allowed_to_edit(null, true)) {
  1494. if (isset($urlview[$i]) && $urlview[$i] == '1') {
  1495. $newurlview = $urlview;
  1496. $newurlview[$i] = '0';
  1497. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview='.Security::remove_XSS($newurlview).'">';
  1498. echo '<img src="../img/icons/22/view_remove_na.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).'</a><br />&nbsp;&nbsp;&nbsp;'.$myrow['description'];
  1499. echo '</th>';
  1500. if (api_is_allowed_to_edit(null, true)) {
  1501. if ($session_id == $myrow['session_id']) {
  1502. echo '<th>'; echo $strVisibility; Link::showcategoryadmintools($myrow['id']); echo '</th>';
  1503. } else {
  1504. echo '<th>'.get_lang('EditionNotAvailableFromSession');
  1505. }
  1506. }
  1507. echo '</tr>';
  1508. echo '</table>';
  1509. echo Link::showlinksofcategory($myrow['id']);
  1510. echo '</tr>';
  1511. } else {
  1512. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&urlview=';
  1513. echo is_array($view) ? implode('', $view) : $view;
  1514. echo '"><img src="../img/icons/22/view_tree_na.png" />&nbsp;&nbsp;'.Security::remove_XSS($myrow['category_title']).$session_img;
  1515. echo'</a><br />&nbsp;&nbsp;&nbsp;';
  1516. echo $myrow['description'];
  1517. if (api_is_allowed_to_edit(null, true)) {
  1518. if ($session_id == $myrow['session_id']) {
  1519. echo '<th style="text-align:center;">';
  1520. echo $strVisibility;
  1521. Link::showcategoryadmintools($myrow['id']);
  1522. echo '</th>';
  1523. }
  1524. }
  1525. echo '</th>';
  1526. echo '</tr>';
  1527. echo '</table>';
  1528. echo '</tr>';
  1529. }
  1530. }*/
  1531. //}
  1532. // Displaying the link of the category
  1533. }
  1534. echo '</table>';
  1535. }
  1536. /**
  1537. * @param int $linkId
  1538. * @param $action
  1539. * @param null $urlview
  1540. * @param null $token
  1541. * @return FormValidator
  1542. */
  1543. public static function getLinkForm($linkId, $action, $token = null)
  1544. {
  1545. $course_id = api_get_course_int_id();
  1546. $session_id = api_get_session_id();
  1547. $linkInfo = Link::get_link_info($linkId);
  1548. $categoryId = isset($linkInfo['category_id']) ? $linkInfo['category_id'] : '';
  1549. $lpId = isset($_GET['lp_id']) ? Security::remove_XSS($_GET['lp_id']) : null;
  1550. $category = 0;
  1551. $form = new FormValidator(
  1552. 'link',
  1553. 'post',
  1554. api_get_self().'?action='.$action.
  1555. '&category_id='.$categoryId.
  1556. '&'.api_get_cidreq().
  1557. '&id='.$linkId.
  1558. '&sec_token='.$token
  1559. );
  1560. if ($action == 'addlink') {
  1561. $form->addHeader(get_lang('LinkAdd'));
  1562. } else {
  1563. $form->addHeader(get_lang('LinkMod'));
  1564. }
  1565. $target_link = "_blank";
  1566. $title = '';
  1567. $category = '';
  1568. $onhomepage = '';
  1569. $description = '';
  1570. if (!empty($linkInfo)) {
  1571. $urllink = $linkInfo['url'];
  1572. $title = $linkInfo['title'];
  1573. $description = $linkInfo['description'];
  1574. $category = $linkInfo['category_id'];
  1575. if ($linkInfo['on_homepage'] != 0) {
  1576. $onhomepage = 1;
  1577. }
  1578. $target_link = $linkInfo['target'];
  1579. }
  1580. $form->addHidden('id', $linkId);
  1581. $form->addText('url', 'URL');
  1582. $form->addRule('url', get_lang('GiveURL'), 'url');
  1583. $form->addText('title', get_lang('LinkName'));
  1584. $form->addTextarea('description', get_lang('Description'));
  1585. $resultcategories = Link::getLinkCategories($course_id, $session_id);
  1586. $options = ['0' => '--'];
  1587. if (!empty($resultcategories)) {
  1588. foreach ($resultcategories as $myrow) {
  1589. $options[$myrow['id']] = $myrow['category_title'];
  1590. }
  1591. }
  1592. $form->addSelect('category_id', get_lang('Category'), $options);
  1593. $form->addCheckBox('on_homepage', null, get_lang('OnHomepage'));
  1594. $targets = array(
  1595. '_self' => get_lang('LinkOpenSelf'),
  1596. '_blank' => get_lang('LinkOpenBlank'),
  1597. '_parent' => get_lang('LinkOpenParent'),
  1598. '_top' => get_lang('LinkOpenTop')
  1599. );
  1600. $form->addSelect(
  1601. 'target',
  1602. array(
  1603. get_lang('LinkTarget'),
  1604. get_lang('AddTargetOfLinkOnHomepage')
  1605. ),
  1606. $targets
  1607. );
  1608. $defaults = array(
  1609. 'url' => empty($urllink) ? 'http://' : Security::remove_XSS($urllink),
  1610. 'title' => Security::remove_XSS($title),
  1611. 'category_id' => $category,
  1612. 'on_homepage' => $onhomepage,
  1613. 'description' => $description,
  1614. 'target' => $target_link
  1615. );
  1616. if (api_get_setting('search_enabled') == 'true') {
  1617. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1618. $specific_fields = get_specific_field_list();
  1619. $form->addCheckBox('index_document', get_lang('SearchFeatureDoIndexLink'), get_lang('Yes'));
  1620. foreach ($specific_fields as $specific_field) {
  1621. $default_values = '';
  1622. if ($action == 'editlink') {
  1623. $filter = array(
  1624. 'field_id' => $specific_field['id'],
  1625. 'ref_id' => intval($_GET['id']),
  1626. 'tool_id' => '\''.TOOL_LINK.'\''
  1627. );
  1628. $values = get_specific_field_values_list($filter, array('value'));
  1629. if (!empty($values)) {
  1630. $arr_str_values = array();
  1631. foreach ($values as $value) {
  1632. $arr_str_values[] = $value['value'];
  1633. }
  1634. $default_values = implode(', ', $arr_str_values);
  1635. }
  1636. }
  1637. $form->addText($specific_field['name'], $specific_field['code']);
  1638. $defaults[$specific_field['name']] = $default_values;
  1639. }
  1640. }
  1641. $form->addHidden('lp_id', $lpId);
  1642. $form->addButtonSave(get_lang('SaveLink'), 'submitLink');
  1643. $form->setDefaults($defaults);
  1644. return $form;
  1645. }
  1646. /**
  1647. * @param int $id
  1648. * @param string $action
  1649. *
  1650. * @return FormValidator
  1651. */
  1652. public static function getCategoryForm($id, $action)
  1653. {
  1654. $form = new FormValidator(
  1655. 'category',
  1656. 'post',
  1657. api_get_self().'?action='.$action.'&'.api_get_cidreq()
  1658. );
  1659. $defaults = [];
  1660. if ($action == 'addcategory') {
  1661. $form->addHeader(get_lang('CategoryAdd'));
  1662. $my_cat_title = get_lang('CategoryAdd');
  1663. } else {
  1664. $form->addHeader(get_lang('CategoryMod'));
  1665. $my_cat_title = get_lang('CategoryMod');
  1666. $defaults = self::getCategory($id);
  1667. }
  1668. $form->addHidden('id', $id);
  1669. $form->addText('category_title', get_lang('CategoryName'));
  1670. $form->addTextarea('description', get_lang('Description'));
  1671. $form->addButtonSave($my_cat_title, 'submitCategory');
  1672. $form->setDefaults($defaults);
  1673. return $form;
  1674. }
  1675. /**
  1676. * @param int $id
  1677. * @return array
  1678. */
  1679. public static function getCategory($id)
  1680. {
  1681. $table = Database::get_course_table(TABLE_LINK_CATEGORY);
  1682. $id = intval($id);
  1683. $courseId = api_get_course_int_id();
  1684. $sql = "SELECT * FROM $table WHERE id = $id AND c_id = $courseId";
  1685. $result = Database::query($sql);
  1686. $category = Database::fetch_array($result, 'ASSOC');
  1687. return $category;
  1688. }
  1689. }