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