linkfunctions.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. }
  163. elseif ($type == "category")
  164. {
  165. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  166. $category_title = trim($_POST['category_title']);
  167. $description = trim($_POST['description']);
  168. if (empty ($category_title))
  169. {
  170. $msgErr = get_lang('GiveCategoryName');
  171. Display::display_error_message(get_lang('GiveCategoryName'));
  172. $ok = false;
  173. }
  174. else
  175. {
  176. // looking for the largest order number for this category
  177. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."");
  178. list ($orderMax) = Database::fetch_row($result);
  179. $order = $orderMax +1;
  180. $sql = "INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('$category_title','$description', '$order')";
  181. api_sql_query($sql, __FILE__, __LINE__);
  182. $catlinkstatus = get_lang('CategoryAdded');
  183. unset ($category_title, $description);
  184. Display::display_confirmation_message(get_lang('CategoryAdded'));
  185. }
  186. }
  187. // "WHAT'S NEW" notification : update last tool Edit
  188. if ($type == "link")
  189. {
  190. global $_user;
  191. global $_course;
  192. global $nameTools;
  193. api_item_property_update($_course, TOOL_LINK, $link_id, "LinkAdded", $_user['user_id']);
  194. }
  195. return $ok;
  196. }
  197. // End of the function addlinkcategory
  198. /**
  199. * Used to delete a link or a category
  200. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  201. */
  202. function deletelinkcategory($type)
  203. {
  204. global $catlinkstatus;
  205. global $_course;
  206. global $_user;
  207. $tbl_link = Database :: get_course_table(TABLE_LINK);
  208. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  209. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  210. if ($type == "link")
  211. {
  212. global $id;
  213. // -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
  214. // make a restore function possible for the platform administrator
  215. //$sql="DELETE FROM $tbl_link WHERE id='".$_GET['id']."'";
  216. api_item_property_update($_course, TOOL_LINK, $id, "delete", $_user['user_id']);
  217. delete_link_from_search_engine(api_get_course_id(), $id);
  218. $catlinkstatus = get_lang("LinkDeleted");
  219. unset ($id);
  220. Display::display_confirmation_message(get_lang('LinkDeleted'));
  221. }
  222. if ($type == "category")
  223. {
  224. global $id;
  225. // first we delete the category itself and afterwards all the links of this category.
  226. $sql = "DELETE FROM ".$tbl_categories." WHERE id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  227. api_sql_query($sql, __FILE__, __LINE__);
  228. $sql = "DELETE FROM ".$tbl_link." WHERE category_id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  229. $catlinkstatus = get_lang('CategoryDeleted');
  230. unset ($id);
  231. api_sql_query($sql, __FILE__, __LINE__);
  232. Display::display_confirmation_message(get_lang('CategoryDeleted'));
  233. }
  234. }
  235. /**
  236. * Removes a link from search engine database
  237. *
  238. * @param string $course_id Course code
  239. * @param int $document_id Document id to delete
  240. */
  241. function delete_link_from_search_engine($course_id, $link_id) {
  242. // remove from search engine if enabled
  243. if (api_get_setting('search_enabled') == 'true') {
  244. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  245. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  246. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  247. $res = api_sql_query($sql, __FILE__, __LINE__);
  248. if (Database::num_rows($res) > 0) {
  249. $row = Database::fetch_array($res);
  250. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  251. $di = new DokeosIndexer();
  252. $di->remove_document((int)$row['search_did']);
  253. }
  254. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  255. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  256. api_sql_query($sql, __FILE__, __LINE__);
  257. // remove terms from db
  258. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  259. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  260. }
  261. }
  262. /**
  263. * Used to edit a link or a category
  264. * @todo rewrite the whole links tool because it is becoming completely cluttered,
  265. * code does not follow the coding conventions, does not use html_quickform, ...
  266. * some features were patched in
  267. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  268. * @todo replace the globals with the appropriate $_POST or $_GET values
  269. */
  270. function editlinkcategory($type)
  271. {
  272. global $catlinkstatus;
  273. global $id;
  274. global $submitLink;
  275. global $submitCategory;
  276. global $_user;
  277. global $_course;
  278. global $nameTools;
  279. global $urllink;
  280. global $title;
  281. global $description;
  282. global $category;
  283. global $selectcategory;
  284. global $description;
  285. global $category_title;
  286. global $onhomepage;
  287. $tbl_link = Database :: get_course_table(TABLE_LINK);
  288. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  289. if ($type == "link")
  290. {
  291. // this is used to populate the link-form with the info found in the database
  292. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".$_GET['id']."'";
  293. $result = api_sql_query($sql, __FILE__, __LINE__);
  294. if ($myrow = mysql_fetch_array($result))
  295. {
  296. $urllink = $myrow["url"];
  297. $title = $myrow["title"];
  298. $description = $myrow["description"];
  299. $category = $myrow["category_id"];
  300. if ($myrow["on_homepage"] <> 0)
  301. {
  302. $onhomepage = "checked";
  303. }
  304. }
  305. // this is used to put the modified info of the link-form into the database
  306. if ($_POST['submitLink'])
  307. {
  308. if ($_POST['onhomepage'] == '')
  309. {
  310. $onhomepage = 0;
  311. }
  312. else
  313. {
  314. $onhomepage = Security::remove_XSS($_POST['onhomepage']);
  315. }
  316. // finding the old category_id
  317. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".Database::escape_string(Security::remove_XSS($_POST['id']))."'";
  318. $result = api_sql_query($sql, __FILE__, __LINE__);
  319. $row = Database::fetch_array($result);
  320. $category_id = $row['category_id'];
  321. if ($category_id <> $_POST['selectcategory'])
  322. {
  323. $sql = "SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'";
  324. $result = api_sql_query($sql);
  325. list ($max_display_order) = Database::fetch_row($result);
  326. $max_display_order ++;
  327. }
  328. else
  329. {
  330. $max_display_order = $row['display_order'];
  331. }
  332. $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']))."'";
  333. api_sql_query($sql, __FILE__, __LINE__);
  334. // update search enchine and its values table if enabled
  335. if (api_get_setting('search_enabled')=='true') {
  336. $link_id = $_POST['id'];
  337. $course_id = api_get_course_id();
  338. $link_url = $_POST['urllink'];
  339. $link_title = $_POST['title'];
  340. $link_description = $_POST['description'];
  341. // actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one
  342. // get search_did
  343. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  344. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  345. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  346. $res = api_sql_query($sql, __FILE__, __LINE__);
  347. if (Database::num_rows($res) > 0) {
  348. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  349. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  350. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  351. $se_ref = Database::fetch_array($res);
  352. $specific_fields = get_specific_field_list();
  353. $ic_slide = new IndexableChunk();
  354. $all_specific_terms = '';
  355. foreach ($specific_fields as $specific_field) {
  356. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  357. if (isset($_REQUEST[$specific_field['code']])) {
  358. $sterms = trim($_REQUEST[$specific_field['code']]);
  359. if (!empty($sterms)) {
  360. $all_specific_terms .= ' '. $sterms;
  361. $sterms = explode(',', $sterms);
  362. foreach ($sterms as $sterm) {
  363. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  364. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  365. }
  366. }
  367. }
  368. }
  369. // build the chunk to index
  370. $ic_slide->addValue("title", $link_title);
  371. $ic_slide->addCourseId($course_id);
  372. $ic_slide->addToolId(TOOL_LINK);
  373. $xapian_data = array(
  374. SE_COURSE_ID => $course_id,
  375. SE_TOOL_ID => TOOL_LINK,
  376. SE_DATA => array('link_id' => (int)$link_id),
  377. SE_USER => (int)api_get_user_id(),
  378. );
  379. $ic_slide->xapian_data = serialize($xapian_data);
  380. $link_description = $all_specific_terms .' '. $link_description;
  381. $ic_slide->addValue("content", $link_description);
  382. // add category name if set
  383. if (isset($_POST['selectcategory']) && $selectcategory>0) {
  384. $table_link_category = Database::get_course_table(TABLE_LINK_CATEGORY);
  385. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  386. $sql_cat = sprintf($sql_cat, $table_link_category, (int)$selectcategory);
  387. $result = api_sql_query($sql_cat, __FILE__, __LINE__);
  388. if (Database::num_rows($result) == 1) {
  389. $row = Database::fetch_array($result);
  390. $ic_slide->addValue("category", $row['category_title']);
  391. }
  392. }
  393. $di = new DokeosIndexer();
  394. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  395. $di->connectDb(NULL, NULL, $lang);
  396. $di->remove_document((int)$se_ref['search_did']);
  397. $di->addChunk($ic_slide);
  398. //index and return search engine document id
  399. $did = $di->index();
  400. if ($did) {
  401. // save it to db
  402. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  403. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  404. api_sql_query($sql,__FILE__,__LINE__);
  405. //var_dump($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. api_sql_query($sql,__FILE__,__LINE__);
  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. {
  420. // this is used to populate the category-form with the info found in the database
  421. if (!$submitCategory)
  422. {
  423. $sql = "SELECT * FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  424. $result = api_sql_query($sql, __FILE__, __LINE__);
  425. if ($myrow = Database::fetch_array($result))
  426. {
  427. $category_title = $myrow["category_title"];
  428. $description = $myrow["description"];
  429. }
  430. }
  431. // this is used to put the modified info of the category-form into the database
  432. if ($submitCategory)
  433. {
  434. $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']))."'";
  435. api_sql_query($sql, __FILE__, __LINE__);
  436. Display::display_confirmation_message(get_lang('CategoryModded'));
  437. }
  438. }
  439. }
  440. // END of function editlinkcat
  441. /**
  442. * creates a correct $view for in the URL
  443. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  444. */
  445. function makedefaultviewcode($locatie)
  446. {
  447. global $aantalcategories;
  448. global $view;
  449. for ($j = 0; $j <= $aantalcategories -1; $j ++)
  450. {
  451. $view[$j] = 0;
  452. }
  453. $view[intval($locatie)] = "1";
  454. }
  455. // END of function makedefaultviewcode
  456. /**
  457. * changes the visibility of a link
  458. * @todo add the changing of the visibility of a course
  459. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  460. */
  461. function change_visibility($id, $scope)
  462. {
  463. global $_course;
  464. global $_user;
  465. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  466. if ($scope == "link")
  467. {
  468. $sqlselect = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_LINK."' and ref='".Database::escape_string($id)."'";
  469. $result = api_sql_query($sqlselect);
  470. $row = Database::fetch_array($result);
  471. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  472. }
  473. Display::display_confirmation_message(get_lang('VisibilityChanged'));
  474. }
  475. /**
  476. * displays all the links of a given category.
  477. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  478. */
  479. function showlinksofcategory($catid)
  480. {
  481. global $is_allowed, $charset, $urlview, $up, $down;
  482. $tbl_link = Database :: get_course_table(TABLE_LINK);
  483. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  484. $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";
  485. $result = api_sql_query($sqlLinks);
  486. $numberoflinks = Database::num_rows($result);
  487. echo '<table class="data_table" width="100%">';
  488. $i = 1;
  489. while ($myrow = Database::fetch_array($result))
  490. {
  491. if($i%2==0) $css_class = 'row_odd';
  492. else $css_class = 'row_even';
  493. $myrow[3] = text_filter($myrow[3]);
  494. if ($myrow['visibility'] == '1')
  495. {
  496. 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\">", htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br/>", $myrow[3], "";
  497. }
  498. else
  499. {
  500. if (api_is_allowed_to_edit())
  501. {
  502. 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\">", htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br />", $myrow[3], "";
  503. }
  504. }
  505. echo '<td style="text-align:center;">';
  506. if (api_is_allowed_to_edit())
  507. {
  508. 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>";
  509. 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>";
  510. // DISPLAY MOVE UP COMMAND only if it is not the top link
  511. if ($i != 1)
  512. {
  513. 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";
  514. }
  515. else
  516. {
  517. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="'.get_lang('Up').'"/>';
  518. }
  519. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  520. if ($i < $numberoflinks)
  521. {
  522. 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";
  523. }
  524. else
  525. {
  526. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="'.get_lang('Down').'"/>';
  527. }
  528. if ($myrow['visibility'] == "1")
  529. {
  530. 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>';
  531. }
  532. if ($myrow['visibility'] == "0")
  533. {
  534. 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>';
  535. }
  536. }
  537. echo '</td>';
  538. echo '</tr>';
  539. $i ++;
  540. }
  541. echo '</table>';
  542. }
  543. /**
  544. * displays the edit, delete and move icons
  545. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  546. */
  547. function showcategoryadmintools($categoryid)
  548. {
  549. global $urlview;
  550. global $aantalcategories;
  551. global $catcounter;
  552. 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>';
  553. 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>";
  554. // DISPLAY MOVE UP COMMAND only if it is not the top link
  555. if ($catcounter != 1)
  556. {
  557. 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";
  558. }
  559. else
  560. {
  561. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="'.get_lang('Up').'"/>';
  562. }
  563. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  564. if ($catcounter < $aantalcategories)
  565. {
  566. 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";
  567. }
  568. else
  569. {
  570. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="'.get_lang('Down').'"/>';
  571. }
  572. $catcounter ++;
  573. }
  574. /**
  575. * move a link or a linkcategory up or down
  576. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  577. */
  578. function movecatlink($catlinkid)
  579. {
  580. global $catmove;
  581. global $up;
  582. global $down;
  583. $tbl_link = Database :: get_course_table(TABLE_LINK);
  584. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  585. if (!empty($_GET['down']))
  586. {
  587. $thiscatlinkId = $_GET['down'];
  588. $sortDirection = "DESC";
  589. }
  590. if (!empty($_GET['up']))
  591. {
  592. $thiscatlinkId = Security::remove_XSS($_GET['up']);
  593. $sortDirection = "ASC";
  594. }
  595. // 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
  596. if ($catmove == "true")
  597. {
  598. $movetable = $tbl_categories;
  599. $catid = $catlinkid;
  600. }
  601. else
  602. {
  603. $movetable = $tbl_link;
  604. //getting the category of the link
  605. if(!empty($thiscatlinkId))
  606. {
  607. $sql = "SELECT category_id from ".$movetable." WHERE id='$thiscatlinkId'";
  608. $result = api_sql_query($sql, __FILE__, __LINE__);
  609. $catid = Database::fetch_array($result);
  610. }
  611. }
  612. // this code is copied and modified from announcements.php
  613. if (!empty($sortDirection))
  614. {
  615. if (!in_array(trim(strtoupper($sortDirection)), array ('ASC', 'DESC')))
  616. die("Bad sort direction used."); //sanity check of sortDirection var
  617. if ($catmove == "true")
  618. {
  619. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." ORDER BY display_order $sortDirection";
  620. }
  621. else
  622. {
  623. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." WHERE category_id='".$catid[0]."' ORDER BY display_order $sortDirection";
  624. }
  625. $linkresult = api_sql_query($sqlcatlinks);
  626. while ($sortrow = Database::fetch_array($linkresult))
  627. {
  628. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  629. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  630. // and we know the next iteration of the while loop is the next one. These should be swapped.
  631. if (isset ($thislinkFound) && $thislinkFound == true)
  632. {
  633. $nextlinkId = $sortrow["id"];
  634. $nextlinkOrdre = $sortrow["display_order"];
  635. api_sql_query("UPDATE ".$movetable."
  636. SET display_order = '$nextlinkOrdre'
  637. WHERE id = '$thiscatlinkId'");
  638. api_sql_query("UPDATE ".$movetable."
  639. SET display_order = '$thislinkOrdre'
  640. WHERE id = '$nextlinkId'");
  641. break;
  642. }
  643. if ($sortrow["id"] == $thiscatlinkId)
  644. {
  645. $thislinkOrdre = $sortrow["display_order"];
  646. $thislinkFound = true;
  647. }
  648. }
  649. }
  650. Display::display_confirmation_message(get_lang('LinkMoved'));
  651. }
  652. /**
  653. * CSV file import functions
  654. * @author Rene Haentjens , Ghent University
  655. */
  656. function get_cat($catname) // get category id (existing or make new)
  657. {
  658. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  659. $result = api_sql_query("SELECT `id` FROM ".$tbl_categories." WHERE `category_title`='".addslashes($catname)."'", __FILE__, __LINE__);
  660. if (Database::num_rows($result) >= 1 && ($row = Database::fetch_array($result)))
  661. return $row['id']; // several categories with same name: take first
  662. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."", __FILE__, __LINE__);
  663. list ($max_order) = Database::fetch_row($result);
  664. api_sql_query("INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('".addslashes($catname)."','','". ($max_order +1)."')", __FILE__, __LINE__);
  665. return Database::insert_id();
  666. }
  667. /**
  668. * CSV file import functions
  669. * @author Rene Haentjens , Ghent University
  670. */
  671. function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
  672. {
  673. $tbl_link = Database :: get_course_table(TABLE_LINK);
  674. $urleq = "url='".addslashes($url)."'";
  675. $cateq = "category_id=".$cat;
  676. $result = api_sql_query("SELECT id FROM $tbl_link WHERE ".$urleq.' AND '.$cateq, __FILE__, __LINE__);
  677. if (Database::num_rows($result) >= 1 && ($row = Database::fetch_array($result)))
  678. {
  679. api_sql_query("UPDATE $tbl_link set title='".addslashes($title)."', description='".addslashes($description)."' WHERE id='".addslashes($id = $row['id'])."'", __FILE__, __LINE__);
  680. $lang_link = get_lang('update_link');
  681. $ipu = "LinkUpdated";
  682. $rv = 1; // 1= upd
  683. }
  684. else // add new link
  685. {
  686. $result = api_sql_query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='".addslashes($cat)."'", __FILE__, __LINE__);
  687. list ($max_order) = mysql_fetch_row($result);
  688. 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__);
  689. $id = Database::insert_id();
  690. $lang_link = get_lang('new_link');
  691. $ipu = "LinkAdded";
  692. $rv = 2; // 2= new
  693. }
  694. global $_course, $nameTools, $_user;
  695. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  696. if ($hidden && $ipu == "LinkAdded")
  697. api_item_property_update($_course, TOOL_LINK, $id, "invisible", $_user['user_id']);
  698. return $rv;
  699. }
  700. /**
  701. * CSV file import functions
  702. * @author Rene Haentjens , Ghent University
  703. */
  704. function import_link($linkdata) // url, category_id, title, description, ...
  705. {
  706. // field names used in the uploaded file
  707. $known_fields = array ('url', 'category', 'title', 'description', 'on_homepage', 'hidden');
  708. $hide_fields = array ('kw', 'kwd', 'kwds', 'keyword', 'keywords');
  709. // all other fields are added to description, as "name:value"
  710. // only one hide_field is assumed to be present, <> is removed from value
  711. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title'])))
  712. return 0; // 0= fail
  713. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  714. $regs = array(); // will be passed to ereg()
  715. foreach ($linkdata as $key => $value)
  716. if (!in_array($key, $known_fields))
  717. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  718. if (($kwlist = trim($regs[1])) != '')
  719. $kw = '<i kw="'.htmlspecialchars($kwlist).'">';
  720. else
  721. $kw = '';
  722. // i.e. assume only one of the $hide_fields will be present
  723. // and if found, hide the value as expando property of an <i> tag
  724. elseif (trim($value)) $d .= ', '.$key.':'.$value;
  725. if ($d)
  726. $d = substr($d, 2).' - ';
  727. 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');
  728. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  729. }
  730. /**
  731. * CSV file import functions
  732. * @author Rene Haentjens , Ghent University
  733. */
  734. function import_csvfile()
  735. {
  736. global $catlinkstatus; // feedback message to user
  737. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r')))
  738. {
  739. // read first line of file (column names) and find ',' or ';'
  740. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== FALSE ? ',' : (strpos($colnames, ';') !== FALSE ? ';' : '');
  741. if ($listsep)
  742. {
  743. $columns = array_map('strtolower', explode($listsep, $colnames));
  744. if (in_array('url', $columns) && in_array('title', $columns))
  745. {
  746. $stats = array (0, 0, 0); // fails, updates, inserts
  747. while (($data = fgetcsv($myFile, 32768, $listsep)))
  748. {
  749. foreach ($data as $i => $text)
  750. $linkdata[$columns[$i]] = $text;
  751. // $linkdata['url', 'title', ...]
  752. $stats[import_link($linkdata)]++;
  753. unset ($linkdata);
  754. }
  755. $catlinkstatus = '';
  756. if ($stats[0])
  757. $catlinkstatus .= $stats[0].' '.get_lang('CsvLinesFailed');
  758. if ($stats[1])
  759. $catlinkstatus .= $stats[1].' '.get_lang('CsvLinesOld');
  760. if ($stats[2])
  761. $catlinkstatus .= $stats[2].' '.get_lang('CsvLinesNew');
  762. }
  763. else
  764. $catlinkstatus = get_lang('CsvFileNoURL'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  765. }
  766. else
  767. $catlinkstatus = get_lang('CsvFileNoSeps'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  768. fclose($myFile);
  769. }
  770. else
  771. $catlinkstatus = get_lang('CsvFileNotFound');
  772. }
  773. ?>