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