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