link.lib.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. /* FUNCTIONS */
  20. /**
  21. * Used to add a link or a category
  22. * @param string $type, "link" or "category"
  23. * @todo replace strings by constants
  24. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  25. */
  26. function addlinkcategory($type) {
  27. global $catlinkstatus;
  28. global $msgErr;
  29. $ok = true;
  30. if ($type == 'link') {
  31. $tbl_link = Database :: get_course_table(TABLE_LINK);
  32. $title = Security :: remove_XSS(stripslashes($_POST['title']));
  33. $urllink = Security :: remove_XSS($_POST['urllink']);
  34. $description = Security :: remove_XSS($_POST['description']);
  35. $selectcategory = Security :: remove_XSS($_POST['selectcategory']);
  36. if ($_POST['onhomepage'] == '') {
  37. $onhomepage = 0;
  38. $target = '_self'; // Default target.
  39. } else {
  40. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  41. $target = Security :: remove_XSS($_POST['target_link']);
  42. }
  43. $urllink = trim($urllink);
  44. $title = trim($title);
  45. $description = trim($description);
  46. // We ensure URL to be absolute.
  47. if (strpos($urllink, '://') === false) {
  48. $urllink = 'http://' . $urllink;
  49. }
  50. // If the title is empty, we use the URL as title.
  51. if ($title == '') {
  52. $title = $urllink;
  53. }
  54. // If the URL is invalid, an error occurs.
  55. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  56. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  57. if (!api_valid_url($urllink, true)) { // A check against an absolute URL.
  58. //
  59. $msgErr = get_lang('GiveURL');
  60. Display :: display_error_message(get_lang('GiveURL'));
  61. $ok = false;
  62. } else {
  63. // Looking for the largest order number for this category.
  64. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_link . " WHERE category_id = '" . intval($_POST['selectcategory']) . "'");
  65. list ($orderMax) = Database :: fetch_row($result);
  66. $order = $orderMax +1;
  67. $session_id = api_get_session_id();
  68. $sql = "INSERT INTO " . $tbl_link . " (url, title, description, category_id, display_order, on_homepage, target, session_id)
  69. VALUES ('" . Database :: escape_string($urllink) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" .
  70. Database :: escape_string($selectcategory) . "','" . Database :: escape_string($order) . "', '" . Database :: escape_string($onhomepage) . "','" .
  71. Database :: escape_string($target) . "','" . Database :: escape_string($session_id) . "')";
  72. $catlinkstatus = get_lang('LinkAdded');
  73. Database :: query($sql);
  74. $link_id = Database :: insert_id();
  75. if ((api_get_setting('search_enabled') == 'true') && $link_id && extension_loaded('xapian')) {
  76. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  77. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  78. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  79. $courseid = api_get_course_id();
  80. $specific_fields = get_specific_field_list();
  81. $ic_slide = new IndexableChunk();
  82. // Add all terms to db.
  83. $all_specific_terms = '';
  84. foreach ($specific_fields as $specific_field) {
  85. if (isset ($_REQUEST[$specific_field['code']])) {
  86. $sterms = trim($_REQUEST[$specific_field['code']]);
  87. if (!empty ($sterms)) {
  88. $all_specific_terms .= ' ' . $sterms;
  89. $sterms = explode(',', $sterms);
  90. foreach ($sterms as $sterm) {
  91. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  92. add_specific_field_value($specific_field['id'], $courseid, TOOL_LINK, $link_id, $sterm);
  93. }
  94. }
  95. }
  96. }
  97. // Build the chunk to index.
  98. $ic_slide->addValue('title', $title);
  99. $ic_slide->addCourseId($courseid);
  100. $ic_slide->addToolId(TOOL_LINK);
  101. $xapian_data = array (
  102. SE_COURSE_ID => $courseid,
  103. SE_TOOL_ID => TOOL_LINK,
  104. SE_DATA => array (
  105. 'link_id' => (int) $link_id
  106. ),
  107. SE_USER => (int) api_get_user_id(),
  108. );
  109. $ic_slide->xapian_data = serialize($xapian_data);
  110. $description = $all_specific_terms . ' ' . $description;
  111. $ic_slide->addValue('content', $description);
  112. // Add category name if set.
  113. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  114. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  115. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  116. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory);
  117. $result = Database :: query($sql_cat);
  118. if (Database :: num_rows($result) == 1) {
  119. $row = Database :: fetch_array($result);
  120. $ic_slide->addValue('category', $row['category_title']);
  121. }
  122. }
  123. $di = new DokeosIndexer();
  124. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  125. $di->connectDb(NULL, NULL, $lang);
  126. $di->addChunk($ic_slide);
  127. // Index and return search engine document id.
  128. $did = $di->index();
  129. if ($did) {
  130. // Save it to db.
  131. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  132. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  133. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  134. $sql = sprintf($sql, $tbl_se_ref, $courseid, TOOL_LINK, $link_id, $did);
  135. Database :: query($sql);
  136. }
  137. }
  138. unset ($urllink, $title, $description, $selectcategory);
  139. Display :: display_confirmation_message(get_lang('LinkAdded'));
  140. }
  141. } elseif ($type == 'category') {
  142. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  143. $category_title = trim($_POST['category_title']);
  144. $description = trim($_POST['description']);
  145. if (empty($category_title)) {
  146. $msgErr = get_lang('GiveCategoryName');
  147. Display :: display_error_message(get_lang('GiveCategoryName'));
  148. $ok = false;
  149. } else {
  150. // Looking for the largest order number for this category.
  151. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories);
  152. list ($orderMax) = Database :: fetch_row($result);
  153. $order = $orderMax +1;
  154. $order = intval($order);
  155. $session_id = api_get_session_id();
  156. $sql = "INSERT INTO ".$tbl_categories." (category_title, description, display_order, session_id)
  157. VALUES ('" .Database::escape_string($category_title) . "', '" . Database::escape_string($description) . "', '$order', '$session_id')";
  158. Database :: query($sql);
  159. $catlinkstatus = get_lang('CategoryAdded');
  160. unset ($category_title, $description);
  161. Display :: display_confirmation_message(get_lang('CategoryAdded'));
  162. }
  163. }
  164. // "WHAT'S NEW" notification : update last tool Edit.
  165. if ($type == 'link') {
  166. global $_user;
  167. global $_course;
  168. global $nameTools;
  169. api_item_property_update($_course, TOOL_LINK, $link_id, 'LinkAdded', $_user['user_id']);
  170. }
  171. return $ok;
  172. }
  173. /**
  174. * Used to delete a link or a category
  175. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  176. */
  177. function deletelinkcategory($type) {
  178. global $catlinkstatus;
  179. global $_course;
  180. $tbl_link = Database :: get_course_table(TABLE_LINK);
  181. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  182. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  183. if ($type == 'link') {
  184. global $id;
  185. // -> Items are no longer fysically deleted, but the visibility is set to 2 (in item_property).
  186. // This will make a restore function possible for the platform administrator.
  187. if (isset ($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
  188. $sql = "UPDATE $tbl_link SET on_homepage='0' WHERE id='" . intval($_GET['id']) . "'";
  189. Database :: query($sql);
  190. }
  191. api_item_property_update($_course, TOOL_LINK, $id, 'delete', api_get_user_id());
  192. delete_link_from_search_engine(api_get_course_id(), $id);
  193. $catlinkstatus = get_lang('LinkDeleted');
  194. unset ($id);
  195. Display :: display_confirmation_message(get_lang('LinkDeleted'));
  196. }
  197. if ($type == 'category') {
  198. global $id;
  199. if (isset ($_GET['id']) && !empty ($_GET['id'])) {
  200. // First we delete the category itself and afterwards all the links of this category.
  201. $sql = "DELETE FROM " . $tbl_categories . " WHERE id='" . intval($_GET['id']) . "'";
  202. Database :: query($sql);
  203. $sql = "DELETE FROM " . $tbl_link . " WHERE category_id='" . intval($_GET['id']) . "'";
  204. $catlinkstatus = get_lang('CategoryDeleted');
  205. unset ($id);
  206. Database :: query($sql);
  207. Display :: display_confirmation_message(get_lang('CategoryDeleted'));
  208. }
  209. }
  210. }
  211. /**
  212. * Removes a link from search engine database
  213. *
  214. * @param string $course_id Course code
  215. * @param int $document_id Document id to delete
  216. */
  217. function delete_link_from_search_engine($course_id, $link_id) {
  218. // Remove from search engine if enabled.
  219. if (api_get_setting('search_enabled') == 'true') {
  220. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  221. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  222. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  223. $res = Database :: query($sql);
  224. if (Database :: num_rows($res) > 0) {
  225. $row = Database :: fetch_array($res);
  226. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  227. $di = new DokeosIndexer();
  228. $di->remove_document((int) $row['search_did']);
  229. }
  230. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  231. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  232. Database :: query($sql);
  233. // Remove terms from db.
  234. require_once (api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  235. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  236. }
  237. }
  238. /**
  239. * Used to edit a link or a category
  240. * @todo Rewrite the whole links tool because it is becoming completely cluttered,
  241. * code does not follow the coding conventions, does not use html_quickform, ...
  242. * Some features were patched in.
  243. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  244. * @todo replace the globals with the appropriate $_POST or $_GET values
  245. */
  246. function editlinkcategory($type) {
  247. global $catlinkstatus;
  248. global $id;
  249. global $submit_link;
  250. global $submit_category;
  251. global $_user;
  252. global $_course;
  253. global $nameTools;
  254. global $urllink;
  255. global $title;
  256. global $description;
  257. global $category;
  258. global $selectcategory;
  259. global $description;
  260. global $category_title;
  261. global $onhomepage;
  262. global $target_link;
  263. $tbl_link = Database :: get_course_table(TABLE_LINK);
  264. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  265. if ($type == 'link') {
  266. // This is used to populate the link-form with the info found in the database.
  267. if (!empty ($_GET['id'])) {
  268. $sql = "SELECT * FROM " . $tbl_link . " WHERE id='" . intval($_GET['id']) . "'";
  269. $result = Database :: query($sql);
  270. if ($myrow = Database :: fetch_array($result)) {
  271. $urllink = $myrow['url'];
  272. $title = $myrow['title'];
  273. $description = $myrow['description'];
  274. $category = $myrow['category_id'];
  275. if ($myrow['on_homepage'] != 0) {
  276. $onhomepage = 'checked';
  277. }
  278. $target_link = $myrow['target'];
  279. }
  280. }
  281. // This is used to put the modified info of the link-form into the database.
  282. if ($_POST['submitLink']) {
  283. // Ivan, 13-OCT-2010: It is a litle bit messy code below, just in case I added some extra-security checks here.
  284. $_POST['urllink'] = trim(Security :: remove_XSS($_POST['urllink']));
  285. $_POST['title'] = trim(Security :: remove_XSS($_POST['title']));
  286. $_POST['description'] = trim(Security :: remove_XSS($_POST['description']));
  287. $_POST['selectcategory'] = intval($_POST['selectcategory']);
  288. $_POST['id'] = intval($_POST['id']);
  289. // We ensure URL to be absolute.
  290. if (strpos($_POST['urllink'], '://') === false) {
  291. $_POST['urllink'] = 'http://' . $_POST['urllink'];
  292. }
  293. // If the title is empty, we use the URL as title.
  294. if ($_POST['title'] == '') {
  295. $_POST['title'] = $_POST['urllink'];
  296. }
  297. // If the URL is invalid, an error occurs.
  298. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  299. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  300. if (!api_valid_url($urllink, true)) { // A check against an absolute URL.
  301. $msgErr = get_lang('GiveURL');
  302. Display :: display_error_message(get_lang('GiveURL'));
  303. return false;
  304. }
  305. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  306. $target = Database::escape_string($_POST['target_link']);
  307. if (empty ($mytarget)) {
  308. $mytarget = '_self';
  309. }
  310. $mytarget = ",target='" . $target . "'";
  311. // Finding the old category_id.
  312. $sql = "SELECT * FROM " . $tbl_link . " WHERE id='" . intval($_POST['id']) . "'";
  313. $result = Database :: query($sql);
  314. $row = Database :: fetch_array($result);
  315. $category_id = $row['category_id'];
  316. if ($category_id != $_POST['selectcategory']) {
  317. $sql = "SELECT MAX(display_order) FROM " . $tbl_link . " WHERE category_id='" . intval($_POST['selectcategory']) . "'";
  318. $result = Database :: query($sql);
  319. list ($max_display_order) = Database :: fetch_row($result);
  320. $max_display_order++;
  321. } else {
  322. $max_display_order = $row['display_order'];
  323. }
  324. $sql = "UPDATE " . $tbl_link . " SET " .
  325. "url='" . Database :: escape_string($_POST['urllink']) . "', " .
  326. "title='" . Database :: escape_string($_POST['title']) . "', " .
  327. "description='" . Database :: escape_string($_POST['description']) . "', " .
  328. "category_id='" . Database :: escape_string($_POST['selectcategory']) . "', " .
  329. "display_order='" . $max_display_order . "', " .
  330. "on_homepage='" . Database :: escape_string($onhomepage) . " ' $mytarget " .
  331. " WHERE id='" . intval($_POST['id']) . "'";
  332. Database :: query($sql);
  333. // Update search enchine and its values table if enabled.
  334. if (api_get_setting('search_enabled') == 'true') {
  335. $link_id = intval($_POST['id']);
  336. $course_id = api_get_course_id();
  337. $link_url = Database :: escape_string($_POST['urllink']);
  338. $link_title = Database :: escape_string($_POST['title']);
  339. $link_description = Database :: escape_string($_POST['description']);
  340. // Actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one.
  341. // Get search_did.
  342. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  343. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  344. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  345. $res = Database :: query($sql);
  346. if (Database :: num_rows($res) > 0) {
  347. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  348. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  349. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  350. $se_ref = Database :: fetch_array($res);
  351. $specific_fields = get_specific_field_list();
  352. $ic_slide = new IndexableChunk();
  353. $all_specific_terms = '';
  354. foreach ($specific_fields as $specific_field) {
  355. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  356. if (isset ($_REQUEST[$specific_field['code']])) {
  357. $sterms = trim($_REQUEST[$specific_field['code']]);
  358. if (!empty ($sterms)) {
  359. $all_specific_terms .= ' ' . $sterms;
  360. $sterms = explode(',', $sterms);
  361. foreach ($sterms as $sterm) {
  362. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  363. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  364. }
  365. }
  366. }
  367. }
  368. // Build the chunk to index.
  369. $ic_slide->addValue("title", $link_title);
  370. $ic_slide->addCourseId($course_id);
  371. $ic_slide->addToolId(TOOL_LINK);
  372. $xapian_data = array (
  373. SE_COURSE_ID => $course_id,
  374. SE_TOOL_ID => TOOL_LINK,
  375. SE_DATA => array (
  376. 'link_id' => (int) $link_id
  377. ),
  378. SE_USER => (int) api_get_user_id(),
  379. );
  380. $ic_slide->xapian_data = serialize($xapian_data);
  381. $link_description = $all_specific_terms . ' ' . $link_description;
  382. $ic_slide->addValue('content', $link_description);
  383. // Add category name if set.
  384. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  385. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  386. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  387. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory);
  388. $result = Database :: query($sql_cat);
  389. if (Database :: num_rows($result) == 1) {
  390. $row = Database :: fetch_array($result);
  391. $ic_slide->addValue('category', $row['category_title']);
  392. }
  393. }
  394. $di = new DokeosIndexer();
  395. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  396. $di->connectDb(NULL, NULL, $lang);
  397. $di->remove_document((int) $se_ref['search_did']);
  398. $di->addChunk($ic_slide);
  399. // Index and return search engine document id.
  400. $did = $di->index();
  401. if ($did) {
  402. // Save it to db.
  403. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  404. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  405. Database :: query($sql);
  406. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  407. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  408. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id, $did);
  409. Database :: query($sql);
  410. }
  411. }
  412. }
  413. // "WHAT'S NEW" notification: update table last_toolEdit.
  414. api_item_property_update($_course, TOOL_LINK, $_POST['id'], 'LinkUpdated', $_user['user_id']);
  415. Display :: display_confirmation_message(get_lang('LinkModded'));
  416. }
  417. }
  418. if ($type == 'category') {
  419. // This is used to populate the category-form with the info found in the database.
  420. if (!$submit_category) {
  421. $sql = "SELECT * FROM " . $tbl_categories . " WHERE id='" . intval($_GET['id']) . "'";
  422. $result = Database :: query($sql);
  423. if ($myrow = Database :: fetch_array($result)) {
  424. $category_title = $myrow['category_title'];
  425. $description = $myrow['description'];
  426. }
  427. }
  428. // This is used to put the modified info of the category-form into the database.
  429. if ($submit_category) {
  430. $sql = "UPDATE " . $tbl_categories . " set category_title='" . Database :: escape_string($_POST['category_title']) . "', description='" . Database :: escape_string($_POST['description']) . "' WHERE id='" . Database :: escape_string($_POST['id']) . "'";
  431. Database :: query($sql);
  432. Display :: display_confirmation_message(get_lang('CategoryModded'));
  433. }
  434. }
  435. return true; // On errors before this statement, exit from this function by returning false value.
  436. }
  437. /**
  438. * Creates a correct $view for in the URL
  439. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  440. */
  441. function makedefaultviewcode($locatie) {
  442. global $aantalcategories, $view;
  443. for ($j = 0; $j <= $aantalcategories -1; $j++) {
  444. $view[$j] = 0;
  445. }
  446. $view[intval($locatie)] = '1';
  447. }
  448. /**
  449. * Changes the visibility of a link
  450. * @todo add the changing of the visibility of a course
  451. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  452. */
  453. function change_visibility($id, $scope) {
  454. global $_course, $_user;
  455. if ($scope == 'link') {
  456. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  457. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  458. }
  459. }
  460. /**
  461. * Displays all the links of a given category.
  462. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  463. */
  464. function showlinksofcategory($catid) {
  465. global $is_allowed, $charset, $urlview, $up, $down, $_user;
  466. $tbl_link = Database :: get_course_table(TABLE_LINK);
  467. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  468. // Condition for the session.
  469. $session_id = api_get_session_id();
  470. $condition_session = api_get_session_condition($session_id, true, true);
  471. $catid = intval($catid);
  472. $sqlLinks = "SELECT * FROM " . $tbl_link . " link, " . $TABLE_ITEM_PROPERTY . " itemproperties WHERE itemproperties.tool='" . TOOL_LINK . "' AND link.id=itemproperties.ref AND link.category_id='" . $catid . "' AND (itemproperties.visibility='0' OR itemproperties.visibility='1') $condition_session ORDER BY link.display_order DESC";
  473. $result = Database :: query($sqlLinks);
  474. $numberoflinks = Database :: num_rows($result);
  475. if ($numberoflinks > 0) {
  476. echo '<table class="data_table" width="100%">';
  477. $i = 1;
  478. while ($myrow = Database :: fetch_array($result)) {
  479. // Validacion when belongs to a session.
  480. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  481. $css_class = $i % 2 == 0 ? $css_class = 'row_odd' : $css_class = 'row_even';
  482. $link_validator = '';
  483. if (api_is_allowed_to_edit(null, true)) {
  484. $link_validator = ''.Display::url(Display::return_icon('preview_view.png', get_lang('CheckURL'), array(), 16), '#', array('onclick'=>"check_url('".$myrow['id']."', '".addslashes($myrow['url'])."');"));
  485. $link_validator .= Display::span('', array('id'=>'url_id_'.$myrow['id']));
  486. }
  487. if ($myrow['visibility'] == '1') {
  488. echo '<tr class="'.$css_class.'">';
  489. echo '<td align="center" valign="middle" width="15">';
  490. echo '<a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow[0], '&amp;link_url=', urlencode($myrow[1]), '" target="_blank"><img src="../../main/img/link.gif" border="0" alt="', get_lang('Link'), '"/></a></td><td width="80%" valign="top"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow[0], '&amp;link_url=', urlencode($myrow[1]), '" target="', $myrow['target'], '">';
  491. echo Security :: remove_XSS($myrow[2]);
  492. echo '</a>';
  493. echo $link_validator;
  494. echo $session_img;
  495. echo '<br />', $myrow[3];
  496. } else {
  497. if (api_is_allowed_to_edit(null, true)) {
  498. echo '<tr class="'.$css_class.'">';
  499. echo '<td align="center" valign="middle" width="15"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow[0], "&amp;link_url=", urlencode($myrow[1]), '" target="_blank" class="invisible">';
  500. echo Display :: return_icon('link_na.gif', get_lang('Link')), '</a>';
  501. echo '</td><td width="80%" valign="top"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow[0], '&amp;link_url=', urlencode($myrow[1]),'" target="', $myrow['target'], '" class="invisible">';
  502. echo Security :: remove_XSS($myrow[2]);
  503. echo "</a>";
  504. echo $link_validator;
  505. echo $session_img, '<br />', $myrow[3];
  506. }
  507. }
  508. echo '<td style="text-align:center;">';
  509. if (api_is_allowed_to_edit(null, true)) {
  510. if ($session_id == $myrow['session_id']) {
  511. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;action=editlink&amp;category=' . (!empty ($category) ? $category : '') . '&amp;id=' . $myrow[0] . '&amp;urlview=' . $urlview . '" title="' . get_lang('Modify') . '">' . Display :: return_icon('edit.png', get_lang('Modify'), array (), 22) . '</a>';
  512. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  513. if ($i != 1) {
  514. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;urlview=' . $urlview . '&amp;up=', $myrow[0], '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), 22) . '', "</a>\n";
  515. } else {
  516. echo Display :: return_icon('up.png', get_lang('Up'), array (), 22) . '</a>';
  517. }
  518. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  519. if ($i < $numberoflinks) {
  520. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;urlview=' . $urlview . '&amp;down=' . $myrow[0] . '" title="' . get_lang('Down') . '">' . Display :: return_icon('down.png', get_lang('Down'), array (), 22) . '', "</a>\n";
  521. } else {
  522. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), 22) . '', "</a>\n";
  523. }
  524. if ($myrow['visibility'] == '1') {
  525. echo '<a href="link.php?' . api_get_cidreq() . '&amp;action=invisible&amp;id=' . $myrow[0] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Hide') . '">' . Display :: return_icon('visible.png', get_lang('Hide'), array (), 22) . '</a>';
  526. }
  527. if ($myrow['visibility'] == '0') {
  528. echo ' <a href="link.php?' . api_get_cidreq() . '&amp;action=visible&amp;id=' . $myrow[0] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Show') . '">' . Display :: return_icon('invisible.png', get_lang('Show'), array (), 22) . '</a>';
  529. }
  530. echo ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;action=deletelink&amp;id=', $myrow[0], '&amp;urlview=', $urlview, "\" onclick=\"javascript: if(!confirm('" . get_lang('LinkDelconfirm') . "')) return false;\" title=\"" . get_lang('Delete') . '">' . Display :: return_icon('delete.png', get_lang('Delete'), array (), 22) . '</a>';
  531. } else {
  532. echo get_lang('EditionNotAvailableFromSession');
  533. }
  534. }
  535. echo '</td></tr>';
  536. $i++;
  537. }
  538. echo '</table>';
  539. }
  540. }
  541. /**
  542. * Displays the edit, delete and move icons
  543. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  544. */
  545. function showcategoryadmintools($categoryid) {
  546. global $urlview;
  547. global $aantalcategories;
  548. global $catcounter;
  549. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;action=editcategory&amp;id=' . $categoryid . '&amp;urlview=' . $urlview . '" title=' . get_lang('Modify') . '">' . Display :: return_icon('edit.png', get_lang('Modify'), array (), 22) . '</a>';
  550. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;action=deletecategory&amp;id=', $categoryid, "&amp;urlview=$urlview\" onclick=\"javascript: if(!confirm('" . get_lang('CategoryDelconfirm') . "')) return false;\">", Display :: return_icon('delete.png', get_lang('Delete'), array (), 22) . '</a>';
  551. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  552. if ($catcounter != 1) {
  553. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;catmove=true&amp;up=', $categoryid, '&amp;urlview=' . $urlview . '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), 22) . '</a>';
  554. } else {
  555. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), 22) . '</a>';
  556. }
  557. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  558. if ($catcounter < $aantalcategories) {
  559. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;catmove=true&amp;down=' . $categoryid . '&amp;urlview=' . $urlview . '">
  560. ' . Display :: return_icon('down.png', get_lang('Down'), array (), 22) . '</a>';
  561. } else {
  562. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), 22) . '</a>';
  563. }
  564. $catcounter++;
  565. }
  566. /**
  567. * move a link or a linkcategory up or down
  568. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  569. */
  570. function movecatlink($catlinkid) {
  571. global $catmove;
  572. global $up;
  573. global $down;
  574. $tbl_link = Database :: get_course_table(TABLE_LINK);
  575. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  576. if (!empty ($down)) {
  577. $thiscatlinkId = intval($down);
  578. $sortDirection = 'DESC';
  579. }
  580. if (!empty ($up)) {
  581. $thiscatlinkId = intval($up);
  582. $sortDirection = 'ASC';
  583. }
  584. // We check if it is a category we are moving or a link. If it is a category, a querystring catmove = true is present in the url.
  585. if ($catmove == 'true') {
  586. $movetable = $tbl_categories;
  587. $catid = $catlinkid;
  588. } else {
  589. $movetable = $tbl_link;
  590. // Getting the category of the link.
  591. if (!empty ($thiscatlinkId)) {
  592. $sql = "SELECT category_id from " . $movetable . " WHERE id='$thiscatlinkId'";
  593. $result = Database :: query($sql);
  594. $catid = Database :: fetch_array($result);
  595. }
  596. }
  597. // This code is copied and modified from announcements.php.
  598. if (!empty($sortDirection)) {
  599. if (!in_array(trim(strtoupper($sortDirection)), array (
  600. 'ASC',
  601. 'DESC'
  602. )))
  603. die('Bad sort direction used.'); // Sanity check of sortDirection var.
  604. if ($catmove == 'true') {
  605. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . " ORDER BY display_order $sortDirection";
  606. } else {
  607. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . " WHERE category_id='" . $catid[0] . "' ORDER BY display_order $sortDirection";
  608. }
  609. $linkresult = Database :: query($sqlcatlinks);
  610. while ($sortrow = Database :: fetch_array($linkresult)) {
  611. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  612. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  613. // and we know the next iteration of the while loop is the next one. These should be swapped.
  614. if (isset ($thislinkFound) && $thislinkFound) {
  615. $nextlinkId = $sortrow['id'];
  616. $nextlinkOrdre = $sortrow['display_order'];
  617. Database :: query("UPDATE " . $movetable . "
  618. SET display_order = '$nextlinkOrdre'
  619. WHERE id = '$thiscatlinkId'");
  620. Database :: query("UPDATE " . $movetable . "
  621. SET display_order = '$thislinkOrdre'
  622. WHERE id = '$nextlinkId'");
  623. break;
  624. }
  625. if ($sortrow['id'] == $thiscatlinkId) {
  626. $thislinkOrdre = $sortrow['display_order'];
  627. $thislinkFound = true;
  628. }
  629. }
  630. }
  631. Display :: display_confirmation_message(get_lang('LinkMoved'));
  632. }
  633. /**
  634. * CSV file import functions
  635. * @author René Haentjens , Ghent University
  636. */
  637. function get_cat($catname) {
  638. // Get category id (existing or make new).
  639. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  640. $result = Database :: query("SELECT id FROM " . $tbl_categories . " WHERE category_title='" . Database::escape_string($catname) . "'");
  641. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  642. return $row['id']; // Several categories with same name: take the first.
  643. }
  644. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories);
  645. list ($max_order) = Database :: fetch_row($result);
  646. Database :: query("INSERT INTO " . $tbl_categories . " (category_title, description, display_order) VALUES ('" . Database::escape_string($catname) . "','','" . ($max_order +1) . "')");
  647. return Database :: insert_id();
  648. }
  649. /**
  650. * CSV file import functions
  651. * @author René Haentjens , Ghent University
  652. */
  653. function put_link($url, $cat, $title, $description, $on_homepage, $hidden) {
  654. $tbl_link = Database :: get_course_table(TABLE_LINK);
  655. $urleq = "url='" . Database :: escape_string($url) . "'";
  656. $cateq = "category_id=" . intval($cat);
  657. $result = Database :: query("SELECT id FROM $tbl_link WHERE " . $urleq . ' AND ' . $cateq);
  658. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  659. Database :: query("UPDATE $tbl_link set title='" . Database :: escape_string($title) . "', description='" . Database :: escape_string($description) . "' WHERE id='" . Database :: escape_string($row['id']) . "'");
  660. $ipu = 'LinkUpdated';
  661. $rv = 1; // 1 = upd
  662. } else {
  663. // Add new link
  664. $result = Database :: query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='" . intval($cat) . "'");
  665. list ($max_order) = Database :: fetch_row($result);
  666. Database :: query("INSERT INTO $tbl_link (url, title, description, category_id, display_order, on_homepage) VALUES ('" . Database :: escape_string($url) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" . intval($cat) . "','" . (intval($max_order) + 1) . "','" . intval($on_homepage) . "')");
  667. $id = Database :: insert_id();
  668. $ipu = 'LinkAdded';
  669. $rv = 2; // 2 = new
  670. }
  671. global $_course, $nameTools, $_user;
  672. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  673. if ($hidden && $ipu == 'LinkAdded') {
  674. api_item_property_update($_course, TOOL_LINK, $id, 'invisible', $_user['user_id']);
  675. }
  676. return $rv;
  677. }
  678. /**
  679. * CSV file import functions
  680. * @author René Haentjens , Ghent University
  681. */
  682. function import_link($linkdata) {
  683. // url, category_id, title, description, ...
  684. // Field names used in the uploaded file
  685. $known_fields = array (
  686. 'url',
  687. 'category',
  688. 'title',
  689. 'description',
  690. 'on_homepage',
  691. 'hidden'
  692. );
  693. $hide_fields = array (
  694. 'kw',
  695. 'kwd',
  696. 'kwds',
  697. 'keyword',
  698. 'keywords'
  699. );
  700. // All other fields are added to description, as "name:value".
  701. // Only one hide_field is assumed to be present, <> is removed from value.
  702. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title']))) {
  703. return 0; // 0 = fail
  704. }
  705. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  706. $regs = array (); // Will be passed to ereg()
  707. foreach ($linkdata as $key => $value)
  708. if (!in_array($key, $known_fields))
  709. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  710. if (($kwlist = trim($regs[1])) != '')
  711. $kw = '<i kw="' . htmlspecialchars($kwlist) . '">';
  712. else
  713. $kw = '';
  714. // i.e. assume only one of the $hide_fields will be present
  715. // and if found, hide the value as expando property of an <i> tag
  716. elseif (trim($value)) {
  717. $d .= ', ' . $key . ':' . $value;
  718. }
  719. if ($d) {
  720. $d = substr($d, 2) . ' - ';
  721. }
  722. return put_link($url, $cat, $title, $kw . ereg_replace('\[((/?(b|big|i|small|sub|sup|u))|br/)\]', '<\\1>', htmlspecialchars($d . $linkdata['description'])) . ($kw ? '</i>' : ''), $linkdata['on_homepage'] ? '1' : '0', $linkdata['hidden'] ? '1' : '0');
  723. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  724. }
  725. /**
  726. * CSV file import functions
  727. * @author René Haentjens , Ghent University
  728. */
  729. function import_csvfile() {
  730. global $catlinkstatus; // Feedback message to user.
  731. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r'))) {
  732. // read first line of file (column names) and find ',' or ';'
  733. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== false ? ',' : (strpos($colnames, ';') !== false ? ';' : '');
  734. if ($listsep) {
  735. $columns = array_map('strtolower', explode($listsep, $colnames));
  736. if (in_array('url', $columns) && in_array('title', $columns)) {
  737. $stats = array (
  738. 0,
  739. 0,
  740. 0
  741. ); // fails, updates, inserts
  742. // Modified by Ivan Tcholakov, 01-FEB-2010.
  743. //while (($data = fgetcsv($myFile, 32768, $listsep))) {
  744. while (($data = api_fgetcsv($myFile, null, $listsep))) {
  745. //
  746. foreach ($data as $i => & $text) {
  747. $linkdata[$columns[$i]] = $text;
  748. }
  749. $stats[import_link($linkdata)]++;
  750. unset ($linkdata);
  751. }
  752. $catlinkstatus = '';
  753. if ($stats[0]) {
  754. $catlinkstatus .= $stats[0] . ' ' . get_lang('CsvLinesFailed');
  755. }
  756. if ($stats[1]) {
  757. $catlinkstatus .= $stats[1] . ' ' . get_lang('CsvLinesOld');
  758. }
  759. if ($stats[2]) {
  760. $catlinkstatus .= $stats[2] . ' ' . get_lang('CsvLinesNew');
  761. }
  762. } else {
  763. $catlinkstatus = get_lang('CsvFileNoURL') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  764. }
  765. } else {
  766. $catlinkstatus = get_lang('CsvFileNoSeps') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  767. }
  768. fclose($myFile);
  769. } else {
  770. $catlinkstatus = get_lang('CsvFileNotFound');
  771. }
  772. }
  773. /**
  774. * This function checks if the url is a youtube link
  775. * @author Jorge Frisancho
  776. * @author Julio Montoya - Fixing code
  777. * @version 1.0
  778. */
  779. function is_youtube_link($url) {
  780. return strrpos($url, "youtube");
  781. }
  782. function get_youtube_video_id($url) {
  783. // This is the length of YouTube's video IDs
  784. $len = 11;
  785. // The ID string starts after "v=", which is usually right after
  786. // "youtube.com/watch?" in the URL
  787. $pos = strpos($url, "?v=");
  788. // In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
  789. // bases covered), it will be after an "&":
  790. if ($pos === false) {
  791. $pos = strpos($url, "&v=");
  792. }
  793. // If still FALSE, URL doesn't have a vid ID
  794. if ($pos === false) {
  795. //die("YouTube video ID not found. Please double-check your URL.");
  796. api_not_allowed();
  797. }
  798. // Offset the start location to match the beginning of the ID string
  799. $pos += 3;
  800. // Get the ID string and return it
  801. $ytvID = substr($url, $pos, $len);
  802. return $ytvID;
  803. }