- Using user-defined functions to display comments properly.
[moodle.git] / mod / glossary / lib.php
CommitLineData
07842023 1<?PHP // $Id$
2
3/// Library of functions and constants for module glossary
4/// (replace glossary with the name of your module and delete this line)
5
e179048e 6require_once("$CFG->dirroot/files/mimetypes.php");
07842023 7
7dd88447 8define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
9define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
c76e673a 10
1ac87c73 11define("GLOSSARY_NO_VIEW", -1);
7dd88447 12define("GLOSSARY_STANDARD_VIEW", 0);
13define("GLOSSARY_CATEGORY_VIEW", 1);
c197e607 14define("GLOSSARY_DATE_VIEW", 2);
1ac87c73 15define("GLOSSARY_AUTHOR_VIEW", 3);
16define("GLOSSARY_ADDENTRY_VIEW", 4);
17define("GLOSSARY_IMPORT_VIEW", 5);
18define("GLOSSARY_EXPORT_VIEW", 6);
19define("GLOSSARY_APPROVAL_VIEW", 7);
fb443f1a 20
767a31c3 21define("GLOSSARY_FORMAT_SIMPLE", 0);
22define("GLOSSARY_FORMAT_CONTINUOUS", 1);
23
07842023 24function glossary_add_instance($glossary) {
25/// Given an object containing all the necessary data,
26/// (defined by the form in mod.html) this function
27/// will create a new instance and return the id number
28/// of the new instance.
29
0de786f7 30 if ( !isset($glossary->globalglossary) ) {
31 $glossary->globalglossary = 0;
32 } elseif ( !isadmin() ) {
33 $glossary->globalglossary = 0;
34 }
35
07842023 36 $glossary->timecreated = time();
37 $glossary->timemodified = $glossary->timecreated;
38
39 # May have to add extra stuff in here #
40
41 return insert_record("glossary", $glossary);
42}
43
44
45function glossary_update_instance($glossary) {
46/// Given an object containing all the necessary data,
47/// (defined by the form in mod.html) this function
48/// will update an existing instance with new data.
5eaa2d34 49global $CFG;
0de786f7 50 if ( !isadmin() ) {
51 unset($glossary->globalglossary);
52 }
53
07842023 54 $glossary->timemodified = time();
55 $glossary->id = $glossary->instance;
56
5eaa2d34 57 $return = update_record("glossary", $glossary);
58 if ($return and $glossary->defaultapproval) {
59 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
60 }
07842023 61
5eaa2d34 62 return $return;
07842023 63}
64
65
66function glossary_delete_instance($id) {
67/// Given an ID of an instance of this module,
68/// this function will permanently delete the instance
69/// and any data that depends on it.
70
71 if (! $glossary = get_record("glossary", "id", "$id")) {
72 return false;
73 }
74
75 $result = true;
76
77 # Delete any dependent records here #
78
79 if (! delete_records("glossary", "id", "$glossary->id")) {
80 $result = false;
49210b90 81 } else {
82 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
83 $cats = "";
1ac87c73 84 foreach ( $categories as $hook ) {
49210b90 85 $cats .= "$cat->id,";
86 }
87 $cats = substr($cats,0,-1);
88 if ($cats) {
89 delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
90 delete_records("glossary_categories", "glossaryid", $glossary->id);
91 }
92 }
93 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
94 $ents = "";
95 foreach ( $entries as $entry ) {
96 $ents .= "$entry->id,";
97 }
98 $ents = substr($ents,0,-1);
99 if ($ents) {
100 delete_records_select("glossary_comments", "entryid in ($ents)");
ea14e783 101 delete_records_select("glossary_alias", "entryid in ($ents)");
49210b90 102 }
103 }
104 delete_records("glossary_entries", "glossaryid", "$glossary->id");
07842023 105 }
07842023 106
107 return $result;
108}
109
110function glossary_user_outline($course, $user, $mod, $glossary) {
111/// Return a small object with summary information about what a
112/// user has done with a given particular instance of this module
113/// Used for user activity reports.
114/// $return->time = the time they did it
115/// $return->info = a short text description
116
1ac87c73 117 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
118 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
119
120 $lastpost = array_pop($entries);
121 $result->time = $lastpost->timemodified;
122 return $result;
123 }
124 return NULL;
125}
126
127function glossary_get_user_entries($glossaryid, $userid) {
128/// Get all the entries for a user in a glossary
129 global $CFG;
130
131 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
132 FROM {$CFG->prefix}glossary g,
133 {$CFG->prefix}glossary_entries e,
134 {$CFG->prefix}user u
135 WHERE g.id = '$glossaryid'
136 AND e.glossaryid = g.id
137 AND e.userid = '$userid'
138 AND e.userid = u.id
139 ORDER BY e.timemodified ASC");
07842023 140}
141
142function glossary_user_complete($course, $user, $mod, $glossary) {
143/// Print a detailed representation of what a user has done with
144/// a given particular instance of this module, for user activity reports.
1ac87c73 145 global $CFG;
07842023 146
1ac87c73 147 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
148 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
149 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
150 print_simple_box_start("center","70%");
151 } else {
152 echo '<table width="95%" boder="0"><tr><td>';
153 }
154 foreach ($entries as $entry) {
155 $cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
156 glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
157 echo '<p>';
158 }
159 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
160 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
161 print_simple_box_end();
162 } else {
163 echo '</td></tr></table>';
164 }
165 }
07842023 166}
167
168function glossary_print_recent_activity($course, $isteacher, $timestart) {
169/// Given a course and a time, this module should find recent activity
170/// that has occurred in glossary activities and print it out.
171/// Return true if there was output, or false is there was none.
172
173 global $CFG, $THEME;
174
175 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
176 "course = '$course->id' AND ".
177 "module = 'glossary' AND ".
c4a35419 178 "(action = 'add entry' OR ".
179 " action = 'approve entry')", "time ASC")) {
07842023 180 return false;
181 }
182
07842023 183 foreach ($logs as $log) {
184 //Create a temp valid module structure (course,id)
1ac87c73 185 $tempmod->course = $log->course;
186 $entry = get_record("glossary_entries","id",$log->info);
187 $tempmod->id = $entry->glossaryid;
188 //Obtain the visible property from the instance
07842023 189 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 190
191 //Only if the mod is visible
c4a35419 192 if ($modvisible and $entry->approved) {
07842023 193 $entries[$log->info] = glossary_log_info($log);
194 $entries[$log->info]->time = $log->time;
195 $entries[$log->info]->url = $log->url;
196 }
07842023 197 }
198
07842023 199 $content = false;
200 if ($entries) {
201 $strftimerecent = get_string("strftimerecent");
202 $content = true;
203 print_headline(get_string("newentries", "glossary").":");
204 foreach ($entries as $entry) {
205 $date = userdate($entry->timemodified, $strftimerecent);
206 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
207 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
208 echo "$entry->concept";
209 echo "</a>\"</font></p>";
210 }
211 }
212
213 return $content;
214}
215
1ac87c73 216function glossary_log_info($log) {
217 global $CFG;
218
219 return get_record_sql("SELECT e.*, u.firstname, u.lastname
220 FROM {$CFG->prefix}glossary_entries e,
221 {$CFG->prefix}user u
222 WHERE e.id = '$log->info'
223 AND u.id = '$log->userid'");
224}
225
07842023 226function glossary_cron () {
227/// Function to be run periodically according to the moodle cron
228/// This function searches for things that need to be done, such
229/// as sending out mail, toggling flags etc ...
230
231 global $CFG;
232
233 return true;
234}
235
236function glossary_grades($glossaryid) {
237/// Must return an array of grades for a given instance of this module,
238/// indexed by user. It also returns a maximum allowed grade.
239
240 $return->grades = NULL;
241 $return->maxgrade = NULL;
242
243 return $return;
244}
245
05855091 246function glossary_get_participants($glossaryid) {
247//Returns the users with data in one glossary
248//(users with records in glossary_entries, students)
249
250 global $CFG;
251
252 //Get students
253 $students = get_records_sql("SELECT DISTINCT u.*
254 FROM {$CFG->prefix}user u,
255 {$CFG->prefix}glossary_entries g
256 WHERE g.glossaryid = '$glossaryid' and
257 u.id = g.userid");
258
259 //Return students array (it contains an array of unique users)
260 return ($students);
261}
07842023 262
263//////////////////////////////////////////////////////////////////////////////////////
264/// Any other glossary functions go here. Each of them must have a name that
265/// starts with glossary_
266
1ac87c73 267function glossary_debug($debug,$text,$br=1) {
268 if ( $debug ) {
269 echo '<font color=red>' . $text . '</font>';
270 if ( $br ) {
271 echo '<br>';
272 }
273 }
07842023 274}
275
ea14e783 276function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
07842023 277 global $CFG;
ea14e783 278 if ($pivot) {
279 $pivot .= ",";
280 }
07842023 281
ea14e783 282 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
07842023 283 FROM {$CFG->prefix}glossary_entries
284 WHERE glossaryid = '$glossaryid'
285 AND id IN ($entrylist)");
286}
ea14e783 287function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
c76e673a 288global $CFG;
289 if ($where) {
290 $where = " and $where";
291 }
292 if ($orderby) {
293 $orderby = " ORDER BY $orderby";
294 }
ea14e783 295 if ($pivot) {
296 $pivot .= ",";
297 }
298 return get_records_sql("SELECT $pivot *
c76e673a 299 FROM {$CFG->prefix}glossary_entries
300 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
301}
302
1ac87c73 303function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
c76e673a 304global $CFG;
305 if ($where) {
306 $where = " and $where";
307 }
308 if ($orderby) {
309 $orderby = " ORDER BY $orderby";
310 }
ea14e783 311 if ($pivot) {
312 $pivot .= ",";
313 }
314 return get_records_sql("SELECT $pivot ge.*
c76e673a 315 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
1ac87c73 316 WHERE (ge.id = c.entryid and c.categoryid = $hook) and
c76e673a 317 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
318}
07842023 319
b1918034 320function glossary_print_entry($course, $cm, $glossary, $entry, $mode="",$hook="",$printicons = 1, $displayformat = -1) {
914cb260 321 global $THEME, $USER, $CFG;
b1918034 322 if ( $displayformat < 0 ) {
323 $displayformat = $glossary->displayformat;
324 }
1ac87c73 325 if ($entry->approved or ($USER->id == $entry->userid and !isteacher($course->id)) or $mode == 'approval') {
c76e673a 326 $permissiongranted = 0;
b1918034 327 $formatfile = "$CFG->dirroot/mod/glossary/formats/$displayformat.php";
c76e673a 328 $functionname = "glossary_print_entry_by_format";
329
b1918034 330 $basicformat = ($displayformat == GLOSSARY_FORMAT_SIMPLE or
331 $displayformat == GLOSSARY_FORMAT_CONTINUOUS);
c76e673a 332 if ( !$basicformat ) {
333 if ( file_exists($formatfile) ) {
334 include_once($formatfile);
335 if (function_exists($functionname) ) {
336 $permissiongranted = 1;
337 }
338 }
339 } else {
340 $permissiongranted = 1;
e179048e 341 }
06d94a52 342
b1918034 343 if ( !$basicformat and $permissiongranted or $displayformat >= 2) {
1ac87c73 344 glossary_print_entry_by_format($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
c76e673a 345 } else {
b1918034 346 switch ( $displayformat ) {
1ac87c73 347 case GLOSSARY_FORMAT_SIMPLE:
348 glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
349 break;
350 case GLOSSARY_FORMAT_CONTINUOUS:
351 glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
352 break;
c76e673a 353 }
767a31c3 354 }
07842023 355 }
07842023 356}
81bdc9e9 357function glossary_print_entry_concept($entry) {
ea14e783 358 echo $entry->concept;
81bdc9e9 359}
1d9ddaaf 360
361function glossary_print_entry_definition($entry) {
362 $definition = str_ireplace($entry->concept,"<nolink>$entry->concept</nolink>",$entry->definition);
584dcac9 363/* if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
364 foreach ($aliases as $alias) {
365 echo "<small><font color=red>$alias->alias: ";
366 $definition = str_ireplace($alias->alias,"<nolink>$alias->alias</nolink>",$definition);
367 echo "<b>" . format_text($definition,2) . "</b>";
368 echo "</font></small><br>";
369 }
370 }
371*/
1d9ddaaf 372 echo format_text($definition, $entry->format);
373}
374
1ac87c73 375function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 376 $return = '';
377 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
378 foreach ($aliases as $alias) {
379 if ($alias->alias) {
380 if ($return == '') {
381 $return = '<select style="font-size:8pt">';
382 }
383 $return .= "<option>$alias->alias</option>";
384 }
385 }
386 if ($return != '') {
387 $return .= '</select>';
388// $return = "<table border=0 align=$align><tr><td>$return</td></tr></table>";
389 }
390 }
1ac87c73 391 if ($type == 'print') {
81bdc9e9 392 echo $return;
393 } else {
394 return $return;
395 }
396}
397
1ac87c73 398function glossary_print_entry_icons($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 399 global $THEME, $USER;
400
401 $importedentry = ($entry->sourceglossaryid == $glossary->id);
402 $isteacher = isteacher($course->id);
403 $ismainglossary = $glossary->mainglossary;
404
405 $return = "<font size=1>";
406
407 if (!$entry->approved) {
408 $return .= get_string("entryishidden","glossary");
409 }
410 $count = count_records("glossary_comments","entryid",$entry->id);
411 if ($count) {
412 $return .= " <a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
413 if ($count == 1) {
414 $return .= get_string("comment", "glossary");
415 } else {
416 $return .= get_string("comments", "glossary");
417 }
418 $return .= "</a>";
419 }
420 $return .= "</font>";
421 if ( $glossary->allowcomments and !isguest()) {
422 $return .= " <a title=\"" . get_string("addcomment","glossary") . "\" href=\"comment.php?id=$cm->id&eid=$entry->id\"><img src=\"comment.gif\" height=16 width=16 border=0></a> ";
423 }
424
425 if ($isteacher or $glossary->studentcanpost and $entry->userid == $USER->id) {
426 // only teachers can export entries so check it out
427 if ($isteacher and !$ismainglossary and !$importedentry) {
428 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
429 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
430
1ac87c73 431 $return .= " <a title=\"" . get_string("exporttomainglossary","glossary") . "\" href=\"exportentry.php?id=$cm->id&entry=$entry->id&mode=$mode&hook=$hook\"><img src=\"export.gif\" height=11 width=11 border=0></a> ";
81bdc9e9 432
433 }
434 }
435
436 if ( $entry->sourceglossaryid ) {
437 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
438 } else {
439 $icon = "../../pix/t/delete.gif";
440 }
441
442 // Exported entries can be updated/deleted only by teachers in the main glossary
443 if ( !$importedentry and ($isteacher or !$ismainglossary) ) {
1ac87c73 444 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
81bdc9e9 445 $return .= $icon;
446 $return .= "\" height=11 width=11 border=0></a> ";
447
1ac87c73 448 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a>";
81bdc9e9 449 } elseif ( $importedentry ) {
450 $return .= " <font size=-1>" . get_string("exportedentry","glossary") . "</font>";
451 }
452 }
453 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
1ac87c73 454 if ($type == 'print') {
81bdc9e9 455 echo $return;
456 } else {
457 return $return;
458 }
459}
460
1ac87c73 461function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons) {
81bdc9e9 462
1ac87c73 463 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,"html");
464 $icons = "";
465 if ( $printicons ) {
466 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
467 }
81bdc9e9 468 if ( $aliases ) {
469 echo '<table border="0" width="100%" align="center"><tr>' .
470 '<td align="right" width="50%" valign=top><font size=1>' .
471 get_string("aliases","glossary") . ': ' . $aliases . '</td>' .
472 '<td align=right width="50%" valign=top>'.
473 $icons .
474 '</td></tr></table>';
475 } else {
476 echo "<p align=right>$icons";
477 }
478}
479
4f4ca7b5 480function glossary_print_entry_attachment($entry,$format=NULL,$align="right") {
1d9ddaaf 481/// valid format values: html : Return the HTML link for the attachment as an icon
482/// text : Return the HTML link for tha attachment as text
483/// blank : Print the output to the screen
484 if ($entry->attachment) {
485 $glossary = get_record("glossary","id",$entry->glossaryid);
486 $entry->course = $glossary->course; //used inside print_attachment
487 echo "<table border=0 align=$align><tr><td>";
488 echo glossary_print_attachments($entry,$format,$align);
489 echo "</td></tr></table>";
490 }
491}
492
1ac87c73 493function glossary_print_entry_approval($cm, $entry, $mode) {
494 if ( $mode == 'approval' and !$entry->approved ) {
495 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&eid=$entry->id&mode=$mode\"><IMG align=\"right\" src=\"check.gif\" border=0 width=\"34\" height=\"34\"></a>";
1d9ddaaf 496 }
497}
07842023 498
1ac87c73 499function glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1) {
07842023 500 global $THEME, $USER;
501
502 $colour = $THEME->cellheading2;
503
504 echo "\n<TR>";
1d9ddaaf 505 echo "<TD WIDTH=100% class=\"generalbox\" valign=\"top\" BGCOLOR=\"#FFFFFF\">";
1ac87c73 506 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 507 glossary_print_entry_attachment($entry,"html","right");
508 echo "<b>";
509 glossary_print_entry_concept($entry);
510 echo ":</b> ";
511 glossary_print_entry_definition($entry);
1ac87c73 512 glossary_print_entry_lower_section($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
07842023 513 echo "</td>";
514 echo "</TR>";
515}
516
1ac87c73 517function glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1) {
767a31c3 518 global $THEME, $USER;
519 if ($entry) {
1ac87c73 520 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 521 glossary_print_entry_attachment($entry,"html","right");
522 glossary_print_entry_concept($entry);
523 echo " ";
1ac87c73 524
1d9ddaaf 525 glossary_print_entry_definition($entry);
1ac87c73 526 if ( $printicons ) {
527 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
528 echo "($icons)";
529 }
ed0201dd 530 }
07842023 531}
532
533function glossary_search_entries($searchterms, $glossary, $includedefinition) {
534/// Returns a list of entries found using an array of search terms
535/// eg word +word -word
536///
537
538 global $CFG;
539
540 if (!isteacher($glossary->course)) {
541 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 542 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 543 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
544 } else {
545
546 $onlyvisible = "";
547 $onlyvisibletable = "";
548 }
549
550 /// Some differences in syntax for PostgreSQL
551 if ($CFG->dbtype == "postgres7") {
552 $LIKE = "ILIKE"; // case-insensitive
553 $NOTLIKE = "NOT ILIKE"; // case-insensitive
554 $REGEXP = "~*";
555 $NOTREGEXP = "!~*";
556 } else {
557 $LIKE = "LIKE";
558 $NOTLIKE = "NOT LIKE";
559 $REGEXP = "REGEXP";
560 $NOTREGEXP = "NOT REGEXP";
561 }
562
563 $conceptsearch = "";
564 $definitionsearch = "";
565
566
567 foreach ($searchterms as $searchterm) {
07842023 568 if ($conceptsearch) {
569 $conceptsearch.= " OR ";
570 }
571 if ($definitionsearch) {
572 $definitionsearch.= " OR ";
573 }
574
575 if (substr($searchterm,0,1) == "+") {
576 $searchterm = substr($searchterm,1);
577 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
578 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
579 } else if (substr($searchterm,0,1) == "-") {
580 $searchterm = substr($searchterm,1);
581 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
582 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
583 } else {
584 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
585 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
586 }
587 }
588
ed0201dd 589 if ( !$includedefinition ) {
590 $definitionsearch = "0";
591 }
07842023 592
593 $selectsql = "{$CFG->prefix}glossary_entries e,
594 {$CFG->prefix}glossary g $onlyvisibletable
595 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 596 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c76e673a 597 AND g.id = $glossary->id AND e.approved != 0";
07842023 598
4d4c38f3 599 return get_records_sql("SELECT e.*
600 FROM $selectsql ORDER BY e.concept ASC");
07842023 601}
602
e179048e 603function glossary_file_area_name($entry) {
604// Creates a directory file name, suitable for make_upload_directory()
605 global $CFG;
606
607 return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
608}
609
610function glossary_file_area($entry) {
611 return make_upload_directory( glossary_file_area_name($entry) );
612}
613
614function glossary_delete_old_attachments($entry, $exception="") {
615// Deletes all the user files in the attachments area for a entry
616// EXCEPT for any file named $exception
617
618 if ($basedir = glossary_file_area($entry)) {
619 if ($files = get_directory_list($basedir)) {
620 foreach ($files as $file) {
621 if ($file != $exception) {
622 unlink("$basedir/$file");
623// notify("Existing file '$file' has been deleted!");
624 }
625 }
626 }
627 if (!$exception) { // Delete directory as well, if empty
628 rmdir("$basedir");
629 }
630 }
631}
632
633function glossary_copy_attachments($entry, $newentry) {
634/// Given a entry object that is being copied to glossaryid,
635/// this function checks that entry
636/// for attachments, and if any are found, these are
637/// copied to the new glossary directory.
638
639 global $CFG;
640
641 $return = true;
642
643 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
644 foreach ($entries as $curentry) {
645 $oldentry->id = $entry->id;
646 $oldentry->course = $entry->course;
647 $oldentry->glossaryid = $curentry->glossaryid;
648 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
649 if (is_dir($oldentrydir)) {
650
651 $newentrydir = glossary_file_area($newentry);
652 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
653 $return = false;
654 }
655 }
656 }
657 }
658 return $return;
659}
660
661function glossary_move_attachments($entry, $glossaryid) {
662/// Given a entry object that is being moved to glossaryid,
663/// this function checks that entry
664/// for attachments, and if any are found, these are
665/// moved to the new glossary directory.
666
667 global $CFG;
668
669 $return = true;
670
671 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
672 foreach ($entries as $entry) {
673 $oldentry->course = $entry->course;
674 $oldentry->glossaryid = $entry->glossaryid;
675 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
676 if (is_dir($oldentrydir)) {
677 $newentry = $oldentry;
678 $newentry->glossaryid = $glossaryid;
679 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
680 if (! @rename($oldentrydir, $newentrydir)) {
681 $return = false;
682 }
683 }
684 }
685 }
686 return $return;
687}
688
689function glossary_add_attachment($entry, $newfile) {
690// $entry is a full entry record, including course and glossary
691// $newfile is a full upload array from $_FILES
692// If successful, this function returns the name of the file
693
694 global $CFG;
695
696 if (empty($newfile['name'])) {
697 return "";
698 }
699
700 $newfile_name = clean_filename($newfile['name']);
701
702 if (valid_uploaded_file($newfile)) {
703 if (! $newfile_name) {
704 notify("This file had a wierd filename and couldn't be uploaded");
705
706 } else if (! $dir = glossary_file_area($entry)) {
707 notify("Attachment could not be stored");
708 $newfile_name = "";
709
710 } else {
711 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
712 chmod("$dir/$newfile_name", $CFG->directorypermissions);
713 glossary_delete_old_attachments($entry, $newfile_name);
714 } else {
715 notify("An error happened while saving the file on the server");
716 $newfile_name = "";
717 }
718 }
719 } else {
720 $newfile_name = "";
721 }
722
723 return $newfile_name;
724}
725
de53b9ac 726function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 727// if return=html, then return a html string.
728// if return=text, then return a text-only string.
729// otherwise, print HTML for non-images, and return image HTML
b764feaa 730// if attachment is an image, $align set its aligment.
e179048e 731 global $CFG;
b764feaa 732
733 $newentry = $entry;
734 if ( $newentry->sourceglossaryid ) {
735 $newentry->glossaryid = $newentry->sourceglossaryid;
736 }
e179048e 737
b764feaa 738 $filearea = glossary_file_area_name($newentry);
e179048e 739
740 $imagereturn = "";
741 $output = "";
742
b764feaa 743 if ($basedir = glossary_file_area($newentry)) {
e179048e 744 if ($files = get_directory_list($basedir)) {
745 $strattachment = get_string("attachment", "glossary");
746 $strpopupwindow = get_string("popupwindow");
747 foreach ($files as $file) {
748 $icon = mimeinfo("icon", $file);
749 if ($CFG->slasharguments) {
750 $ffurl = "file.php/$filearea/$file";
751 } else {
752 $ffurl = "file.php?file=/$filearea/$file";
753 }
754 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
755
756 if ($return == "html") {
757 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
758 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
759 } else if ($return == "text") {
760 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
761
762 } else {
763 if ($icon == "image.gif") { // Image attachments don't get printed as links
de53b9ac 764 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\" align=$align>";
e179048e 765 } else {
766 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
767 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
768 echo "<br />";
769 }
770 }
771 }
772 }
773 }
774
775 if ($return) {
776 return $output;
777 }
778
779 return $imagereturn;
780}
781
7dd88447 782function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 783
784if ( !$tTHEME ) {
785 global $THEME;
786 $tTHEME = $THEME;
787}
788
7dd88447 789$tablecolor = $tTHEME->TabTableBGColor;
790$currenttabcolor = $tTHEME->ActiveTabColor;
791$tabcolor = $tTHEME->InactiveTabColor;
792$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 793
7dd88447 794$tablewidth = $tTHEME->TabTableWidth;
795$tabsperrow = $tTHEME->TabsPerRow;
796$tabseparation = $tTHEME->TabSeparation;
06d94a52 797
7dd88447 798$tabs = count($data);
799$tabwidth = (int) (100 / $tabsperrow);
06d94a52 800
7dd88447 801$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
802
803$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 804
805?>
806 <center>
7dd88447 807 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 808 <tr>
809 <td width="100%">
810
811 <table border="0" cellpadding="0" cellspacing="0" width="100%">
812
ad58adac 813<?php
7dd88447 814$tabproccessed = 0;
815for ($row = 0; $row < $numrows; $row++) {
06d94a52 816 echo "<tr>\n";
7dd88447 817 if ( $row != $currentrow ) {
818 for ($col = 0; $col < $tabsperrow; $col++) {
819 if ( $tabproccessed < $tabs ) {
ad58adac 820 if ( $col == 0 ) {
7dd88447 821 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 822 }
7dd88447 823 if ($tabproccessed == $currenttab) {
824 $currentcolor = $currenttabcolor;
e94c179a 825 $currentstyle = 'generaltabselected';
06d94a52 826 } else {
7dd88447 827 $currentcolor = $tabcolor;
e94c179a 828 $currentstyle = 'generaltab';
06d94a52 829 }
e94c179a 830 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 831 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
832 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 833 }
7dd88447 834
ed0201dd 835 if ( !$data[$tabproccessed]->link ) {
836 echo "<font color=\"$inactivefontcolor\">";
837 }
7dd88447 838 echo $data[$tabproccessed]->caption;
ed0201dd 839 if ( !$data[$tabproccessed]->link ) {
840 echo "</font>";
841 }
7dd88447 842
843 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 844 echo "</a>";
845 }
846 echo "</b></td>";
847
7dd88447 848 if ( $col < $tabsperrow ) {
849 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 850 }
06d94a52 851 } else {
7dd88447 852 $currentcolor = "";
06d94a52 853 }
7dd88447 854 $tabproccessed++;
06d94a52 855 }
856 } else {
7dd88447 857 $firsttabincurrentrow = $tabproccessed;
858 $tabproccessed += $tabsperrow;
06d94a52 859 }
7dd88447 860 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 861}
862 echo "<tr>\n";
7dd88447 863 $tabproccessed = $firsttabincurrentrow;
864 for ($col = 0; $col < $tabsperrow; $col++) {
865 if ( $tabproccessed < $tabs ) {
ad58adac 866 if ( $col == 0 ) {
7dd88447 867 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 868 }
7dd88447 869 if ($tabproccessed == $currenttab) {
870 $currentcolor = $currenttabcolor;
e8ff59d6 871 $currentstyle = 'generaltabselected';
06d94a52 872 } else {
7dd88447 873 $currentcolor = $tabcolor;
e8ff59d6 874 $currentstyle = 'generaltab';
06d94a52 875 }
e8ff59d6 876 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 877 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
878 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 879 }
7dd88447 880
ed0201dd 881 if ( !$data[$tabproccessed]->link ) {
882 echo "<font color=\"$inactivefontcolor\">";
883 }
7dd88447 884 echo $data[$tabproccessed]->caption;
ed0201dd 885 if ( !$data[$tabproccessed]->link ) {
886 echo "</font>";
887 }
7dd88447 888
889 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 890 echo "</a>";
891 }
892 echo "</b></td>";
893
7dd88447 894 if ($col < $tabsperrow) {
895 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 896 }
06d94a52 897 } else {
7dd88447 898 if ($numrows > 1) {
899 $currentcolor = $tabcolor;
06d94a52 900 } else {
7dd88447 901 $currentcolor = "";
06d94a52 902 }
7dd88447 903 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 904 echo "</td>";
905
7dd88447 906 $col = $tabsperrow;
06d94a52 907 }
7dd88447 908 $tabproccessed++;
06d94a52 909 }
910 echo "</tr>\n";
911 ?>
912
913 </table>
914 </td>
915 </tr>
916 <tr>
7dd88447 917 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr></td>
06d94a52 918 </tr>
919 <tr>
7dd88447 920 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 921 <center>
ad58adac 922<?php
06d94a52 923}
924
ad58adac 925function glossary_print_tabbed_table_end() {
06d94a52 926 echo "</center><p></td></tr></table></center>";
927}
928
1ac87c73 929function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
c76e673a 930 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c197e607 931 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 932 }
1ac87c73 933 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 934
c4a35419 935 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 936
1ac87c73 937 glossary_print_all_links($cm, $glossary, $mode, $hook);
c76e673a 938
1ac87c73 939 glossary_print_sorting_links($cm, $mode, $sortkey, $sortorder);
c76e673a 940}
941
1ac87c73 942function glossary_print_addentry_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
ea14e783 943 echo '<center>' . get_string("explainaddentry","glossary") . '<p>';
944}
945
1ac87c73 946function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 947 echo '<center>' . get_string("explainimport","glossary") . '<p>';
948}
949
1ac87c73 950function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 951 echo '<center>' . get_string("explainexport","glossary") . '<p>';
952}
953
1ac87c73 954function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
955 if ( $mode != 'date' ) {
c197e607 956 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
957 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
958 }
c76e673a 959
1ac87c73 960 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 961
c4a35419 962 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 963
1ac87c73 964 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 965 } else {
1ac87c73 966 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
967 }
968}
969
970function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
971 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c4a35419 972 echo '<center>' . get_string("explainalphabet","glossary") . '<br \>';
c197e607 973 }
1ac87c73 974
c4a35419 975 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
976 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 977 glossary_print_all_links($cm, $glossary, $mode, $hook);
c4a35419 978// echo "<br />";
c76e673a 979}
980
1ac87c73 981function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
c76e673a 982global $CFG, $THEME;
c197e607 983 echo '<table border=0 width=100%>';
984 echo '<tr>';
c76e673a 985
c197e607 986 echo '<td align=center width=20%>';
c76e673a 987 if ( isteacher($course->id) ) {
988 $options['id'] = $cm->id;
1ac87c73 989 $options['mode'] = 'cat';
990 $options['hook'] = $hook;
c76e673a 991 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
992 }
c197e607 993 echo '</td>';
c76e673a 994
c197e607 995 echo '<td align=center width=60%>';
996 echo '<b>';
c76e673a 997
998 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
999 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1000
c76e673a 1001 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1002 $selected = '';
c76e673a 1003 if ( $categories ) {
1004 foreach ($categories as $currentcategory) {
1005 $url = $currentcategory->id;
1006 if ( $category ) {
1007 if ($currentcategory->id == $category->id) {
1008 $selected = $url;
1009 }
1010 }
1011 $menu[$url] = $currentcategory->name;
1012 }
1013 }
1014 if ( !$selected ) {
1015 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1016 }
1017
1018 if ( $category ) {
1019 echo $category->name;
1020 } else {
1ac87c73 1021 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1022
1023 echo get_string("entrieswithoutcategory","glossary");
1024 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1025
1ac87c73 1026 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1027
1028 echo get_string("allcategories","glossary");
1029 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1030
1031 }
1032 }
c197e607 1033 echo '</b></td>';
1034 echo '<td align=center width=20%>';
c76e673a 1035
1ac87c73 1036 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=cat&hook=", $menu, "catmenu", $selected, "",
c76e673a 1037 "", "", false);
677038ee 1038
c197e607 1039 echo '</td>';
1040 echo '</tr>';
c76e673a 1041
c197e607 1042 echo '</table>';
c76e673a 1043}
1044
1ac87c73 1045function glossary_print_all_links($cm, $glossary, $mode, $hook) {
c76e673a 1046global $CFG;
1047 if ( $glossary->showall and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
1048 $strallentries = get_string("allentries", "glossary");
1ac87c73 1049 if ( $hook == 'ALL' ) {
c76e673a 1050 echo "<b>$strallentries</b>";
1051 } else {
1052 $strexplainall = strip_tags(get_string("explainall","glossary"));
1ac87c73 1053 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=ALL\">$strallentries</a>";
c76e673a 1054 }
1055 }
1056}
1057
1ac87c73 1058function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1059global $CFG;
1060 if ( $glossary->showspecial and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
1061 $strspecial = get_string("special", "glossary");
1ac87c73 1062 if ( $hook == 'SPECIAL' ) {
677038ee 1063 echo "<b>$strspecial</b> | ";
1064 } else {
1065 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1ac87c73 1066 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=SPECIAL\">$strspecial</a> | ";
677038ee 1067 }
914cb260 1068 }
c76e673a 1069}
677038ee 1070
c4a35419 1071function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1072global $CFG;
1073 if ( $glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
c4274149 1074 $alphabet = explode(",", get_string("alphabet"));
677038ee 1075 $letters_by_line = 14;
1076 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1077 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1078 echo "<b>$alphabet[$i]</b>";
1079 } else {
c4a35419 1080 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=$alphabet[$i]&sortkey=$sortkey&sortorder=$sortorder\">$alphabet[$i]</a>";
677038ee 1081 }
1082 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1083 echo ' | ';
677038ee 1084 } else {
c197e607 1085 echo '<br>';
677038ee 1086 }
767a31c3 1087 }
677038ee 1088 }
c76e673a 1089}
1090
1ac87c73 1091function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1092global $CFG;
677038ee 1093
c4a35419 1094 $asc = get_string("ascending","glossary");
1095 $desc = get_string("descending","glossary");
1096 $bopen = '<b>';
1097 $bclose = '</b>';
1098
c197e607 1099 $neworder = '';
677038ee 1100 if ( $sortorder ) {
c197e607 1101 if ( $sortorder == 'asc' ) {
1102 $neworder = '&sortorder=desc';
c4a35419 1103 $newordertitle = $desc;
677038ee 1104 } else {
c197e607 1105 $neworder = '&sortorder=asc';
c4a35419 1106 $newordertitle = $asc;
677038ee 1107 }
1108 $icon = " <img src=\"$sortorder.gif\" border=0 width=16 height=16>";
1109 } else {
c4a35419 1110 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1111 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1112 $icon = "";
c4a35419 1113 $newordertitle = $asc;
677038ee 1114 } else {
c4a35419 1115 $newordertitle = $desc;
c197e607 1116 $neworder = '&sortorder=desc';
1117 $icon = ' <img src="asc.gif" border=0 width=16 height=16>';
677038ee 1118 }
1119 }
c4a35419 1120 $ficon = '';
1121 $fneworder = '';
1122 $fbtag = '';
1123 $fendbtag = '';
1124
1125 $sicon = '';
1126 $sneworder = '';
1127 $stag = '';
1128 $sendbtag = '';
1129
1130 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1131 $ficon = $icon;
1132 $fneworder = $neworder;
1133 $fordertitle = $newordertitle;
1134 $sordertitle = $asc;
1135 $fbtag = $bopen;
1136 $fendbtag = $bclose;
1137 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1138 $sicon = $icon;
1139 $sneworder = $neworder;
1140 $fordertitle = $asc;
1141 $sordertitle = $newordertitle;
1142 $sbtag = $bopen;
1143 $sendbtag = $bclose;
677038ee 1144 } else {
c4a35419 1145 $fordertitle = $asc;
1146 $sordertitle = $asc;
677038ee 1147 }
c4a35419 1148
1149 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1150 $forder = 'CREATION';
1151 $sorder = 'UPDATE';
1152 $fsort = get_string("sortbycreation", "glossary");
1153 $ssort = get_string("sortbylastupdate", "glossary");
1154
1155 $sort = get_string("sortchronogically", "glossary");
1156 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1157 $forder = 'FIRSTNAME';
1158 $sorder = 'LASTNAME';
1159 $fsort = get_string("firstname");
1160 $ssort = get_string("lastname");
1161
1162 $sort = get_string("sortby", "glossary");
1163 }
1164
1165 echo "<br>$sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$sorder$sneworder&mode=$mode\">$ssort$sicon</a>$sendbtag | ".
1166 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$forder$fneworder&mode=$mode\">$fsort$ficon</a>$fendbtag<br \>";
fb443f1a 1167}
ad58adac 1168
1169function glossary_sort_entries ( $entry0, $entry1 ) {
1170 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
1171 return -1;
1172 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
1173 return 1;
1174 } else {
1175 return 0;
1176 }
1177}
1178
ed0201dd 1179function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1180 global $THEME, $CFG, $USER;
1181
1d9ddaaf 1182 $colour = $THEME->cellheading2;
ed0201dd 1183
1184 $user = get_record("user", "id", $comment->userid);
1185 $strby = get_string("writtenby","glossary");
1186
1187 echo "<table class=\"generalbox\" BORDER=1 CELLSPACING=0 valign=top cellpadding=0 width=70% border=0><tr><td>";
1188
1189 echo "\n<TABLE width=\"100%\" BORDER=0 CELLSPACING=0 valign=top cellpadding=5><tr>";
1190
1191 echo "\n<TD BGCOLOR=\"$THEME->cellheading\" WIDTH=25% VALIGN=TOP align=right >";
1192 print_user_picture($user->id, $course->id, $user->picture);
1193 echo "<br><FONT SIZE=2>$strby $user->firstname $user->lastname</font>";
1194 echo "<br><FONT SIZE=1>(".get_string("lastedited").": ".userdate($comment->timemodified).")</FONT></small><br>";
1195 echo "</TD>";
c197e607 1196
1d9ddaaf 1197 echo "<TD valign=top WIDTH=75% BGCOLOR=\"$THEME->cellcontent\">";
ed0201dd 1198 if ($comment) {
c197e607 1199 echo format_text($comment->comment, $comment->format);
ed0201dd 1200 } else {
c197e607 1201 echo "<center>";
ed0201dd 1202 print_string("nocomment", "glossary");
c197e607 1203 echo "</center>";
ed0201dd 1204 }
1205
1206 echo "<p align=right>";
c197e607 1207 if ( (time() - $comment->timemodified < $CFG->maxeditingtime and $USER->id == $comment->userid) or isteacher($course->id) ) {
ed0201dd 1208 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=edit\"><img alt=\"" . get_string("edit") . "\" src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a> ";
c197e607 1209 }
1210 if ( $USER->id == $comment->userid or isteacher($course->id) ) {
ed0201dd 1211 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=delete\"><img alt=\"" . get_string("delete") . "\" src=\"../../pix/t/delete.gif\" height=11 width=11 border=0></a>";
c197e607 1212 }
ed0201dd 1213 echo "</td>";
c197e607 1214
ed0201dd 1215 echo "</tr></TABLE>\n";
1216
1217 echo "</td></tr></table>";
1218}
1219
b1918034 1220function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
1d9ddaaf 1221 global $THEME, $USER;
cca6f7f1 1222
1d9ddaaf 1223 $colour = $THEME->cellheading2;
cca6f7f1 1224
1d9ddaaf 1225 echo "\n<center><table width=95% border=0><tr>";
1226 echo "<td width=100%\">";
1227 if ( $entries ) {
1228 foreach ( $entries as $entry ) {
1229 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1230 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1231 }
1d9ddaaf 1232 if (! $course = get_record("course", "id", $glossary->course)) {
1233 error("Glossary is misconfigured - don't know what course it's from");
1234 }
0de786f7 1235 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1236 error("Glossary is misconfigured - don't know what course module it is ");
1237 }
584dcac9 1238 $dp = $displayformat;
1239 // Hard-coded until the Display formats manager is done.
1240 if ( $dp == -1 and $glossary->displayformat == 6 ) {
1241 $dp = 2;
1242 }
1243 glossary_print_entry($course, $cm, $glossary, $entry, "","",0,$dp);
cca6f7f1 1244 }
cca6f7f1 1245 }
1d9ddaaf 1246 echo "</td>";
1247 echo "</tr></table></center>";
1248}
4f4ca7b5 1249
1ac87c73 1250function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
4f4ca7b5 1251global $CFG;
1252 glossary_check_moddata_dir($glossary);
1253 $h = glossary_open_xml($glossary);
1254
1255 $status = fwrite ($h,glossary_start_tag("INFO",1,true));
1256 fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
1257 fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
1258 fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost));
1259 fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
1260 fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
1261 fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
1262 fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
1263 fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
1264 fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
1265 fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
1266 fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
1ac87c73 1267 fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
4f4ca7b5 1268
748b1932 1269 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1270 $status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
1271 foreach ($entries as $entry) {
046a797c 1272 $permissiongranted = 1;
1ac87c73 1273 if ( $hook ) {
1274 switch ( $hook ) {
046a797c 1275 case "ALL":
1276 case "SPECIAL":
1277 break;
1278 default:
1ac87c73 1279 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1280 break;
1281 }
1282 }
1ac87c73 1283 if ( $hook ) {
1284 switch ( $hook ) {
046a797c 1285 case GLOSSARY_SHOW_ALL_CATEGORIES:
1286 break;
1287 case GLOSSARY_SHOW_NOT_CATEGORISED:
1288 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1289 break;
1290 default:
1ac87c73 1291 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1292 break;
1293 }
1294 }
1295 if ( $entry->approved and $permissiongranted ) {
748b1932 1296 $status = fwrite($h,glossary_start_tag("ENTRY",3,true));
1297 fwrite($h,glossary_full_tag("CONCEPT",4,false,$entry->concept));
1298 fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
1299 fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
1300 fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
1301 fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
1302 fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
1303 fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
1304
7965be79 1305 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1306 $status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
1307 foreach ($aliases as $alias) {
1308 $status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
1309 fwrite($h,glossary_full_tag("NAME",6,false,$alias->alias));
1310 $status = fwrite($h,glossary_end_tag("ALIAS",5,true));
1311 }
00758851 1312 $status = fwrite($h,glossary_end_tag("ALIASES",4,true));
7965be79 1313 }
748b1932 1314 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1315 $status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
1316 foreach ($catentries as $catentry) {
1317 $category = get_record("glossary_categories","id",$catentry->categoryid);
1318
1319 $status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
1320 fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
1ac87c73 1321 fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
748b1932 1322 $status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
1323 }
1324 $status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
1325 }
4f4ca7b5 1326
748b1932 1327 $status =fwrite($h,glossary_end_tag("ENTRY",3,true));
4f4ca7b5 1328 }
1329 }
748b1932 1330 $status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
1331
4f4ca7b5 1332 }
748b1932 1333
1334
1335 $status =fwrite ($h,glossary_end_tag("INFO",1,true));
1336
4f4ca7b5 1337 $h = glossary_close_xml($h);
1338}
1339// Functions designed by Eloy Lafuente
1340//
1341//Function to create, open and write header of the xml file
1342function glossary_open_xml($glossary) {
1343
1344 global $CFG;
1345
1346 $status = true;
1347
1348 //Open for writing
1349
1350 $file = $CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name) ."/glossary.xml";
1351 $h = fopen($file,"w");
1352 //Writes the header
1353 $status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1354 if ($status) {
1355 $status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
1356 }
1357 if ($status) {
1358 return $h;
1359 } else {
1360 return false;
1361 }
1362}
1363
748b1932 1364function glossary_read_imported_file($file) {
1365require_once "../../lib/xmlize.php";
1366 $h = fopen($file,"r");
1367 $line = '';
1368 if ($h) {
1369 while ( !feof($h) ) {
1370 $char = fread($h,1024);
1371 $line .= $char;
1372 }
1373 fclose($h);
1374 }
1375 return xmlize($line);
1376}
4f4ca7b5 1377//Close the file
1378function glossary_close_xml($h) {
1379 $status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
1380 return fclose($h);
1381}
1382
1383//Return the xml start tag
1384function glossary_start_tag($tag,$level=0,$endline=false) {
1385 if ($endline) {
1386 $endchar = "\n";
1387 } else {
1388 $endchar = "";
1389 }
1390 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1391}
1392
1393//Return the xml end tag
1394function glossary_end_tag($tag,$level=0,$endline=true) {
1395 if ($endline) {
1396 $endchar = "\n";
1397 } else {
1398 $endchar = "";
1399 }
1400 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1401}
1402
1403//Return the start tag, the contents and the end tag
1404function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
1405 $st = glossary_start_tag($tag,$level,$endline);
1406 $co="";
1407 if ($to_utf) {
1408 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
1409 } else {
1410 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
1411 }
1412 $et = glossary_end_tag($tag,0,true);
1413 return $st.$co.$et;
1414}
1415
1416 //Function to check and create the needed moddata dir to
1417 //save all the mod backup files. We always name it moddata
1418 //to be able to restore it, but in restore we check for
1419 //$CFG->moddata !!
1420function glossary_check_moddata_dir($glossary) {
1421
1422 global $CFG;
1423
1424 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
1425 if ( $status ) {
1426 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
1427 if ( $status ) {
1428 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name),true);
1429 }
1430 }
1431 return $status;
1432}
1433
1434//Function to check if a directory exists
1435//and, optionally, create it
1436function glossary_check_dir_exists($dir,$create=false) {
1437
1438 global $CFG;
1439
1440 $status = true;
1441 if(!is_dir($dir)) {
1442 if (!$create) {
1443 $status = false;
1444 } else {
1445 umask(0000);
1446 $status = mkdir ($dir,$CFG->directorypermissions);
1447 }
1448 }
1449 return $status;
1450}
1ac87c73 1451?>