linkfunctions.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 = $_POST['title'];
  57. $urllink = $_POST['urllink'];
  58. $description = $_POST['description'];
  59. $selectcategory = $_POST['selectcategory'];
  60. if ($_POST['onhomepage'] == '')
  61. {
  62. $onhomepage = 0;
  63. }
  64. else
  65. {
  66. $onhomepage = $_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='".$_POST['selectcategory']."'");
  92. list ($orderMax) = mysql_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. unset ($urllink, $title, $description, $selectcategory);
  98. Display::display_confirmation_message(get_lang('LinkAdded'));
  99. }
  100. }
  101. elseif ($type == "category")
  102. {
  103. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  104. $category_title = trim($_POST['category_title']);
  105. $description = trim($_POST['description']);
  106. if (empty ($category_title))
  107. {
  108. $msgErr = get_lang('GiveCategoryName');
  109. Display::display_error_message(get_lang('GiveCategoryName'));
  110. $ok = false;
  111. }
  112. else
  113. {
  114. // looking for the largest order number for this category
  115. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."");
  116. list ($orderMax) = mysql_fetch_row($result);
  117. $order = $orderMax +1;
  118. $sql = "INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('$category_title','$description', '$order')";
  119. api_sql_query($sql, __FILE__, __LINE__);
  120. $catlinkstatus = get_lang('CategoryAdded');
  121. unset ($category_title, $description);
  122. Display::display_confirmation_message(get_lang('CategoryAdded'));
  123. }
  124. }
  125. // "WHAT'S NEW" notification : update last tool Edit
  126. if ($type == "link")
  127. {
  128. global $_user;
  129. global $_course;
  130. global $nameTools;
  131. api_item_property_update($_course, TOOL_LINK, mysql_insert_id(), "LinkAdded", $_user['user_id']);
  132. }
  133. return $ok;
  134. }
  135. // End of the function addlinkcategory
  136. /**
  137. * Used to delete a link or a category
  138. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  139. */
  140. function deletelinkcategory($type)
  141. {
  142. global $catlinkstatus;
  143. global $_course;
  144. global $_user;
  145. $tbl_link = Database :: get_course_table(TABLE_LINK);
  146. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  147. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  148. if ($type == "link")
  149. {
  150. global $id;
  151. // -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
  152. // make a restore function possible for the platform administrator
  153. //$sql="DELETE FROM $tbl_link WHERE id='".$_GET['id']."'";
  154. api_item_property_update($_course, TOOL_LINK, $id, "delete", $_user['user_id']);
  155. $catlinkstatus = get_lang("LinkDeleted");
  156. unset ($id);
  157. Display::display_confirmation_message(get_lang('LinkDeleted'));
  158. }
  159. if ($type == "category")
  160. {
  161. global $id;
  162. // first we delete the category itself and afterwards all the links of this category.
  163. $sql = "DELETE FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  164. api_sql_query($sql, __FILE__, __LINE__);
  165. $sql = "DELETE FROM ".$tbl_link." WHERE category_id='".$_GET['id']."'";
  166. $catlinkstatus = get_lang('CategoryDeleted');
  167. unset ($id);
  168. api_sql_query($sql, __FILE__, __LINE__);
  169. Display::display_confirmation_message(get_lang('CategoryDeleted'));
  170. }
  171. }
  172. /**
  173. * Used to edit a link or a category
  174. * @todo rewrite the whole links tool because it is becoming completely cluttered,
  175. * code does not follow the coding conventions, does not use html_quickform, ...
  176. * some features were patched in
  177. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  178. * @todo replace the globals with the appropriate $_POST or $_GET values
  179. */
  180. function editlinkcategory($type)
  181. {
  182. global $catlinkstatus;
  183. global $id;
  184. global $submitLink;
  185. global $submitCategory;
  186. global $_user;
  187. global $_course;
  188. global $nameTools;
  189. global $urllink;
  190. global $title;
  191. global $description;
  192. global $category;
  193. global $selectcategory;
  194. global $description;
  195. global $category_title;
  196. global $onhomepage;
  197. $tbl_link = Database :: get_course_table(TABLE_LINK);
  198. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  199. if ($type == "link")
  200. {
  201. // this is used to populate the link-form with the info found in the database
  202. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".$_GET['id']."'";
  203. $result = api_sql_query($sql, __FILE__, __LINE__);
  204. if ($myrow = mysql_fetch_array($result))
  205. {
  206. $urllink = $myrow["url"];
  207. $title = $myrow["title"];
  208. $description = $myrow["description"];
  209. $category = $myrow["category_id"];
  210. if ($myrow["on_homepage"] <> 0)
  211. {
  212. $onhomepage = "checked";
  213. }
  214. }
  215. // this is used to put the modified info of the link-form into the database
  216. if ($_POST['submitLink'])
  217. {
  218. if ($_POST['onhomepage'] == '')
  219. {
  220. $onhomepage = 0;
  221. }
  222. else
  223. {
  224. $onhomepage = $_POST['onhomepage'];
  225. }
  226. // finding the old category_id
  227. $sql = "SELECT * FROM ".$tbl_link." WHERE id='".$_POST['id']."'";
  228. $result = api_sql_query($sql, __FILE__, __LINE__);
  229. $row = mysql_fetch_array($result);
  230. $category_id = $row['category_id'];
  231. if ($category_id <> $_POST['selectcategory'])
  232. {
  233. $sql = "SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'";
  234. $result = api_sql_query($sql);
  235. list ($max_display_order) = mysql_fetch_row($result);
  236. $max_display_order ++;
  237. }
  238. else
  239. {
  240. $max_display_order = $row['display_order'];
  241. }
  242. $sql = "UPDATE ".$tbl_link." set url='".$_POST['urllink']."', title='".$_POST['title']."', description='".$_POST['description']."', category_id='".$_POST['selectcategory']."', display_order='".$max_display_order."', on_homepage='".$_POST['onhomepage']."' WHERE id='".$_POST['id']."'";
  243. api_sql_query($sql, __FILE__, __LINE__);
  244. // "WHAT'S NEW" notification: update table last_toolEdit
  245. api_item_property_update($_course, TOOL_LINK, $_POST['id'], "LinkUpdated", $_user['user_id']);
  246. Display::display_confirmation_message(get_lang('LinkModded'));
  247. }
  248. }
  249. if ($type == "category")
  250. {
  251. // this is used to populate the category-form with the info found in the database
  252. if (!$submitCategory)
  253. {
  254. $sql = "SELECT * FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  255. $result = api_sql_query($sql, __FILE__, __LINE__);
  256. if ($myrow = mysql_fetch_array($result))
  257. {
  258. $category_title = $myrow["category_title"];
  259. $description = $myrow["description"];
  260. }
  261. }
  262. // this is used to put the modified info of the category-form into the database
  263. if ($submitCategory)
  264. {
  265. $sql = "UPDATE ".$tbl_categories." set category_title='".$_POST['category_title']."', description='".$_POST['description']."' WHERE id='".$_POST['id']."'";
  266. api_sql_query($sql, __FILE__, __LINE__);
  267. Display::display_confirmation_message(get_lang('CategoryModded'));
  268. }
  269. }
  270. }
  271. // END of function editlinkcat
  272. /**
  273. * creates a correct $view for in the URL
  274. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  275. */
  276. function makedefaultviewcode($locatie)
  277. {
  278. global $aantalcategories;
  279. global $view;
  280. for ($j = 0; $j <= $aantalcategories -1; $j ++)
  281. {
  282. $view[$j] = 0;
  283. }
  284. $view[intval($locatie)] = "1";
  285. }
  286. // END of function makedefaultviewcode
  287. /**
  288. * changes the visibility of a link
  289. * @todo add the changing of the visibility of a course
  290. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  291. */
  292. function change_visibility($id, $scope)
  293. {
  294. global $_course;
  295. global $_user;
  296. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  297. if ($scope == "link")
  298. {
  299. $sqlselect = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_LINK."' and ref='".$id."'";
  300. $result = api_sql_query($sqlselect);
  301. $row = mysql_fetch_array($result);
  302. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  303. }
  304. Display::display_confirmation_message(get_lang('VisibilityChanged'));
  305. }
  306. /**
  307. * displays all the links of a given category.
  308. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  309. */
  310. function showlinksofcategory($catid)
  311. {
  312. global $is_allowed, $charset, $urlview, $up, $down;
  313. $tbl_link = Database :: get_course_table(TABLE_LINK);
  314. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  315. $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";
  316. $result = api_sql_query($sqlLinks);
  317. $numberoflinks = mysql_num_rows($result);
  318. echo '<table class="data_table" width="100%">';
  319. $i = 1;
  320. while ($myrow = mysql_fetch_array($result))
  321. {
  322. if($i%2==0) $css_class = 'row_odd';
  323. else $css_class = 'row_even';
  324. $myrow[3] = text_filter($myrow[3]);
  325. if ($myrow['visibility'] == '1')
  326. {
  327. 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('Links')."\"/>", "</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], "";
  328. }
  329. else
  330. {
  331. if (api_is_allowed_to_edit())
  332. {
  333. 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('Links')),"</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], "";
  334. }
  335. }
  336. echo '<td style="text-align:center;">';
  337. if (api_is_allowed_to_edit())
  338. {
  339. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=editlink&amp;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>";
  340. 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>";
  341. // DISPLAY MOVE UP COMMAND only if it is not the top link
  342. if ($i != 1)
  343. {
  344. 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=\"Up\"/>", "</a>\n";
  345. }
  346. else
  347. {
  348. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="Up"/>';
  349. }
  350. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  351. if ($i < $numberoflinks)
  352. {
  353. 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=\"Down\"/>", "</a>\n";
  354. }
  355. else
  356. {
  357. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="Up"/>';
  358. }
  359. if ($myrow['visibility'] == "1")
  360. {
  361. echo '<a href="link.php?'.api_get_cidreq().'&action=invisible&amp;id='.$myrow['id'].'&amp;scope=link&amp;urlview='.$urlview.'" title="'.get_lang('langVisible').'"><img src="../img/visible.gif" border="0" /></a>';
  362. }
  363. if ($myrow['visibility'] == "0")
  364. {
  365. echo '<a href="link.php?'.api_get_cidreq().'&action=visible&amp;id='.$myrow['id'].'&amp;scope=link&amp;urlview='.$urlview.'" title="'.get_lang('langVisible').'"><img src="../img/invisible.gif" border="0" /></a>';
  366. }
  367. }
  368. echo '</td>';
  369. echo '</tr>';
  370. $i ++;
  371. }
  372. echo '</table>';
  373. }
  374. /**
  375. * displays the edit, delete and move icons
  376. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  377. */
  378. function showcategoryadmintools($categoryid)
  379. {
  380. global $urlview;
  381. global $aantalcategories;
  382. global $catcounter;
  383. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=editcategory&amp;id='.$categoryid.'&amp;urlview=$amp;urlview\" title="'.get_lang('Modify').'" ><img src="../img/edit.gif" border="0" alt="'.get_lang('Modify').' "/></a>';
  384. 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>";
  385. // DISPLAY MOVE UP COMMAND only if it is not the top link
  386. if ($catcounter != 1)
  387. {
  388. 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=\"Up\"/>", "</a>\n";
  389. }
  390. else
  391. {
  392. echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="Up"/>';
  393. }
  394. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  395. if ($catcounter < $aantalcategories)
  396. {
  397. 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=\"Down\"/>", "</a>\n";
  398. }
  399. else
  400. {
  401. echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="Up"/>';
  402. }
  403. $catcounter ++;
  404. }
  405. /**
  406. * move a link or a linkcategory up or down
  407. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  408. */
  409. function movecatlink($catlinkid)
  410. {
  411. global $catmove;
  412. global $up;
  413. global $down;
  414. $tbl_link = Database :: get_course_table(TABLE_LINK);
  415. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  416. if ($_GET['down'])
  417. {
  418. $thiscatlinkId = $_GET['down'];
  419. $sortDirection = "DESC";
  420. }
  421. if ($_GET['up'])
  422. {
  423. $thiscatlinkId = $_GET['up'];
  424. $sortDirection = "ASC";
  425. }
  426. // 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
  427. if ($catmove == "true")
  428. {
  429. $movetable = $tbl_categories;
  430. $catid = $catlinkid;
  431. }
  432. else
  433. {
  434. $movetable = $tbl_link;
  435. //getting the category of the link
  436. $sql = "SELECT category_id from ".$movetable." WHERE id='$thiscatlinkId'";
  437. $result = api_sql_query($sql, __FILE__, __LINE__);
  438. $catid = mysql_fetch_array($result);
  439. }
  440. // this code is copied and modified from announcements.php
  441. if ($sortDirection)
  442. {
  443. if (!in_array(trim(strtoupper($sortDirection)), array ('ASC', 'DESC')))
  444. die("Bad sort direction used."); //sanity check of sortDirection var
  445. if ($catmove == "true")
  446. {
  447. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." ORDER BY display_order $sortDirection";
  448. }
  449. else
  450. {
  451. $sqlcatlinks = "SELECT id, display_order FROM ".$movetable." WHERE category_id='".$catid[0]."' ORDER BY display_order $sortDirection";
  452. }
  453. $linkresult = api_sql_query($sqlcatlinks);
  454. while ($sortrow = mysql_fetch_array($linkresult))
  455. {
  456. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  457. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  458. // and we know the next iteration of the while loop is the next one. These should be swapped.
  459. if (isset ($thislinkFound) && $thislinkFound == true)
  460. {
  461. $nextlinkId = $sortrow["id"];
  462. $nextlinkOrdre = $sortrow["display_order"];
  463. api_sql_query("UPDATE ".$movetable."
  464. SET display_order = '$nextlinkOrdre'
  465. WHERE id = '$thiscatlinkId'");
  466. api_sql_query("UPDATE ".$movetable."
  467. SET display_order = '$thislinkOrdre'
  468. WHERE id = '$nextlinkId'");
  469. break;
  470. }
  471. if ($sortrow["id"] == $thiscatlinkId)
  472. {
  473. $thislinkOrdre = $sortrow["display_order"];
  474. $thislinkFound = true;
  475. }
  476. }
  477. }
  478. Display::display_confirmation_message(get_lang('LinkMoved'));
  479. }
  480. /**
  481. * CSV file import functions
  482. * @author Rene Haentjens , Ghent University
  483. */
  484. function get_cat($catname) // get category id (existing or make new)
  485. {
  486. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  487. $result = api_sql_query("SELECT `id` FROM ".$tbl_categories." WHERE `category_title`='".addslashes($catname)."'", __FILE__, __LINE__);
  488. if (mysql_num_rows($result) >= 1 && ($row = mysql_fetch_array($result)))
  489. return $row['id']; // several categories with same name: take first
  490. $result = api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories."", __FILE__, __LINE__);
  491. list ($max_order) = mysql_fetch_row($result);
  492. api_sql_query("INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('".addslashes($catname)."','','". ($max_order +1)."')", __FILE__, __LINE__);
  493. return mysql_insert_id();
  494. }
  495. /**
  496. * CSV file import functions
  497. * @author Rene Haentjens , Ghent University
  498. */
  499. function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
  500. {
  501. $tbl_link = Database :: get_course_table(TABLE_LINK);
  502. $urleq = "url='".addslashes($url)."'";
  503. $cateq = "category_id=".$cat;
  504. $result = api_sql_query("SELECT id FROM $tbl_link WHERE ".$urleq.' AND '.$cateq, __FILE__, __LINE__);
  505. if (mysql_num_rows($result) >= 1 && ($row = mysql_fetch_array($result)))
  506. {
  507. api_sql_query("UPDATE $tbl_link set title='".addslashes($title)."', description='".addslashes($description)."' WHERE id='".addslashes($id = $row['id'])."'", __FILE__, __LINE__);
  508. $lang_link = get_lang('update_link');
  509. $ipu = "LinkUpdated";
  510. $rv = 1; // 1= upd
  511. }
  512. else // add new link
  513. {
  514. $result = api_sql_query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='".addslashes($cat)."'", __FILE__, __LINE__);
  515. list ($max_order) = mysql_fetch_row($result);
  516. 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__);
  517. $id = mysql_insert_id();
  518. $lang_link = get_lang('new_link');
  519. $ipu = "LinkAdded";
  520. $rv = 2; // 2= new
  521. }
  522. global $_course, $nameTools, $_user;
  523. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  524. if ($hidden && $ipu == "LinkAdded")
  525. api_item_property_update($_course, TOOL_LINK, $id, "invisible", $_user['user_id']);
  526. return $rv;
  527. }
  528. /**
  529. * CSV file import functions
  530. * @author Rene Haentjens , Ghent University
  531. */
  532. function import_link($linkdata) // url, category_id, title, description, ...
  533. {
  534. // field names used in the uploaded file
  535. $known_fields = array ('url', 'category', 'title', 'description', 'on_homepage', 'hidden');
  536. $hide_fields = array ('kw', 'kwd', 'kwds', 'keyword', 'keywords');
  537. // all other fields are added to description, as "name:value"
  538. // only one hide_field is assumed to be present, <> is removed from value
  539. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title'])))
  540. return 0; // 0= fail
  541. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  542. $regs = array(); // will be passed to ereg()
  543. foreach ($linkdata as $key => $value)
  544. if (!in_array($key, $known_fields))
  545. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  546. if (($kwlist = trim($regs[1])) != '')
  547. $kw = '<i kw="'.htmlspecialchars($kwlist).'">';
  548. else
  549. $kw = '';
  550. // i.e. assume only one of the $hide_fields will be present
  551. // and if found, hide the value as expando property of an <i> tag
  552. elseif (trim($value)) $d .= ', '.$key.':'.$value;
  553. if ($d)
  554. $d = substr($d, 2).' - ';
  555. 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');
  556. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  557. }
  558. /**
  559. * CSV file import functions
  560. * @author Rene Haentjens , Ghent University
  561. */
  562. function import_csvfile()
  563. {
  564. global $catlinkstatus; // feedback message to user
  565. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r')))
  566. {
  567. // read first line of file (column names) and find ',' or ';'
  568. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== FALSE ? ',' : (strpos($colnames, ';') !== FALSE ? ';' : '');
  569. if ($listsep)
  570. {
  571. $columns = array_map('strtolower', explode($listsep, $colnames));
  572. if (in_array('url', $columns) && in_array('title', $columns))
  573. {
  574. $stats = array (0, 0, 0); // fails, updates, inserts
  575. while (($data = fgetcsv($myFile, 32768, $listsep)))
  576. {
  577. foreach ($data as $i => $text)
  578. $linkdata[$columns[$i]] = $text;
  579. // $linkdata['url', 'title', ...]
  580. $stats[import_link($linkdata)]++;
  581. unset ($linkdata);
  582. }
  583. $catlinkstatus = '';
  584. if ($stats[0])
  585. $catlinkstatus .= $stats[0].' '.get_lang('CsvLinesFailed');
  586. if ($stats[1])
  587. $catlinkstatus .= $stats[1].' '.get_lang('CsvLinesOld');
  588. if ($stats[2])
  589. $catlinkstatus .= $stats[2].' '.get_lang('CsvLinesNew');
  590. }
  591. else
  592. $catlinkstatus = get_lang('CsvFileNoURL'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  593. }
  594. else
  595. $catlinkstatus = get_lang('CsvFileNoSeps'). ($colnames ? get_lang('CsvFileLine1').htmlspecialchars(substr($colnames, 0, 200)).'...' : '');
  596. fclose($myFile);
  597. }
  598. else
  599. $catlinkstatus = get_lang('CsvFileNotFound');
  600. }
  601. ?>