linkfunctions.php 22 KB

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