[svn r17117] Implemented options to order glossary terms (see FS#3293)

skala
Cristian Fasanando 16 years ago
parent 7599fe9e48
commit a1bf612ed7
  1. 194
      main/glossary/glossaryfunction.inc.php
  2. 151
      main/glossary/index.php

@ -5,28 +5,44 @@
* @author Christian Fasanando
* This library enables maintenance of the glossary tool
*/
/**
* This function retrieves glossary details by course
* This function retrieves glossary details by course
* and order by a type (1 = By Start Date, 2 = By End Date, 3 = By Term Name)
* @return array Array of type ([glossary_id=>w,name=>x,description=>y],[])
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @version november 2008, dokeos 1.8.6
*/
function get_glossary_details() {
function get_glossary_details($type) {
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
$sql = "SELECT glossary_id, name, description
FROM $t_glossary";
$t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
$safe_type = (int)$type;
if (!empty($safe_type) && $safe_type==1) {
$sql = "SELECT g.glossary_id, g.name, g.description, g.display_order
FROM $t_glossary g,$t_item_propery ip WHERE g.glossary_id=ip.ref AND tool = '".TOOL_GLOSSARY."' ORDER BY ip.insert_date DESC ";
} elseif (!empty($safe_type) && $safe_type==2) {
$sql = "SELECT g.glossary_id, g.name, g.description, g.display_order
FROM $t_glossary g,$t_item_propery ip WHERE g.glossary_id=ip.ref AND tool = '".TOOL_GLOSSARY."' ORDER BY ip.lastedit_date DESC ";
} elseif (!empty($safe_type) && $safe_type==3) {
$sql = "SELECT g.glossary_id, g.name, g.description, g.display_order
FROM $t_glossary g,$t_item_propery ip WHERE g.glossary_id=ip.ref AND tool = '".TOOL_GLOSSARY."' ORDER BY g.name ASC ";
} else {
$sql = "SELECT g.glossary_id, g.name, g.description, g.display_order
FROM $t_glossary g,$t_item_propery ip WHERE g.glossary_id=ip.ref AND tool = '".TOOL_GLOSSARY."' ORDER BY g.display_order,g.name ASC ";
}
$result = api_sql_query($sql, __FILE__, __LINE__);
return $result;
}
}
/**
* This function add glosary details by course
* @param name type String
* @param description type String
* @return boolean
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @version november 2008, dokeos 1.8.6
*/
function add_glossary_details($name,$description) {
@ -36,11 +52,34 @@ function add_glossary_details($name,$description) {
}
$safe_name = Database::escape_string($name);
$safe_description = Database::escape_string($description);
$sql = "INSERT INTO $t_glossary (name, description) VALUES('$safe_name', '$safe_description')";
$get_max = "SELECT MAX(display_order) FROM $t_glossary";
$res_max = api_sql_query($get_max, __FILE__, __LINE__);
$dsp=0;
if (Database::num_rows($res_max)<1) {
$dsp = 1;
} else {
$row = Database::fetch_array($res_max);
$dsp = $row[0]+1;
}
$safe_dsp = (int)$dsp;
// check if term name exists
$sql = "SELECT * FROM $t_glossary WHERE name = '$safe_name'";
$result = @api_sql_query($sql,__FILE__,__LINE__);
$count = Database::num_rows($result);
if ($count > 0) {
return false;
}
$sql = "INSERT INTO $t_glossary (name, description,display_order) VALUES('$safe_name', '$safe_description',$safe_dsp)";
$result = api_sql_query($sql, __FILE__, __LINE__);
$id = Database::get_last_insert_id();
if ($id>0) {
//insert into item_property
api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,$id,'GlossaryAdded',api_get_user_id());
}
return $result;
}
}
/**
* This function edit glosary details by course
@ -48,7 +87,7 @@ function add_glossary_details($name,$description) {
* @param name type String
* @param description type String
* @return boolean
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @version november 2008, dokeos 1.8.6
*/
function edit_glossary_details($glossary_id,$name,$description) {
@ -58,10 +97,13 @@ function edit_glossary_details($glossary_id,$name,$description) {
$safe_name = Database::escape_string($name);
$safe_description = Database::escape_string($description);
$safe_glossary_id = Database::escape_string($glossary_id);
$safe_glossary_id = (int)$glossary_id;
$sql = "UPDATE $t_glossary SET name='$safe_name', description='$safe_description' WHERE glossary_id=$safe_glossary_id";
$result = api_sql_query($sql, __FILE__, __LINE__);
//update glossary into item_property
api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,$safe_glossary_id,'GlossaryModified',api_get_user_id());
return $result;
}
@ -69,16 +111,140 @@ function edit_glossary_details($glossary_id,$name,$description) {
* This function delete glosary details by course
* @param glossary_id int
* @return boolean
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @author Christian Fasanando <christian.fasanando@dokeos.com>,
* @version november 2008, dokeos 1.8.6
*/
function delete_glossary_details($glossary_id) {
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
$safe_glossary_id = Database::escape_string($glossary_id);
if (empty($glossary_id)) { return false; }
$sql = "DELETE FROM $t_glossary WHERE glossary_id=$safe_glossary_id";
$result = api_sql_query($sql, __FILE__, __LINE__);
// update display_order
$sql = "SELECT * FROM $t_glossary";
$result = api_sql_query($sql, __FILE__, __LINE__);
$dsp=1;
while ($row=Database::fetch_array($result)) {
$id = $row['glossary_id'];
update_display_order($dsp,$id);
$dsp++;
}
//update glossary into item_property
api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,$safe_glossary_id,'GlossaryDeleted',api_get_user_id());
return $result;
}
/**
* This function update glossary display order
* @param $n_order int
* @param $glossary_id int
*
*/
function update_display_order($n_order,$glossary_id) {
$t_glossary = Database::get_course_table(TABLE_GLOSSARY);
$safe_n_order = (int)$n_order;
$safe_glossary_id = (int)$glossary_id;
if (empty($n_order) || empty($glossary_id)) {
return false;
}
$sql = "UPDATE $t_glossary SET display_order = $safe_n_order WHERE glossary_id = $safe_glossary_id";
$result = @api_sql_query($sql,__FILE__,__LINE__);
return $result;
}
/**
* Move a term up (display_order)
* @param integer Glossary ID
*/
function move_up($glossary_id)
{
$tbl_glossary = Database::get_course_table(TABLE_GLOSSARY);
$sql = "SELECT * FROM $tbl_glossary ORDER BY display_order";
$res = api_sql_query($sql, __FILE__, __LINE__);
if ($res === false) {
return false;
}
$gs = array();
$g_order = array();
$num = Database::num_rows($res);
//first check the order is correct, globally (might be wrong because
//of versions < 1.8.4)
if ($num > 0) {
$i = 1;
while ($row = Database::fetch_array($res)) {
if ($row['display_order'] != $i) { //if we find a gap in the order, we need to fix it
$need_fix = true;
$sql_u = "UPDATE $tbl_glossary SET display_order = $i WHERE glossary_id = ".$row['glossary_id'];
$res_u = api_sql_query($sql_u, __FILE__, __LINE__);
}
$row['display_order'] = $i;
$gs[$row['glossary_id']] = $row;
$g_order[$i] = $row['glossary_id'];
$i++;
}
}
if($num>1) {
//if there's only one element, no need to sort
$order = $gs[$glossary_id]['display_order'];
if ($order>1) {
//if it's the first element, no need to move up
$sql_u1 = "UPDATE $tbl_glossary SET display_order = $order WHERE glossary_id = ".$g_order[$order-1];
$res_u1 = api_sql_query($sql_u1, __FILE__, __LINE__);
$sql_u2 = "UPDATE $tbl_glossary SET display_order = ".($order-1)." WHERE glossary_id = ".$glossary_id;
$res_u2 = api_sql_query($sql_u2, __FILE__, __LINE__);
}
}
}
/**
* Move a term down (display_order)
* @param integer Glossary ID
*/
function move_down($glossary_id)
{
$tbl_glossary = Database::get_course_table(TABLE_GLOSSARY);
$sql = "SELECT * FROM $tbl_glossary ORDER BY display_order";
$res = api_sql_query($sql, __FILE__, __LINE__);
if ($res === false) {
return false;
}
$gs = array();
$g_order = array();
$num = Database::num_rows($res);
$max = 0;
//first check the order is correct, globally (might be wrong because
//of versions < 1.8.4)
if ($num>0) {
$i = 1;
while ($row = Database::fetch_array($res)) {
$max = $i;
if ($row['display_order'] != $i) {
//if we find a gap in the order, we need to fix it
$need_fix = true;
$sql_u = "UPDATE $tbl_glossary SET display_order = $i WHERE glossary_id = ".$row['glossary_id'];
$res_u = api_sql_query($sql_u, __FILE__, __LINE__);
}
$row['display_order'] = $i;
$gs[$row['glossary_id']] = $row;
$g_order[$i] = $row['glossary_id'];
$i++;
}
}
if ($num>1) {
//if there's only one element, no need to sort
$order = $gs[$glossary_id]['display_order'];
if($order<$max) {
//if it's the first element, no need to move up
$sql_u1 = "UPDATE $tbl_glossary SET display_order = $order WHERE glossary_id = ".$g_order[$order+1];
$res_u1 = api_sql_query($sql_u1, __FILE__, __LINE__);
$sql_u2 = "UPDATE $tbl_glossary SET display_order = ".($order+1)." WHERE glossary_id = ".$glossary_id;
$res_u2 = api_sql_query($sql_u2, __FILE__, __LINE__);
}
}
}

@ -9,7 +9,7 @@ $language_file = array('glossary');
require_once('../inc/global.inc.php');
api_protect_course_script(true);
require_once('glossaryfunction.inc.php');
$status = $_user['status'];
$is_allowed_to_edit = api_is_allowed_to_edit();
/*
* Header
*/
@ -22,7 +22,7 @@ function confirmation (name)
{return true;}
else
{return false;}
}
}
function text_focus(){
document.form_glossary.n_glossary.focus();
document.form_glossary.n_glossary.select();
@ -31,36 +31,37 @@ function text_focus(){
$tool = TOOL_GLOSSARY;
Display::display_header(get_lang(ucfirst($tool)));
//---------------------------------------------------------
if ($status == 1) {
if ($is_allowed_to_edit) {
echo '<a href="index.php?'.api_get_cidreq().'&action=addglossary"><img src="../img/filenew.gif" title ="'.get_lang('TermAddNew').'"> '.get_lang('TermAddNew').'</a>';
/*======================================
Form Glossary
Form Glossary
======================================*/
echo '<p><div>';
if ($_GET['action'] == 'addglossary') {
echo '<form name="frm_glossary" action="index.php?'.api_get_cidreq().'">';
echo '<div class="glossary-add-form">';
if ($_GET['action'] == 'addglossary') {
echo '<form name="frm_glossary" action="index.php?'.api_get_cidreq().'" method="post">';
echo '<div class="term_glossary">'.get_lang('TermName').'<br /><input type="text" name="name_glossary"></div>';
echo '<div class="definition_glossary">'.get_lang('TermDefinition').'<br /><textarea cols="60" rows="5" maxlength="255" name="description_glossary"></textarea></div>';
echo '<div class="action_glossary"><input type="submit" value="'.get_lang('TermAddButton').'"></div>';
echo '</form>';
echo '</form>';
}
echo '</div></p><hr />';
echo '</div>';
}
/*======================================
Add Glossary Details
======================================*/
$name_glossary = Security::remove_XSS($_GET['name_glossary']);
$description_glossary = Security::remove_XSS($_GET['description_glossary']);
$add_glossary = add_glossary_details($name_glossary,$description_glossary);
/*======================================
Edit Glossary Details
======================================*/
======================================*/
$g_id = Security::remove_XSS($_GET['g_id']);
$n_glossary = Security::remove_XSS($_GET['n_glossary']);
@ -70,11 +71,11 @@ $edit_glossary = edit_glossary_details($g_id,$n_glossary,$d_glossary);
/*======================================
Delete Glossary Details
======================================*/
if ($_GET['action'] == 'delete_glossary') {
$g_id = Security::remove_XSS($_GET['glossary_id']);
$g_id = Security::remove_XSS($_GET['glossary_id']);
$delete_glossary = delete_glossary_details($g_id);
Display::display_confirmation_message(get_lang('TermDeleted'));
}
@ -82,35 +83,115 @@ if ($_GET['action'] == 'delete_glossary') {
Display Glossary Details
======================================*/
$glossary_list=get_glossary_details(); //returns a results resource
Database::num_rows($glossary_list);
// order by up/down
$action = (!empty($_REQUEST['action'])?$_REQUEST['action']:'');
switch($action) {
case 'move_lp_up':
move_up($_REQUEST['id']);
break;
case 'move_lp_down':
move_down($_REQUEST['id']);
break;
}
// order by type (one = By Start Date, two = By End Date, three = By Term Name)
$type = (int)$_GET['type'];
$glossary_list=get_glossary_details($type); //returns a results resource
$max = Database::num_rows($glossary_list);
$current = 0;
if ($max > 1) {
if ($type == 1) {
echo '<div class="glossary-orderby-link">'.get_lang('OrderBy').'&nbsp;:&nbsp;'.get_lang('CreationDate').'&nbsp;|
<a href="index.php?'.api_get_cidreq().'&type=2">'.get_lang('UpdateDate').'</a>&nbsp;|&nbsp<a href="index.php?'.api_get_cidreq().'&type=3">'.get_lang('TermName').'</a>
&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=4">'.get_lang('PreSelectedOrder').'</a>
</div>';
} elseif ($type == 2) {
echo '<div class="glossary-orderby-link">'.get_lang('OrderBy').'&nbsp;:&nbsp;<a href="index.php?'.api_get_cidreq().'&type=1">'.get_lang('CreationDate').'</a>&nbsp;|&nbsp;
'.get_lang('UpdateDate').'&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=3">'.get_lang('TermName').'</a>
&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=4">'.get_lang('PreSelectedOrder').'</a>
</div>';
} elseif ($type == 3) {
echo '<div class="glossary-orderby-link">'.get_lang('OrderBy').'&nbsp;:&nbsp;<a href="index.php?'.api_get_cidreq().'&type=1">'.get_lang('CreationDate').'</a>&nbsp;|&nbsp;
<a href="index.php?'.api_get_cidreq().'&type=2">'.get_lang('UpdateDate').'</a>&nbsp;|&nbsp;'.get_lang('TermName').'
&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=4">'.get_lang('PreSelectedOrder').'</a>
</div>';
} elseif ($type == 4){
echo '<div class="glossary-orderby-link">'.get_lang('OrderBy').'&nbsp;:&nbsp;<a href="index.php?'.api_get_cidreq().'&type=1">'.get_lang('CreationDate').'</a>&nbsp;|&nbsp;
<a href="index.php?'.api_get_cidreq().'&type=2">'.get_lang('UpdateDate').'</a>&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=3">'.get_lang('TermName').'</a>
&nbsp;|&nbsp;'.get_lang('PreSelectedOrder').'</a>
</div>';
} else {
echo '<div class="glossary-orderby-link">'.get_lang('OrderBy').'&nbsp;:&nbsp;<a href="index.php?'.api_get_cidreq().'&type=1">'.get_lang('CreationDate').'</a>&nbsp;|&nbsp;
<a href="index.php?'.api_get_cidreq().'&type=2">'.get_lang('UpdateDate').'</a>&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=3">'.get_lang('TermName').'</a>
&nbsp;|&nbsp;<a href="index.php?'.api_get_cidreq().'&type=4">'.get_lang('PreSelectedOrder').'</a>
</div>';
}
}
echo '<br />';
echo '<div class="glossary-terms-list">';
// glossary list
while ($row_glossary_list=Database::fetch_array($glossary_list)) {
if ( ($_GET['action'] == 'edit_glossary') && ($_GET['glossary_id'] == $row_glossary_list['glossary_id']) ) {
//echo '<body onload="text_focus()">';
echo '<div class="glossary-term-edit-form">';
echo '<form name="form_glossary" action="index.php'.api_get_cidreq().'">';
echo '<input type="hidden" name="g_id" value="'.Security::remove_XSS($_GET['glossary_id']).'">';
echo '<span class="glossary-term-edit-title">'.get_lang('TermName').'</span><br />';
echo '<input type="text" name="n_glossary" value="'.$row_glossary_list['name'].'" onfocus="this.select()"><br />';
echo '<span class="glossary-term-edit-desc">'.get_lang('TermDefinition').'</span><br /><textarea cols="60" rows="5" maxlength="255" name="d_glossary" onfocus="this.select()">'.$row_glossary_list['description'].'</textarea><br />';
echo '<input type="submit" value="'.get_lang('TermUpdateButton').'">';
echo '</form></div>';
$dsp_order = '';
if ( ($_GET['action'] == 'edit_glossary') && ($_GET['glossary_id'] == $row_glossary_list['glossary_id']) ) {
if ($is_allowed_to_edit) {
echo '<div class="glossary-term-edit-form"><a name="term-'.$row_glossary_list['glossary_id'].'"></a>';
echo '<form name="form_glossary" action="index.php?'.api_get_cidreq().'">';
echo '<input type="hidden" name="g_id" value="'.Security::remove_XSS($_GET['glossary_id']).'">';
echo '<span class="glossary-term-edit-title">'.get_lang('TermName').'</span><br />';
echo '<input type="text" name="n_glossary" value="'.$row_glossary_list['name'].'" onfocus="this.select()"><br />';
echo '<span class="glossary-term-edit-desc">'.get_lang('TermDefinition').'</span><br /><textarea cols="60" rows="5" maxlength="255" name="d_glossary" onfocus="this.select()">'.$row_glossary_list['description'].'</textarea><br />';
echo '<input type="submit" value="'.get_lang('TermUpdateButton').'">';
echo '</form></div>';
}
} else {
echo '<div class="glossary-term"><a name="term-'.$row_glossary_list['glossary_id'].'"></a><span class="glossary-term-title">'.$row_glossary_list['name'].'</span><br />';
echo '<span class="glossary-term-desc">'.$row_glossary_list['description'].'</span><br />';
if ($status == 1) {
echo '<span class="glossary-term-desc">'.$row_glossary_list['description'].'</span><br />';
if ($is_allowed_to_edit) {
$id = $row_glossary_list['glossary_id'];
$icon_edit ='edit.gif';
$icon_delete ='delete.gif';
$icon_move_down = 'down.gif';
$icon_move_up ='up.gif';
$icon_gray_down = 'down_na.gif';
$icon_gray_up = 'up_na.gif';
// links order by up/down
if (isset($type) && $type == 4) {
if ($row_glossary_list['display_order'] == 1 && $max != 1) {
$dsp_order .= '<a href="index.php?'.api_get_cidreq().'&action=move_lp_down&id='.$id.'&type=4">' .
'<img src="../img/'.$icon_move_down.'" border="0" alt="'.get_lang("MoveDown").'" title="'.get_lang("MoveDown").'"/>' .
'</a><img src="../img/'.$icon_gray_up.'" border="0" alt="" title="" />';
} elseif($current == $max-1 && $max != 1) {
//last element
$dsp_order .= '<img src="../img/'.$icon_gray_down.'" border="0" alt="" title="" /><a href="index.php?'.api_get_cidreq().'&action=move_lp_up&id='.$id.'&type=4">' .
'<img src="../img/'.$icon_move_up.'" border="0" alt="'.get_lang("MoveUp").'" title="'.get_lang("MoveUp").'"/>' .
'</a>';
} elseif($max == 1) {
$dsp_order = '&nbsp;';
} else {
$dsp_order .= '<a href="index.php?'.api_get_cidreq().'&action=move_lp_down&id='.$id.'&type=4">' .
'<img src="../img/'.$icon_move_down.'" border="0" alt="'.get_lang("MoveDown").'" title="'.get_lang("MoveDown").'"/>' .
'</a>&nbsp;';
$dsp_order .= '<a href="index.php?'.api_get_cidreq().'&action=move_lp_up&id='.$id.'&type=4">' .
'<img src="../img/'.$icon_move_up.'" border="0" alt="'.get_lang("MoveDown").'" title="'.get_lang("MoveDown").'"/>' .
'</a>';
}
}
// action links
echo '<span class="glossary-term-action-links">';
echo '<a href="index.php?'.api_get_cidreq().'&action=edit_glossary&glossary_id='.$row_glossary_list['glossary_id'].'"><img src="../img/'.$icon_edit.'" title ="'.get_lang('TermEditAction').'"></a>&nbsp;';
echo '<a href="index.php?'.api_get_cidreq().'&action=delete_glossary&glossary_id='.$row_glossary_list['glossary_id'].'" onclick="return confirmation(\''.$row_glossary_list['name'].'\');"><img src="../img/'.$icon_delete.'" title ="'.get_lang('TermDeleteAction').'"></a></dd>';
echo '</span>';
echo '<a href="index.php?'.api_get_cidreq().'&action=edit_glossary&glossary_id='.$id.'#term-'.$id.'"><img src="../img/'.$icon_edit.'" title ="'.get_lang('TermEditAction').'"></a>&nbsp;';
echo '<a href="index.php?'.api_get_cidreq().'&action=delete_glossary&glossary_id='.$id.'" onclick="return confirmation(\''.$row_glossary_list['name'].'\');"><img src="../img/'.$icon_delete.'" title ="'.get_lang('TermDeleteAction').'"></a></dd>';
echo $dsp_order;
echo '</span>';
}
echo '</div>';
}
}
}
$current++;
}
echo '</div>';
/*
==============================================================================
FOOTER

Loading…
Cancel
Save