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