link.lib.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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. }