dokeos-link
[ class tree: dokeos-link ] [ index: dokeos-link ] [ all elements ]

Source for file linkfunctions.php

Documentation is available at linkfunctions.php

  1. <?php
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2004-2008 Dokeos S.A.
  7.     Copyright (c) 2003 Ghent University (UGent)
  8.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9.  
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.  
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.  
  18.     See the GNU General Public License for more details.
  19.  
  20.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  21.     Mail: info@dokeos.com
  22. ==============================================================================
  23. */
  24. /**
  25. ==============================================================================
  26. * Function library for the links tool.
  27. *
  28. * This is a complete remake of the original link tool.
  29. * New features:
  30. * - Organize links into categories;
  31. * - favorites/bookmarks interface;
  32. * - move links up/down within a category;
  33. * - move categories up/down;
  34. * - expand/collapse all categories;
  35. * - add link to 'root' category => category-less link is always visible.
  36. *
  37. *    @author Patrick Cool, complete remake (December 2003 - January 2004)
  38. *    @author Rene Haentjens, CSV file import (October 2004)
  39. *    @package dokeos.link
  40. ==============================================================================
  41. */
  42.  
  43. /*
  44. ==============================================================================
  45.         FUNCTIONS
  46. ==============================================================================
  47. */
  48.  
  49. /**
  50. * Used to add a link or a category
  51. @param string $type, "link" or "category"
  52. @todo replace strings by constants
  53. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  54. */
  55. function addlinkcategory($type)
  56. {
  57.     global $catlinkstatus;
  58.     global $msgErr;
  59.  
  60.     $ok true;
  61.  
  62.     if ($type == "link")
  63.     {
  64.         $tbl_link Database :: get_course_table(TABLE_LINK);
  65.  
  66.         $title $_POST['title'];
  67.         $urllink $_POST['urllink'];
  68.         $description $_POST['description'];
  69.         $selectcategory $_POST['selectcategory'];
  70.         if ($_POST['onhomepage'== '')
  71.         {
  72.             $onhomepage 0;
  73.         }
  74.         else
  75.         {
  76.             $onhomepage $_POST['onhomepage'];
  77.         }
  78.  
  79.         $urllink trim($urllink);
  80.         $title trim($title);
  81.         $description trim($description);
  82.  
  83.         // if title is empty, an error occurs
  84.         if (empty ($urllinkOR $urllink == 'http://')
  85.         {
  86.             $msgErr get_lang('GiveURL');
  87.  
  88.             Display::display_error_message(get_lang('GiveURL'));
  89.  
  90.             $ok false;
  91.         }
  92.         // if the title is empty, we use the url as the title
  93.         else
  94.         {
  95.             if (empty ($title))
  96.             {
  97.                 $title $urllink;
  98.             }
  99.  
  100.             // we check weither the $url starts with http://, if not we add this
  101.             if (!strstr($urllink'://'))
  102.             {
  103.                 $urllink "http://".$urllink;
  104.             }
  105.  
  106.             // looking for the largest order number for this category
  107.             $result api_sql_query("SELECT MAX(display_order) FROM  ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'");
  108.  
  109.             list ($orderMaxmysql_fetch_row($result);
  110.  
  111.             $order $orderMax +1;
  112.  
  113.             $sql "INSERT INTO ".$tbl_link." (url, title, description, category_id,display_order, on_homepage) VALUES ('$urllink','$title','$description','$selectcategory','$order', '$onhomepage')";
  114.             $catlinkstatus get_lang('LinkAdded');
  115.             api_sql_query($sql__FILE____LINE__);
  116.             unset ($urllink$title$description$selectcategory);
  117.  
  118.             Display::display_confirmation_message(get_lang('LinkAdded'));
  119.         }
  120.     }
  121.     elseif ($type == "category")
  122.     {
  123.         $tbl_categories Database :: get_course_table(TABLE_LINK_CATEGORY);
  124.  
  125.         $category_title trim($_POST['category_title']);
  126.         $description trim($_POST['description']);
  127.  
  128.         if (empty ($category_title))
  129.         {
  130.             $msgErr get_lang('GiveCategoryName');
  131.  
  132.             Display::display_error_message(get_lang('GiveCategoryName'));
  133.  
  134.             $ok false;
  135.         }
  136.         else
  137.         {
  138.             // looking for the largest order number for this category
  139.             $result api_sql_query("SELECT MAX(display_order) FROM  ".$tbl_categories."");
  140.  
  141.             list ($orderMaxmysql_fetch_row($result);
  142.  
  143.             $order $orderMax +1;
  144.  
  145.             $sql "INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('$category_title','$description', '$order')";
  146.             api_sql_query($sql__FILE____LINE__);
  147.  
  148.             $catlinkstatus get_lang('CategoryAdded');
  149.  
  150.             unset ($category_title$description);
  151.  
  152.             Display::display_confirmation_message(get_lang('CategoryAdded'));
  153.         }
  154.     }
  155.  
  156.     // "WHAT'S NEW" notification : update last tool Edit
  157.     if ($type == "link")
  158.     {
  159.         global $_user;
  160.         global $_course;
  161.         global $nameTools;
  162.  
  163.         api_item_property_update($_courseTOOL_LINKmysql_insert_id()"LinkAdded"$_user['user_id']);
  164.     }
  165.  
  166.     return $ok;
  167. }
  168. // End of the function addlinkcategory
  169.  
  170. /**
  171. * Used to delete a link or a category
  172. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  173. */
  174. function deletelinkcategory($type)
  175. {
  176.     global $catlinkstatus;
  177.     global $_course;
  178.     global $_user;
  179.     $tbl_link Database :: get_course_table(TABLE_LINK);
  180.     $tbl_categories Database :: get_course_table(TABLE_LINK_CATEGORY);
  181.     $TABLE_ITEM_PROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY);
  182.  
  183.     if ($type == "link")
  184.     {
  185.         global $id;
  186.         // -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
  187.         // make a restore function possible for the platform administrator
  188.         //$sql="DELETE FROM $tbl_link WHERE id='".$_GET['id']."'";
  189.         api_item_property_update($_courseTOOL_LINK$id"delete"$_user['user_id']);
  190.         $catlinkstatus get_lang("LinkDeleted");
  191.         unset ($id);
  192.  
  193.         Display::display_confirmation_message(get_lang('LinkDeleted'));
  194.     }
  195.     if ($type == "category")
  196.     {
  197.         global $id;
  198.  
  199.         // first we delete the category itself and afterwards all the links of this category.
  200.         $sql "DELETE FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  201.         api_sql_query($sql__FILE____LINE__);
  202.         $sql "DELETE FROM ".$tbl_link." WHERE category_id='".$_GET['id']."'";
  203.         $catlinkstatus get_lang('CategoryDeleted');
  204.         unset ($id);
  205.         api_sql_query($sql__FILE____LINE__);
  206.  
  207.         Display::display_confirmation_message(get_lang('CategoryDeleted'));
  208.     }
  209. }
  210.  
  211. /**
  212. * Used to edit a link or a category
  213. @todo rewrite the whole links tool because it is becoming completely cluttered,
  214. *          code does not follow the coding conventions, does not use html_quickform, ...
  215. *          some features were patched in
  216. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  217. @todo replace the globals with the appropriate $_POST or $_GET values
  218. */
  219. function editlinkcategory($type)
  220. {
  221.     global $catlinkstatus;
  222.     global $id;
  223.     global $submitLink;
  224.     global $submitCategory;
  225.     global $_user;
  226.     global $_course;
  227.     global $nameTools;
  228.     global $urllink;
  229.     global $title;
  230.     global $description;
  231.     global $category;
  232.     global $selectcategory;
  233.     global $description;
  234.     global $category_title;
  235.     global $onhomepage;
  236.  
  237.     $tbl_link         Database :: get_course_table(TABLE_LINK);
  238.     $tbl_categories Database :: get_course_table(TABLE_LINK_CATEGORY);
  239.  
  240.     if ($type == "link")
  241.     {
  242.         // this is used to populate the link-form with the info found in the database
  243.         $sql "SELECT * FROM ".$tbl_link." WHERE id='".$_GET['id']."'";
  244.         $result api_sql_query($sql__FILE____LINE__);
  245.         if ($myrow mysql_fetch_array($result))
  246.         {
  247.             $urllink $myrow["url"];
  248.             $title $myrow["title"];
  249.             $description $myrow["description"];
  250.             $category $myrow["category_id"];
  251.             if ($myrow["on_homepage"<> 0)
  252.             {
  253.                 $onhomepage "checked";
  254.             }
  255.         }
  256.         // this is used to put the modified info of the link-form into the database
  257.         if ($_POST['submitLink'])
  258.         {
  259.             if ($_POST['onhomepage'== '')
  260.             {
  261.                 $onhomepage 0;
  262.             }
  263.             else
  264.             {
  265.                 $onhomepage $_POST['onhomepage'];
  266.             }
  267.  
  268.             // finding the old category_id
  269.             $sql "SELECT * FROM ".$tbl_link." WHERE id='".$_POST['id']."'";
  270.             $result api_sql_query($sql__FILE____LINE__);
  271.             $row mysql_fetch_array($result);
  272.             $category_id $row['category_id'];
  273.  
  274.             if ($category_id <> $_POST['selectcategory'])
  275.             {
  276.                 $sql "SELECT MAX(display_order) FROM ".$tbl_link." WHERE category_id='".$_POST['selectcategory']."'";
  277.                 $result api_sql_query($sql);
  278.                 list ($max_display_ordermysql_fetch_row($result);
  279.                 $max_display_order ++;
  280.             }
  281.             else
  282.             {
  283.                 $max_display_order $row['display_order'];
  284.             }
  285.  
  286.             $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']."'";
  287.             api_sql_query($sql__FILE____LINE__);
  288.  
  289.             // "WHAT'S NEW" notification: update table last_toolEdit
  290.             api_item_property_update($_courseTOOL_LINK$_POST['id']"LinkUpdated"$_user['user_id']);
  291.  
  292.             Display::display_confirmation_message(get_lang('LinkModded'));
  293.         }
  294.     }
  295.     if ($type == "category")
  296.     {
  297.         // this is used to populate the category-form with the info found in the database
  298.         if (!$submitCategory)
  299.         {
  300.             $sql "SELECT * FROM ".$tbl_categories." WHERE id='".$_GET['id']."'";
  301.             $result api_sql_query($sql__FILE____LINE__);
  302.             if ($myrow mysql_fetch_array($result))
  303.             {
  304.                 $category_title $myrow["category_title"];
  305.                 $description $myrow["description"];
  306.             }
  307.         }
  308.         // this is used to put the modified info of the category-form into the database
  309.         if ($submitCategory)
  310.         {
  311.             $sql "UPDATE ".$tbl_categories." set category_title='".$_POST['category_title']."', description='".$_POST['description']."' WHERE id='".$_POST['id']."'";
  312.             api_sql_query($sql__FILE____LINE__);
  313.             Display::display_confirmation_message(get_lang('CategoryModded'));
  314.         }
  315.  
  316.  
  317.     }
  318. }
  319. // END of function editlinkcat
  320.  
  321. /**
  322. * creates a correct $view for in the URL
  323. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  324. */
  325. function makedefaultviewcode($locatie)
  326. {
  327.     global $aantalcategories;
  328.     global $view;
  329.  
  330.     for ($j 0$j <= $aantalcategories -1$j ++)
  331.     {
  332.         $view[$j0;
  333.     }
  334.     $view[intval($locatie)"1";
  335. }
  336. // END of function makedefaultviewcode
  337.  
  338. /**
  339. * changes the visibility of a link
  340. @todo add the changing of the visibility of a course
  341. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  342. */
  343. function change_visibility($id$scope)
  344. {
  345.     global $_course;
  346.     global $_user;
  347.     $TABLE_ITEM_PROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY);
  348.  
  349.     if ($scope == "link")
  350.     {
  351.         $sqlselect "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_LINK."' and ref='".$id."'";
  352.         $result api_sql_query($sqlselect);
  353.         $row mysql_fetch_array($result);
  354.         api_item_property_update($_courseTOOL_LINK$id$_GET['action']$_user['user_id']);
  355.     }
  356.  
  357.     Display::display_confirmation_message(get_lang('VisibilityChanged'));
  358. }
  359.  
  360. /**
  361. * displays all the links of a given category.
  362. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  363. */
  364. function showlinksofcategory($catid)
  365. {
  366.     global $is_allowed$charset$urlview$up$down;
  367.     $tbl_link Database :: get_course_table(TABLE_LINK);
  368.  
  369.     $TABLE_ITEM_PROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY);
  370.  
  371.     $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";
  372.     $result api_sql_query($sqlLinks);
  373.     $numberoflinks mysql_num_rows($result);
  374.     
  375.     echo '<table class="data_table" width="100%">';
  376.     $i 1;
  377.     while ($myrow mysql_fetch_array($result))
  378.     {
  379.         if($i%2==0$css_class 'row_odd';
  380.         else $css_class 'row_even';
  381.         
  382.         $myrow[3text_filter($myrow[3]);
  383.         if ($myrow['visibility'== '1')
  384.         {
  385.             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]"";
  386.         }
  387.         else
  388.         {
  389.             if (api_is_allowed_to_edit())
  390.             {
  391.                 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]"";
  392.             }
  393.         }
  394.         
  395.         echo '<td style="text-align:center;">';
  396.         if (api_is_allowed_to_edit())
  397.         {
  398.             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>";            
  399.             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>";
  400.             // DISPLAY MOVE UP COMMAND only if it is not the top link
  401.             if ($i != 1)
  402.             {
  403.                 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";
  404.             }
  405.             else 
  406.             {
  407.                 echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="Up"/>';
  408.             }    
  409.             
  410.             // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  411.             if ($i $numberoflinks)
  412.             {
  413.                 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";
  414.             }
  415.             else
  416.             {
  417.                 echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="Up"/>';    
  418.             }
  419.             
  420.             if ($myrow['visibility'== "1")
  421.             {
  422.                 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>'
  423.             }
  424.             if ($myrow['visibility'== "0")
  425.             {
  426.                  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>';
  427.             }
  428.         
  429.         echo '</td>';
  430.         echo '</tr>';
  431.         $i ++;
  432.     }
  433.     echo '</table>';
  434. }
  435.  
  436. /**
  437. * displays the edit, delete and move icons
  438. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  439. */
  440. function showcategoryadmintools($categoryid)
  441. {
  442.     global $urlview;
  443.     global $aantalcategories;
  444.     global $catcounter;
  445.  
  446.     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>';
  447.     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>";
  448.  
  449.     // DISPLAY MOVE UP COMMAND only if it is not the top link    
  450.     if ($catcounter != 1)
  451.     {
  452.         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";
  453.     }
  454.     else
  455.     {
  456.         echo '<img src="'.api_get_path(WEB_IMG_PATH).'up_na.gif" border=0 alt="Up"/>';    
  457.     }    
  458.     // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
  459.     if ($catcounter $aantalcategories)
  460.     {
  461.         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";
  462.     }
  463.     else
  464.     {
  465.         echo '<img src="'.api_get_path(WEB_IMG_PATH).'down_na.gif" border=0 alt="Up"/>';
  466.     }        
  467.     $catcounter ++;
  468. }
  469.  
  470. /**
  471. * move a link or a linkcategory up or down
  472. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  473. */
  474. function movecatlink($catlinkid)
  475. {
  476.     global $catmove;
  477.     global $up;
  478.     global $down;
  479.     $tbl_link Database :: get_course_table(TABLE_LINK);
  480.     $tbl_categories Database :: get_course_table(TABLE_LINK_CATEGORY);
  481.  
  482.     if (!empty($_GET['down']))
  483.     {
  484.         $thiscatlinkId $_GET['down'];
  485.         $sortDirection "DESC";
  486.     }
  487.     if (!empty($_GET['up']))
  488.     {
  489.         $thiscatlinkId $_GET['up'];
  490.         $sortDirection "ASC";
  491.     }
  492.  
  493.     // 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
  494.     if ($catmove == "true")
  495.     {
  496.         $movetable $tbl_categories;
  497.         $catid $catlinkid;
  498.     }
  499.     else
  500.     {
  501.         $movetable $tbl_link;
  502.         //getting the category of the link
  503.         if(!empty($thiscatlinkId))
  504.         {
  505.             $sql "SELECT category_id from ".$movetable." WHERE id='$thiscatlinkId'";
  506.             $result api_sql_query($sql__FILE____LINE__);
  507.             $catid mysql_fetch_array($result);
  508.         }
  509.     }
  510.  
  511.     // this code is copied and modified from announcements.php
  512.     if (!empty($sortDirection))
  513.     {
  514.         if (!in_array(trim(strtoupper($sortDirection))array ('ASC''DESC')))
  515.             die("Bad sort direction used.")//sanity check of sortDirection var
  516.         if ($catmove == "true")
  517.         {
  518.             $sqlcatlinks "SELECT id, display_order FROM ".$movetable." ORDER BY display_order $sortDirection";
  519.         }
  520.         else
  521.         {
  522.             $sqlcatlinks "SELECT id, display_order FROM ".$movetable." WHERE category_id='".$catid[0]."' ORDER BY display_order $sortDirection";
  523.         }
  524.         $linkresult api_sql_query($sqlcatlinks);
  525.         while ($sortrow mysql_fetch_array($linkresult))
  526.         {
  527.             // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  528.             // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  529.             // and we know the next iteration of the while loop is the next one. These should be swapped.
  530.             if (isset ($thislinkFound&& $thislinkFound == true)
  531.             {
  532.                 $nextlinkId $sortrow["id"];
  533.                 $nextlinkOrdre $sortrow["display_order"];
  534.  
  535.                 api_sql_query("UPDATE ".$movetable."
  536.                                          SET display_order = '$nextlinkOrdre'
  537.                                          WHERE id =  '$thiscatlinkId'");
  538.  
  539.                 api_sql_query("UPDATE ".$movetable."
  540.                                          SET display_order = '$thislinkOrdre'
  541.                                          WHERE id =  '$nextlinkId'");
  542.  
  543.                 break;
  544.             }
  545.  
  546.             if ($sortrow["id"== $thiscatlinkId)
  547.             {
  548.                 $thislinkOrdre $sortrow["display_order"];
  549.                 $thislinkFound true;
  550.             }
  551.         }
  552.     }
  553.  
  554. }
  555.  
  556. /**
  557. * CSV file import functions
  558. @author Rene Haentjens , Ghent University
  559. */
  560. function get_cat($catname// get category id (existing or make new)
  561. {
  562.     $tbl_categories Database :: get_course_table(TABLE_LINK_CATEGORY);
  563.  
  564.     $result api_sql_query("SELECT `id` FROM ".$tbl_categories." WHERE `category_title`='".addslashes($catname)."'"__FILE____LINE__);
  565.  
  566.     if (mysql_num_rows($result>= && ($row mysql_fetch_array($result)))
  567.         return $row['id']// several categories with same name: take first
  568.  
  569.     $result api_sql_query("SELECT MAX(display_order) FROM ".$tbl_categories.""__FILE____LINE__);
  570.     list ($max_ordermysql_fetch_row($result);
  571.  
  572.     api_sql_query("INSERT INTO ".$tbl_categories." (category_title, description, display_order) VALUES ('".addslashes($catname)."','','"($max_order +1)."')"__FILE____LINE__);
  573.  
  574.     return mysql_insert_id();
  575. }
  576. /**
  577. * CSV file import functions
  578. @author Rene Haentjens , Ghent University
  579. */
  580. function put_link($url$cat$title$description$on_homepage$hidden)
  581. {
  582.     $tbl_link Database :: get_course_table(TABLE_LINK);
  583.  
  584.     $urleq "url='".addslashes($url)."'";
  585.     $cateq "category_id=".$cat;
  586.  
  587.     $result api_sql_query("SELECT id FROM $tbl_link WHERE ".$urleq.' AND '.$cateq__FILE____LINE__);
  588.  
  589.     if (mysql_num_rows($result>= && ($row mysql_fetch_array($result)))
  590.     {
  591.         api_sql_query("UPDATE $tbl_link set title='".addslashes($title)."', description='".addslashes($description)."' WHERE id='".addslashes($id $row['id'])."'"__FILE____LINE__);
  592.  
  593.         $lang_link get_lang('update_link');
  594.         $ipu "LinkUpdated";
  595.         $rv 1// 1= upd
  596.     }
  597.     else // add new link
  598.         {
  599.         $result api_sql_query("SELECT MAX(display_order) FROM  $tbl_link WHERE category_id='".addslashes($cat)."'"__FILE____LINE__);
  600.         list ($max_ordermysql_fetch_row($result);
  601.  
  602.         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__);
  603.  
  604.         $id mysql_insert_id();
  605.         $lang_link get_lang('new_link');
  606.         $ipu "LinkAdded";
  607.         $rv 2// 2= new
  608.     }
  609.  
  610.     global $_course$nameTools$_user;
  611.     api_item_property_update($_courseTOOL_LINK$id$ipu$_user['user_id']);
  612.  
  613.     if ($hidden && $ipu == "LinkAdded")
  614.         api_item_property_update($_courseTOOL_LINK$id"invisible"$_user['user_id']);
  615.  
  616.     return $rv;
  617. }
  618. /**
  619. * CSV file import functions
  620. @author Rene Haentjens , Ghent University
  621. */
  622. function import_link($linkdata// url, category_id, title, description, ...
  623. {
  624.     // field names used in the uploaded file
  625.     $known_fields array ('url''category''title''description''on_homepage''hidden');
  626.     $hide_fields array ('kw''kwd''kwds''keyword''keywords');
  627.  
  628.     // all other fields are added to description, as "name:value"
  629.  
  630.     // only one hide_field is assumed to be present, <> is removed from value
  631.  
  632.     if (!($url trim($linkdata['url'])) || !($title trim($linkdata['title'])))
  633.         return 0// 0= fail
  634.  
  635.     $cat ($catname trim($linkdata['category'])) get_cat($catname0;
  636.  
  637.     $regs array()// will be passed to ereg()
  638.     foreach ($linkdata as $key => $value)
  639.         if (!in_array($key$known_fields))
  640.             if (in_array($key$hide_fields&& ereg('^<?([^>]*)>?$'$value$regs)) // possibly in <...>
  641.                 if (($kwlist trim($regs[1])) != '')
  642.                     $kw '<i kw="'.htmlspecialchars($kwlist).'">';
  643.                 else
  644.                     $kw '';
  645.     // i.e. assume only one of the $hide_fields will be present
  646.     // and if found, hide the value as expando property of an <i> tag
  647.     elseif (trim($value)) $d .= ', '.$key.':'.$value;
  648.     if ($d)
  649.         $d substr($d2).' - ';
  650.  
  651.     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');
  652.     // i.e. allow some BBcode tags, e.g. [b]...[/b]
  653. }
  654. /**
  655. * CSV file import functions
  656. @author Rene Haentjens , Ghent University
  657. */
  658. function import_csvfile()
  659. {
  660.     global $catlinkstatus// feedback message to user
  661.  
  662.     if (is_uploaded_file($filespec $_FILES['import_file']['tmp_name']&& filesize($filespec&& ($myFile fopen($filespec'r')))
  663.     {
  664.         // read first line of file (column names) and find ',' or ';'
  665.         $listsep strpos($colnames trim(fgets($myFile))','!== FALSE ',' (strpos($colnames';'!== FALSE ';' '');
  666.  
  667.         if ($listsep)
  668.         {
  669.             $columns array_map('strtolower'explode($listsep$colnames));
  670.  
  671.             if (in_array('url'$columns&& in_array('title'$columns))
  672.             {
  673.                 $stats array (000)// fails, updates, inserts
  674.  
  675.                 while (($data fgetcsv($myFile32768$listsep)))
  676.                 {
  677.                     foreach ($data as $i => $text)
  678.                         $linkdata[$columns[$i]] $text;
  679.                     //  $linkdata['url', 'title', ...]
  680.  
  681.                     $stats[import_link($linkdata)]++;
  682.                     unset ($linkdata);
  683.                 }
  684.  
  685.                 $catlinkstatus '';
  686.  
  687.                 if ($stats[0])
  688.                     $catlinkstatus .= $stats[0].' '.get_lang('CsvLinesFailed');
  689.                 if ($stats[1])
  690.                     $catlinkstatus .= $stats[1].' '.get_lang('CsvLinesOld');
  691.                 if ($stats[2])
  692.                     $catlinkstatus .= $stats[2].' '.get_lang('CsvLinesNew');
  693.             }
  694.             else
  695.                 $catlinkstatus get_lang('CsvFileNoURL')($colnames get_lang('CsvFileLine1').htmlspecialchars(substr($colnames0200)).'...' '');
  696.         }
  697.         else
  698.             $catlinkstatus get_lang('CsvFileNoSeps')($colnames get_lang('CsvFileLine1').htmlspecialchars(substr($colnames0200)).'...' '');
  699.         fclose($myFile);
  700.     }
  701.     else
  702.         $catlinkstatus get_lang('CsvFileNotFound');
  703. }
  704. ?>

Documentation generated on Thu, 12 Jun 2008 14:00:12 -0500 by phpDocumentor 1.4.1