Source for file linkfunctions.php
Documentation is available at linkfunctions.php
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2004-2008 Dokeos S.A.
Copyright (c) 2003 Ghent University (UGent)
Copyright (c) 2001 Universite catholique de Louvain (UCL)
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
==============================================================================
==============================================================================
* Function library for the links tool.
* This is a complete remake of the original link tool.
* - Organize links into categories;
* - favorites/bookmarks interface;
* - move links up/down within a category;
* - move categories up/down;
* - expand/collapse all categories;
* - add link to 'root' category => category-less link is always visible.
* @author Patrick Cool, complete remake (December 2003 - January 2004)
* @author Rene Haentjens, CSV file import (October 2004)
==============================================================================
==============================================================================
==============================================================================
* Used to add a link or a category
* @param string $type, "link" or "category"
* @todo replace strings by constants
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
$title = $_POST['title'];
$urllink = $_POST['urllink'];
$description = $_POST['description'];
$selectcategory = $_POST['selectcategory'];
if ($_POST['onhomepage'] == '')
$onhomepage = $_POST['onhomepage'];
$urllink = trim($urllink);
$description = trim($description);
// if title is empty, an error occurs
if (empty ($urllink) OR $urllink == 'http://')
// if the title is empty, we use the url as the title
// we check weither the $url starts with http://, if not we add this
$urllink = "http://". $urllink;
// looking for the largest order number for this category
$result = api_sql_query("SELECT MAX(display_order) FROM ". $tbl_link. " WHERE category_id='". $_POST['selectcategory']. "'");
$sql = "INSERT INTO ". $tbl_link. " (url, title, description, category_id,display_order, on_homepage) VALUES ('$urllink','$title','$description','$selectcategory','$order', '$onhomepage')";
unset ($urllink, $title, $description, $selectcategory);
elseif ($type == "category")
$category_title = trim($_POST['category_title']);
$description = trim($_POST['description']);
if (empty ($category_title))
// looking for the largest order number for this category
$result = api_sql_query("SELECT MAX(display_order) FROM ". $tbl_categories. "");
$sql = "INSERT INTO ". $tbl_categories. " (category_title, description, display_order) VALUES ('$category_title','$description', '$order')";
$catlinkstatus = get_lang('CategoryAdded');
unset ($category_title, $description);
// "WHAT'S NEW" notification : update last tool Edit
// End of the function addlinkcategory
* Used to delete a link or a category
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
// -> items are no longer fysically deleted, but the visibility is set to 2 (in item_property). This will
// make a restore function possible for the platform administrator
//$sql="DELETE FROM $tbl_link WHERE id='".$_GET['id']."'";
$catlinkstatus = get_lang("LinkDeleted");
// first we delete the category itself and afterwards all the links of this category.
$sql = "DELETE FROM ". $tbl_categories. " WHERE id='". $_GET['id']. "'";
$sql = "DELETE FROM ". $tbl_link. " WHERE category_id='". $_GET['id']. "'";
$catlinkstatus = get_lang('CategoryDeleted');
* Used to edit a link or a category
* @todo rewrite the whole links tool because it is becoming completely cluttered,
* code does not follow the coding conventions, does not use html_quickform, ...
* some features were patched in
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @todo replace the globals with the appropriate $_POST or $_GET values
// this is used to populate the link-form with the info found in the database
$sql = "SELECT * FROM ". $tbl_link. " WHERE id='". $_GET['id']. "'";
$urllink = $myrow["url"];
$title = $myrow["title"];
$description = $myrow["description"];
$category = $myrow["category_id"];
if ($myrow["on_homepage"] <> 0)
// this is used to put the modified info of the link-form into the database
if ($_POST['submitLink'])
if ($_POST['onhomepage'] == '')
$onhomepage = $_POST['onhomepage'];
// finding the old category_id
$sql = "SELECT * FROM ". $tbl_link. " WHERE id='". $_POST['id']. "'";
$category_id = $row['category_id'];
if ($category_id <> $_POST['selectcategory'])
$sql = "SELECT MAX(display_order) FROM ". $tbl_link. " WHERE category_id='". $_POST['selectcategory']. "'";
$max_display_order = $row['display_order'];
$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']. "'";
// "WHAT'S NEW" notification: update table last_toolEdit
// this is used to populate the category-form with the info found in the database
$sql = "SELECT * FROM ". $tbl_categories. " WHERE id='". $_GET['id']. "'";
$category_title = $myrow["category_title"];
$description = $myrow["description"];
// this is used to put the modified info of the category-form into the database
$sql = "UPDATE ". $tbl_categories. " set category_title='". $_POST['category_title']. "', description='". $_POST['description']. "' WHERE id='". $_POST['id']. "'";
// END of function editlinkcat
* creates a correct $view for in the URL
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
global $aantalcategories;
for ($j = 0; $j <= $aantalcategories - 1; $j ++ )
$view[intval($locatie)] = "1";
// END of function makedefaultviewcode
* changes the visibility of a link
* @todo add the changing of the visibility of a course
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
$sqlselect = "SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='". TOOL_LINK. "' and ref='". $id. "'";
* displays all the links of a given category.
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
global $is_allowed, $charset, $urlview, $up, $down;
$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";
echo '<table class="data_table" width="100%">';
if($i% 2== 0) $css_class = 'row_odd';
else $css_class = 'row_even';
if ($myrow['visibility'] == '1')
echo "<tr class='". $css_class. "'>", "<td align=\"center\" valign=\"middle\" width=\"15\">", "<a href=\"link_goto.php?". api_get_cidreq(). "&link_id=", $myrow[0], "&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], "&link_url=", urlencode($myrow[1]), "\" target=\"_blank\">", htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br/>", $myrow[3], "";
echo "<tr class='". $css_class. "'>", "<td align=\"center\" valign=\"middle\" width=\"15\">", "<a href=\"link_goto.php?". api_get_cidreq(). "&link_id=", $myrow[0], "&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], "&link_url=", urlencode($myrow[1]), "\" target=\"_blank\" class=\"invisible\">", htmlentities($myrow[2],ENT_QUOTES,$charset), "</a>\n", "<br />", $myrow[3], "";
echo '<td style="text-align:center;">';
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&action=editlink&category=". (!empty($category)? $category: ''). "&id=$myrow[0]&urlview=$urlview\" title=\"". get_lang('Modify'). "\" >", "<img src=\"../img/edit.gif\" border=\"0\" alt=\"", get_lang('Modify'), "\" />", "</a>";
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&action=deletelink&id=", $myrow[0], "&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>";
// DISPLAY MOVE UP COMMAND only if it is not the top link
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&urlview=". $urlview. "&up=", $myrow["id"], "\" title=\"". get_lang('Up'). "\" >", "<img src=../img/up.gif border=0 alt=\"Up\"/>", "</a>\n";
// DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&urlview=". $urlview. "&down=". $myrow["id"]. "\" title=\"". get_lang('Down'). "\" >", "<img src=\"../img/down.gif\" border=\"0\" alt=\"Down\"/>", "</a>\n";
if ($myrow['visibility'] == "1")
echo '<a href="link.php?'. api_get_cidreq(). '&action=invisible&id='. $myrow['id']. '&scope=link&urlview='. $urlview. '" title="'. get_lang('langVisible'). '"><img src="../img/visible.gif" border="0" /></a>';
if ($myrow['visibility'] == "0")
echo '<a href="link.php?'. api_get_cidreq(). '&action=visible&id='. $myrow['id']. '&scope=link&urlview='. $urlview. '" title="'. get_lang('langVisible'). '"><img src="../img/invisible.gif" border="0" /></a>';
* displays the edit, delete and move icons
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
global $aantalcategories;
echo '<a href="'. api_get_self(). '?'. api_get_cidreq(). '&action=editcategory&id='. $categoryid. '&urlview=$amp;urlview\" title="'. get_lang('Modify'). '" ><img src="../img/edit.gif" border="0" alt="'. get_lang('Modify'). ' "/></a>';
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&action=deletecategory&id=", $categoryid, "&urlview=$urlview\" onclick=\"javascript:if(!confirm('". get_lang('CategoryDelconfirm'). "')) return false;\">", "<img src=\"../img/delete.gif\" border=\"0\" alt=\"", get_lang('Delete'), "\"/>", "</a>";
// DISPLAY MOVE UP COMMAND only if it is not the top link
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&catmove=true&up=", $categoryid, "&urlview=$urlview\" title=\"". get_lang('Up'). "\" >", "<img src=../img/up.gif border=0 alt=\"Up\"/>", "</a>\n";
// DISPLAY MOVE DOWN COMMAND only if it is not the bottom link
if ($catcounter < $aantalcategories)
echo "<a href=\"". api_get_self(). "?". api_get_cidreq(). "&catmove=true&down=". $categoryid. "&urlview=$urlview\">", "<img src=\"../img/down.gif\" border=\"0\" alt=\"Down\"/>", "</a>\n";
* move a link or a linkcategory up or down
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
if (!empty($_GET['down']))
$thiscatlinkId = $_GET['down'];
$thiscatlinkId = $_GET['up'];
// 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
$movetable = $tbl_categories;
//getting the category of the link
if(!empty($thiscatlinkId))
$sql = "SELECT category_id from ". $movetable. " WHERE id='$thiscatlinkId'";
// this code is copied and modified from announcements.php
if (!empty($sortDirection))
die("Bad sort direction used."); //sanity check of sortDirection var
$sqlcatlinks = "SELECT id, display_order FROM ". $movetable. " ORDER BY display_order $sortDirection";
$sqlcatlinks = "SELECT id, display_order FROM ". $movetable. " WHERE category_id='". $catid[0]. "' ORDER BY display_order $sortDirection";
// STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
// This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
// and we know the next iteration of the while loop is the next one. These should be swapped.
if (isset ($thislinkFound) && $thislinkFound == true)
$nextlinkId = $sortrow["id"];
$nextlinkOrdre = $sortrow["display_order"];
SET display_order = '$nextlinkOrdre'
WHERE id = '$thiscatlinkId'");
SET display_order = '$thislinkOrdre'
WHERE id = '$nextlinkId'");
if ($sortrow["id"] == $thiscatlinkId)
$thislinkOrdre = $sortrow["display_order"];
* CSV file import functions
* @author Rene Haentjens , Ghent University
function get_cat($catname) // get category id (existing or make new)
$result = api_sql_query("SELECT `id` FROM ". $tbl_categories. " WHERE `category_title`='". addslashes($catname). "'", __FILE__ , __LINE__ );
return $row['id']; // several categories with same name: take first
$result = api_sql_query("SELECT MAX(display_order) FROM ". $tbl_categories. "", __FILE__ , __LINE__ );
api_sql_query("INSERT INTO ". $tbl_categories. " (category_title, description, display_order) VALUES ('". addslashes($catname). "','','". ($max_order + 1). "')", __FILE__ , __LINE__ );
* CSV file import functions
* @author Rene Haentjens , Ghent University
function put_link($url, $cat, $title, $description, $on_homepage, $hidden)
$cateq = "category_id=". $cat;
$result = api_sql_query("SELECT id FROM $tbl_link WHERE ". $urleq. ' AND '. $cateq, __FILE__ , __LINE__ );
$result = api_sql_query("SELECT MAX(display_order) FROM $tbl_link WHERE category_id='". addslashes($cat). "'", __FILE__ , __LINE__ );
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__ );
global $_course, $nameTools, $_user;
if ($hidden && $ipu == "LinkAdded")
* CSV file import functions
* @author Rene Haentjens , Ghent University
function import_link($linkdata) // url, category_id, title, description, ...
// field names used in the uploaded file
$known_fields = array ('url', 'category', 'title', 'description', 'on_homepage', 'hidden');
$hide_fields = array ('kw', 'kwd', 'kwds', 'keyword', 'keywords');
// all other fields are added to description, as "name:value"
// only one hide_field is assumed to be present, <> is removed from value
if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title'])))
$cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
$regs = array(); // will be passed to ereg()
foreach ($linkdata as $key => $value)
if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
if (($kwlist = trim($regs[1])) != '')
// i.e. assume only one of the $hide_fields will be present
// and if found, hide the value as expando property of an <i> tag
elseif (trim($value)) $d .= ', '. $key. ':'. $value;
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');
// i.e. allow some BBcode tags, e.g. [b]...[/b]
* CSV file import functions
* @author Rene Haentjens , Ghent University
global $catlinkstatus; // feedback message to user
// read first line of file (column names) and find ',' or ';'
$listsep = strpos($colnames = trim(fgets($myFile)), ',') !== FALSE ? ',' : (strpos($colnames, ';') !== FALSE ? ';' : '');
$stats = array (0, 0, 0); // fails, updates, inserts
while (($data = fgetcsv($myFile, 32768, $listsep)))
foreach ($data as $i => $text)
$linkdata[$columns[$i]] = $text;
// $linkdata['url', 'title', ...]
$catlinkstatus .= $stats[0]. ' '. get_lang('CsvLinesFailed');
$catlinkstatus .= $stats[1]. ' '. get_lang('CsvLinesOld');
$catlinkstatus .= $stats[2]. ' '. get_lang('CsvLinesNew');
$catlinkstatus = get_lang('CsvFileNotFound');
|