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