Glossary Adding import/export to CSV added by Bas Wijnen + changing ifs with a switch see #3857

skala
Julio Montoya 13 years ago
parent 3bb24ad716
commit 97e371795d
  1. 244
      main/glossary/index.php
  2. 52
      main/inc/lib/glossary.lib.php

@ -1,9 +1,9 @@
<?php //$id: $
<?php
/* For licensing terms, see /license.txt */
/**
* @package chamilo.glossary
* @author Christian Fasanando, initial version
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium, refactoring and tighter integration in Dokeos
* @author Bas Wijnen import/export to CSV
*/
// The language file that needs to be included.
@ -12,6 +12,8 @@ $language_file = array('glossary');
// Including the global initialization file.
require_once '../inc/global.inc.php';
require_once api_get_path(LIBRARY_PATH).'glossary.lib.php';
require_once api_get_path(LIBRARY_PATH).'export.lib.inc.php';
require_once api_get_path(LIBRARY_PATH).'import.lib.php';
// The section (tabs).
$this_section = SECTION_COURSES;
@ -19,9 +21,6 @@ $this_section = SECTION_COURSES;
// Notification for unauthorized people.
api_protect_course_script(true);
// Including additional libraries.
require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
// Additional javascripts.
$htmlHeadXtra[] = GlossaryManager::javascript_glossary();
$htmlHeadXtra[] = '<script type="text/javascript">
@ -38,6 +37,12 @@ $tool = TOOL_GLOSSARY;
// Tracking
event_access_tool(TOOL_GLOSSARY);
function sorter($item1, $item2) {
if ($item1[2] == $item2[2])
return 0;
return $item1[2] < $item2[2] ? -1 : 1;
}
// Displaying the header
if (isset($_GET['action']) && ($_GET['action'] == 'addglossary' || $_GET['action'] == 'edit_glossary')) {
@ -45,6 +50,17 @@ if (isset($_GET['action']) && ($_GET['action'] == 'addglossary' || $_GET['action
$interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('ToolGlossary'));
}
if (isset($_GET['action']) && $_GET['action'] == 'export') {
$data = GlossaryManager::get_glossary_data(0, GlossaryManager::get_number_glossary_terms (api_get_session_id()), 0, 'ASC');
usort($data, "sorter");
$list = array();
foreach($data as $line) {
$list[] = array ($line[0], $line[1]);
}
$filename = 'glossary_course_'.api_get_course_id();
Export::export_table_csv($list,$filename);
}
Display::display_header(get_lang(ucfirst($tool)));
// Tool introduction
@ -58,96 +74,144 @@ if (isset($_GET['action']) && $_GET['action'] == 'changeview' AND in_array($_GET
}
}
$action = isset($_GET['action']) ? $_GET['action'] : null;
if (api_is_allowed_to_edit(null, true)) {
// Adding a glossary
if (isset($_GET['action']) && $_GET['action'] == 'addglossary') {
// initiate the object
$form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
// settting the form elements
$form->addElement('header', '', get_lang('TermAddNew'));
$form->addElement('text', 'glossary_title', get_lang('TermName'), array('size'=>'80', 'id'=>'glossary_title'));
//$form->applyFilter('glossary_title', 'html_filter');
$form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
$form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermAddButton'), 'class="save"');
// setting the rules
$form->addRule('glossary_title',get_lang('ThisFieldIsRequired'), 'required');
// The validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
GlossaryManager::save_glossary($values);
switch ($action) {
case 'addglosary':
$form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
// settting the form elements
$form->addElement('header', '', get_lang('TermAddNew'));
$form->addElement('text', 'glossary_title', get_lang('TermName'), array('size'=>'80', 'id'=>'glossary_title'));
//$form->applyFilter('glossary_title', 'html_filter');
$form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
$form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermAddButton'), 'class="save"');
// setting the rules
$form->addRule('glossary_title',get_lang('ThisFieldIsRequired'), 'required');
// The validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
GlossaryManager::save_glossary($values);
}
Security::clear_token();
GlossaryManager::display_glossary();
} else {
$token = Security::get_token();
$form->addElement('hidden','sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
break;
case 'edit_glossary':
if (is_numeric($_GET['glossary_id'])) {
// initiate the object
$form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.Security::remove_XSS($_GET['glossary_id']));
// settting the form elements
$form->addElement('header', '', get_lang('TermEdit'));
$form->addElement('hidden', 'glossary_id');
$form->addElement('text', 'glossary_title', get_lang('TermName'),array('size'=>'80'));
//$form->applyFilter('glossary_title', 'html_filter');
$form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
$element = $form->addElement('text', 'insert_date', get_lang('CreationDate'),array('size'=>'100'));
$element ->freeze();
$element = $form->addElement('text', 'update_date', get_lang('UpdateDate'),array('size'=>'100'));
$element ->freeze();
$form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermUpdateButton'), 'class="save"');
// setting the defaults
$glossary_data = GlossaryManager::get_glossary_information($_GET['glossary_id']);
// Date treatment for timezones
if (!empty($glossary_data['insert_date']) && $glossary_data['insert_date'] != '0000-00-00 00:00:00:') {
$glossary_data['insert_date'] = api_get_local_time($glossary_data['insert_date']);
} else {
$glossary_data['insert_date'] = '';
}
if (!empty($glossary_data['update_date']) && $glossary_data['update_date'] != '0000-00-00 00:00:00:') {
$glossary_data['update_date'] = api_get_local_time($glossary_data['update_date']);
} else {
$glossary_data['update_date'] = '';
}
$form->setDefaults($glossary_data);
// setting the rules
$form->addRule('glossary_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
// The validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
GlossaryManager::update_glossary($values);
}
Security::clear_token();
GlossaryManager::display_glossary();
} else {
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
}
Security::clear_token();
break;
case 'delete_glossary':
GlossaryManager::delete_glossary($_GET['glossary_id']);
GlossaryManager::display_glossary();
break;
case 'moveup':
GlossaryManager::move_glossary('up',$_GET['glossary_id']);
GlossaryManager::display_glossary();
break;
case 'movedown':
GlossaryManager::move_glossary('down',$_GET['glossary_id']);
GlossaryManager::display_glossary();
} else {
$token = Security::get_token();
$form->addElement('hidden','sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
} else if (isset($_GET['action']) && $_GET['action'] == 'edit_glossary' && is_numeric($_GET['glossary_id'])) { // Editing a glossary
// initiate the object
$form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.Security::remove_XSS($_GET['glossary_id']));
// settting the form elements
$form->addElement('header', '', get_lang('TermEdit'));
$form->addElement('hidden', 'glossary_id');
$form->addElement('text', 'glossary_title', get_lang('TermName'),array('size'=>'80'));
//$form->applyFilter('glossary_title', 'html_filter');
$form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
$element = $form->addElement('text', 'insert_date', get_lang('CreationDate'),array('size'=>'100'));
$element ->freeze();
$element = $form->addElement('text', 'update_date', get_lang('UpdateDate'),array('size'=>'100'));
$element ->freeze();
$form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermUpdateButton'), 'class="save"');
// setting the defaults
$glossary_data = GlossaryManager::get_glossary_information($_GET['glossary_id']);
// Date treatment for timezones
if (!empty($glossary_data['insert_date']) && $glossary_data['insert_date'] != '0000-00-00 00:00:00:') {
$glossary_data['insert_date'] = api_get_local_time($glossary_data['insert_date']);
} else {
$glossary_data['insert_date'] = '';
}
if (!empty($glossary_data['update_date']) && $glossary_data['update_date'] != '0000-00-00 00:00:00:') {
$glossary_data['update_date'] = api_get_local_time($glossary_data['update_date']);
} else {
$glossary_data['update_date'] = '';
}
$form->setDefaults($glossary_data);
// setting the rules
$form->addRule('glossary_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
// The validation or display
if ($form->validate()) {
$check = Security::check_token('post');
if ($check) {
$values = $form->exportValues();
GlossaryManager::update_glossary($values);
break;
case 'import':
$form = new FormValidator('glossary','post', api_get_self().'?action=import');
$form->addElement('header', '', get_lang('ImportGlossary'));
$form->addElement('checkbox', 'replace', get_lang('ReplaceGlossary'));
$form->addElement('file', 'file');
$form->addElement('style_submit_button', 'SubmitImport', get_lang('ImportGlossaryButton'), 'class="save"');
$form->display();
if ($form->validate()) {
if (isset($_POST['replace']) && $_POST['replace']) {
foreach (GlossaryManager::get_glossary_terms () as $term) {
if (!GlossaryManager::delete_glossary ($term['id'], false)) {
Display::display_error_message (get_lang ("CannotDeleteGlossary") . ':' . $term['id']);
}
}
}
$data = Import::csv_to_array($_FILES['file']['tmp_name']);
if (!empty($data[0])) {
$data = $data[0];
$good = 0;
$bad = 0;
foreach($data as $key => $term) {
if (GlossaryManager::save_glossary(array('glossary_title' => $key, 'glossary_comment' => $term), false))
$good++;
else
$bad++;
}
}
Display::display_confirmation_message (get_lang ("TermsImported") . ':' . $good);
if ($bad)
Display::display_error_message (get_lang ("TermsNotImported") . ':' . $bad);
GlossaryManager::display_glossary();
}
Security::clear_token();
break;
default:
GlossaryManager::display_glossary();
} else {
$token = Security::get_token();
$form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token));
$form->display();
}
} else if (isset($_GET['action']) && $_GET['action'] == 'delete_glossary' && is_numeric($_GET['glossary_id'])) {// deleting a glossary
GlossaryManager::delete_glossary(Security::remove_XSS($_GET['glossary_id']));
GlossaryManager::display_glossary();
} else if (isset($_GET['action']) && $_GET['action'] == 'moveup' && is_numeric($_GET['glossary_id'])) { // moving a glossary term up
GlossaryManager::move_glossary('up',$_GET['glossary_id']);
GlossaryManager::display_glossary();
} else if (isset($_GET['action']) && $_GET['action'] == 'movedown' && is_numeric($_GET['glossary_id'])) { // moving a glossary term down
GlossaryManager::move_glossary('down',$_GET['glossary_id']);
GlossaryManager::display_glossary();
} else {
GlossaryManager::display_glossary();
break;
}
} else {
GlossaryManager::display_glossary();

@ -81,7 +81,7 @@ class GlossaryManager {
* @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
* @version januari 2009, dokeos 1.8.6
*/
function save_glossary($values) {
function save_glossary($values, $message = true) {
if (!is_array($values) or !isset($values['glossary_title'])) {
return false;
}
@ -97,7 +97,8 @@ class GlossaryManager {
// check if the glossary term already exists
if (GlossaryManager::glossary_exists($values['glossary_title'])) {
// display the feedback message
Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
if ($message)
Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
return false;
} else {
$sql = "INSERT INTO $t_glossary (c_id, name, description, display_order, session_id)
@ -115,7 +116,8 @@ class GlossaryManager {
api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, $id, 'GlossaryAdded', api_get_user_id());
$_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
// display the feedback message
Display::display_confirmation_message(get_lang('TermAdded'));
if ($message)
Display::display_confirmation_message(get_lang('TermAdded'));
return $id;
}
}
@ -129,7 +131,7 @@ class GlossaryManager {
* @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
* @version januari 2009, dokeos 1.8.6
*/
function update_glossary($values) {
function update_glossary($values, $message = true) {
// Database table definition
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
@ -138,7 +140,8 @@ class GlossaryManager {
// check if the glossary term already exists
if (GlossaryManager::glossary_exists($values['glossary_title'],$values['glossary_id'])) {
// display the feedback message
Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
if ($message)
Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
return false;
} else {
$sql = "UPDATE $t_glossary SET
@ -150,7 +153,8 @@ class GlossaryManager {
//update glossary into item_property
api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, Database::escape_string($values['glossary_id']), 'GlossaryUpdated', api_get_user_id());
// display the feedback message
Display::display_confirmation_message(get_lang('TermUpdated'));
if ($message)
Display::display_confirmation_message(get_lang('TermUpdated'));
}
return true;
}
@ -247,7 +251,7 @@ class GlossaryManager {
* @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
* @version januari 2009, dokeos 1.8.6
*/
function delete_glossary($glossary_id) {
function delete_glossary($glossary_id, $message = true) {
// Database table definition
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
$course_id = api_get_course_int_id();
@ -263,7 +267,8 @@ class GlossaryManager {
// reorder the remaining terms
GlossaryManager::reorder_glossary();
$_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
Display::display_confirmation_message(get_lang('TermDeleted'));
if ($message)
Display::display_confirmation_message(get_lang('TermDeleted'));
return true;
}
@ -286,7 +291,11 @@ class GlossaryManager {
if (api_is_allowed_to_edit(null,true)) {
echo '<a href="index.php?'.api_get_cidreq().'&action=addglossary&msg=add">'.Display::return_icon('new_glossary_term.png',get_lang('TermAddNew'),'','32').'</a>';
}
echo '<a href="index.php?'.api_get_cidreq().'&action=export">'.Display::return_icon('export_csv.png',get_lang('ExportGlossaryAsCSV'),'','32').'</a>';
if (api_is_allowed_to_edit(null,true)) {
echo '<a href="index.php?'.api_get_cidreq().'&action=import">'.Display::return_icon('import_csv.png',get_lang('ImportGlossary'),'','32').'</a>';
}
if ((isset($_SESSION['glossary_view']) && $_SESSION['glossary_view'] == 'table') or (!isset($_SESSION['glossary_view']))){
echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=list">'.Display::return_icon('view_detailed.png',get_lang('ListView'),'','32').'</a>';
} else {
@ -439,24 +448,7 @@ class GlossaryManager {
* @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
* @version januari 2009, dokeos 1.8.6
*/
function actions_filter($glossary_id, $url_params, $row) {
/*
if (!$_SESSION['max_glossary_display'] OR $_SESSION['max_glossary_display'] == '') {
$_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
}
if (empty($_GET['glossary_column'])) {
if ($row[0] > 1) {
$return .= '<a href="'.api_get_self().'?action=moveup&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'">'.Display::return_icon('up.png', get_lang('Up'),'',22).'</a>';
} else {
$return .= Display::return_icon('up_na.png','&nbsp;','',22);
}
if ($row[0] < $_SESSION['max_glossary_display']) {
$return .= '<a href="'.api_get_self().'?action=movedown&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'">'.Display::return_icon('down.png',get_lang('Down'),'',22).'</a>';
} else {
$return .= Display::return_icon('down_na.png','&nbsp;','',22);
}
}*/
function actions_filter($glossary_id, $url_params, $row) {
$glossary_id = $row[2];
$return = '<a href="'.api_get_self().'?action=edit_glossary&amp;glossary_id='.$glossary_id.'&'.api_get_cidreq().'&msg=edit">'.Display::return_icon('edit.png',get_lang('Edit'),'',22).'</a>';
$glossary_data = GlossaryManager::get_glossary_information($glossary_id);
@ -522,7 +514,7 @@ class GlossaryManager {
* @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
* @version januari 2009, dokeos 1.8.6
*/
function move_glossary($direction, $glossary_id) {
function move_glossary($direction, $glossary_id, $message = true) {
// Database table definition
$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
@ -553,7 +545,7 @@ class GlossaryManager {
$sql2 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($current_display_order)."' WHERE c_id = $course_id AND glossary_id = '".Database::escape_string($next_id)."'";
$res = Database::query($sql1);
$res = Database::query($sql2);
Display::display_confirmation_message(get_lang('TermMoved'));
if ($message)
Display::display_confirmation_message(get_lang('TermMoved'));
}
}

Loading…
Cancel
Save