Project

General

Profile

Feature #3857 » glossary.patch

Bas Wijnen, 03/09/2011 23:16

View differences:

main/glossary/index.php Thu Sep 01 22:42:44 2011 +0200 → main/glossary/index.php Sat Sep 03 23:16:31 2011 +0200
12 12
// Including the global initialization file.
13 13
require_once '../inc/global.inc.php';
14 14
require_once api_get_path(LIBRARY_PATH).'glossary.lib.php';
15
require_once api_get_path(LIBRARY_PATH).'csv.lib.php';
15 16

  
16 17
// The section (tabs).
17 18
$this_section = SECTION_COURSES;
......
35 36
// setting the tool constants
36 37
$tool = TOOL_GLOSSARY;
37 38

  
38
// Tracking
39
event_access_tool(TOOL_GLOSSARY);
40

  
41
// Displaying the header
42

  
43
if (isset($_GET['action']) && ($_GET['action'] == 'addglossary' || $_GET['action'] == 'edit_glossary')) {
44
    $tool='GlossaryManagement';
45
    $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('ToolGlossary'));
39
function sorter($item1, $item2) {
40
	if ($item1[2] == $item2[2])
41
		return 0;
42
	return $item1[2] < $item2[2] ? -1 : 1;
46 43
}
47 44

  
48
Display::display_header(get_lang(ucfirst($tool)));
45
if (isset($_GET['action']) && $_GET['action'] == 'export') {
46
	header ('Content-Type: text/csv;encoding=utf8');
47
	header ('Content-Disposition: filename="glossary.csv"');
48
	$data = GlossaryManager::get_glossary_data(0, GlossaryManager::get_number_glossary_terms (api_get_session_id()), 0, 'ASC');
49
	usort($data, "sorter");
50
	$list = array ();
51
	foreach($data as $line) {
52
		$list[] = array ($line[0], $line[1]);
53
	}
54
	echo CSV::export ($list);
55
} else {
56
  // Tracking
57
  event_access_tool(TOOL_GLOSSARY);
49 58

  
50
// Tool introduction
51
Display::display_introduction_section(TOOL_GLOSSARY);
59
  // Displaying the header
52 60

  
53
if (isset($_GET['action']) && $_GET['action'] == 'changeview' AND in_array($_GET['view'],array('list','table'))) {
54
    $_SESSION['glossary_view'] = $_GET['view'];
55
} else {
56
  if (!isset($_SESSION['glossary_view'])) {
57
    $_SESSION['glossary_view'] = 'table';//Default option
61
  if (isset($_GET['action']) && ($_GET['action'] == 'addglossary' || $_GET['action'] == 'edit_glossary')) {
62
      $tool='GlossaryManagement';
63
      $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('ToolGlossary'));
58 64
  }
59
}
60 65

  
61
if (api_is_allowed_to_edit(null, true)) {
62
    // Adding a glossary
63
    if (isset($_GET['action']) && $_GET['action'] == 'addglossary') {
66
  Display::display_header(get_lang(ucfirst($tool)));
67

  
68
  // Tool introduction
69
  Display::display_introduction_section(TOOL_GLOSSARY);
70

  
71
  if (isset($_GET['action']) && $_GET['action'] == 'changeview' AND in_array($_GET['view'],array('list','table'))) {
72
      $_SESSION['glossary_view'] = $_GET['view'];
73
  } else {
74
    if (!isset($_SESSION['glossary_view'])) {
75
      $_SESSION['glossary_view'] = 'table';//Default option
76
    }
77
  }
78
  if (api_is_allowed_to_edit(null, true)) {
79
    if (isset($_GET['action']) && $_GET['action'] == 'import') {
80
	// Create form to upload csv file.
81
        $form = new FormValidator('glossary','post', api_get_self().'?action=do_import');
82
        $form->addElement('header', '', get_lang('ImportGlossary'));
83
        $form->addElement('checkbox', 'replace', get_lang('ReplaceGlossary'));
84
        $form->addElement('file', 'file');
85
        $form->addElement('style_submit_button', 'SubmitImport', get_lang('ImportGlossaryButton'), 'class="save"');
86
	$form->display();
87
    } else if (isset($_GET['action']) && $_GET['action'] == 'do_import') {
88
	if (isset($_POST['replace']) && $_POST['replace']) {
89
		// Remove entire glossary.
90
		foreach (GlossaryManager::get_glossary_terms () as $term) {
91
			if (!GlossaryManager::delete_glossary ($term['id'], false))
92
				Display::display_error_message (get_lang ("CannotDeleteGlossary") . ':' . $term['id']);
93
		}
94
	}
95
	$data = CSV::import (file_get_contents($_FILES['file']['tmp_name']));
96
	$good = 0;
97
	$bad = 0;
98
	foreach($data as $term) {
99
		if (GlossaryManager::save_glossary(array('glossary_title' => $term[0], 'glossary_comment' => $term[1]), false))
100
			$good++;
101
		else
102
			$bad++;
103
	}
104
	Display::display_confirmation_message (get_lang ("TermsImported") . ':' . $good);
105
	if ($bad)
106
		Display::display_error_message (get_lang ("TermsNotImported") . ':' . $bad);
107
	GlossaryManager::display_glossary();
108
    } else if (isset($_GET['action']) && $_GET['action'] == 'addglossary') { // Adding a glossary
64 109
        // initiate the object
65 110
        $form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
66 111
        // settting the form elements
......
155 200
    } else {
156 201
        GlossaryManager::display_glossary();
157 202
    }
158
} else {
203
  } else {
159 204
    GlossaryManager::display_glossary();
205
  }
206

  
207
  // Footer
208
  Display::display_footer();
160 209
}
161

  
162
// Footer
163
Display::display_footer();
/dev/null Thu Jan 01 00:00:00 1970 +0000 → main/inc/lib/csv.lib.php Sat Sep 03 23:16:31 2011 +0200
1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
*	Code library for csv import and export
5
*
6
* @author Bas Wijnen		<wijnen@debian.org>
7
* @package chamilo.csv
8
*/
9
/**
10
 * Class
11
 * @package chamilo.csv
12
 */
13
class CSV {
14
	private function read_word ($data, &$pos) {
15
		if ($data[$pos] == '"') {
16
			$start = $pos;
17
			$end = strpos ($data, '"', $pos + 1);
18
			while ($data[$end + 1] == '"') {
19
				$pos = $end + 1;
20
				$end = strpos ($data, '"', $pos + 1);
21
			}
22
			$word = str_replace ('""', '"', substr ($data, $start, $end - $start));
23
			$pos = $end + 1;
24
		} else {
25
			$comma = strpos ($data, ',', $pos);
26
			$cr = strpos ($data, "\r", $pos);
27
			$end = strpos ($data, "\n", $pos);
28
			if ($cr !== false && $cr < $end)
29
				$end = $cr;
30
			if ($comma !== false && $comma < $end)
31
				$end = $comma;
32
			$word = substr ($data, $pos, $end - $pos);
33
			$pos = $end;
34
		}
35
		return $word;
36
	}
37
	/**
38
	 * Import a csv file
39
	 *
40
	 * @param string $data
41
	 * @return array
42
	 */
43
	function import($data) {
44
		$data = $data . "\n";
45
		$ret = array ();
46
		$pos = 0;
47
		while ($pos < strlen ($data)) {
48
			$line = array ();
49
			$line[] = self::read_word ($data, $pos);
50
			while ($data[$pos] == ',' || $data[$pos] == ';') {
51
				$pos++;
52
				$line[] = self::read_word ($data, $pos);
53
			}
54
			$ret[] = $line;
55
			while ($pos < strlen ($data) && ($data[$pos] == "\r" || $data[$pos] == "\n"))
56
				$pos++;
57
		}
58
		return $ret;
59
	}
60
	/**
61
	 * Export to a csv file
62
	 *
63
	 * @param array $data
64
	 * @return string
65
	 */
66
	function export($data) {
67
		$ret = "";
68
		foreach($data as $line) {
69
			$words = array ();
70
			foreach($line as $word) {
71
				if (strpos ($word, '"') === false && strpos ($word, "\n") === false && strpos ($word, "\r") === false && strpos ($word, ',') === false && strpos ($word, ';') == false)
72
					$words[] = $word;
73
				else
74
					$words[] = '"' . str_replace ('"', '""', $word) . '"';
75
			}
76
			$ret = $ret . implode (',', $words) . "\r\n";
77
		}
78
		return $ret;
79
	}
80
}
main/inc/lib/glossary.lib.php Thu Sep 01 22:42:44 2011 +0200 → main/inc/lib/glossary.lib.php Sat Sep 03 23:16:31 2011 +0200
77 77
	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
78 78
	 * @version januari 2009, dokeos 1.8.6
79 79
	 */
80
	function save_glossary($values) {
80
	function save_glossary($values, $message = true) {
81 81
		if (!is_array($values) or !isset($values['glossary_title'])) {
82 82
			return false;
83 83
		}
......
94 94
		if (GlossaryManager::glossary_exists($values['glossary_title']))
95 95
		{
96 96
			// display the feedback message
97
			Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
97
			if ($message)
98
				Display::display_error_message($values['glossary_title'] . ':' . get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
98 99
			return false;
99 100
		} else {
100 101
			$sql = "INSERT INTO $t_glossary (name, description, display_order, session_id)
......
111 112
			api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, $id, 'GlossaryAdded', api_get_user_id());
112 113
			$_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
113 114
			// display the feedback message
114
			Display::display_confirmation_message(get_lang('TermAdded'));
115
			if ($message)
116
				Display::display_confirmation_message(get_lang('TermAdded'));
115 117
            return $id;
116 118
		}
117 119
	}
......
125 127
	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
126 128
	 * @version januari 2009, dokeos 1.8.6
127 129
	 */
128
	function update_glossary($values) {
130
	function update_glossary($values, $message = true) {
129 131
		// Database table definition
130 132
		$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
131 133
		// check if the glossary term already exists
132 134
		if (GlossaryManager::glossary_exists($values['glossary_title'],$values['glossary_id']))
133 135
		{
134 136
			// display the feedback message
135
			Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
137
			if ($message)
138
				Display::display_error_message($values['glossary_title'] . ':' . get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
136 139
			return false;
137 140
		} else {
138 141
			$sql = "UPDATE $t_glossary SET
......
144 147
			//update glossary into item_property
145 148
			api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, Database::escape_string($values['glossary_id']), 'GlossaryUpdated', api_get_user_id());
146 149
			// display the feedback message
147
			Display::display_confirmation_message(get_lang('TermUpdated'));
150
			if ($message)
151
				Display::display_confirmation_message(get_lang('TermUpdated'));
148 152
		}
149 153
		return true;
150 154
	}
......
235 239
	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
236 240
	 * @version januari 2009, dokeos 1.8.6
237 241
	 */
238
	function delete_glossary($glossary_id) {
242
	function delete_glossary($glossary_id, $message = true) {
239 243
		// Database table definition
240 244
		$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
241 245
		if (empty($glossary_id)) { return false; }
......
249 253
		// reorder the remaining terms
250 254
		GlossaryManager::reorder_glossary();
251 255
		$_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
252
		Display::display_confirmation_message(get_lang('TermDeleted'));
256
		if ($message)
257
			Display::display_confirmation_message(get_lang('TermDeleted'));
253 258
		return true;
254 259
	}
255 260

  
......
278 283
		} else {
279 284
			echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=table">'.Display::return_icon('view_text.png',get_lang('TableView'),'','32').'</a>';
280 285
		}
286
		echo '<a href="index.php?'.api_get_cidreq().'&action=export">'.Display::return_icon('export_csv.png',get_lang('ExportGlossaryAsCSV'),'','32').'</a>';
287
		if (api_is_allowed_to_edit(null,true)) {
288
			echo '<a href="index.php?'.api_get_cidreq().'&action=import">'.Display::return_icon('import_glossary.png',get_lang('ImportGlossary'),'','32').'</a>';
289
		}
281 290
		echo '</div>';
282 291
		if (!$_SESSION['glossary_view'] OR $_SESSION['glossary_view'] == 'table') {
283 292
			$table = new SortableTable('glossary', array('GlossaryManager','get_number_glossary_terms'), array('GlossaryManager','get_glossary_data'),0);
......
504 513
	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
505 514
	 * @version januari 2009, dokeos 1.8.6
506 515
	 */
507
	function move_glossary($direction, $glossary_id) {
516
	function move_glossary($direction, $glossary_id, $message = true) {
508 517
		// Database table definition
509 518
		$t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
510 519

  
......
535 544
		$res = Database::query($sql1);
536 545
		$res = Database::query($sql2);
537 546

  
538
		Display::display_confirmation_message(get_lang('TermMoved'));
547
		if ($message)
548
			Display::display_confirmation_message(get_lang('TermMoved'));
539 549
	}
540 550
}
main/lang/dutch/glossary.inc.php Thu Sep 01 22:42:44 2011 +0200 → main/lang/dutch/glossary.inc.php Sat Sep 03 23:16:31 2011 +0200
22 22
$TermMoved = "Het begrip werd verplaatst";
23 23
$ShowGlossaryInExtraToolsTitle = "Tool begrippen uit het glossarium in andere modules";
24 24
$ShowGlossaryInExtraToolsComment = "Mogen begrippen uit het cursusglossarium weergegeven worden in andere modules zoals bv. leerpaden en oefeningen ?";
25
?>
25
$ImportGlossary = "Importeer woordenlijst uit CSV";
26
$ReplaceGlossary = "Vervang volledige woordenlijst";
27
$ImportGlossaryButton = "Importeer CSV";
28
$CannotDeleteGlossary = "Kan term niet verwijderen";
29
$TermsImported = "Geïmporteerde termen";
30
$TermsNotImported = "Niet geïmporteerde termen";
31
$ExportGlossaryAsCSV = "Exporteer woordenlijst naar CSV";
32
?>
main/lang/english/glossary.inc.php Thu Sep 01 22:42:44 2011 +0200 → main/lang/english/glossary.inc.php Sat Sep 03 23:16:31 2011 +0200
25 25
$TermMoved = "The term has moved";
26 26
$ShowGlossaryInExtraToolsTitle = "Show the glossary terms in extra tools";
27 27
$ShowGlossaryInExtraToolsComment = "From here you can configure how to add the glossary terms in extra tools as learning path and exercice tool";
28
?>
28
$ImportGlossary = "Import glossary from CSV";
29
$ReplaceGlossary = "Replace entire glossary";
30
$ImportGlossaryButton = "Import";
31
$CannotDeleteGlossary = "Cannot delete term";
32
$TermsImported = "Terms imported";
33
$TermsNotImported = "Terms not imported";
34
$ExportGlossaryAsCSV = "Export glossary as CSV";
35
?>
(1-1/6)