link.lib.php 70 KB

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