Merged from MOODLE_14_STABLE: Fix for postgres-invalid-sql (must have AS between...
[moodle.git] / mod / glossary / lib.php
CommitLineData
41905731 1<?php // $Id$
07842023 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
da9c60ec 21//Check for global configure default variables
22
23if (!isset($CFG->glossary_studentspost)) {
24 set_config("glossary_studentspost", 1); // Students can post entries.
25}
26
27if (!isset($CFG->glossary_dupentries)) {
28 set_config("glossary_dupentries", 0); // Duplicate entries are not allowed.
29}
30
31if (!isset($CFG->glossary_allowcomments)) {
32 set_config("glossary_allowcomments", 0); // Comments are not allowed.
33}
34
35if (!isset($CFG->glossary_linkbydefault)) {
36 set_config("glossary_linkbydefault", 1); // Linking entries is enabled.
37}
38
39if (!isset($CFG->glossary_defaultapproval)) {
40 set_config("glossary_defaultapproval", 1); // Entries are approved.
41}
42
43if (!isset($CFG->glossary_entbypage)) {
44 set_config("glossary_entbypage", 10); // 10 entries are showed.
45}
46
47/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
48
07842023 49function glossary_add_instance($glossary) {
50/// Given an object containing all the necessary data,
51/// (defined by the form in mod.html) this function
52/// will create a new instance and return the id number
53/// of the new instance.
54
7567ff7b 55 if (!isset($glossary->userating) || !$glossary->userating) {
63dd5fb2 56 $glossary->assessed = 0;
57 }
58
59 if (!empty($glossary->ratingtime)) {
60 $glossary->assesstimestart = make_timestamp($glossary->startyear, $glossary->startmonth, $glossary->startday,
61 $glossary->starthour, $glossary->startminute, 0);
62 $glossary->assesstimefinish = make_timestamp($glossary->finishyear, $glossary->finishmonth, $glossary->finishday,
63 $glossary->finishhour, $glossary->finishminute, 0);
64 } else {
65 $glossary->assesstimestart = 0;
66 $glossary->assesstimefinish = 0;
67 }
68
0de786f7 69 if ( !isset($glossary->globalglossary) ) {
70 $glossary->globalglossary = 0;
71 } elseif ( !isadmin() ) {
72 $glossary->globalglossary = 0;
73 }
74
07842023 75 $glossary->timecreated = time();
76 $glossary->timemodified = $glossary->timecreated;
77
78 # May have to add extra stuff in here #
79
80 return insert_record("glossary", $glossary);
81}
82
83
84function glossary_update_instance($glossary) {
85/// Given an object containing all the necessary data,
86/// (defined by the form in mod.html) this function
87/// will update an existing instance with new data.
5eaa2d34 88global $CFG;
0de786f7 89 if ( !isadmin() ) {
90 unset($glossary->globalglossary);
91 }
da5754f8 92 if (empty($glossary->globalglossary)) {
93 $glossary->globalglossary = 0;
94 }
0de786f7 95
07842023 96 $glossary->timemodified = time();
97 $glossary->id = $glossary->instance;
98
2603b5b3 99 if (!isset($glossary->userating) || !$glossary->userating) {
63dd5fb2 100 $glossary->assessed = 0;
101 }
102
103 if (!empty($glossary->ratingtime)) {
104 $glossary->assesstimestart = make_timestamp($glossary->startyear, $glossary->startmonth, $glossary->startday,
105 $glossary->starthour, $glossary->startminute, 0);
106 $glossary->assesstimefinish = make_timestamp($glossary->finishyear, $glossary->finishmonth, $glossary->finishday,
107 $glossary->finishhour, $glossary->finishminute, 0);
108 } else {
109 $glossary->assesstimestart = 0;
110 $glossary->assesstimefinish = 0;
111 }
112
5eaa2d34 113 $return = update_record("glossary", $glossary);
114 if ($return and $glossary->defaultapproval) {
115 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
116 }
07842023 117
5eaa2d34 118 return $return;
07842023 119}
120
121
122function glossary_delete_instance($id) {
123/// Given an ID of an instance of this module,
124/// this function will permanently delete the instance
125/// and any data that depends on it.
126
127 if (! $glossary = get_record("glossary", "id", "$id")) {
128 return false;
129 }
130
131 $result = true;
132
133 # Delete any dependent records here #
134
135 if (! delete_records("glossary", "id", "$glossary->id")) {
136 $result = false;
49210b90 137 } else {
138 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
139 $cats = "";
4070cddf 140 foreach ( $categories as $cat ) {
49210b90 141 $cats .= "$cat->id,";
142 }
143 $cats = substr($cats,0,-1);
144 if ($cats) {
145 delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
146 delete_records("glossary_categories", "glossaryid", $glossary->id);
147 }
148 }
149 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
150 $ents = "";
151 foreach ( $entries as $entry ) {
072f7533 152 if ( $entry->sourceglossaryid ) {
153 $entry->glossaryid = $entry->sourceglossaryid;
154 $entry->sourceglossaryid = 0;
155 update_record("glossary_entries",$entry);
156 } else {
157 $ents .= "$entry->id,";
158 }
49210b90 159 }
160 $ents = substr($ents,0,-1);
161 if ($ents) {
162 delete_records_select("glossary_comments", "entryid in ($ents)");
ea14e783 163 delete_records_select("glossary_alias", "entryid in ($ents)");
e29ad7eb 164 delete_records_select("glossary_ratings", "entryid in ($ents)");
49210b90 165 }
166 }
072f7533 167 glossary_delete_attachments($glossary);
49210b90 168 delete_records("glossary_entries", "glossaryid", "$glossary->id");
07842023 169 }
07842023 170
171 return $result;
172}
173
174function glossary_user_outline($course, $user, $mod, $glossary) {
175/// Return a small object with summary information about what a
176/// user has done with a given particular instance of this module
177/// Used for user activity reports.
178/// $return->time = the time they did it
179/// $return->info = a short text description
180
1ac87c73 181 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
182 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
183
63dd5fb2 184 $lastentry = array_pop($entries);
185 $result->time = $lastentry->timemodified;
1ac87c73 186 return $result;
187 }
188 return NULL;
189}
190
191function glossary_get_user_entries($glossaryid, $userid) {
192/// Get all the entries for a user in a glossary
193 global $CFG;
194
195 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
196 FROM {$CFG->prefix}glossary g,
197 {$CFG->prefix}glossary_entries e,
198 {$CFG->prefix}user u
199 WHERE g.id = '$glossaryid'
200 AND e.glossaryid = g.id
201 AND e.userid = '$userid'
202 AND e.userid = u.id
203 ORDER BY e.timemodified ASC");
07842023 204}
205
206function glossary_user_complete($course, $user, $mod, $glossary) {
207/// Print a detailed representation of what a user has done with
208/// a given particular instance of this module, for user activity reports.
1ac87c73 209 global $CFG;
07842023 210
1ac87c73 211 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
a359c29b 212 echo '<table width="95%" border="0"><tr><td>';
1ac87c73 213 foreach ($entries as $entry) {
214 $cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
215 glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
216 echo '<p>';
217 }
a359c29b 218 echo '</td></tr></table>';
1ac87c73 219 }
07842023 220}
221
222function glossary_print_recent_activity($course, $isteacher, $timestart) {
223/// Given a course and a time, this module should find recent activity
224/// that has occurred in glossary activities and print it out.
225/// Return true if there was output, or false is there was none.
226
227 global $CFG, $THEME;
228
229 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
230 "course = '$course->id' AND ".
231 "module = 'glossary' AND ".
c4a35419 232 "(action = 'add entry' OR ".
233 " action = 'approve entry')", "time ASC")) {
07842023 234 return false;
235 }
236
a359c29b 237 $entries = array();
238
07842023 239 foreach ($logs as $log) {
240 //Create a temp valid module structure (course,id)
1ac87c73 241 $tempmod->course = $log->course;
242 $entry = get_record("glossary_entries","id",$log->info);
243 $tempmod->id = $entry->glossaryid;
244 //Obtain the visible property from the instance
07842023 245 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 246
247 //Only if the mod is visible
c4a35419 248 if ($modvisible and $entry->approved) {
07842023 249 $entries[$log->info] = glossary_log_info($log);
250 $entries[$log->info]->time = $log->time;
d7d21c38 251 $entries[$log->info]->url = str_replace('&', '&amp;', $log->url);
07842023 252 }
07842023 253 }
254
07842023 255 $content = false;
256 if ($entries) {
257 $strftimerecent = get_string("strftimerecent");
258 $content = true;
259 print_headline(get_string("newentries", "glossary").":");
260 foreach ($entries as $entry) {
261 $date = userdate($entry->timemodified, $strftimerecent);
5b4388f3 262
263 $user = get_record("user","id",$entry->userid);
264 $fullname = fullname($user, $isteacher);
41905731 265 echo "<p><font size=\"1\">$date - $fullname<br />";
839f2456 266 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/view.php?g=$entry->glossaryid&amp;mode=entry&amp;hook=$entry->id\">";
07842023 267 echo "$entry->concept";
268 echo "</a>\"</font></p>";
269 }
270 }
271
272 return $content;
273}
274
1ac87c73 275function glossary_log_info($log) {
276 global $CFG;
277
278 return get_record_sql("SELECT e.*, u.firstname, u.lastname
279 FROM {$CFG->prefix}glossary_entries e,
280 {$CFG->prefix}user u
281 WHERE e.id = '$log->info'
282 AND u.id = '$log->userid'");
283}
284
07842023 285function glossary_cron () {
286/// Function to be run periodically according to the moodle cron
287/// This function searches for things that need to be done, such
288/// as sending out mail, toggling flags etc ...
289
290 global $CFG;
291
292 return true;
293}
294
295function glossary_grades($glossaryid) {
296/// Must return an array of grades for a given instance of this module,
297/// indexed by user. It also returns a maximum allowed grade.
63dd5fb2 298 if (!$glossary = get_record("glossary", "id", $glossaryid)) {
299 return false;
300 }
301 if (!$glossary->assessed) {
302 return false;
303 }
304 $scalemenu = make_grades_menu($glossary->scale);
305
306 $currentuser = 0;
307 $ratingsuser = array();
308
309 if ($ratings = glossary_get_user_grades($glossaryid)) {
310 foreach ($ratings as $rating) { // Ordered by user
311 if ($currentuser and $rating->userid != $currentuser) {
312 if (!empty($ratingsuser)) {
313 if ($glossary->scale < 0) {
314 $return->grades[$currentuser] = glossary_get_ratings_mean(0, $scalemenu, $ratingsuser);
315 $return->grades[$currentuser] .= "<br />".glossary_get_ratings_summary(0, $scalemenu, $ratingsuser);
316 } else {
317 $total = 0;
318 $count = 0;
319 foreach ($ratingsuser as $ra) {
320 $total += $ra;
321 $count ++;
322 }
37d543b5 323 $return->grades[$currentuser] = (string) format_float($total/$count, 2);
324 if ( count($ratingsuser) > 1 ) {
325 $return->grades[$currentuser] .= " (" . count($ratingsuser) . ")";
326 }
63dd5fb2 327 }
328 } else {
329 $return->grades[$currentuser] = "";
330 }
331 $ratingsuser = array();
332 }
333 $ratingsuser[] = $rating->rating;
334 $currentuser = $rating->userid;
335 }
336 if (!empty($ratingsuser)) {
337 if ($glossary->scale < 0) {
338 $return->grades[$currentuser] = glossary_get_ratings_mean(0, $scalemenu, $ratingsuser);
339 $return->grades[$currentuser] .= "<br />".glossary_get_ratings_summary(0, $scalemenu, $ratingsuser);
340 } else {
341 $total = 0;
342 $count = 0;
343 foreach ($ratingsuser as $ra) {
344 $total += $ra;
345 $count ++;
346 }
37d543b5 347 $return->grades[$currentuser] = (string) format_float((float)$total/(float)$count, 2);
348
349 if ( count($ratingsuser) > 1 ) {
350 $return->grades[$currentuser] .= " (" . count($ratingsuser) . ")";
351 }
63dd5fb2 352 }
353 } else {
354 $return->grades[$currentuser] = "";
355 }
356 } else {
357 $return->grades = array();
358 }
07842023 359
63dd5fb2 360 if ($glossary->scale < 0) {
361 $return->maxgrade = "";
362 } else {
363 $return->maxgrade = $glossary->scale;
364 }
07842023 365 return $return;
366}
367
05855091 368function glossary_get_participants($glossaryid) {
369//Returns the users with data in one glossary
370//(users with records in glossary_entries, students)
371
372 global $CFG;
373
374 //Get students
375 $students = get_records_sql("SELECT DISTINCT u.*
376 FROM {$CFG->prefix}user u,
377 {$CFG->prefix}glossary_entries g
378 WHERE g.glossaryid = '$glossaryid' and
379 u.id = g.userid");
380
381 //Return students array (it contains an array of unique users)
382 return ($students);
383}
07842023 384
0f1a97c2 385function glossary_scale_used ($glossaryid,$scaleid) {
386//This function returns if a scale is being used by one glossary
387
388 $return = false;
389
390 $rec = get_record("glossary","id","$glossaryid","scale","-$scaleid");
391
2127fedd 392 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 393 $return = true;
394 }
395
396 return $return;
397}
398
07842023 399//////////////////////////////////////////////////////////////////////////////////////
400/// Any other glossary functions go here. Each of them must have a name that
401/// starts with glossary_
402
a359c29b 403//This function return an array of valid glossary_formats records
404//Everytime it's called, every existing format is checked, new formats
405//are included if detected and old formats are deleted and any glossary
406//using an invalid format is updated to the default (dictionary).
407function glossary_get_available_formats() {
408
409 global $CFG;
410
411 //Get available formats (plugin) and insert (if necessary) them into glossary_formats
77495793 412 $formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
413 $pluginformats = array();
a359c29b 414 foreach ($formats as $format) {
415 //If the format file exists
416 if (file_exists($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
417 include_once($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
418 //If the function exists
419 if (function_exists('glossary_show_entry_'.$format)) {
77495793 420 //Acummulate it as a valid format
421 $pluginformats[] = $format;
a359c29b 422 //If the format doesn't exist in the table
423 if (!$rec = get_record('glossary_formats','name',$format)) {
424 //Insert the record in glossary_formats
425 $gf->name = $format;
426 $gf->popupformatname = $format;
427 $gf->visible = 1;
428 insert_record("glossary_formats",$gf);
429 }
430 }
431 }
432 }
433
434 //Delete non_existent formats from glossary_formats table
435 $formats = get_records("glossary_formats");
436 foreach ($formats as $format) {
437 $todelete = false;
77495793 438 //If the format in DB isn't a valid previously detected format then delete the record
439 if (!in_array($format->name,$pluginformats)) {
a359c29b 440 $todelete = true;
a359c29b 441 }
442
443 if ($todelete) {
444 //Delete the format
445 delete_records('glossary_formats','name',$format->name);
446 //Reasign existing glossaries to default (dictionary) format
447 if ($glossaries = get_records('glossary','displayformat',$format->name)) {
448 foreach($glossaries as $glossary) {
449 set_field('glossary','displayformat','dictionary','id',$glossary->id);
450 }
451 }
452 }
453 }
454
455 //Now everything is ready in glossary_formats table
456 $formats = get_records("glossary_formats");
457
458 return $formats;
459}
460
1ac87c73 461function glossary_debug($debug,$text,$br=1) {
462 if ( $debug ) {
41905731 463 echo '<font color="red">' . $text . '</font>';
1ac87c73 464 if ( $br ) {
a9ef4a63 465 echo '<br />';
1ac87c73 466 }
467 }
07842023 468}
469
ea14e783 470function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
07842023 471 global $CFG;
ea14e783 472 if ($pivot) {
473 $pivot .= ",";
474 }
07842023 475
ea14e783 476 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
07842023 477 FROM {$CFG->prefix}glossary_entries
478 WHERE glossaryid = '$glossaryid'
479 AND id IN ($entrylist)");
480}
359f2758 481
482function glossary_get_entries_search($concept, $courseid) {
483 global $CFG;
484
485 $conceptupper = strtoupper(trim($concept));
486
487 return get_records_sql("SELECT e.*, g.name as glossaryname
488 FROM {$CFG->prefix}glossary_entries e,
489 {$CFG->prefix}glossary g
490 WHERE e.glossaryid = g.id
491 AND ( (e.casesensitive != 0 and UPPER(concept) = '$conceptupper')
492 OR (e.casesensitive = 0 and concept = '$concept'))
493 AND (g.course = '$courseid' OR g.globalglossary = 1)
494 AND e.usedynalink != 0
495 AND g.usedynalink != 0");
496}
497
ea14e783 498function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
c76e673a 499global $CFG;
500 if ($where) {
501 $where = " and $where";
502 }
503 if ($orderby) {
504 $orderby = " ORDER BY $orderby";
505 }
ea14e783 506 if ($pivot) {
507 $pivot .= ",";
508 }
509 return get_records_sql("SELECT $pivot *
c76e673a 510 FROM {$CFG->prefix}glossary_entries
511 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
512}
513
1ac87c73 514function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
c76e673a 515global $CFG;
516 if ($where) {
517 $where = " and $where";
518 }
519 if ($orderby) {
520 $orderby = " ORDER BY $orderby";
521 }
ea14e783 522 if ($pivot) {
523 $pivot .= ",";
524 }
525 return get_records_sql("SELECT $pivot ge.*
c76e673a 526 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
63dd5fb2 527 WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
c76e673a 528 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
529}
07842023 530
ae06e00e 531function glossary_print_entry($course, $cm, $glossary, $entry, $mode="",$hook="",$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) {
914cb260 532 global $THEME, $USER, $CFG;
37d543b5 533 $return = false;
b1918034 534 if ( $displayformat < 0 ) {
535 $displayformat = $glossary->displayformat;
536 }
b620a995 537 if ($entry->approved or ($USER->id == $entry->userid) or ($mode == 'approval' and !$entry->approved) ) {
a359c29b 538 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
ae06e00e 539 if ($printview) {
540 $functionname = 'glossary_print_entry_'.$displayformat;
541 } else {
542 $functionname = 'glossary_show_entry_'.$displayformat;
543 }
a359c29b 544
545 if (file_exists($formatfile)) {
546 include_once($formatfile);
547 if (function_exists($functionname)) {
548 $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
ae06e00e 549 } else if ($printview) {
550 //If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format
551 $return = glossary_print_entry_default($entry);
c76e673a 552 }
767a31c3 553 }
07842023 554 }
a359c29b 555 return $return;
07842023 556}
ae06e00e 557 //Default (old) print format used if custom function doesn't exist in format
558function glossary_print_entry_default ($entry) {
559 echo '<b>'. strip_tags($entry->concept) . ': </b>';
560 $options->para = false;
561 $definition = format_text('<nolink>' . strip_tags($entry->definition) . '</nolink>', $entry->format,$options);
562 echo ($definition);
563 echo '<br /><br />';
564}
a359c29b 565
81bdc9e9 566function glossary_print_entry_concept($entry) {
d34b9c5f 567 $options->para = false;
e0147c9b 568 $text = format_text('<nolink>' . $entry->concept . '</nolink>', FORMAT_MOODLE, $options);
ff8352de 569 if (!empty($entry->highlight)) {
570 $text = highlight($entry->highlight, $text);
571 }
572 echo $text;
81bdc9e9 573}
1d9ddaaf 574
575function glossary_print_entry_definition($entry) {
701fff21 576
f287e69c 577 $definition = $entry->definition;
578
701fff21 579 $tags = array();
b5937aac 580 $urls = array();
581 $addrs = array();
f287e69c 582
701fff21 583 //Calculate all the strings to be no-linked
584 //First, the concept
e2ba5060 585 $term = preg_quote(trim($entry->concept),"/");
701fff21 586 $pat = '/('.$term.')/is';
587 $doNolinks[] = $pat;
588 //Now the aliases
589 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
590 foreach ($aliases as $alias) {
e2ba5060 591 $term = preg_quote(trim($alias->alias),"/");
701fff21 592 $pat = '/('.$term.')/is';
593 $doNolinks[] = $pat;
594 }
f287e69c 595 }
596
701fff21 597 //Extract all tags from definition
598 preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
f287e69c 599
701fff21 600 //Save them into tags array to use them later
601 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
602 $tags['<@'.$key.'@>'] = $value;
f287e69c 603 }
604
701fff21 605 //Take off every tag from definition
606 if ( $tags ) {
607 $definition = str_replace($tags,array_keys($tags),$definition);
f287e69c 608 }
7275671a 609
610 //Extract all URLS with protocol (http://domain.com) from definition
611 preg_match_all('/([[:space:]]|^|\(|\[)([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_urls);
612
613 //Save them into urls array to use them later
614 foreach (array_unique($list_of_urls[0]) as $key=>$value) {
615 $urls['<*'.$key.'*>'] = $value;
616 }
617
618 //Take off every url from definition
619 if ( $urls ) {
620 $definition = str_replace($urls,array_keys($urls),$definition);
621 }
622
623 //Extract all WEB ADDRESSES (www.domain.com) from definition
624 preg_match_all('/([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_addresses);
625
626 //Save them into addrs array to use them later
627 foreach (array_unique($list_of_addresses[0]) as $key=>$value) {
628 $addrs['<+'.$key.'+>'] = $value;
629 }
630
631 //Take off every addr from definition
632 if ( $addrs ) {
633 $definition = str_replace($addrs,array_keys($addrs),$definition);
634 }
635
701fff21 636 //Put doNolinks (concept + aliases) enclosed by <nolink> tag
637 $definition= preg_replace($doNolinks,'<nolink>$1</nolink>',$definition);
638
7275671a 639 //Restore addrs
640 if ( $addrs ) {
641 $definition = str_replace(array_keys($addrs),$addrs,$definition);
642 }
643
644 //Restore urls
645 if ( $urls ) {
646 $definition = str_replace(array_keys($urls),$urls,$definition);
647 }
648
701fff21 649 //Restore tags
650 if ( $tags ) {
651 $definition = str_replace(array_keys($tags),$tags,$definition);
584dcac9 652 }
ff8352de 653
3eb2f7ec 654 $options->para = false;
655 $text = format_text($definition, $entry->format,$options);
ff8352de 656 if (!empty($entry->highlight)) {
657 $text = highlight($entry->highlight, $text);
658 }
359f2758 659 if (isset($entry->footer)) { // Unparsed footer info
660 $text .= $entry->footer;
661 }
ff8352de 662 echo $text;
1d9ddaaf 663}
664
1ac87c73 665function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 666 $return = '';
667 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
668 foreach ($aliases as $alias) {
33cc423e 669 if (trim($alias->alias)) {
81bdc9e9 670 if ($return == '') {
671 $return = '<select style="font-size:8pt">';
672 }
673 $return .= "<option>$alias->alias</option>";
674 }
675 }
676 if ($return != '') {
677 $return .= '</select>';
41905731 678// $return = "<table border=\"0\" align=$align><tr><td>$return</td></tr></table>";
81bdc9e9 679 }
680 }
1ac87c73 681 if ($type == 'print') {
81bdc9e9 682 echo $return;
683 } else {
684 return $return;
685 }
686}
687
1ac87c73 688function glossary_print_entry_icons($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
302130de 689 global $THEME, $USER, $CFG;
81bdc9e9 690
691 $importedentry = ($entry->sourceglossaryid == $glossary->id);
692 $isteacher = isteacher($course->id);
693 $ismainglossary = $glossary->mainglossary;
694
41905731 695 $return = "<font size=\"1\">";
81bdc9e9 696 if (!$entry->approved) {
697 $return .= get_string("entryishidden","glossary");
698 }
66d7ae88 699 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
700
701 $return .= "</font> ";
a0d1e2bb 702
703
944d82d4 704 if ( ($glossary->allowcomments && !isguest()) || isteacher($glossary->course)) {
839f2456 705 $return .= " <a title=\"" . get_string("addcomment","glossary") . "\" href=\"comment.php?id=$cm->id&amp;eid=$entry->id\"><img src=\"comment.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
81bdc9e9 706 }
707
59c265b3 708 if ($isteacher or ($glossary->studentcanpost and $entry->userid == $USER->id)) {
81bdc9e9 709 // only teachers can export entries so check it out
710 if ($isteacher and !$ismainglossary and !$importedentry) {
711 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
712 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
713
839f2456 714 $return .= " <a title=\"" . get_string("exporttomainglossary","glossary") . "\" href=\"exportentry.php?id=$cm->id&amp;entry=$entry->id&amp;mode=$mode&amp;hook=$hook\"><img src=\"export.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
81bdc9e9 715
716 }
717 }
718
719 if ( $entry->sourceglossaryid ) {
720 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
721 } else {
096b5432 722 $icon = "$CFG->pixpath/t/delete.gif";
81bdc9e9 723 }
724
ff1e2621 725 //Decide if an entry is editable:
726 // -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
727 // -The user is teacher or he is a student with time permissions (edit period or editalways defined).
516c7276 728 $ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
3a512f86 729 if ( !$importedentry and ($isteacher or ($entry->userid == $USER->id and $ineditperiod))) {
839f2456 730 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&amp;mode=delete&amp;entry=$entry->id&amp;prevmode=$mode&amp;hook=$hook\"><img src=\"";
81bdc9e9 731 $return .= $icon;
839f2456 732 $return .= "\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
81bdc9e9 733
839f2456 734 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&amp;e=$entry->id&amp;mode=$mode&amp;hook=$hook\"><img src=\"$CFG->pixpath/t/edit.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a>";
81bdc9e9 735 } elseif ( $importedentry ) {
41905731 736 $return .= " <font size=\"-1\">" . get_string("exportedentry","glossary") . "</font>";
81bdc9e9 737 }
738 }
739 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
1ac87c73 740 if ($type == 'print') {
81bdc9e9 741 echo $return;
742 } else {
743 return $return;
a0d1e2bb 744 }
745}
746
747function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
748 $return = '';
749
750 $count = count_records("glossary_comments","entryid",$entry->id);
751 if ($count) {
41905731 752 $return = "<font size=\"1\">";
839f2456 753 $return .= "<a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$count ";
a0d1e2bb 754 if ($count == 1) {
755 $return .= get_string("comment", "glossary");
756 } else {
757 $return .= get_string("comments", "glossary");
758 }
759 $return .= "</a>";
760 $return .= "</font>";
761 }
762
763 if ($type == 'print') {
764 echo $return;
765 } else {
766 return $return;
81bdc9e9 767 }
768}
769
26983d03 770function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
81bdc9e9 771
26983d03 772 if ($aliases) {
773 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,"html");
774 }
1ac87c73 775 $icons = "";
26983d03 776 $return = "";
1ac87c73 777 if ( $printicons ) {
778 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
779 }
a359c29b 780 if ($aliases || $printicons || $ratings) {
781 echo '<table border="0" width="100%" align="center">';
782 if ( $aliases ) {
41905731 783 echo '<tr><td align="center" valign="top"><font size="1">' .
a359c29b 784 get_string("aliases","glossary") . ': ' . $aliases . '</td></tr>';
785 }
786 if ($icons) {
41905731 787 echo '<tr><td align="right" valign="top">'. $icons . '</td></tr>';
a359c29b 788 }
789 if ($ratings) {
41905731 790 echo '<tr><td align="right" valign="top">';
a359c29b 791 $return = glossary_print_entry_ratings($course, $entry, $ratings);
792 echo '</td></tr>';
793 }
794 echo '</table>';
81bdc9e9 795 }
a359c29b 796 return $return;
81bdc9e9 797}
798
77495793 799function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
1d9ddaaf 800/// valid format values: html : Return the HTML link for the attachment as an icon
801/// text : Return the HTML link for tha attachment as text
802/// blank : Print the output to the screen
803 if ($entry->attachment) {
804 $glossary = get_record("glossary","id",$entry->glossaryid);
805 $entry->course = $glossary->course; //used inside print_attachment
77495793 806 if ($insidetable) {
41905731 807 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap=\"nowrap\">\n";
77495793 808 }
1d9ddaaf 809 echo glossary_print_attachments($entry,$format,$align);
77495793 810 if ($insidetable) {
811 echo "</td></tr></table>\n";
812 }
1d9ddaaf 813 }
814}
815
77495793 816function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
1ac87c73 817 if ( $mode == 'approval' and !$entry->approved ) {
77495793 818 if ($insidetable) {
819 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\">\n";
820 }
839f2456 821 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&amp;eid=$entry->id&amp;mode=$mode\"><img align=\"$align\" src=\"check.gif\" border=\"0\" width=\"34\" height=\"34\" alt=\"\" /></a>\n";
77495793 822 if ($insidetable) {
823 echo "</td></tr></table>\n";
824 }
1d9ddaaf 825 }
826}
07842023 827
c80828fe 828function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {
829// It returns all entries from all glossaries that matches the specified criteria
830// within a given $course. It performs an $extended search if necessary.
831// It restrict the search to only one $glossary if the $glossary parameter is set.
07842023 832
833 global $CFG;
c80828fe 834 if ( !$glossary ) {
835 if ( $glossaries = get_records("glossary", "course", $course->id) ) {
836 $glos = "";
837 foreach ( $glossaries as $glossary ) {
838 $glos .= "$glossary->id,";
839 }
840 $glos = substr($ents,0,-1);
841 }
842 } else {
843 $glos = $glossary->id;
844 }
845
07842023 846 if (!isteacher($glossary->course)) {
847 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 848 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 849 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
850 } else {
851
852 $onlyvisible = "";
853 $onlyvisibletable = "";
854 }
855
63dd5fb2 856 /// Some differences in syntax for entrygreSQL
a5e48d96 857 switch ($CFG->dbtype) {
858 case 'postgres7':
07842023 859 $LIKE = "ILIKE"; // case-insensitive
860 $NOTLIKE = "NOT ILIKE"; // case-insensitive
861 $REGEXP = "~*";
862 $NOTREGEXP = "!~*";
a5e48d96 863 break;
864 case 'mysql':
865 default:
07842023 866 $LIKE = "LIKE";
867 $NOTLIKE = "NOT LIKE";
868 $REGEXP = "REGEXP";
869 $NOTREGEXP = "NOT REGEXP";
a5e48d96 870 break;
871 }
07842023 872
873 $conceptsearch = "";
874 $definitionsearch = "";
875
876
877 foreach ($searchterms as $searchterm) {
07842023 878 if ($conceptsearch) {
879 $conceptsearch.= " OR ";
880 }
881 if ($definitionsearch) {
882 $definitionsearch.= " OR ";
883 }
884
885 if (substr($searchterm,0,1) == "+") {
886 $searchterm = substr($searchterm,1);
887 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
888 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
889 } else if (substr($searchterm,0,1) == "-") {
890 $searchterm = substr($searchterm,1);
891 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
892 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
893 } else {
894 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
895 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
896 }
897 }
898
c80828fe 899 if ( !$extended ) {
ed0201dd 900 $definitionsearch = "0";
901 }
07842023 902
903 $selectsql = "{$CFG->prefix}glossary_entries e,
904 {$CFG->prefix}glossary g $onlyvisibletable
905 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 906 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c80828fe 907 AND g.id IN ($glos) AND e.approved != 0";
07842023 908
4d4c38f3 909 return get_records_sql("SELECT e.*
910 FROM $selectsql ORDER BY e.concept ASC");
07842023 911}
912
c80828fe 913function glossary_search_entries($searchterms, $glossary, $extended) {
914 $course = get_record("course","id",$glossary->course);
915 return glossary_search($course,$searchterms,$extended,$glossary);
916}
917
e179048e 918function glossary_file_area_name($entry) {
e179048e 919 global $CFG;
e29ad7eb 920// Creates a directory file name, suitable for make_upload_directory()
e179048e 921
e29ad7eb 922 // I'm doing this workaround for make it works for delete_instance also
923 // (when called from delete_instance, glossary is already deleted so
924 // getting the course from mdl_glossary does not work)
925 $module = get_record("modules","name","glossary");
926 $cm = get_record("course_modules","module",$module->id,"instance",$entry->glossaryid);
927 return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
e179048e 928}
929
930function glossary_file_area($entry) {
931 return make_upload_directory( glossary_file_area_name($entry) );
932}
933
e29ad7eb 934function glossary_main_file_area($glossary) {
935 $modarea = glossary_mod_file_area($glossary);
936 return "$modarea/$glossary->id";
937}
938
939function glossary_mod_file_area($glossary) {
940 global $CFG;
941
942 return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
943}
944
e179048e 945function glossary_delete_old_attachments($entry, $exception="") {
946// Deletes all the user files in the attachments area for a entry
947// EXCEPT for any file named $exception
948
949 if ($basedir = glossary_file_area($entry)) {
950 if ($files = get_directory_list($basedir)) {
951 foreach ($files as $file) {
952 if ($file != $exception) {
953 unlink("$basedir/$file");
954// notify("Existing file '$file' has been deleted!");
955 }
956 }
957 }
958 if (!$exception) { // Delete directory as well, if empty
959 rmdir("$basedir");
960 }
961 }
962}
072f7533 963function glossary_delete_attachments($glossary) {
e29ad7eb 964// Deletes all the user files in the attachments area for the glossary
965 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
966 $deleted = 0;
967 foreach ($entries as $entry) {
968 if ( $entry->attachment ) {
969 if ($basedir = glossary_file_area($entry)) {
970 if ($files = get_directory_list($basedir)) {
971 foreach ($files as $file) {
972 unlink("$basedir/$file");
973 }
974 }
975 rmdir("$basedir");
976 $deleted++;
977 }
978 }
979 }
980 if ( $deleted ) {
981 $attachmentdir = glossary_main_file_area($glossary);
982 $glossarydir = glossary_mod_file_area($glossary);
983
984 rmdir("$attachmentdir");
985 if (!$files = get_directory_list($glossarydir) ) {
986 rmdir( "$glossarydir" );
987 }
988 }
989 }
990}
e179048e 991
992function glossary_copy_attachments($entry, $newentry) {
993/// Given a entry object that is being copied to glossaryid,
994/// this function checks that entry
995/// for attachments, and if any are found, these are
996/// copied to the new glossary directory.
997
998 global $CFG;
999
1000 $return = true;
1001
1002 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
1003 foreach ($entries as $curentry) {
1004 $oldentry->id = $entry->id;
1005 $oldentry->course = $entry->course;
1006 $oldentry->glossaryid = $curentry->glossaryid;
1007 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1008 if (is_dir($oldentrydir)) {
1009
1010 $newentrydir = glossary_file_area($newentry);
1011 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
1012 $return = false;
1013 }
1014 }
1015 }
1016 }
1017 return $return;
1018}
1019
1020function glossary_move_attachments($entry, $glossaryid) {
1021/// Given a entry object that is being moved to glossaryid,
1022/// this function checks that entry
1023/// for attachments, and if any are found, these are
1024/// moved to the new glossary directory.
1025
1026 global $CFG;
1027
18b8fbfa 1028 require_once($CFG->dirroot.'/lib/uploadlib.php');
1029
e179048e 1030 $return = true;
1031
1032 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
1033 foreach ($entries as $entry) {
1034 $oldentry->course = $entry->course;
1035 $oldentry->glossaryid = $entry->glossaryid;
1036 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1037 if (is_dir($oldentrydir)) {
1038 $newentry = $oldentry;
1039 $newentry->glossaryid = $glossaryid;
1040 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
18b8fbfa 1041 $files = get_directory_list($oldentrydir); // get it before we rename it.
e179048e 1042 if (! @rename($oldentrydir, $newentrydir)) {
1043 $return = false;
1044 }
18b8fbfa 1045 foreach ($files as $file) {
1046 // this is not tested as I can't find anywhere that calls this function, grepping the source.
1047 clam_change_log($oldentrydir.'/'.$file,$newentrydir.'/'.$file);
1048 }
e179048e 1049 }
1050 }
1051 }
1052 return $return;
1053}
1054
18b8fbfa 1055function glossary_add_attachment($entry, $inputname) {
e179048e 1056// $entry is a full entry record, including course and glossary
1057// $newfile is a full upload array from $_FILES
1058// If successful, this function returns the name of the file
1059
1060 global $CFG;
1061
18b8fbfa 1062 if (!$glossary = get_record("glossary","id",$entry->glossaryid)) {
1063 return false;
1064 }
1065
1066 if (!$course = get_record("course","id",$glossary->course)) {
1067 return false;
e179048e 1068 }
1069
18b8fbfa 1070 require_once($CFG->dirroot.'/lib/uploadlib.php');
36c2c0ef 1071 $um = new upload_manager($inputname,true,false,$course,false,0,false,true);
18b8fbfa 1072 $dir = glossary_file_area_name($entry);
e179048e 1073
18b8fbfa 1074 if ($um->process_file_uploads($dir)) {
1075 return $um->get_new_filename();
e179048e 1076 }
18b8fbfa 1077 // upload manager will take care of errors.
e179048e 1078}
1079
de53b9ac 1080function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 1081// if return=html, then return a html string.
1082// if return=text, then return a text-only string.
1083// otherwise, print HTML for non-images, and return image HTML
b764feaa 1084// if attachment is an image, $align set its aligment.
e179048e 1085 global $CFG;
b764feaa 1086
1087 $newentry = $entry;
1088 if ( $newentry->sourceglossaryid ) {
1089 $newentry->glossaryid = $newentry->sourceglossaryid;
1090 }
e179048e 1091
b764feaa 1092 $filearea = glossary_file_area_name($newentry);
e179048e 1093
1094 $imagereturn = "";
1095 $output = "";
1096
b764feaa 1097 if ($basedir = glossary_file_area($newentry)) {
e179048e 1098 if ($files = get_directory_list($basedir)) {
1099 $strattachment = get_string("attachment", "glossary");
1100 $strpopupwindow = get_string("popupwindow");
1101 foreach ($files as $file) {
1102 $icon = mimeinfo("icon", $file);
1103 if ($CFG->slasharguments) {
1104 $ffurl = "file.php/$filearea/$file";
1105 } else {
1106 $ffurl = "file.php?file=/$filearea/$file";
1107 }
839f2456 1108 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\" />";
e179048e 1109
1110 if ($return == "html") {
41905731 1111 $output .= "<a target=\"_image\" href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1112 $output .= "<a target=\"_image\" href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
e179048e 1113 } else if ($return == "text") {
1114 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1115
1116 } else {
1117 if ($icon == "image.gif") { // Image attachments don't get printed as links
839f2456 1118 $imagereturn .= "<img src=\"$CFG->wwwroot/$ffurl\" align=\"$align\" alt=\"\" />";
e179048e 1119 } else {
1120 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
41905731 1121 echo "<a target=\"_image\" href=\"$CFG->wwwroot/$ffurl\">$file</a>";
e179048e 1122 echo "<br />";
1123 }
1124 }
1125 }
1126 }
1127 }
1128
1129 if ($return) {
1130 return $output;
1131 }
1132
1133 return $imagereturn;
1134}
1135
7dd88447 1136function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 1137
1138if ( !$tTHEME ) {
1139 global $THEME;
1140 $tTHEME = $THEME;
1141}
1142
7dd88447 1143$tablecolor = $tTHEME->TabTableBGColor;
1144$currenttabcolor = $tTHEME->ActiveTabColor;
1145$tabcolor = $tTHEME->InactiveTabColor;
1146$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 1147
7dd88447 1148$tablewidth = $tTHEME->TabTableWidth;
1149$tabsperrow = $tTHEME->TabsPerRow;
1150$tabseparation = $tTHEME->TabSeparation;
06d94a52 1151
7dd88447 1152$tabs = count($data);
1153$tabwidth = (int) (100 / $tabsperrow);
06d94a52 1154
7dd88447 1155$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
1156
1157$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 1158
1159?>
1160 <center>
7dd88447 1161 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 1162 <tr>
1163 <td width="100%">
1164
1165 <table border="0" cellpadding="0" cellspacing="0" width="100%">
1166
ad58adac 1167<?php
7dd88447 1168$tabproccessed = 0;
1169for ($row = 0; $row < $numrows; $row++) {
06d94a52 1170 echo "<tr>\n";
7dd88447 1171 if ( $row != $currentrow ) {
1172 for ($col = 0; $col < $tabsperrow; $col++) {
1173 if ( $tabproccessed < $tabs ) {
ad58adac 1174 if ( $col == 0 ) {
7dd88447 1175 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 1176 }
7dd88447 1177 if ($tabproccessed == $currenttab) {
1178 $currentcolor = $currenttabcolor;
e94c179a 1179 $currentstyle = 'generaltabselected';
06d94a52 1180 } else {
7dd88447 1181 $currentcolor = $tabcolor;
e94c179a 1182 $currentstyle = 'generaltab';
06d94a52 1183 }
505eec9c 1184 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 1185 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
1186 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 1187 }
7dd88447 1188
ed0201dd 1189 if ( !$data[$tabproccessed]->link ) {
1190 echo "<font color=\"$inactivefontcolor\">";
1191 }
7dd88447 1192 echo $data[$tabproccessed]->caption;
ed0201dd 1193 if ( !$data[$tabproccessed]->link ) {
1194 echo "</font>";
1195 }
7dd88447 1196
1197 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 1198 echo "</a>";
1199 }
1200 echo "</b></td>";
1201
7dd88447 1202 if ( $col < $tabsperrow ) {
1203 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 1204 }
06d94a52 1205 } else {
7dd88447 1206 $currentcolor = "";
06d94a52 1207 }
7dd88447 1208 $tabproccessed++;
06d94a52 1209 }
1210 } else {
7dd88447 1211 $firsttabincurrentrow = $tabproccessed;
1212 $tabproccessed += $tabsperrow;
06d94a52 1213 }
7dd88447 1214 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 1215}
1216 echo "<tr>\n";
7dd88447 1217 $tabproccessed = $firsttabincurrentrow;
1218 for ($col = 0; $col < $tabsperrow; $col++) {
1219 if ( $tabproccessed < $tabs ) {
ad58adac 1220 if ( $col == 0 ) {
7dd88447 1221 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 1222 }
7dd88447 1223 if ($tabproccessed == $currenttab) {
1224 $currentcolor = $currenttabcolor;
e8ff59d6 1225 $currentstyle = 'generaltabselected';
06d94a52 1226 } else {
7dd88447 1227 $currentcolor = $tabcolor;
e8ff59d6 1228 $currentstyle = 'generaltab';
06d94a52 1229 }
3eb2f7ec 1230
1231 if (!isset($data[$tabproccessed]->link)) {
1232 $data[$tabproccessed]->link = NULL;
1233 }
505eec9c 1234 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 1235 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
1236 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 1237 }
7dd88447 1238
ed0201dd 1239 if ( !$data[$tabproccessed]->link ) {
1240 echo "<font color=\"$inactivefontcolor\">";
1241 }
7dd88447 1242 echo $data[$tabproccessed]->caption;
ed0201dd 1243 if ( !$data[$tabproccessed]->link ) {
1244 echo "</font>";
1245 }
7dd88447 1246
1247 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 1248 echo "</a>";
1249 }
1250 echo "</b></td>";
1251
7dd88447 1252 if ($col < $tabsperrow) {
1253 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 1254 }
06d94a52 1255 } else {
7dd88447 1256 if ($numrows > 1) {
1257 $currentcolor = $tabcolor;
06d94a52 1258 } else {
7dd88447 1259 $currentcolor = "";
06d94a52 1260 }
7dd88447 1261 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 1262 echo "</td>";
1263
7dd88447 1264 $col = $tabsperrow;
06d94a52 1265 }
7dd88447 1266 $tabproccessed++;
06d94a52 1267 }
1268 echo "</tr>\n";
1269 ?>
1270
1271 </table>
1272 </td>
1273 </tr>
1274 <tr>
a9ef4a63 1275 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr /></td>
06d94a52 1276 </tr>
1277 <tr>
7dd88447 1278 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 1279 <center>
ad58adac 1280<?php
06d94a52 1281}
1282
ad58adac 1283function glossary_print_tabbed_table_end() {
06d94a52 1284 echo "</center><p></td></tr></table></center>";
1285}
1286
1ac87c73 1287function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1288 if ($glossary->showalphabet) {
c197e607 1289 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 1290 }
1ac87c73 1291 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1292
c4a35419 1293 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 1294
1ac87c73 1295 glossary_print_all_links($cm, $glossary, $mode, $hook);
c76e673a 1296
db87439a 1297 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
c76e673a 1298}
1299
1ac87c73 1300function glossary_print_addentry_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
ea14e783 1301 echo '<center>' . get_string("explainaddentry","glossary") . '<p>';
1302}
1303
1ac87c73 1304function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1305 echo '<center>' . get_string("explainimport","glossary") . '<p>';
1306}
1307
1ac87c73 1308function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1309 echo '<center>' . get_string("explainexport","glossary") . '<p>';
1310}
1311
1ac87c73 1312function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1313 if ( $mode != 'date' ) {
a359c29b 1314 if ($glossary->showalphabet) {
c197e607 1315 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
1316 }
c76e673a 1317
1ac87c73 1318 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1319
c4a35419 1320 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 1321
1ac87c73 1322 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 1323 } else {
1ac87c73 1324 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1325 }
1326}
1327
1328function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1329 if ($glossary->showalphabet) {
d7143408 1330 echo '<center>' . get_string("explainalphabet","glossary") . '<br />';
c197e607 1331 }
1ac87c73 1332
c4a35419 1333 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1334 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 1335 glossary_print_all_links($cm, $glossary, $mode, $hook);
c4a35419 1336// echo "<br />";
c76e673a 1337}
1338
1ac87c73 1339function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
c76e673a 1340global $CFG, $THEME;
41905731 1341 echo '<table border="0" width="100%">';
c197e607 1342 echo '<tr>';
c76e673a 1343
41905731 1344 echo '<td align="center" width="20%">';
840d3ba8 1345 if ( isteacher($glossary->course) ) {
c76e673a 1346 $options['id'] = $cm->id;
1ac87c73 1347 $options['mode'] = 'cat';
1348 $options['hook'] = $hook;
c76e673a 1349 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1350 }
c197e607 1351 echo '</td>';
c76e673a 1352
41905731 1353 echo '<td align="center" width="60%">';
c197e607 1354 echo '<b>';
c76e673a 1355
1356 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1357 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1358
c76e673a 1359 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1360 $selected = '';
c76e673a 1361 if ( $categories ) {
1362 foreach ($categories as $currentcategory) {
1363 $url = $currentcategory->id;
1364 if ( $category ) {
1365 if ($currentcategory->id == $category->id) {
1366 $selected = $url;
1367 }
1368 }
ae06e00e 1369 $menu[$url] = clean_text($currentcategory->name); //Only clean, not filters
c76e673a 1370 }
1371 }
1372 if ( !$selected ) {
1373 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1374 }
1375
1376 if ( $category ) {
ae06e00e 1377 echo format_text($category->name);
c76e673a 1378 } else {
1ac87c73 1379 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1380
1381 echo get_string("entrieswithoutcategory","glossary");
1382 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1383
1ac87c73 1384 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1385
1386 echo get_string("allcategories","glossary");
1387 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1388
1389 }
1390 }
c197e607 1391 echo '</b></td>';
41905731 1392 echo '<td align="center" width="20%">';
c76e673a 1393
839f2456 1394 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=cat&amp;hook=", $menu, "catmenu", $selected, "",
c76e673a 1395 "", "", false);
677038ee 1396
c197e607 1397 echo '</td>';
1398 echo '</tr>';
c76e673a 1399
c197e607 1400 echo '</table>';
c76e673a 1401}
1402
1ac87c73 1403function glossary_print_all_links($cm, $glossary, $mode, $hook) {
c76e673a 1404global $CFG;
a359c29b 1405 if ( $glossary->showall) {
c76e673a 1406 $strallentries = get_string("allentries", "glossary");
1ac87c73 1407 if ( $hook == 'ALL' ) {
c76e673a 1408 echo "<b>$strallentries</b>";
1409 } else {
1410 $strexplainall = strip_tags(get_string("explainall","glossary"));
839f2456 1411 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=ALL\">$strallentries</a>";
c76e673a 1412 }
1413 }
1414}
1415
1ac87c73 1416function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1417global $CFG;
a359c29b 1418 if ( $glossary->showspecial) {
c76e673a 1419 $strspecial = get_string("special", "glossary");
1ac87c73 1420 if ( $hook == 'SPECIAL' ) {
677038ee 1421 echo "<b>$strspecial</b> | ";
1422 } else {
1423 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
839f2456 1424 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=SPECIAL\">$strspecial</a> | ";
677038ee 1425 }
914cb260 1426 }
c76e673a 1427}
677038ee 1428
c4a35419 1429function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1430global $CFG;
a359c29b 1431 if ( $glossary->showalphabet) {
c4274149 1432 $alphabet = explode(",", get_string("alphabet"));
677038ee 1433 $letters_by_line = 14;
1434 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1435 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1436 echo "<b>$alphabet[$i]</b>";
1437 } else {
839f2456 1438 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=$alphabet[$i]&amp;sortkey=$sortkey&amp;sortorder=$sortorder\">$alphabet[$i]</a>";
677038ee 1439 }
1440 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1441 echo ' | ';
677038ee 1442 } else {
a9ef4a63 1443 echo '<br />';
677038ee 1444 }
767a31c3 1445 }
677038ee 1446 }
c76e673a 1447}
1448
1ac87c73 1449function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1450global $CFG;
677038ee 1451
c4a35419 1452 $asc = get_string("ascending","glossary");
1453 $desc = get_string("descending","glossary");
1454 $bopen = '<b>';
1455 $bclose = '</b>';
1456
c197e607 1457 $neworder = '';
677038ee 1458 if ( $sortorder ) {
c197e607 1459 if ( $sortorder == 'asc' ) {
839f2456 1460 $neworder = '&amp;sortorder=desc';
c4a35419 1461 $newordertitle = $desc;
677038ee 1462 } else {
839f2456 1463 $neworder = '&amp;sortorder=asc';
c4a35419 1464 $newordertitle = $asc;
677038ee 1465 }
839f2456 1466 $icon = " <img src=\"$sortorder.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"\" />";
677038ee 1467 } else {
c4a35419 1468 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1469 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1470 $icon = "";
c4a35419 1471 $newordertitle = $asc;
677038ee 1472 } else {
c4a35419 1473 $newordertitle = $desc;
839f2456 1474 $neworder = '&amp;sortorder=desc';
1475 $icon = ' <img src="asc.gif" border="0" width="16" height="16" alt="" />';
677038ee 1476 }
1477 }
c4a35419 1478 $ficon = '';
1479 $fneworder = '';
1480 $fbtag = '';
1481 $fendbtag = '';
1482
1483 $sicon = '';
1484 $sneworder = '';
ae078733 1485
1486 $sbtag = '';
1487 $fbtag = '';
1488 $fendbtag = '';
1489 $sendbtag = '';
1490
c4a35419 1491 $sendbtag = '';
1492
1493 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1494 $ficon = $icon;
1495 $fneworder = $neworder;
1496 $fordertitle = $newordertitle;
1497 $sordertitle = $asc;
1498 $fbtag = $bopen;
1499 $fendbtag = $bclose;
1500 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1501 $sicon = $icon;
1502 $sneworder = $neworder;
1503 $fordertitle = $asc;
1504 $sordertitle = $newordertitle;
1505 $sbtag = $bopen;
1506 $sendbtag = $bclose;
677038ee 1507 } else {
c4a35419 1508 $fordertitle = $asc;
1509 $sordertitle = $asc;
677038ee 1510 }
c4a35419 1511
1512 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1513 $forder = 'CREATION';
1514 $sorder = 'UPDATE';
1515 $fsort = get_string("sortbycreation", "glossary");
1516 $ssort = get_string("sortbylastupdate", "glossary");
1517
1518 $sort = get_string("sortchronogically", "glossary");
1519 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1520 $forder = 'FIRSTNAME';
1521 $sorder = 'LASTNAME';
1522 $fsort = get_string("firstname");
1523 $ssort = get_string("lastname");
1524
1525 $sort = get_string("sortby", "glossary");
1526 }
1527
839f2456 1528 echo "<br />$sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;sortkey=$sorder$sneworder&amp;mode=$mode\">$ssort$sicon</a>$sendbtag | ".
1529 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;sortkey=$forder$fneworder&amp;mode=$mode\">$fsort$ficon</a>$fendbtag<br />";
fb443f1a 1530}
ad58adac 1531
1532function glossary_sort_entries ( $entry0, $entry1 ) {
1533 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
1534 return -1;
1535 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
1536 return 1;
1537 } else {
1538 return 0;
1539 }
1540}
1541
ed0201dd 1542function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1543 global $THEME, $CFG, $USER;
1544
1d9ddaaf 1545 $colour = $THEME->cellheading2;
ed0201dd 1546
1547 $user = get_record("user", "id", $comment->userid);
1548 $strby = get_string("writtenby","glossary");
072f7533 1549 $fullname = fullname($user, isteacher($course->id));
ed0201dd 1550
79be5e9a 1551 echo '<table align="center" border="0" width="70%" cellpadding="3" cellspacing="0" class="forumpost">';
1552 echo "<tr>";
1553 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
1554 print_user_picture($user->id, $course->id, $user->picture);
1555 echo "</td>";
072f7533 1556 echo "<td bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
1557 echo "<p>";
839f2456 1558 echo "<font size=\"2\"><a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">$fullname</a></font><br />";
41905731 1559 echo "<font size=\"1\">".get_string("lastedited").": ".userdate($comment->timemodified)."</font>";
79be5e9a 1560 echo "</p></td></tr>";
ed0201dd 1561
79be5e9a 1562 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostside\" width=\"10\">";
1563 echo "&nbsp;";
1564 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
072f7533 1565
1566 echo format_text($comment->comment, $comment->format);
1567
41905731 1568 echo "<div align=\"right\"><p align=\"right\">";
3a512f86 1569 $ineditperiod = ((time() - $comment->timemodified < $CFG->maxeditingtime) || $glossary->editalways);
944d82d4 1570 if ( ($glossary->allowcomments && $ineditperiod && $USER->id == $comment->userid) || isteacher($course->id) ) {
839f2456 1571 echo "<a href=\"comment.php?id=$cm->id&amp;eid=$entry->id&amp;cid=$comment->id&amp;action=edit\"><img
1572 alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" height=\"11\" width=\"11\" border=\"0\" /></a> ";
c197e607 1573 }
944d82d4 1574 if ( ($glossary->allowcomments && $USER->id == $comment->userid) || isteacher($course->id) ) {
839f2456 1575 echo "<a href=\"comment.php?id=$cm->id&amp;eid=$entry->id&amp;cid=$comment->id&amp;action=delete\"><img
1576 alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" /></a>";
c197e607 1577 }
c197e607 1578
072f7533 1579 echo "</p>";
1580 echo "</div>";
1581 echo "</td></tr>\n</table>\n\n";
ed0201dd 1582
ed0201dd 1583}
1584
63dd5fb2 1585function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
a359c29b 1586
1587 global $USER;
1588
63dd5fb2 1589 $ratingsmenuused = false;
1590 if (!empty($ratings) and !empty($USER->id)) {
1591 $useratings = true;
1592 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1593 if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) {
1594 $useratings = false;
1595 }
1596 }
1597 if ($useratings) {
1598 if (isteacher($course->id)) {
1599 glossary_print_ratings_mean($entry->id, $ratings->scale);
1600 if ($USER->id != $entry->userid) {
1601 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1602 $ratingsmenuused = true;
1603 }
1604 } else if ($USER->id == $entry->userid) {
1605 glossary_print_ratings_mean($entry->id, $ratings->scale);
1606 } else if (!empty($ratings->allow) ) {
1607 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1608 $ratingsmenuused = true;
1609 }
1610 }
1611 }
1612 return $ratingsmenuused;
1613}
1614
b1918034 1615function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
a359c29b 1616 global $THEME, $USER,$CFG;
cca6f7f1 1617
1d9ddaaf 1618 $colour = $THEME->cellheading2;
cca6f7f1 1619
41905731 1620 echo "\n<center><table width=\"95%\" border=\"0\"><tr>";
1621 echo "<td width=\"100%\"\">";
1d9ddaaf 1622 if ( $entries ) {
1623 foreach ( $entries as $entry ) {
1624 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1625 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1626 }
1d9ddaaf 1627 if (! $course = get_record("course", "id", $glossary->course)) {
1628 error("Glossary is misconfigured - don't know what course it's from");
1629 }
0de786f7 1630 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1631 error("Glossary is misconfigured - don't know what course module it is ");
1632 }
1f63b7c6 1633
1634 //If displayformat is present, override glossary->displayformat
a359c29b 1635 if ($displayformat < 0) {
1f63b7c6 1636 $dp = $glossary->displayformat;
a359c29b 1637 } else {
1f63b7c6 1638 $dp = $displayformat;
1639 }
1640
a359c29b 1641 //Get popupformatname
1642 $format = get_record('glossary_formats','name',$dp);
1643 $displayformat = $format->popupformatname;
1644
1645 //Check displayformat variable and set to default if necessary
1646 if (!$displayformat) {
1647 $displayformat = 'dictionary';
584dcac9 1648 }
1f63b7c6 1649
a359c29b 1650 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
1651 $functionname = 'glossary_show_entry_'.$displayformat;
1652
1653 if (file_exists($formatfile)) {
1654 include_once($formatfile);
1655 if (function_exists($functionname)) {
1656 $functionname($course, $cm, $glossary, $entry,'','','','');
1657 }
1658 }
cca6f7f1 1659 }
cca6f7f1 1660 }
1d9ddaaf 1661 echo "</td>";
1662 echo "</tr></table></center>";
1663}
4f4ca7b5 1664
1ac87c73 1665function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
4f4ca7b5 1666global $CFG;
1667 glossary_check_moddata_dir($glossary);
36062759 1668
1669 if (!$h = glossary_open_xml($glossary)) {
1670 error("An error occurred while opening a file to write to.");
1671 }
4f4ca7b5 1672
1673 $status = fwrite ($h,glossary_start_tag("INFO",1,true));
1674 fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
1675 fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
16527801 1676 fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost));
4f4ca7b5 1677 fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
73e6ac63 1678 fwrite ($h,glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat));
4f4ca7b5 1679 fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
1680 fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
1681 fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
1682 fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
1683 fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
1684 fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
1685 fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
1ac87c73 1686 fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
4f4ca7b5 1687
748b1932 1688 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1689 $status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
1690 foreach ($entries as $entry) {
046a797c 1691 $permissiongranted = 1;
1ac87c73 1692 if ( $hook ) {
1693 switch ( $hook ) {
046a797c 1694 case "ALL":
1695 case "SPECIAL":
1696 break;
1697 default:
1ac87c73 1698 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1699 break;
1700 }
1701 }
1ac87c73 1702 if ( $hook ) {
1703 switch ( $hook ) {
046a797c 1704 case GLOSSARY_SHOW_ALL_CATEGORIES:
1705 break;
1706 case GLOSSARY_SHOW_NOT_CATEGORISED:
1707 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1708 break;
1709 default:
1ac87c73 1710 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1711 break;
1712 }
1713 }
1714 if ( $entry->approved and $permissiongranted ) {
748b1932 1715 $status = fwrite($h,glossary_start_tag("ENTRY",3,true));
b9656030 1716 fwrite($h,glossary_full_tag("CONCEPT",4,false,trim($entry->concept)));
748b1932 1717 fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
1718 fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
1719 fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
1720 fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
1721 fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
1722 fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
1723
7965be79 1724 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1725 $status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
1726 foreach ($aliases as $alias) {
1727 $status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
b9656030 1728 fwrite($h,glossary_full_tag("NAME",6,false,trim($alias->alias)));
7965be79 1729 $status = fwrite($h,glossary_end_tag("ALIAS",5,true));
1730 }
00758851 1731 $status = fwrite($h,glossary_end_tag("ALIASES",4,true));
7965be79 1732 }
748b1932 1733 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1734 $status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
1735 foreach ($catentries as $catentry) {
1736 $category = get_record("glossary_categories","id",$catentry->categoryid);
1737
1738 $status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
1739 fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
1ac87c73 1740 fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
748b1932 1741 $status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
1742 }
1743 $status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
1744 }
4f4ca7b5 1745
748b1932 1746 $status =fwrite($h,glossary_end_tag("ENTRY",3,true));
4f4ca7b5 1747 }
1748 }
748b1932 1749 $status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
1750
4f4ca7b5 1751 }
748b1932 1752
1753
1754 $status =fwrite ($h,glossary_end_tag("INFO",1,true));
1755
4f4ca7b5 1756 $h = glossary_close_xml($h);
1757}
1758// Functions designed by Eloy Lafuente
1759//
1760//Function to create, open and write header of the xml file
1761function glossary_open_xml($glossary) {
1762
1763 global $CFG;
1764
1765 $status = true;
1766
1767 //Open for writing
1768
36062759 1769 $glossaryname = clean_filename(strip_tags($glossary->name));
1770 $pathname = make_upload_directory("$glossary->course/glossary/$glossaryname");
1771 $filename = "$pathname/glossary.xml";
1772
1773 if (!$h = fopen($filename,"w")) {
1774 notify("Error opening '$filename'");
1775 return false;
1776 }
1777
4f4ca7b5 1778 //Writes the header
1779 $status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1780 if ($status) {
1781 $status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
1782 }
1783 if ($status) {
1784 return $h;
1785 } else {
1786 return false;
1787 }
1788}
1789
748b1932 1790function glossary_read_imported_file($file) {
1791require_once "../../lib/xmlize.php";
1792 $h = fopen($file,"r");
1793 $line = '';
1794 if ($h) {
1795 while ( !feof($h) ) {
b9656030 1796 $char = fread($h,1024);
748b1932 1797 $line .= $char;
1798 }
1799 fclose($h);
1800 }
1801 return xmlize($line);
1802}
4f4ca7b5 1803//Close the file
1804function glossary_close_xml($h) {
1805 $status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
1806 return fclose($h);
1807}
1808
1809//Return the xml start tag
1810function glossary_start_tag($tag,$level=0,$endline=false) {
1811 if ($endline) {
1812 $endchar = "\n";
1813 } else {
1814 $endchar = "";
1815 }
1816 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1817}
1818
1819//Return the xml end tag
1820function glossary_end_tag($tag,$level=0,$endline=true) {
1821 if ($endline) {
1822 $endchar = "\n";
1823 } else {
1824 $endchar = "";
1825 }
1826 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1827}
1828
1829//Return the start tag, the contents and the end tag
1830function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
1831 $st = glossary_start_tag($tag,$level,$endline);
1832 $co="";
1833 if ($to_utf) {
1834 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
1835 } else {
1836 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
1837 }
1838 $et = glossary_end_tag($tag,0,true);
1839 return $st.$co.$et;
1840}
1841
1842 //Function to check and create the needed moddata dir to
1843 //save all the mod backup files. We always name it moddata
1844 //to be able to restore it, but in restore we check for
1845 //$CFG->moddata !!
1846function glossary_check_moddata_dir($glossary) {
1847
1848 global $CFG;
1849
1850 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
1851 if ( $status ) {
1852 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
1853 if ( $status ) {
1854 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name),true);
1855 }
1856 }
1857 return $status;
1858}
1859
1860//Function to check if a directory exists
1861//and, optionally, create it
1862function glossary_check_dir_exists($dir,$create=false) {
1863
1864 global $CFG;
1865
1866 $status = true;
1867 if(!is_dir($dir)) {
1868 if (!$create) {
1869 $status = false;
1870 } else {
1871 umask(0000);
1872 $status = mkdir ($dir,$CFG->directorypermissions);
1873 }
1874 }
1875 return $status;
1876}
63dd5fb2 1877/*
1878* Adding grading functions
1879*/
1880
1881function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1882/// Returns a list of ratings for a particular entry - sorted.
1883 global $CFG;
1884 return get_records_sql("SELECT u.*, r.rating, r.time
1885 FROM {$CFG->prefix}glossary_ratings r,
1886 {$CFG->prefix}user u
1887 WHERE r.entryid = '$entryid'
1888 AND r.userid = u.id
1889 ORDER BY $sort");
1890}
1891
1892function glossary_get_user_grades($glossaryid) {
1893/// Get all user grades for a glossary
1894 global $CFG;
1895
1896 return get_records_sql("SELECT r.id, e.userid, r.rating
1897 FROM {$CFG->prefix}glossary_entries e,
1898 {$CFG->prefix}glossary_ratings r
1899 WHERE e.glossaryid = '$glossaryid'
1900 AND r.entryid = e.id
1901 ORDER by e.userid ");
1902}
1903
1904function glossary_count_unrated_entries($glossaryid, $userid) {
1905// How many unrated entries are in the given glossary for a given user?
1906 global $CFG;
1907 if ($entries = get_record_sql("SELECT count(*) as num
1908 FROM {$CFG->prefix}glossary_entries
1909 WHERE glossaryid = '$glossaryid'
1910 AND userid <> '$userid' ")) {
1911
1912 if ($rated = get_record_sql("SELECT count(*) as num
1913 FROM {$CFG->prefix}glossary_entries e,
1914 {$CFG->prefix}glossary_ratings r
1915 WHERE e.glossaryid = '$glossaryid'
1916 AND e.id = r.entryid
1917 AND r.userid = '$userid'")) {
1918 $difference = $entries->num - $rated->num;
1919 if ($difference > 0) {
1920 return $difference;
1921 } else {
1922 return 0; // Just in case there was a counting error
1923 }
1924 } else {
1925 return $entries->num;
1926 }
1927 } else {
1928 return 0;
1929 }
1930}
1931
1932function glossary_print_ratings_mean($entryid, $scale) {
1933/// Print the multiple ratings on a entry given to the current user by others.
1934/// Scale is an array of ratings
1935
1936 static $strrate;
1937
1938 $mean = glossary_get_ratings_mean($entryid, $scale);
1939
1940 if ($mean !== "") {
1941
1942 if (empty($strratings)) {
1943 $strratings = get_string("ratings", "glossary");
1944 }
1945
41905731 1946 echo "<font size=\"-1\">$strratings: ";
63dd5fb2 1947 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
37d543b5 1948 echo "</font>";
63dd5fb2 1949 }
1950}
1951
1952
1953function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1954/// Return the mean rating of a entry given to the current user by others.
1955/// Scale is an array of possible ratings in the scale
1956/// Ratings is an optional simple array of actual ratings (just integers)
1957
1958 if (!$ratings) {
1959 $ratings = array();
1960 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1961 foreach ($rates as $rate) {
1962 $ratings[] = $rate->rating;
1963 }
1964 }
1965 }
1966
1967 $count = count($ratings);
1968
1969 if ($count == 0) {
1970 return "";
1971
1972 } else if ($count == 1) {
1973 return $scale[$ratings[0]];
1974
1975 } else {
1976 $total = 0;
1977 foreach ($ratings as $rating) {
1978 $total += $rating;
1979 }
1980 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1981
1982 if (isset($scale[$mean])) {
1983 return $scale[$mean]." ($count)";
1984 } else {
1985 return "$mean ($count)"; // Should never happen, hopefully
1986 }
1987 }
1988}
1989
1990function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1991/// Return a summary of entry ratings given to the current user by others.
1992/// Scale is an array of possible ratings in the scale
1993/// Ratings is an optional simple array of actual ratings (just integers)
1994
1995 if (!$ratings) {
1996 $ratings = array();
1997 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1998 foreach ($rates as $rate) {
1999 $rating[] = $rate->rating;
2000 }
2001 }
2002 }
2003
2004
2005 if (!$count = count($ratings)) {
2006 return "";
2007 }
2008
2009
2010 foreach ($scale as $key => $scaleitem) {
2011 $sumrating[$key] = 0;
2012 }
2013
2014 foreach ($ratings as $rating) {
2015 $sumrating[$rating]++;
2016 }
2017
2018 $summary = "";
2019 foreach ($scale as $key => $scaleitem) {
2020 $summary = $sumrating[$key].$summary;
2021 if ($key > 1) {
2022 $summary = "/$summary";
2023 }
2024 }
2025 return $summary;
2026}
2027
2028function glossary_print_rating_menu($entryid, $userid, $scale) {
2029/// Print the menu of ratings as part of a larger form.
2030/// If the entry has already been - set that value.
2031/// Scale is an array of ratings
2032
2033 static $strrate;
2034
2035 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
2036 $rating->rating = 0;
2037 }
2038
2039 if (empty($strrate)) {
2040 $strrate = get_string("rate", "glossary");
2041 }
2042
2043 choose_from_menu($scale, $entryid, $rating->rating, "$strrate...");
2044}
2045
cc41c129 2046?>