linkfunctions.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 René 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 = Database::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. $session_id = api_get_session_id();
  94. $sql = "INSERT INTO ".$tbl_link." (url, title, description, category_id, display_order, on_homepage, target, session_id) VALUES ('$urllink','$title','$description','$selectcategory','$order', '$onhomepage','$target','$session_id')";
  95. $catlinkstatus = get_lang('LinkAdded');
  96. Database::query($sql, __FILE__, __LINE__);
  97. $link_id = Database::insert_id();
  98. if ( (api_get_setting('search_enabled')=='true') && $link_id && extension_loaded('xapian')) {
  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 = Database::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. Database::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 = Database::query("SELECT MAX(display_order) FROM ".$tbl_categories."");
  173. list ($orderMax) = Database::fetch_row($result);
  174. $order = $orderMax +1;
  175. $session_id = api_get_session_id();
  176. $sql = "INSERT INTO ".$tbl_categories." (category_title, description, display_order, session_id) VALUES ('".Security::remove_XSS($category_title)."','".Security::remove_XSS($description)."', '$order', '$session_id')";
  177. Database::query($sql, __FILE__, __LINE__);
  178. $catlinkstatus = get_lang('CategoryAdded');
  179. unset ($category_title, $description);
  180. Display::display_confirmation_message(get_lang('CategoryAdded'));
  181. }
  182. }
  183. // "WHAT'S NEW" notification : update last tool Edit
  184. if ($type == "link")
  185. {
  186. global $_user;
  187. global $_course;
  188. global $nameTools;
  189. api_item_property_update($_course, TOOL_LINK, $link_id, "LinkAdded", $_user['user_id']);
  190. }
  191. return $ok;
  192. }
  193. // End of the function addlinkcategory
  194. /**
  195. * Used to delete a link or a category
  196. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  197. */
  198. function deletelinkcategory($type)
  199. {
  200. global $catlinkstatus;
  201. global $_course;
  202. global $_user;
  203. $tbl_link = Database :: get_course_table(TABLE_LINK);
  204. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  205. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  206. if ($type == "link")
  207. {
  208. global $id;
  209. // -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
  210. // make a restore function possible for the platform administrator
  211. if (isset($_GET['id']) && $_GET['id']==strval(intval($_GET['id']))) {
  212. $sql="UPDATE $tbl_link SET on_homepage='0' WHERE id='".Database::escape_string($_GET['id'])."'";
  213. Database::query($sql,__FILE__,__LINE__);
  214. }
  215. api_item_property_update($_course, TOOL_LINK, $id, "delete", $_user['user_id']);
  216. delete_link_from_search_engine(api_get_course_id(), $id);
  217. $catlinkstatus = get_lang("LinkDeleted");
  218. unset ($id);
  219. Display::display_confirmation_message(get_lang('LinkDeleted'));
  220. }
  221. if ($type == "category")
  222. {
  223. global $id;
  224. // first we delete the category itself and afterwards all the links of this category.
  225. $sql = "DELETE FROM ".$tbl_categories." WHERE id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  226. Database::query($sql, __FILE__, __LINE__);
  227. $sql = "DELETE FROM ".$tbl_link." WHERE category_id='".Database::escape_string(Security::remove_XSS($_GET['id']))."'";
  228. $catlinkstatus = get_lang('CategoryDeleted');
  229. unset ($id);
  230. Database::query($sql, __FILE__, __LINE__);
  231. Display::display_confirmation_message(get_lang('CategoryDeleted'));
  232. }
  233. }
  234. /**
  235. * Removes a link from search engine database
  236. *
  237. * @param string $course_id Course code
  238. * @param int $document_id Document id to delete
  239. */
  240. function delete_link_from_search_engine($course_id, $link_id) {
  241. // remove from search engine if enabled
  242. if (api_get_setting('search_enabled') == 'true') {
  243. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  244. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  245. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  246. $res = Database::query($sql, __FILE__, __LINE__);
  247. if (Database::num_rows($res) > 0) {
  248. $row = Database::fetch_array($res);
  249. require_once(api_get_path(LIBRARY_PATH) .'search/DokeosIndexer.class.php');
  250. $di = new DokeosIndexer();
  251. $di->remove_document((int)$row['search_did']);
  252. }
  253. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  254. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  255. Database::query($sql, __FILE__, __LINE__);
  256. // remove terms from db
  257. require_once(api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php');
  258. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  259. }
  260. }
  261. /**
  262. * Used to edit a link or a category
  263. * @todo rewrite the whole links tool because it is becoming completely cluttered,
  264. * code does not follow the coding conventions, does not use html_quickform, ...
  265. * some features were patched in
  266. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  267. * @todo replace the globals with the appropriate $_POST or $_GET values
  268. */
  269. function editlinkcategory($type)
  270. {
  271. global $catlinkstatus;
  272. global $id;
  273. global $submitLink;
  274. global $submitCategory;
  275. global $_user;
  276. global $_course;
  277. global $nameTools;
  278. global $urllink;
  279. global $title;
  280. global $description;
  281. global $category;
  282. global $selectcategory;
  283. global $description;
  284. global $category_title;
  285. global $onhomepage;
  286. $tbl_link = Database :: get_course_table(TABLE_LINK);
  287. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  288. if ($type == "link")
  289. {
  290. // this is used to populate the link-form with the info found in the database
  291. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".$_GET['id']."'";
  292. $result = Database::query($sql, __FILE__, __LINE__);
  293. if ($myrow = Database::fetch_array($result))
  294. {
  295. $urllink = $myrow["url"];
  296. $title = $myrow["title"];
  297. $description = $myrow["description"];
  298. $category = $myrow["category_id"];
  299. if ($myrow["on_homepage"] <> 0)
  300. {
  301. $onhomepage = "checked";
  302. }
  303. }
  304. // this is used to put the modified info of the link-form into the database
  305. if ($_POST['submitLink'])
  306. {
  307. if ($_POST['onhomepage'] == '') {
  308. $onhomepage = 0;
  309. $mytarget='';
  310. } else {
  311. $onhomepage = Security::remove_XSS($_POST['onhomepage']);
  312. $target = Security::remove_XSS($_POST['target_link']);
  313. $mytarget=",target='".$target."'";
  314. }
  315. // finding the old category_id
  316. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".Database::escape_string(Security::remove_XSS($_POST['id']))."'";
  317. $result = Database::query($sql, __FILE__, __LINE__);
  318. $row = Database::fetch_array($result);
  319. $category_id = $row['category_id'];
  320. if ($category_id <> $_POST['selectcategory']) {
  321. $sql = "SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'";
  322. $result = Database::query($sql);
  323. list ($max_display_order) = Database::fetch_row($result);
  324. $max_display_order ++;
  325. } else {
  326. $max_display_order = $row['display_order'];
  327. }
  328. $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']))."'";
  329. Database::query($sql, __FILE__, __LINE__);
  330. // update search enchine and its values table if enabled
  331. if (api_get_setting('search_enabled')=='true') {
  332. $link_id = $_POST['id'];
  333. $course_id = api_get_course_id();
  334. $link_url = $_POST['urllink'];
  335. $link_title = $_POST['title'];
  336. $link_description = $_POST['description'];
  337. // actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one
  338. // get search_did
  339. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  340. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  341. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  342. $res = Database::query($sql, __FILE__, __LINE__);
  343. if (Database::num_rows($res) > 0) {
  344. require_once(api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php');
  345. require_once(api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php');
  346. require_once(api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  347. $se_ref = Database::fetch_array($res);
  348. $specific_fields = get_specific_field_list();
  349. $ic_slide = new IndexableChunk();
  350. $all_specific_terms = '';
  351. foreach ($specific_fields as $specific_field) {
  352. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  353. if (isset($_REQUEST[$specific_field['code']])) {
  354. $sterms = trim($_REQUEST[$specific_field['code']]);
  355. if (!empty($sterms)) {
  356. $all_specific_terms .= ' '. $sterms;
  357. $sterms = explode(',', $sterms);
  358. foreach ($sterms as $sterm) {
  359. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  360. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  361. }
  362. }
  363. }
  364. }
  365. // build the chunk to index
  366. $ic_slide->addValue("title", $link_title);
  367. $ic_slide->addCourseId($course_id);
  368. $ic_slide->addToolId(TOOL_LINK);
  369. $xapian_data = array(
  370. SE_COURSE_ID => $course_id,
  371. SE_TOOL_ID => TOOL_LINK,
  372. SE_DATA => array('link_id' => (int)$link_id),
  373. SE_USER => (int)api_get_user_id(),
  374. );
  375. $ic_slide->xapian_data = serialize($xapian_data);
  376. $link_description = $all_specific_terms .' '. $link_description;
  377. $ic_slide->addValue("content", $link_description);
  378. // add category name if set
  379. if (isset($_POST['selectcategory']) && $selectcategory>0) {
  380. $table_link_category = Database::get_course_table(TABLE_LINK_CATEGORY);
  381. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  382. $sql_cat = sprintf($sql_cat, $table_link_category, (int)$selectcategory);
  383. $result = Database::query($sql_cat, __FILE__, __LINE__);
  384. if (Database::num_rows($result) == 1) {
  385. $row = Database::fetch_array($result);
  386. $ic_slide->addValue("category", $row['category_title']);
  387. }
  388. }
  389. $di = new DokeosIndexer();
  390. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  391. $di->connectDb(NULL, NULL, $lang);
  392. $di->remove_document((int)$se_ref['search_did']);
  393. $di->addChunk($ic_slide);
  394. //index and return search engine document id
  395. $did = $di->index();
  396. if ($did) {
  397. // save it to db
  398. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  399. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  400. Database::query($sql,__FILE__,__LINE__);
  401. //var_dump($sql);
  402. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  403. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  404. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id, $did);
  405. Database::query($sql,__FILE__,__LINE__);
  406. }
  407. }
  408. }
  409. // "WHAT'S NEW" notification: update table last_toolEdit
  410. api_item_property_update($_course, TOOL_LINK, $_POST['id'], "LinkUpdated", $_user['user_id']);
  411. Display::display_confirmation_message(get_lang('LinkModded'));
  412. }
  413. }
  414. if ($type == "category")
  415. {
  416. // this is used to populate the category-form with the info found in the database
  417. if (!$submitCategory)
  418. {
  419. $sql = "SELECT * FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  420. $result = Database::query($sql, __FILE__, __LINE__);
  421. if ($myrow = Database::fetch_array($result))
  422. {
  423. $category_title = $myrow["category_title"];
  424. $description = $myrow["description"];
  425. }
  426. }
  427. // this is used to put the modified info of the category-form into the database
  428. if ($submitCategory)
  429. {
  430. $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']))."'";
  431. Database::query($sql, __FILE__, __LINE__);
  432. Display::display_confirmation_message(get_lang('CategoryModded'));
  433. }
  434. }
  435. }
  436. // END of function editlinkcat
  437. /**
  438. * creates a correct $view for in the URL
  439. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  440. */
  441. function makedefaultviewcode($locatie)
  442. {
  443. global $aantalcategories;
  444. global $view;
  445. for ($j = 0; $j <= $aantalcategories -1; $j ++)
  446. {
  447. $view[$j] = 0;
  448. }
  449. $view[intval($locatie)] = "1";
  450. }
  451. // END of function makedefaultviewcode
  452. /**
  453. * changes the visibility of a link
  454. * @todo add the changing of the visibility of a course
  455. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  456. */
  457. function change_visibility($id, $scope)
  458. {
  459. global $_course;
  460. global $_user;
  461. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  462. if ($scope == "link")
  463. {
  464. $sqlselect = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_LINK."' and ref='".Database::escape_string($id)."'";
  465. $result = Database::query($sqlselect);
  466. $row = Database::fetch_array($result);
  467. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  468. }
  469. Display::display_confirmation_message(get_lang('VisibilityChanged'));
  470. }
  471. /**
  472. * displays all the links of a given category.
  473. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  474. */
  475. function showlinksofcategory($catid)
  476. {
  477. global $is_allowed, $charset, $urlview, $up, $down, $_user;
  478. $tbl_link = Database :: get_course_table(TABLE_LINK);
  479. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  480. //condition for the session
  481. $session_id = api_get_session_id();
  482. $condition_session = api_get_session_condition($session_id);
  483. $sqlLinks = "SELECT * FROM ".$tbl_link." link, ".$TABLE_ITEM_PROPERTY." itemproperties WHERE itemproperties.tool='".TOOL_LINK."' AND link.id=itemproperties.ref AND link.category_id='".$catid."' AND (itemproperties.visibility='0' OR itemproperties.visibility='1') $condition_session ORDER BY link.display_order DESC";
  484. $result = Database::query($sqlLinks);
  485. $numberoflinks = Database::num_rows($result);
  486. echo '<table class="data_table" width="100%">';
  487. $i = 1;
  488. while ($myrow = Database::fetch_array($result)) {
  489. //validacion when belongs to a session
  490. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  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\">", api_htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>", $session_img , "<br/>", $myrow[3], "";
  497. }
  498. else
  499. {
  500. if (api_is_allowed_to_edit(null,true))
  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\">", api_htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", $session_img , "<br />", $myrow[3], "";
  503. }
  504. }
  505. echo '<td style="text-align:center;">';
  506. if (api_is_allowed_to_edit(null,true))
  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 = Database::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 = Database::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. Database::query("UPDATE ".$movetable."
  636. SET display_order = '$nextlinkOrdre'
  637. WHERE id = '$thiscatlinkId'");
  638. Database::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 René 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 = Database::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 = Database::query("SELECT MAX(display_order) FROM ".$tbl_categories."", __FILE__, __LINE__);
  663. list ($max_order) = Database::fetch_row($result);
  664. Database::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 René 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 = Database::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. Database::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 = Database::query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='".addslashes($cat)."'", __FILE__, __LINE__);
  687. list ($max_order) = Database::fetch_row($result);
  688. Database::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 René 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 René 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. ?>