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