linkfunctions.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * Function library for the links tool.
  22. *
  23. * This is a complete remake of the original link tool.
  24. * New features:
  25. * - Organize links into categories;
  26. * - favorites/bookmarks interface;
  27. * - move links up/down within a category;
  28. * - move categories up/down;
  29. * - expand/collapse all categories;
  30. * - add link to 'root' category => category-less link is always visible.
  31. *
  32. * @author Patrick Cool, complete remake (December 2003 - January 2004)
  33. * @author Rene Haentjens, CSV file import (October 2004)
  34. * @package dokeos.link
  35. ==============================================================================
  36. */
  37. /*
  38. ==============================================================================
  39. FUNCTIONS
  40. ==============================================================================
  41. */
  42. /**
  43. * Used to add a link or a category
  44. * @param string $type, "link" or "category"
  45. * @todo replace strings by constants
  46. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  47. */
  48. function addlinkcategory($type)
  49. {
  50. global $catlinkstatus;
  51. global $msgErr;
  52. $ok = true;
  53. if ($type == "link")
  54. {
  55. $tbl_link = Database :: get_course_table(TABLE_LINK);
  56. $title = Security::remove_XSS($_POST['title']);
  57. $urllink = Security::remove_XSS($_POST['urllink']);
  58. $description = Security::remove_XSS($_POST['description']);
  59. $selectcategory = Security::remove_XSS($_POST['selectcategory']);
  60. if ($_POST['onhomepage'] == '')
  61. {
  62. $onhomepage = 0;
  63. }
  64. else
  65. {
  66. $onhomepage = Security::remove_XSS($_POST['onhomepage']);
  67. }
  68. $urllink = trim($urllink);
  69. $title = trim($title);
  70. $description = trim($description);
  71. // if title is empty, an error occurs
  72. if (empty ($urllink) OR $urllink == 'http://')
  73. {
  74. $msgErr = get_lang('GiveURL');
  75. Display::display_error_message(get_lang('GiveURL'));
  76. $ok = false;
  77. }
  78. // if the title is empty, we use the url as the title
  79. else
  80. {
  81. if (empty ($title))
  82. {
  83. $title = $urllink;
  84. }
  85. // we check weither the $url starts with http://, if not we add this
  86. if (!strstr($urllink, '://'))
  87. {
  88. $urllink = "http://".$urllink;
  89. }
  90. // looking for the largest order number for this category
  91. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".Database::escape_string($_POST['selectcategory'])."'");
  92. list ($orderMax) = Database::fetch_row($result);
  93. $order = $orderMax +1;
  94. $sql = "INSERT INTO ".$tbl_link." (url, title, description, category_id,display_order, on_homepage) VALUES ('$urllink','$title','$description','$selectcategory','$order', '$onhomepage')";
  95. $catlinkstatus = get_lang('LinkAdded');
  96. api_sql_query($sql, __FILE__, __LINE__);
  97. $link_id = Database::insert_id();
  98. if ( (api_get_setting('search_enabled')=='true') && $link_id) {
  99. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  100. require_once(api_get_path(LIBRARY_PATH) .'search/IndexableChunk.class.php');
  101. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  102. $courseid = api_get_course_id();
  103. $specific_fields = get_specific_field_list();
  104. $ic_slide = new IndexableChunk();
  105. // add all terms to db
  106. $all_specific_terms = '';
  107. foreach ($specific_fields as $specific_field) {
  108. if (isset($_REQUEST[$specific_field['code']])) {
  109. $sterms = trim($_REQUEST[$specific_field['code']]);
  110. if (!empty($sterms)) {
  111. $all_specific_terms .= ' '. $sterms;
  112. $sterms = explode(',', $sterms);
  113. foreach ($sterms as $sterm) {
  114. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  115. add_specific_field_value($specific_field['id'], $courseid, TOOL_LINK, $link_id, $sterm);
  116. }
  117. }
  118. }
  119. }
  120. // build the chunk to index
  121. $ic_slide->addValue("title", $title);
  122. $ic_slide->addCourseId($courseid);
  123. $ic_slide->addToolId(TOOL_LINK);
  124. $xapian_data = array(
  125. SE_COURSE_ID => $courseid,
  126. SE_TOOL_ID => TOOL_LINK,
  127. SE_DATA => array('link_id' => (int)$link_id),
  128. SE_USER => (int)api_get_user_id(),
  129. );
  130. $ic_slide->xapian_data = serialize($xapian_data);
  131. $description = $all_specific_terms .' '. $description;
  132. $ic_slide->addValue("content", $description);
  133. // add category name if set
  134. if (isset($_POST['selectcategory']) && $selectcategory>0) {
  135. $table_link_category = Database::get_course_table(TABLE_LINK_CATEGORY);
  136. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  137. $sql_cat = sprintf($sql_cat, $table_link_category, (int)$selectcategory);
  138. $result = api_sql_query($sql_cat, __FILE__, __LINE__);
  139. if (Database::num_rows($result) == 1) {
  140. $row = Database::fetch_array($result);
  141. $ic_slide->addValue("category", $row['category_title']);
  142. }
  143. }
  144. $di = new DokeosIndexer();
  145. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  146. $di->connectDb(NULL, NULL, $lang);
  147. $di->addChunk($ic_slide);
  148. //index and return search engine document id
  149. $did = $di->index();
  150. if ($did) {
  151. // save it to db
  152. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  153. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  154. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  155. $sql = sprintf($sql, $tbl_se_ref, $courseid, TOOL_LINK, $link_id, $did);
  156. api_sql_query($sql,__FILE__,__LINE__);
  157. }
  158. }
  159. unset ($urllink, $title, $description, $selectcategory);
  160. Display::display_confirmation_message(get_lang('LinkAdded'));
  161. }
  162. } elseif ($type == "category") {
  163. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  164. $category_title = trim($_POST['category_title']);
  165. $description = trim($_POST['description']);
  166. if (empty ($category_title)) {
  167. $msgErr = get_lang('GiveCategoryName');
  168. Display::display_error_message(get_lang('GiveCategoryName'));
  169. $ok = false;
  170. } else {
  171. // looking for the largest order number for this category
  172. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."");
  173. list ($orderMax) = Database::fetch_row($result);
  174. $order = $orderMax +1;
  175. $sql = "INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('".Security::remove_XSS($category_title)."','".Security::remove_XSS($description)."', '$order')";
  176. api_sql_query($sql, __FILE__, __LINE__);
  177. $catlinkstatus = get_lang('CategoryAdded');
  178. unset ($category_title, $description);
  179. Display::display_confirmation_message(get_lang('CategoryAdded'));
  180. }
  181. }
  182. // "WHAT'S NEW" notification : update last tool Edit
  183. if ($type == "link")
  184. {
  185. global $_user;
  186. global $_course;
  187. global $nameTools;
  188. api_item_property_update($_course, TOOL_LINK, $link_id, "LinkAdded", $_user['user_id']);
  189. }
  190. return $ok;
  191. }
  192. // End of the function addlinkcategory
  193. /**
  194. * Used to delete a link or a category
  195. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  196. */
  197. function deletelinkcategory($type)
  198. {
  199. global $catlinkstatus;
  200. global $_course;
  201. global $_user;
  202. $tbl_link = Database :: get_course_table(TABLE_LINK);
  203. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  204. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  205. if ($type == "link")
  206. {
  207. global $id;
  208. // -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
  209. // make a restore function possible for the platform administrator
  210. //$sql="DELETE FROM $tbl_link WHERE id='".$_GET['id']."'";
  211. api_item_property_update($_course, TOOL_LINK, $id, "delete", $_user['user_id']);
  212. delete_link_from_search_engine(api_get_course_id(), $id);
  213. $catlinkstatus = get_lang("LinkDeleted");
  214. unset ($id);
  215. Display::display_confirmation_message(get_lang('LinkDeleted'));
  216. }
  217. if ($type == "category")
  218. {
  219. global $id;
  220. // first we delete the category itself and afterwards all the links of this category.
  221. $sql = "DELETE FROM ".$tbl_categories." WHERE id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  222. api_sql_query($sql, __FILE__, __LINE__);
  223. $sql = "DELETE FROM ".$tbl_link." WHERE category_id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  224. $catlinkstatus = get_lang('CategoryDeleted');
  225. unset ($id);
  226. api_sql_query($sql, __FILE__, __LINE__);
  227. Display::display_confirmation_message(get_lang('CategoryDeleted'));
  228. }
  229. }
  230. /**
  231. * Removes a link from search engine database
  232. *
  233. * @param string $course_id Course code
  234. * @param int $document_id Document id to delete
  235. */
  236. function delete_link_from_search_engine($course_id, $link_id) {
  237. // remove from search engine if enabled
  238. if (api_get_setting('search_enabled') == 'true') {
  239. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  240. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  241. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  242. $res = api_sql_query($sql, __FILE__, __LINE__);
  243. if (Database::num_rows($res) > 0) {
  244. $row = Database::fetch_array($res);
  245. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  246. $di = new DokeosIndexer();
  247. $di->remove_document((int)$row['search_did']);
  248. }
  249. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  250. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  251. api_sql_query($sql, __FILE__, __LINE__);
  252. // remove terms from db
  253. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  254. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  255. }
  256. }
  257. /**
  258. * Used to edit a link or a category
  259. * @todo rewrite the whole links tool because it is becoming completely cluttered,
  260. * code does not follow the coding conventions, does not use html_quickform, ...
  261. * some features were patched in
  262. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  263. * @todo replace the globals with the appropriate $_POST or $_GET values
  264. */
  265. function editlinkcategory($type)
  266. {
  267. global $catlinkstatus;
  268. global $id;
  269. global $submitLink;
  270. global $submitCategory;
  271. global $_user;
  272. global $_course;
  273. global $nameTools;
  274. global $urllink;
  275. global $title;
  276. global $description;
  277. global $category;
  278. global $selectcategory;
  279. global $description;
  280. global $category_title;
  281. global $onhomepage;
  282. $tbl_link = Database :: get_course_table(TABLE_LINK);
  283. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  284. if ($type == "link")
  285. {
  286. // this is used to populate the link-form with the info found in the database
  287. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".$_GET['id']."'";
  288. $result = api_sql_query($sql, __FILE__, __LINE__);
  289. if ($myrow = mysql_fetch_array($result))
  290. {
  291. $urllink = $myrow["url"];
  292. $title = $myrow["title"];
  293. $description = $myrow["description"];
  294. $category = $myrow["category_id"];
  295. if ($myrow["on_homepage"] <> 0)
  296. {
  297. $onhomepage = "checked";
  298. }
  299. }
  300. // this is used to put the modified info of the link-form into the database
  301. if ($_POST['submitLink'])
  302. {
  303. if ($_POST['onhomepage'] == '')
  304. {
  305. $onhomepage = 0;
  306. }
  307. else
  308. {
  309. $onhomepage = Security::remove_XSS($_POST['onhomepage']);
  310. }
  311. // finding the old category_id
  312. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".Database::escape_string(Security::remove_XSS($_POST['id']))."'";
  313. $result = api_sql_query($sql, __FILE__, __LINE__);
  314. $row = Database::fetch_array($result);
  315. $category_id = $row['category_id'];
  316. if ($category_id <> $_POST['selectcategory'])
  317. {
  318. $sql = "SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'";
  319. $result = api_sql_query($sql);
  320. list ($max_display_order) = Database::fetch_row($result);
  321. $max_display_order ++;
  322. }
  323. else
  324. {
  325. $max_display_order = $row['display_order'];
  326. }
  327. $sql = "UPDATE ".$tbl_link." set url='".Database::escape_string(Security::remove_XSS($_POST['urllink']))."', title='".Database::escape_string(Security::remove_XSS($_POST['title']))."', description='".Database::escape_string(Security::remove_XSS($_POST['description']))."', category_id='".Database::escape_string(Security::remove_XSS($_POST['selectcategory']))."', display_order='".$max_display_order."', on_homepage='".Database::escape_string(Security::remove_XSS($_POST['onhomepage']))."' WHERE id='".Database::escape_string(Security::remove_XSS($_POST['id']))."'";
  328. api_sql_query($sql, __FILE__, __LINE__);
  329. // update search enchine and its values table if enabled
  330. if (api_get_setting('search_enabled')=='true') {
  331. $link_id = $_POST['id'];
  332. $course_id = api_get_course_id();
  333. $link_url = $_POST['urllink'];
  334. $link_title = $_POST['title'];
  335. $link_description = $_POST['description'];
  336. // actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one
  337. // get search_did
  338. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  339. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  340. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  341. $res = api_sql_query($sql, __FILE__, __LINE__);
  342. if (Database::num_rows($res) > 0) {
  343. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  344. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  345. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  346. $se_ref = Database::fetch_array($res);
  347. $specific_fields = get_specific_field_list();
  348. $ic_slide = new IndexableChunk();
  349. $all_specific_terms = '';
  350. foreach ($specific_fields as $specific_field) {
  351. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  352. if (isset($_REQUEST[$specific_field['code']])) {
  353. $sterms = trim($_REQUEST[$specific_field['code']]);
  354. if (!empty($sterms)) {
  355. $all_specific_terms .= ' '. $sterms;
  356. $sterms = explode(',', $sterms);
  357. foreach ($sterms as $sterm) {
  358. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  359. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  360. }
  361. }
  362. }
  363. }
  364. // build the chunk to index
  365. $ic_slide->addValue("title", $link_title);
  366. $ic_slide->addCourseId($course_id);
  367. $ic_slide->addToolId(TOOL_LINK);
  368. $xapian_data = array(
  369. SE_COURSE_ID => $course_id,
  370. SE_TOOL_ID => TOOL_LINK,
  371. SE_DATA => array('link_id' => (int)$link_id),
  372. SE_USER => (int)api_get_user_id(),
  373. );
  374. $ic_slide->xapian_data = serialize($xapian_data);
  375. $link_description = $all_specific_terms .' '. $link_description;
  376. $ic_slide->addValue("content", $link_description);
  377. // add category name if set
  378. if (isset($_POST['selectcategory']) && $selectcategory>0) {
  379. $table_link_category = Database::get_course_table(TABLE_LINK_CATEGORY);
  380. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  381. $sql_cat = sprintf($sql_cat, $table_link_category, (int)$selectcategory);
  382. $result = api_sql_query($sql_cat, __FILE__, __LINE__);
  383. if (Database::num_rows($result) == 1) {
  384. $row = Database::fetch_array($result);
  385. $ic_slide->addValue("category", $row['category_title']);
  386. }
  387. }
  388. $di = new DokeosIndexer();
  389. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  390. $di->connectDb(NULL, NULL, $lang);
  391. $di->remove_document((int)$se_ref['search_did']);
  392. $di->addChunk($ic_slide);
  393. //index and return search engine document id
  394. $did = $di->index();
  395. if ($did) {
  396. // save it to db
  397. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  398. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  399. api_sql_query($sql,__FILE__,__LINE__);
  400. //var_dump($sql);
  401. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  402. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  403. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id, $did);
  404. api_sql_query($sql,__FILE__,__LINE__);
  405. }
  406. }
  407. }
  408. // "WHAT'S NEW" notification: update table last_toolEdit
  409. api_item_property_update($_course, TOOL_LINK, $_POST['id'], "LinkUpdated", $_user['user_id']);
  410. Display::display_confirmation_message(get_lang('LinkModded'));
  411. }
  412. }
  413. if ($type == "category")
  414. {
  415. // this is used to populate the category-form with the info found in the database
  416. if (!$submitCategory)
  417. {
  418. $sql = "SELECT * FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  419. $result = api_sql_query($sql, __FILE__, __LINE__);
  420. if ($myrow = Database::fetch_array($result))
  421. {
  422. $category_title = $myrow["category_title"];
  423. $description = $myrow["description"];
  424. }
  425. }
  426. // this is used to put the modified info of the category-form into the database
  427. if ($submitCategory)
  428. {
  429. $sql = "UPDATE ".$tbl_categories." set category_title='".Database::escape_string(Security::remove_XSS($_POST['category_title']))."', description='".Database::escape_string(Security::remove_XSS($_POST['description']))."' WHERE id='".Database::escape_string(Security::remove_XSS($_POST['id']))."'";
  430. api_sql_query($sql, __FILE__, __LINE__);
  431. Display::display_confirmation_message(get_lang('CategoryModded'));
  432. }
  433. }
  434. }
  435. // END of function editlinkcat
  436. /**
  437. * creates a correct $view for in the URL
  438. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  439. */
  440. function makedefaultviewcode($locatie)
  441. {
  442. global $aantalcategories;
  443. global $view;
  444. for ($j = 0; $j <= $aantalcategories -1; $j ++)
  445. {
  446. $view[$j] = 0;
  447. }
  448. $view[intval($locatie)] = "1";
  449. }
  450. // END of function makedefaultviewcode
  451. /**
  452. * changes the visibility of a link
  453. * @todo add the changing of the visibility of a course
  454. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  455. */
  456. function change_visibility($id, $scope)
  457. {
  458. global $_course;
  459. global $_user;
  460. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  461. if ($scope == "link")
  462. {
  463. $sqlselect = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_LINK."' and ref='".Database::escape_string($id)."'";
  464. $result = api_sql_query($sqlselect);
  465. $row = Database::fetch_array($result);
  466. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  467. }
  468. Display::display_confirmation_message(get_lang('VisibilityChanged'));
  469. }
  470. /**
  471. * displays all the links of a given category.
  472. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  473. */
  474. function showlinksofcategory($catid)
  475. {
  476. global $is_allowed, $charset, $urlview, $up, $down;
  477. $tbl_link = Database :: get_course_table(TABLE_LINK);
  478. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  479. $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')ORDER BY link.display_order DESC";
  480. $result = api_sql_query($sqlLinks);
  481. $numberoflinks = Database::num_rows($result);
  482. echo '<table class="data_table" width="100%">';
  483. $i = 1;
  484. while ($myrow = Database::fetch_array($result))
  485. {
  486. if($i%2==0) $css_class = 'row_odd';
  487. else $css_class = 'row_even';
  488. $myrow[3] = text_filter($myrow[3]);
  489. if ($myrow['visibility'] == '1')
  490. {
  491. echo "<tr class='".$css_class."'>", "<td align=\"center\" valign=\"middle\" width=\"15\">", "<a href=\"link_goto.php?".api_get_cidreq()."&link_id=", $myrow[0], "&amp;link_url=", urlencode($myrow[1]), "\" target=\"_blank\">", "<img src=\"../../main/img/file_html.gif\" border=\"0\" alt=\"".get_lang('Link')."\"/>", "</a></td>", "<td width=\"80%\" valign=\"top\">", "<a href=\"link_goto.php?".api_get_cidreq()."&link_id=", $myrow[0], "&amp;link_url=", urlencode($myrow[1]), "\" target=\"_blank\">", api_htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br/>", $myrow[3], "";
  492. }
  493. else
  494. {
  495. if (api_is_allowed_to_edit())
  496. {
  497. echo "<tr class='".$css_class."'>", "<td align=\"center\" valign=\"middle\" width=\"15\">", "<a href=\"link_goto.php?".api_get_cidreq()."&link_id=", $myrow[0], "&amp;link_url=", urlencode($myrow[1]), "\" target=\"_blank\" class=\"invisible\">", Display::return_icon('file_html_na.gif', get_lang('Link')),"</a></td>", "<td width=\"80%\" valign=\"top\">", "<a href=\"link_goto.php?".api_get_cidreq()."&link_id=", $myrow[0], "&amp;link_url=", urlencode($myrow[1]), "\" target=\"_blank\" class=\"invisible\">", api_htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br />", $myrow[3], "";
  498. }
  499. }
  500. echo '<td style="text-align:center;">';
  501. if (api_is_allowed_to_edit())
  502. {
  503. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=editlink&amp;category=".(!empty($category)?$category:'')."&amp;id=".$myrow[0]." &amp;urlview=$urlview \" title=\"".get_lang('Modify')."\" >", "<img src=\"../img/edit.gif\" border=\"0\" alt=\"", get_lang('Modify'), "\" />", "</a>";
  504. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=deletelink&amp;id=", $myrow[0], "&amp;urlview=", $urlview, "\" onclick=\"javascript:if(!confirm('".get_lang('LinkDelconfirm')."')) return false;\" title=\"".get_lang('Delete')."\" >", "<img src=\"../img/delete.gif\" border=\"0\" alt=\"", get_lang('Delete'), "\" />", "</a>";
  505. // DISPLAY MOVE UP COMMAND only if it is not the top link
  506. if ($i != 1)
  507. {
  508. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&urlview=".$urlview."&amp;up=", $myrow["id"], "\" title=\"".get_lang('Up')."\" >", "<img src=../img/up.gif border=0 alt=\"".get_lang('Up')."\"/>", "</a>\n";
  509. }
  510. else
  511. {
  512. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="'.get_lang('Up').'"/>';
  513. }
  514. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  515. if ($i < $numberoflinks)
  516. {
  517. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&urlview=".$urlview."&amp;down=".$myrow["id"]."\" title=\"".get_lang('Down')."\" >", "<img src=\"../img/down.gif\" border=\"0\" alt=\"".get_lang('Down')."\"/>", "</a>\n";
  518. }
  519. else
  520. {
  521. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="'.get_lang('Down').'"/>';
  522. }
  523. if ($myrow['visibility'] == "1")
  524. {
  525. echo '<a href="link.php?'.api_get_cidreq().'&action=invisible&amp;id='.$myrow['id'].'&amp;scope=link&amp;urlview='.$urlview.'" title="'.get_lang('Hide').'"><img src="../img/visible.gif" border="0" /></a>';
  526. }
  527. if ($myrow['visibility'] == "0")
  528. {
  529. echo '<a href="link.php?'.api_get_cidreq().'&action=visible&amp;id='.$myrow['id'].'&amp;scope=link&amp;urlview='.$urlview.'" title="'.get_lang('Show').'"><img src="../img/invisible.gif" border="0" /></a>';
  530. }
  531. }
  532. echo '</td>';
  533. echo '</tr>';
  534. $i ++;
  535. }
  536. echo '</table>';
  537. }
  538. /**
  539. * displays the edit, delete and move icons
  540. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  541. */
  542. function showcategoryadmintools($categoryid)
  543. {
  544. global $urlview;
  545. global $aantalcategories;
  546. global $catcounter;
  547. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=editcategory&amp;id='.$categoryid.'&amp;urlview='.$urlview.'" title='.get_lang('Modify').' "><img src="../img/edit.gif" border="0" alt="'.get_lang('Modify').' "/></a>';
  548. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=deletecategory&amp;id=", $categoryid, "&amp;urlview=$urlview\" onclick=\"javascript:if(!confirm('".get_lang('CategoryDelconfirm')."')) return false;\">", "<img src=\"../img/delete.gif\" border=\"0\" alt=\"", get_lang('Delete'), "\"/>", "</a>";
  549. // DISPLAY MOVE UP COMMAND only if it is not the top link
  550. if ($catcounter != 1)
  551. {
  552. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&catmove=true&amp;up=", $categoryid, "&amp;urlview=$urlview\" title=\"".get_lang('Up')."\" >", "<img src=../img/up.gif border=0 alt=\"".get_lang('Up')."\"/>", "</a>\n";
  553. }
  554. else
  555. {
  556. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="'.get_lang('Up').'"/>';
  557. }
  558. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  559. if ($catcounter < $aantalcategories)
  560. {
  561. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&catmove=true&amp;down=".$categoryid."&amp;urlview=$urlview\">", "<img src=\"../img/down.gif\" border=\"0\" alt=\"".get_lang('Down')."\"/>", "</a>\n";
  562. }
  563. else
  564. {
  565. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="'.get_lang('Down').'"/>';
  566. }
  567. $catcounter ++;
  568. }
  569. /**
  570. * move a link or a linkcategory up or down
  571. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  572. */
  573. function movecatlink($catlinkid)
  574. {
  575. global $catmove;
  576. global $up;
  577. global $down;
  578. $tbl_link = Database :: get_course_table(TABLE_LINK);
  579. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  580. if (!empty($_GET['down']))
  581. {
  582. $thiscatlinkId = $_GET['down'];
  583. $sortDirection = "DESC";
  584. }
  585. if (!empty($_GET['up']))
  586. {
  587. $thiscatlinkId = Security::remove_XSS($_GET['up']);
  588. $sortDirection = "ASC";
  589. }
  590. // 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
  591. if ($catmove == "true")
  592. {
  593. $movetable = $tbl_categories;
  594. $catid = $catlinkid;
  595. }
  596. else
  597. {
  598. $movetable = $tbl_link;
  599. //getting the category of the link
  600. if(!empty($thiscatlinkId))
  601. {
  602. $sql = "SELECT category_id from ".$movetable." WHERE id='$thiscatlinkId'";
  603. $result = api_sql_query($sql, __FILE__, __LINE__);
  604. $catid = Database::fetch_array($result);
  605. }
  606. }
  607. // this code is copied and modified from announcements.php
  608. if (!empty($sortDirection))
  609. {
  610. if (!in_array(trim(strtoupper($sortDirection)), array ('ASC', 'DESC')))
  611. die("Bad sort direction used."); //sanity check of sortDirection var
  612. if ($catmove == "true")
  613. {
  614. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." ORDER BY display_order $sortDirection";
  615. }
  616. else
  617. {
  618. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." WHERE category_id='".$catid[0]."' ORDER BY display_order $sortDirection";
  619. }
  620. $linkresult = api_sql_query($sqlcatlinks);
  621. while ($sortrow = Database::fetch_array($linkresult))
  622. {
  623. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  624. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  625. // and we know the next iteration of the while loop is the next one. These should be swapped.
  626. if (isset ($thislinkFound) && $thislinkFound == true)
  627. {
  628. $nextlinkId = $sortrow["id"];
  629. $nextlinkOrdre = $sortrow["display_order"];
  630. api_sql_query("UPDATE ".$movetable."
  631. SET display_order = '$nextlinkOrdre'
  632. WHERE id = '$thiscatlinkId'");
  633. api_sql_query("UPDATE ".$movetable."
  634. SET display_order = '$thislinkOrdre'
  635. WHERE id = '$nextlinkId'");
  636. break;
  637. }
  638. if ($sortrow["id"] == $thiscatlinkId)
  639. {
  640. $thislinkOrdre = $sortrow["display_order"];
  641. $thislinkFound = true;
  642. }
  643. }
  644. }
  645. Display::display_confirmation_message(get_lang('LinkMoved'));
  646. }
  647. /**
  648. * CSV file import functions
  649. * @author Rene Haentjens , Ghent University
  650. */
  651. function get_cat($catname) // get category id (existing or make new)
  652. {
  653. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  654. $result = api_sql_query("SELECT `id` FROM ".$tbl_categories." WHERE `category_title`='".addslashes($catname)."'", __FILE__, __LINE__);
  655. if (Database::num_rows($result) >= 1 && ($row = Database::fetch_array($result)))
  656. return $row['id']; // several categories with same name: take first
  657. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."", __FILE__, __LINE__);
  658. list ($max_order) = Database::fetch_row($result);
  659. api_sql_query("INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('".addslashes($catname)."','','". ($max_order +1)."')", __FILE__, __LINE__);
  660. return Database::insert_id();
  661. }
  662. /**
  663. * CSV file import functions
  664. * @author Rene Haentjens , Ghent University
  665. */
  666. function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
  667. {
  668. $tbl_link = Database :: get_course_table(TABLE_LINK);
  669. $urleq = "url='".addslashes($url)."'";
  670. $cateq = "category_id=".$cat;
  671. $result = api_sql_query("SELECT id FROM $tbl_link WHERE ".$urleq.' AND '.$cateq, __FILE__, __LINE__);
  672. if (Database::num_rows($result) >= 1 && ($row = Database::fetch_array($result)))
  673. {
  674. api_sql_query("UPDATE $tbl_link set title='".addslashes($title)."', description='".addslashes($description)."' WHERE id='".addslashes($id = $row['id'])."'", __FILE__, __LINE__);
  675. $lang_link = get_lang('update_link');
  676. $ipu = "LinkUpdated";
  677. $rv = 1; // 1= upd
  678. }
  679. else // add new link
  680. {
  681. $result = api_sql_query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='".addslashes($cat)."'", __FILE__, __LINE__);
  682. list ($max_order) = mysql_fetch_row($result);
  683. api_sql_query("INSERT INTO $tbl_link (url, title, description, category_id, display_order, on_homepage) VALUES ('".addslashes($url)."','".addslashes($title)."','".addslashes($description)."','".addslashes($cat)."','". ($max_order +1)."','".$on_homepage."')", __FILE__, __LINE__);
  684. $id = Database::insert_id();
  685. $lang_link = get_lang('new_link');
  686. $ipu = "LinkAdded";
  687. $rv = 2; // 2= new
  688. }
  689. global $_course, $nameTools, $_user;
  690. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  691. if ($hidden && $ipu == "LinkAdded")
  692. api_item_property_update($_course, TOOL_LINK, $id, "invisible", $_user['user_id']);
  693. return $rv;
  694. }
  695. /**
  696. * CSV file import functions
  697. * @author Rene Haentjens , Ghent University
  698. */
  699. function import_link($linkdata) // url, category_id, title, description, ...
  700. {
  701. // field names used in the uploaded file
  702. $known_fields = array ('url', 'category', 'title', 'description', 'on_homepage', 'hidden');
  703. $hide_fields = array ('kw', 'kwd', 'kwds', 'keyword', 'keywords');
  704. // all other fields are added to description, as "name:value"
  705. // only one hide_field is assumed to be present, <> is removed from value
  706. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title'])))
  707. return 0; // 0= fail
  708. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  709. $regs = array(); // will be passed to ereg()
  710. foreach ($linkdata as $key => $value)
  711. if (!in_array($key, $known_fields))
  712. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  713. if (($kwlist = trim($regs[1])) != '')
  714. $kw = '<i kw="'.htmlspecialchars($kwlist).'">';
  715. else
  716. $kw = '';
  717. // i.e. assume only one of the $hide_fields will be present
  718. // and if found, hide the value as expando property of an <i> tag
  719. elseif (trim($value)) $d .= ', '.$key.':'.$value;
  720. if ($d)
  721. $d = substr($d, 2).' - ';
  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 Rene Haentjens , Ghent University
  728. */
  729. function import_csvfile()
  730. {
  731. global $catlinkstatus; // feedback message to user
  732. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r')))
  733. {
  734. // read first line of file (column names) and find ',' or ';'
  735. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== FALSE ? ',' : (strpos($colnames, ';') !== FALSE ? ';' : '');
  736. if ($listsep)
  737. {
  738. $columns = array_map('strtolower', explode($listsep, $colnames));
  739. if (in_array('url', $columns) && in_array('title', $columns))
  740. {
  741. $stats = array (0, 0, 0); // fails, updates, inserts
  742. while (($data = fgetcsv($myFile, 32768, $listsep)))
  743. {
  744. foreach ($data as $i => $text)
  745. $linkdata[$columns[$i]] = $text;
  746. // $linkdata['url', 'title', ...]
  747. $stats[import_link($linkdata)]++;
  748. unset ($linkdata);
  749. }
  750. $catlinkstatus = '';
  751. if ($stats[0])
  752. $catlinkstatus .= $stats[0].' '.get_lang('CsvLinesFailed');
  753. if ($stats[1])
  754. $catlinkstatus .= $stats[1].' '.get_lang('CsvLinesOld');
  755. if ($stats[2])
  756. $catlinkstatus .= $stats[2].' '.get_lang('CsvLinesNew');
  757. }
  758. else
  759. $catlinkstatus = get_lang('CsvFileNoURL'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  760. }
  761. else
  762. $catlinkstatus = get_lang('CsvFileNoSeps'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  763. fclose($myFile);
  764. }
  765. else
  766. $catlinkstatus = get_lang('CsvFileNotFound');
  767. }
  768. ?>