link.lib.php 39 KB

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