linkfunctions.php 34 KB

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