Silly typo
[moodle.git] / mod / glossary / lib.php
CommitLineData
07842023 1<?PHP // $Id$
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 = "";
1ac87c73 140 foreach ( $categories as $hook ) {
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;
251 $entries[$log->info]->url = $log->url;
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);
265 echo "<p><font size=1>$date - $fullname<br>";
67905ace 266 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/view.php?g=$entry->glossaryid&mode=entry&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 ) {
463 echo '<font color=red>' . $text . '</font>';
464 if ( $br ) {
465 echo '<br>';
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
63dd5fb2 531function glossary_print_entry($course, $cm, $glossary, $entry, $mode="",$hook="",$printicons = 1, $displayformat = -1, $ratings = NULL) {
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';
539 $functionname = 'glossary_show_entry_'.$displayformat;
540
541 if (file_exists($formatfile)) {
542 include_once($formatfile);
543 if (function_exists($functionname)) {
544 $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
c76e673a 545 }
767a31c3 546 }
07842023 547 }
a359c29b 548 return $return;
07842023 549}
a359c29b 550
81bdc9e9 551function glossary_print_entry_concept($entry) {
d34b9c5f 552 $options->para = false;
e0147c9b 553 $text = format_text('<nolink>' . $entry->concept . '</nolink>', FORMAT_MOODLE, $options);
ff8352de 554 if (!empty($entry->highlight)) {
555 $text = highlight($entry->highlight, $text);
556 }
557 echo $text;
81bdc9e9 558}
1d9ddaaf 559
560function glossary_print_entry_definition($entry) {
701fff21 561
f287e69c 562 $definition = $entry->definition;
563
701fff21 564 $tags = array();
f287e69c 565
701fff21 566 //Calculate all the strings to be no-linked
567 //First, the concept
e2ba5060 568 $term = preg_quote(trim($entry->concept),"/");
701fff21 569 $pat = '/('.$term.')/is';
570 $doNolinks[] = $pat;
571 //Now the aliases
572 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
573 foreach ($aliases as $alias) {
e2ba5060 574 $term = preg_quote(trim($alias->alias),"/");
701fff21 575 $pat = '/('.$term.')/is';
576 $doNolinks[] = $pat;
577 }
f287e69c 578 }
579
701fff21 580 //Extract all tags from definition
581 preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
f287e69c 582
701fff21 583 //Save them into tags array to use them later
584 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
585 $tags['<@'.$key.'@>'] = $value;
f287e69c 586 }
587
701fff21 588 //Take off every tag from definition
589 if ( $tags ) {
590 $definition = str_replace($tags,array_keys($tags),$definition);
f287e69c 591 }
701fff21 592
593 //Put doNolinks (concept + aliases) enclosed by <nolink> tag
594 $definition= preg_replace($doNolinks,'<nolink>$1</nolink>',$definition);
595
596 //Restore tags
597 if ( $tags ) {
598 $definition = str_replace(array_keys($tags),$tags,$definition);
584dcac9 599 }
ff8352de 600
3eb2f7ec 601 $options->para = false;
602 $text = format_text($definition, $entry->format,$options);
ff8352de 603 if (!empty($entry->highlight)) {
604 $text = highlight($entry->highlight, $text);
605 }
359f2758 606 if (isset($entry->footer)) { // Unparsed footer info
607 $text .= $entry->footer;
608 }
ff8352de 609 echo $text;
1d9ddaaf 610}
611
1ac87c73 612function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 613 $return = '';
614 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
615 foreach ($aliases as $alias) {
33cc423e 616 if (trim($alias->alias)) {
81bdc9e9 617 if ($return == '') {
618 $return = '<select style="font-size:8pt">';
619 }
620 $return .= "<option>$alias->alias</option>";
621 }
622 }
623 if ($return != '') {
624 $return .= '</select>';
625// $return = "<table border=0 align=$align><tr><td>$return</td></tr></table>";
626 }
627 }
1ac87c73 628 if ($type == 'print') {
81bdc9e9 629 echo $return;
630 } else {
631 return $return;
632 }
633}
634
1ac87c73 635function glossary_print_entry_icons($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
302130de 636 global $THEME, $USER, $CFG;
81bdc9e9 637
638 $importedentry = ($entry->sourceglossaryid == $glossary->id);
639 $isteacher = isteacher($course->id);
640 $ismainglossary = $glossary->mainglossary;
641
642 $return = "<font size=1>";
81bdc9e9 643 if (!$entry->approved) {
644 $return .= get_string("entryishidden","glossary");
645 }
66d7ae88 646 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
647
648 $return .= "</font> ";
a0d1e2bb 649
650
81bdc9e9 651 if ( $glossary->allowcomments and !isguest()) {
e0920b21 652 $return .= " <a title=\"" . get_string("addcomment","glossary") . "\" href=\"comment.php?id=$cm->id&eid=$entry->id\"><img src=\"comment.gif\" height=11 width=11 border=0></a> ";
81bdc9e9 653 }
654
d6bbfb22 655 if ($isteacher or $glossary->studentcanpost and $entry->userid == $USER->id) {
81bdc9e9 656 // only teachers can export entries so check it out
657 if ($isteacher and !$ismainglossary and !$importedentry) {
658 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
659 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
660
1ac87c73 661 $return .= " <a title=\"" . get_string("exporttomainglossary","glossary") . "\" href=\"exportentry.php?id=$cm->id&entry=$entry->id&mode=$mode&hook=$hook\"><img src=\"export.gif\" height=11 width=11 border=0></a> ";
81bdc9e9 662
663 }
664 }
665
666 if ( $entry->sourceglossaryid ) {
667 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
668 } else {
096b5432 669 $icon = "$CFG->pixpath/t/delete.gif";
81bdc9e9 670 }
671
ff1e2621 672 //Decide if an entry is editable:
673 // -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
674 // -The user is teacher or he is a student with time permissions (edit period or editalways defined).
675 $ineditperiod = (time() - $entry->timemodified < $CFG->maxeditingtime);
676 if ( !$importedentry and ($isteacher or ($entry->userid == $USER->id and ($glossary->editalways or $ineditperiod)))) {
1ac87c73 677 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
81bdc9e9 678 $return .= $icon;
679 $return .= "\" height=11 width=11 border=0></a> ";
680
096b5432 681 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"$CFG->pixpath/t/edit.gif\" height=11 width=11 border=0></a>";
81bdc9e9 682 } elseif ( $importedentry ) {
683 $return .= " <font size=-1>" . get_string("exportedentry","glossary") . "</font>";
684 }
685 }
686 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
1ac87c73 687 if ($type == 'print') {
81bdc9e9 688 echo $return;
689 } else {
690 return $return;
a0d1e2bb 691 }
692}
693
694function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
695 $return = '';
696
697 $count = count_records("glossary_comments","entryid",$entry->id);
698 if ($count) {
699 $return = "<font size=1>";
700 $return .= "<a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
701 if ($count == 1) {
702 $return .= get_string("comment", "glossary");
703 } else {
704 $return .= get_string("comments", "glossary");
705 }
706 $return .= "</a>";
707 $return .= "</font>";
708 }
709
710 if ($type == 'print') {
711 echo $return;
712 } else {
713 return $return;
81bdc9e9 714 }
715}
716
26983d03 717function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
81bdc9e9 718
26983d03 719 if ($aliases) {
720 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,"html");
721 }
1ac87c73 722 $icons = "";
26983d03 723 $return = "";
1ac87c73 724 if ( $printicons ) {
725 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
726 }
a359c29b 727 if ($aliases || $printicons || $ratings) {
728 echo '<table border="0" width="100%" align="center">';
729 if ( $aliases ) {
730 echo '<tr><td align="center" valign="top"><font size=1>' .
731 get_string("aliases","glossary") . ': ' . $aliases . '</td></tr>';
732 }
733 if ($icons) {
734 echo '<tr><td align=right valign=top>'. $icons . '</td></tr>';
735 }
736 if ($ratings) {
737 echo '<tr><td align=right valign=top>';
738 $return = glossary_print_entry_ratings($course, $entry, $ratings);
739 echo '</td></tr>';
740 }
741 echo '</table>';
81bdc9e9 742 }
a359c29b 743 return $return;
81bdc9e9 744}
745
77495793 746function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
1d9ddaaf 747/// valid format values: html : Return the HTML link for the attachment as an icon
748/// text : Return the HTML link for tha attachment as text
749/// blank : Print the output to the screen
750 if ($entry->attachment) {
751 $glossary = get_record("glossary","id",$entry->glossaryid);
752 $entry->course = $glossary->course; //used inside print_attachment
77495793 753 if ($insidetable) {
754 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap>\n";
755 }
1d9ddaaf 756 echo glossary_print_attachments($entry,$format,$align);
77495793 757 if ($insidetable) {
758 echo "</td></tr></table>\n";
759 }
1d9ddaaf 760 }
761}
762
77495793 763function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
1ac87c73 764 if ( $mode == 'approval' and !$entry->approved ) {
77495793 765 if ($insidetable) {
766 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\">\n";
767 }
768 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&eid=$entry->id&mode=$mode\"><img align=\"$align\" src=\"check.gif\" border=0 width=\"34\" height=\"34\"></a>\n";
769 if ($insidetable) {
770 echo "</td></tr></table>\n";
771 }
1d9ddaaf 772 }
773}
07842023 774
c80828fe 775function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {
776// It returns all entries from all glossaries that matches the specified criteria
777// within a given $course. It performs an $extended search if necessary.
778// It restrict the search to only one $glossary if the $glossary parameter is set.
07842023 779
780 global $CFG;
c80828fe 781 if ( !$glossary ) {
782 if ( $glossaries = get_records("glossary", "course", $course->id) ) {
783 $glos = "";
784 foreach ( $glossaries as $glossary ) {
785 $glos .= "$glossary->id,";
786 }
787 $glos = substr($ents,0,-1);
788 }
789 } else {
790 $glos = $glossary->id;
791 }
792
07842023 793 if (!isteacher($glossary->course)) {
794 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 795 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 796 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
797 } else {
798
799 $onlyvisible = "";
800 $onlyvisibletable = "";
801 }
802
63dd5fb2 803 /// Some differences in syntax for entrygreSQL
a5e48d96 804 switch ($CFG->dbtype) {
805 case 'postgres7':
07842023 806 $LIKE = "ILIKE"; // case-insensitive
807 $NOTLIKE = "NOT ILIKE"; // case-insensitive
808 $REGEXP = "~*";
809 $NOTREGEXP = "!~*";
a5e48d96 810 break;
811 case 'mysql':
812 default:
07842023 813 $LIKE = "LIKE";
814 $NOTLIKE = "NOT LIKE";
815 $REGEXP = "REGEXP";
816 $NOTREGEXP = "NOT REGEXP";
a5e48d96 817 break;
818 }
07842023 819
820 $conceptsearch = "";
821 $definitionsearch = "";
822
823
824 foreach ($searchterms as $searchterm) {
07842023 825 if ($conceptsearch) {
826 $conceptsearch.= " OR ";
827 }
828 if ($definitionsearch) {
829 $definitionsearch.= " OR ";
830 }
831
832 if (substr($searchterm,0,1) == "+") {
833 $searchterm = substr($searchterm,1);
834 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
835 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
836 } else if (substr($searchterm,0,1) == "-") {
837 $searchterm = substr($searchterm,1);
838 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
839 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
840 } else {
841 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
842 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
843 }
844 }
845
c80828fe 846 if ( !$extended ) {
ed0201dd 847 $definitionsearch = "0";
848 }
07842023 849
850 $selectsql = "{$CFG->prefix}glossary_entries e,
851 {$CFG->prefix}glossary g $onlyvisibletable
852 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 853 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c80828fe 854 AND g.id IN ($glos) AND e.approved != 0";
07842023 855
4d4c38f3 856 return get_records_sql("SELECT e.*
857 FROM $selectsql ORDER BY e.concept ASC");
07842023 858}
859
c80828fe 860function glossary_search_entries($searchterms, $glossary, $extended) {
861 $course = get_record("course","id",$glossary->course);
862 return glossary_search($course,$searchterms,$extended,$glossary);
863}
864
e179048e 865function glossary_file_area_name($entry) {
e179048e 866 global $CFG;
e29ad7eb 867// Creates a directory file name, suitable for make_upload_directory()
e179048e 868
e29ad7eb 869 // I'm doing this workaround for make it works for delete_instance also
870 // (when called from delete_instance, glossary is already deleted so
871 // getting the course from mdl_glossary does not work)
872 $module = get_record("modules","name","glossary");
873 $cm = get_record("course_modules","module",$module->id,"instance",$entry->glossaryid);
874 return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
e179048e 875}
876
877function glossary_file_area($entry) {
878 return make_upload_directory( glossary_file_area_name($entry) );
879}
880
e29ad7eb 881function glossary_main_file_area($glossary) {
882 $modarea = glossary_mod_file_area($glossary);
883 return "$modarea/$glossary->id";
884}
885
886function glossary_mod_file_area($glossary) {
887 global $CFG;
888
889 return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
890}
891
e179048e 892function glossary_delete_old_attachments($entry, $exception="") {
893// Deletes all the user files in the attachments area for a entry
894// EXCEPT for any file named $exception
895
896 if ($basedir = glossary_file_area($entry)) {
897 if ($files = get_directory_list($basedir)) {
898 foreach ($files as $file) {
899 if ($file != $exception) {
900 unlink("$basedir/$file");
901// notify("Existing file '$file' has been deleted!");
902 }
903 }
904 }
905 if (!$exception) { // Delete directory as well, if empty
906 rmdir("$basedir");
907 }
908 }
909}
072f7533 910function glossary_delete_attachments($glossary) {
e29ad7eb 911// Deletes all the user files in the attachments area for the glossary
912 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
913 $deleted = 0;
914 foreach ($entries as $entry) {
915 if ( $entry->attachment ) {
916 if ($basedir = glossary_file_area($entry)) {
917 if ($files = get_directory_list($basedir)) {
918 foreach ($files as $file) {
919 unlink("$basedir/$file");
920 }
921 }
922 rmdir("$basedir");
923 $deleted++;
924 }
925 }
926 }
927 if ( $deleted ) {
928 $attachmentdir = glossary_main_file_area($glossary);
929 $glossarydir = glossary_mod_file_area($glossary);
930
931 rmdir("$attachmentdir");
932 if (!$files = get_directory_list($glossarydir) ) {
933 rmdir( "$glossarydir" );
934 }
935 }
936 }
937}
e179048e 938
939function glossary_copy_attachments($entry, $newentry) {
940/// Given a entry object that is being copied to glossaryid,
941/// this function checks that entry
942/// for attachments, and if any are found, these are
943/// copied to the new glossary directory.
944
945 global $CFG;
946
947 $return = true;
948
949 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
950 foreach ($entries as $curentry) {
951 $oldentry->id = $entry->id;
952 $oldentry->course = $entry->course;
953 $oldentry->glossaryid = $curentry->glossaryid;
954 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
955 if (is_dir($oldentrydir)) {
956
957 $newentrydir = glossary_file_area($newentry);
958 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
959 $return = false;
960 }
961 }
962 }
963 }
964 return $return;
965}
966
967function glossary_move_attachments($entry, $glossaryid) {
968/// Given a entry object that is being moved to glossaryid,
969/// this function checks that entry
970/// for attachments, and if any are found, these are
971/// moved to the new glossary directory.
972
973 global $CFG;
974
975 $return = true;
976
977 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
978 foreach ($entries as $entry) {
979 $oldentry->course = $entry->course;
980 $oldentry->glossaryid = $entry->glossaryid;
981 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
982 if (is_dir($oldentrydir)) {
983 $newentry = $oldentry;
984 $newentry->glossaryid = $glossaryid;
985 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
986 if (! @rename($oldentrydir, $newentrydir)) {
987 $return = false;
988 }
989 }
990 }
991 }
992 return $return;
993}
994
995function glossary_add_attachment($entry, $newfile) {
996// $entry is a full entry record, including course and glossary
997// $newfile is a full upload array from $_FILES
998// If successful, this function returns the name of the file
999
1000 global $CFG;
1001
1002 if (empty($newfile['name'])) {
1003 return "";
1004 }
1005
1006 $newfile_name = clean_filename($newfile['name']);
1007
1008 if (valid_uploaded_file($newfile)) {
1009 if (! $newfile_name) {
1010 notify("This file had a wierd filename and couldn't be uploaded");
1011
1012 } else if (! $dir = glossary_file_area($entry)) {
1013 notify("Attachment could not be stored");
1014 $newfile_name = "";
1015
1016 } else {
1017 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
1018 chmod("$dir/$newfile_name", $CFG->directorypermissions);
1019 glossary_delete_old_attachments($entry, $newfile_name);
1020 } else {
1021 notify("An error happened while saving the file on the server");
1022 $newfile_name = "";
1023 }
1024 }
1025 } else {
1026 $newfile_name = "";
1027 }
1028
1029 return $newfile_name;
1030}
1031
de53b9ac 1032function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 1033// if return=html, then return a html string.
1034// if return=text, then return a text-only string.
1035// otherwise, print HTML for non-images, and return image HTML
b764feaa 1036// if attachment is an image, $align set its aligment.
e179048e 1037 global $CFG;
b764feaa 1038
1039 $newentry = $entry;
1040 if ( $newentry->sourceglossaryid ) {
1041 $newentry->glossaryid = $newentry->sourceglossaryid;
1042 }
e179048e 1043
b764feaa 1044 $filearea = glossary_file_area_name($newentry);
e179048e 1045
1046 $imagereturn = "";
1047 $output = "";
1048
b764feaa 1049 if ($basedir = glossary_file_area($newentry)) {
e179048e 1050 if ($files = get_directory_list($basedir)) {
1051 $strattachment = get_string("attachment", "glossary");
1052 $strpopupwindow = get_string("popupwindow");
1053 foreach ($files as $file) {
1054 $icon = mimeinfo("icon", $file);
1055 if ($CFG->slasharguments) {
1056 $ffurl = "file.php/$filearea/$file";
1057 } else {
1058 $ffurl = "file.php?file=/$filearea/$file";
1059 }
096b5432 1060 $image = "<img border=0 src=\"$CFG->pixpath/f/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
e179048e 1061
1062 if ($return == "html") {
1063 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1064 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
1065 } else if ($return == "text") {
1066 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1067
1068 } else {
1069 if ($icon == "image.gif") { // Image attachments don't get printed as links
77495793 1070 $imagereturn .= "<img src=\"$CFG->wwwroot/$ffurl\" align=$align>";
e179048e 1071 } else {
1072 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1073 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1074 echo "<br />";
1075 }
1076 }
1077 }
1078 }
1079 }
1080
1081 if ($return) {
1082 return $output;
1083 }
1084
1085 return $imagereturn;
1086}
1087
7dd88447 1088function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 1089
1090if ( !$tTHEME ) {
1091 global $THEME;
1092 $tTHEME = $THEME;
1093}
1094
7dd88447 1095$tablecolor = $tTHEME->TabTableBGColor;
1096$currenttabcolor = $tTHEME->ActiveTabColor;
1097$tabcolor = $tTHEME->InactiveTabColor;
1098$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 1099
7dd88447 1100$tablewidth = $tTHEME->TabTableWidth;
1101$tabsperrow = $tTHEME->TabsPerRow;
1102$tabseparation = $tTHEME->TabSeparation;
06d94a52 1103
7dd88447 1104$tabs = count($data);
1105$tabwidth = (int) (100 / $tabsperrow);
06d94a52 1106
7dd88447 1107$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
1108
1109$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 1110
1111?>
1112 <center>
7dd88447 1113 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 1114 <tr>
1115 <td width="100%">
1116
1117 <table border="0" cellpadding="0" cellspacing="0" width="100%">
1118
ad58adac 1119<?php
7dd88447 1120$tabproccessed = 0;
1121for ($row = 0; $row < $numrows; $row++) {
06d94a52 1122 echo "<tr>\n";
7dd88447 1123 if ( $row != $currentrow ) {
1124 for ($col = 0; $col < $tabsperrow; $col++) {
1125 if ( $tabproccessed < $tabs ) {
ad58adac 1126 if ( $col == 0 ) {
7dd88447 1127 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 1128 }
7dd88447 1129 if ($tabproccessed == $currenttab) {
1130 $currentcolor = $currenttabcolor;
e94c179a 1131 $currentstyle = 'generaltabselected';
06d94a52 1132 } else {
7dd88447 1133 $currentcolor = $tabcolor;
e94c179a 1134 $currentstyle = 'generaltab';
06d94a52 1135 }
505eec9c 1136 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 1137 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
1138 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 1139 }
7dd88447 1140
ed0201dd 1141 if ( !$data[$tabproccessed]->link ) {
1142 echo "<font color=\"$inactivefontcolor\">";
1143 }
7dd88447 1144 echo $data[$tabproccessed]->caption;
ed0201dd 1145 if ( !$data[$tabproccessed]->link ) {
1146 echo "</font>";
1147 }
7dd88447 1148
1149 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 1150 echo "</a>";
1151 }
1152 echo "</b></td>";
1153
7dd88447 1154 if ( $col < $tabsperrow ) {
1155 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 1156 }
06d94a52 1157 } else {
7dd88447 1158 $currentcolor = "";
06d94a52 1159 }
7dd88447 1160 $tabproccessed++;
06d94a52 1161 }
1162 } else {
7dd88447 1163 $firsttabincurrentrow = $tabproccessed;
1164 $tabproccessed += $tabsperrow;
06d94a52 1165 }
7dd88447 1166 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 1167}
1168 echo "<tr>\n";
7dd88447 1169 $tabproccessed = $firsttabincurrentrow;
1170 for ($col = 0; $col < $tabsperrow; $col++) {
1171 if ( $tabproccessed < $tabs ) {
ad58adac 1172 if ( $col == 0 ) {
7dd88447 1173 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 1174 }
7dd88447 1175 if ($tabproccessed == $currenttab) {
1176 $currentcolor = $currenttabcolor;
e8ff59d6 1177 $currentstyle = 'generaltabselected';
06d94a52 1178 } else {
7dd88447 1179 $currentcolor = $tabcolor;
e8ff59d6 1180 $currentstyle = 'generaltab';
06d94a52 1181 }
3eb2f7ec 1182
1183 if (!isset($data[$tabproccessed]->link)) {
1184 $data[$tabproccessed]->link = NULL;
1185 }
505eec9c 1186 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 1187 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
1188 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 1189 }
7dd88447 1190
ed0201dd 1191 if ( !$data[$tabproccessed]->link ) {
1192 echo "<font color=\"$inactivefontcolor\">";
1193 }
7dd88447 1194 echo $data[$tabproccessed]->caption;
ed0201dd 1195 if ( !$data[$tabproccessed]->link ) {
1196 echo "</font>";
1197 }
7dd88447 1198
1199 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 1200 echo "</a>";
1201 }
1202 echo "</b></td>";
1203
7dd88447 1204 if ($col < $tabsperrow) {
1205 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 1206 }
06d94a52 1207 } else {
7dd88447 1208 if ($numrows > 1) {
1209 $currentcolor = $tabcolor;
06d94a52 1210 } else {
7dd88447 1211 $currentcolor = "";
06d94a52 1212 }
7dd88447 1213 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 1214 echo "</td>";
1215
7dd88447 1216 $col = $tabsperrow;
06d94a52 1217 }
7dd88447 1218 $tabproccessed++;
06d94a52 1219 }
1220 echo "</tr>\n";
1221 ?>
1222
1223 </table>
1224 </td>
1225 </tr>
1226 <tr>
7dd88447 1227 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr></td>
06d94a52 1228 </tr>
1229 <tr>
7dd88447 1230 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 1231 <center>
ad58adac 1232<?php
06d94a52 1233}
1234
ad58adac 1235function glossary_print_tabbed_table_end() {
06d94a52 1236 echo "</center><p></td></tr></table></center>";
1237}
1238
1ac87c73 1239function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1240 if ($glossary->showalphabet) {
c197e607 1241 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 1242 }
1ac87c73 1243 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1244
c4a35419 1245 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 1246
1ac87c73 1247 glossary_print_all_links($cm, $glossary, $mode, $hook);
c76e673a 1248
db87439a 1249 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
c76e673a 1250}
1251
1ac87c73 1252function glossary_print_addentry_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
ea14e783 1253 echo '<center>' . get_string("explainaddentry","glossary") . '<p>';
1254}
1255
1ac87c73 1256function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1257 echo '<center>' . get_string("explainimport","glossary") . '<p>';
1258}
1259
1ac87c73 1260function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1261 echo '<center>' . get_string("explainexport","glossary") . '<p>';
1262}
1263
1ac87c73 1264function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1265 if ( $mode != 'date' ) {
a359c29b 1266 if ($glossary->showalphabet) {
c197e607 1267 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
1268 }
c76e673a 1269
1ac87c73 1270 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1271
c4a35419 1272 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 1273
1ac87c73 1274 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 1275 } else {
1ac87c73 1276 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1277 }
1278}
1279
1280function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1281 if ($glossary->showalphabet) {
d7143408 1282 echo '<center>' . get_string("explainalphabet","glossary") . '<br />';
c197e607 1283 }
1ac87c73 1284
c4a35419 1285 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1286 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 1287 glossary_print_all_links($cm, $glossary, $mode, $hook);
c4a35419 1288// echo "<br />";
c76e673a 1289}
1290
1ac87c73 1291function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
c76e673a 1292global $CFG, $THEME;
c197e607 1293 echo '<table border=0 width=100%>';
1294 echo '<tr>';
c76e673a 1295
c197e607 1296 echo '<td align=center width=20%>';
840d3ba8 1297 if ( isteacher($glossary->course) ) {
c76e673a 1298 $options['id'] = $cm->id;
1ac87c73 1299 $options['mode'] = 'cat';
1300 $options['hook'] = $hook;
c76e673a 1301 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1302 }
c197e607 1303 echo '</td>';
c76e673a 1304
c197e607 1305 echo '<td align=center width=60%>';
1306 echo '<b>';
c76e673a 1307
1308 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1309 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1310
c76e673a 1311 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1312 $selected = '';
c76e673a 1313 if ( $categories ) {
1314 foreach ($categories as $currentcategory) {
1315 $url = $currentcategory->id;
1316 if ( $category ) {
1317 if ($currentcategory->id == $category->id) {
1318 $selected = $url;
1319 }
1320 }
1321 $menu[$url] = $currentcategory->name;
1322 }
1323 }
1324 if ( !$selected ) {
1325 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1326 }
1327
1328 if ( $category ) {
1329 echo $category->name;
1330 } else {
1ac87c73 1331 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1332
1333 echo get_string("entrieswithoutcategory","glossary");
1334 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1335
1ac87c73 1336 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1337
1338 echo get_string("allcategories","glossary");
1339 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1340
1341 }
1342 }
c197e607 1343 echo '</b></td>';
1344 echo '<td align=center width=20%>';
c76e673a 1345
1ac87c73 1346 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=cat&hook=", $menu, "catmenu", $selected, "",
c76e673a 1347 "", "", false);
677038ee 1348
c197e607 1349 echo '</td>';
1350 echo '</tr>';
c76e673a 1351
c197e607 1352 echo '</table>';
c76e673a 1353}
1354
1ac87c73 1355function glossary_print_all_links($cm, $glossary, $mode, $hook) {
c76e673a 1356global $CFG;
a359c29b 1357 if ( $glossary->showall) {
c76e673a 1358 $strallentries = get_string("allentries", "glossary");
1ac87c73 1359 if ( $hook == 'ALL' ) {
c76e673a 1360 echo "<b>$strallentries</b>";
1361 } else {
1362 $strexplainall = strip_tags(get_string("explainall","glossary"));
1ac87c73 1363 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=ALL\">$strallentries</a>";
c76e673a 1364 }
1365 }
1366}
1367
1ac87c73 1368function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1369global $CFG;
a359c29b 1370 if ( $glossary->showspecial) {
c76e673a 1371 $strspecial = get_string("special", "glossary");
1ac87c73 1372 if ( $hook == 'SPECIAL' ) {
677038ee 1373 echo "<b>$strspecial</b> | ";
1374 } else {
1375 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1ac87c73 1376 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=SPECIAL\">$strspecial</a> | ";
677038ee 1377 }
914cb260 1378 }
c76e673a 1379}
677038ee 1380
c4a35419 1381function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1382global $CFG;
a359c29b 1383 if ( $glossary->showalphabet) {
c4274149 1384 $alphabet = explode(",", get_string("alphabet"));
677038ee 1385 $letters_by_line = 14;
1386 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1387 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1388 echo "<b>$alphabet[$i]</b>";
1389 } else {
c4a35419 1390 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=$alphabet[$i]&sortkey=$sortkey&sortorder=$sortorder\">$alphabet[$i]</a>";
677038ee 1391 }
1392 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1393 echo ' | ';
677038ee 1394 } else {
c197e607 1395 echo '<br>';
677038ee 1396 }
767a31c3 1397 }
677038ee 1398 }
c76e673a 1399}
1400
1ac87c73 1401function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1402global $CFG;
677038ee 1403
c4a35419 1404 $asc = get_string("ascending","glossary");
1405 $desc = get_string("descending","glossary");
1406 $bopen = '<b>';
1407 $bclose = '</b>';
1408
c197e607 1409 $neworder = '';
677038ee 1410 if ( $sortorder ) {
c197e607 1411 if ( $sortorder == 'asc' ) {
1412 $neworder = '&sortorder=desc';
c4a35419 1413 $newordertitle = $desc;
677038ee 1414 } else {
c197e607 1415 $neworder = '&sortorder=asc';
c4a35419 1416 $newordertitle = $asc;
677038ee 1417 }
1418 $icon = " <img src=\"$sortorder.gif\" border=0 width=16 height=16>";
1419 } else {
c4a35419 1420 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1421 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1422 $icon = "";
c4a35419 1423 $newordertitle = $asc;
677038ee 1424 } else {
c4a35419 1425 $newordertitle = $desc;
c197e607 1426 $neworder = '&sortorder=desc';
1427 $icon = ' <img src="asc.gif" border=0 width=16 height=16>';
677038ee 1428 }
1429 }
c4a35419 1430 $ficon = '';
1431 $fneworder = '';
1432 $fbtag = '';
1433 $fendbtag = '';
1434
1435 $sicon = '';
1436 $sneworder = '';
ae078733 1437
1438 $sbtag = '';
1439 $fbtag = '';
1440 $fendbtag = '';
1441 $sendbtag = '';
1442
c4a35419 1443 $sendbtag = '';
1444
1445 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1446 $ficon = $icon;
1447 $fneworder = $neworder;
1448 $fordertitle = $newordertitle;
1449 $sordertitle = $asc;
1450 $fbtag = $bopen;
1451 $fendbtag = $bclose;
1452 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1453 $sicon = $icon;
1454 $sneworder = $neworder;
1455 $fordertitle = $asc;
1456 $sordertitle = $newordertitle;
1457 $sbtag = $bopen;
1458 $sendbtag = $bclose;
677038ee 1459 } else {
c4a35419 1460 $fordertitle = $asc;
1461 $sordertitle = $asc;
677038ee 1462 }
c4a35419 1463
1464 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1465 $forder = 'CREATION';
1466 $sorder = 'UPDATE';
1467 $fsort = get_string("sortbycreation", "glossary");
1468 $ssort = get_string("sortbylastupdate", "glossary");
1469
1470 $sort = get_string("sortchronogically", "glossary");
1471 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1472 $forder = 'FIRSTNAME';
1473 $sorder = 'LASTNAME';
1474 $fsort = get_string("firstname");
1475 $ssort = get_string("lastname");
1476
1477 $sort = get_string("sortby", "glossary");
1478 }
1479
1480 echo "<br>$sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$sorder$sneworder&mode=$mode\">$ssort$sicon</a>$sendbtag | ".
d7143408 1481 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$forder$fneworder&mode=$mode\">$fsort$ficon</a>$fendbtag<br />";
fb443f1a 1482}
ad58adac 1483
1484function glossary_sort_entries ( $entry0, $entry1 ) {
1485 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
1486 return -1;
1487 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
1488 return 1;
1489 } else {
1490 return 0;
1491 }
1492}
1493
ed0201dd 1494function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1495 global $THEME, $CFG, $USER;
1496
1d9ddaaf 1497 $colour = $THEME->cellheading2;
ed0201dd 1498
1499 $user = get_record("user", "id", $comment->userid);
1500 $strby = get_string("writtenby","glossary");
072f7533 1501 $fullname = fullname($user, isteacher($course->id));
ed0201dd 1502
79be5e9a 1503 echo '<table align="center" border="0" width="70%" cellpadding="3" cellspacing="0" class="forumpost">';
1504 echo "<tr>";
1505 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
1506 print_user_picture($user->id, $course->id, $user->picture);
1507 echo "</td>";
072f7533 1508 echo "<td bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
1509 echo "<p>";
d7143408 1510 echo "<font size=2><a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a></font><br />";
79be5e9a 1511 echo "<font size=1>".get_string("lastedited").": ".userdate($comment->timemodified)."</font>";
1512 echo "</p></td></tr>";
ed0201dd 1513
79be5e9a 1514 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostside\" width=\"10\">";
1515 echo "&nbsp;";
1516 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
072f7533 1517
1518 echo format_text($comment->comment, $comment->format);
1519
072f7533 1520 echo "<div align=right><p align=right>";
c197e607 1521 if ( (time() - $comment->timemodified < $CFG->maxeditingtime and $USER->id == $comment->userid) or isteacher($course->id) ) {
79be5e9a 1522 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=edit\"><img
096b5432 1523 alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" height=11 width=11 border=0></a> ";
c197e607 1524 }
1525 if ( $USER->id == $comment->userid or isteacher($course->id) ) {
79be5e9a 1526 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=delete\"><img
096b5432 1527 alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" height=11 width=11 border=0></a>";
c197e607 1528 }
c197e607 1529
072f7533 1530 echo "</p>";
1531 echo "</div>";
1532 echo "</td></tr>\n</table>\n\n";
ed0201dd 1533
ed0201dd 1534}
1535
63dd5fb2 1536function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
a359c29b 1537
1538 global $USER;
1539
63dd5fb2 1540 $ratingsmenuused = false;
1541 if (!empty($ratings) and !empty($USER->id)) {
1542 $useratings = true;
1543 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1544 if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) {
1545 $useratings = false;
1546 }
1547 }
1548 if ($useratings) {
1549 if (isteacher($course->id)) {
1550 glossary_print_ratings_mean($entry->id, $ratings->scale);
1551 if ($USER->id != $entry->userid) {
1552 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1553 $ratingsmenuused = true;
1554 }
1555 } else if ($USER->id == $entry->userid) {
1556 glossary_print_ratings_mean($entry->id, $ratings->scale);
1557 } else if (!empty($ratings->allow) ) {
1558 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1559 $ratingsmenuused = true;
1560 }
1561 }
1562 }
1563 return $ratingsmenuused;
1564}
1565
b1918034 1566function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
a359c29b 1567 global $THEME, $USER,$CFG;
cca6f7f1 1568
1d9ddaaf 1569 $colour = $THEME->cellheading2;
cca6f7f1 1570
1d9ddaaf 1571 echo "\n<center><table width=95% border=0><tr>";
1572 echo "<td width=100%\">";
1573 if ( $entries ) {
1574 foreach ( $entries as $entry ) {
1575 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1576 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1577 }
1d9ddaaf 1578 if (! $course = get_record("course", "id", $glossary->course)) {
1579 error("Glossary is misconfigured - don't know what course it's from");
1580 }
0de786f7 1581 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1582 error("Glossary is misconfigured - don't know what course module it is ");
1583 }
1f63b7c6 1584
1585 //If displayformat is present, override glossary->displayformat
a359c29b 1586 if ($displayformat < 0) {
1f63b7c6 1587 $dp = $glossary->displayformat;
a359c29b 1588 } else {
1f63b7c6 1589 $dp = $displayformat;
1590 }
1591
a359c29b 1592 //Get popupformatname
1593 $format = get_record('glossary_formats','name',$dp);
1594 $displayformat = $format->popupformatname;
1595
1596 //Check displayformat variable and set to default if necessary
1597 if (!$displayformat) {
1598 $displayformat = 'dictionary';
584dcac9 1599 }
1f63b7c6 1600
a359c29b 1601 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
1602 $functionname = 'glossary_show_entry_'.$displayformat;
1603
1604 if (file_exists($formatfile)) {
1605 include_once($formatfile);
1606 if (function_exists($functionname)) {
1607 $functionname($course, $cm, $glossary, $entry,'','','','');
1608 }
1609 }
cca6f7f1 1610 }
cca6f7f1 1611 }
1d9ddaaf 1612 echo "</td>";
1613 echo "</tr></table></center>";
1614}
4f4ca7b5 1615
1ac87c73 1616function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
4f4ca7b5 1617global $CFG;
1618 glossary_check_moddata_dir($glossary);
36062759 1619
1620 if (!$h = glossary_open_xml($glossary)) {
1621 error("An error occurred while opening a file to write to.");
1622 }
4f4ca7b5 1623
1624 $status = fwrite ($h,glossary_start_tag("INFO",1,true));
1625 fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
1626 fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
16527801 1627 fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost));
4f4ca7b5 1628 fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
73e6ac63 1629 fwrite ($h,glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat));
4f4ca7b5 1630 fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
1631 fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
1632 fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
1633 fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
1634 fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
1635 fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
1636 fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
1ac87c73 1637 fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
4f4ca7b5 1638
748b1932 1639 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1640 $status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
1641 foreach ($entries as $entry) {
046a797c 1642 $permissiongranted = 1;
1ac87c73 1643 if ( $hook ) {
1644 switch ( $hook ) {
046a797c 1645 case "ALL":
1646 case "SPECIAL":
1647 break;
1648 default:
1ac87c73 1649 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1650 break;
1651 }
1652 }
1ac87c73 1653 if ( $hook ) {
1654 switch ( $hook ) {
046a797c 1655 case GLOSSARY_SHOW_ALL_CATEGORIES:
1656 break;
1657 case GLOSSARY_SHOW_NOT_CATEGORISED:
1658 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1659 break;
1660 default:
1ac87c73 1661 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1662 break;
1663 }
1664 }
1665 if ( $entry->approved and $permissiongranted ) {
748b1932 1666 $status = fwrite($h,glossary_start_tag("ENTRY",3,true));
b9656030 1667 fwrite($h,glossary_full_tag("CONCEPT",4,false,trim($entry->concept)));
748b1932 1668 fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
1669 fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
1670 fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
1671 fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
1672 fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
1673 fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
1674
7965be79 1675 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1676 $status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
1677 foreach ($aliases as $alias) {
1678 $status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
b9656030 1679 fwrite($h,glossary_full_tag("NAME",6,false,trim($alias->alias)));
7965be79 1680 $status = fwrite($h,glossary_end_tag("ALIAS",5,true));
1681 }
00758851 1682 $status = fwrite($h,glossary_end_tag("ALIASES",4,true));
7965be79 1683 }
748b1932 1684 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1685 $status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
1686 foreach ($catentries as $catentry) {
1687 $category = get_record("glossary_categories","id",$catentry->categoryid);
1688
1689 $status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
1690 fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
1ac87c73 1691 fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
748b1932 1692 $status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
1693 }
1694 $status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
1695 }
4f4ca7b5 1696
748b1932 1697 $status =fwrite($h,glossary_end_tag("ENTRY",3,true));
4f4ca7b5 1698 }
1699 }
748b1932 1700 $status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
1701
4f4ca7b5 1702 }
748b1932 1703
1704
1705 $status =fwrite ($h,glossary_end_tag("INFO",1,true));
1706
4f4ca7b5 1707 $h = glossary_close_xml($h);
1708}
1709// Functions designed by Eloy Lafuente
1710//
1711//Function to create, open and write header of the xml file
1712function glossary_open_xml($glossary) {
1713
1714 global $CFG;
1715
1716 $status = true;
1717
1718 //Open for writing
1719
36062759 1720 $glossaryname = clean_filename(strip_tags($glossary->name));
1721 $pathname = make_upload_directory("$glossary->course/glossary/$glossaryname");
1722 $filename = "$pathname/glossary.xml";
1723
1724 if (!$h = fopen($filename,"w")) {
1725 notify("Error opening '$filename'");
1726 return false;
1727 }
1728
4f4ca7b5 1729 //Writes the header
1730 $status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1731 if ($status) {
1732 $status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
1733 }
1734 if ($status) {
1735 return $h;
1736 } else {
1737 return false;
1738 }
1739}
1740
748b1932 1741function glossary_read_imported_file($file) {
1742require_once "../../lib/xmlize.php";
1743 $h = fopen($file,"r");
1744 $line = '';
1745 if ($h) {
1746 while ( !feof($h) ) {
b9656030 1747 $char = fread($h,1024);
748b1932 1748 $line .= $char;
1749 }
1750 fclose($h);
1751 }
1752 return xmlize($line);
1753}
4f4ca7b5 1754//Close the file
1755function glossary_close_xml($h) {
1756 $status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
1757 return fclose($h);
1758}
1759
1760//Return the xml start tag
1761function glossary_start_tag($tag,$level=0,$endline=false) {
1762 if ($endline) {
1763 $endchar = "\n";
1764 } else {
1765 $endchar = "";
1766 }
1767 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1768}
1769
1770//Return the xml end tag
1771function glossary_end_tag($tag,$level=0,$endline=true) {
1772 if ($endline) {
1773 $endchar = "\n";
1774 } else {
1775 $endchar = "";
1776 }
1777 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1778}
1779
1780//Return the start tag, the contents and the end tag
1781function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
1782 $st = glossary_start_tag($tag,$level,$endline);
1783 $co="";
1784 if ($to_utf) {
1785 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
1786 } else {
1787 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
1788 }
1789 $et = glossary_end_tag($tag,0,true);
1790 return $st.$co.$et;
1791}
1792
1793 //Function to check and create the needed moddata dir to
1794 //save all the mod backup files. We always name it moddata
1795 //to be able to restore it, but in restore we check for
1796 //$CFG->moddata !!
1797function glossary_check_moddata_dir($glossary) {
1798
1799 global $CFG;
1800
1801 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
1802 if ( $status ) {
1803 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
1804 if ( $status ) {
1805 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name),true);
1806 }
1807 }
1808 return $status;
1809}
1810
1811//Function to check if a directory exists
1812//and, optionally, create it
1813function glossary_check_dir_exists($dir,$create=false) {
1814
1815 global $CFG;
1816
1817 $status = true;
1818 if(!is_dir($dir)) {
1819 if (!$create) {
1820 $status = false;
1821 } else {
1822 umask(0000);
1823 $status = mkdir ($dir,$CFG->directorypermissions);
1824 }
1825 }
1826 return $status;
1827}
63dd5fb2 1828/*
1829* Adding grading functions
1830*/
1831
1832function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1833/// Returns a list of ratings for a particular entry - sorted.
1834 global $CFG;
1835 return get_records_sql("SELECT u.*, r.rating, r.time
1836 FROM {$CFG->prefix}glossary_ratings r,
1837 {$CFG->prefix}user u
1838 WHERE r.entryid = '$entryid'
1839 AND r.userid = u.id
1840 ORDER BY $sort");
1841}
1842
1843function glossary_get_user_grades($glossaryid) {
1844/// Get all user grades for a glossary
1845 global $CFG;
1846
1847 return get_records_sql("SELECT r.id, e.userid, r.rating
1848 FROM {$CFG->prefix}glossary_entries e,
1849 {$CFG->prefix}glossary_ratings r
1850 WHERE e.glossaryid = '$glossaryid'
1851 AND r.entryid = e.id
1852 ORDER by e.userid ");
1853}
1854
1855function glossary_count_unrated_entries($glossaryid, $userid) {
1856// How many unrated entries are in the given glossary for a given user?
1857 global $CFG;
1858 if ($entries = get_record_sql("SELECT count(*) as num
1859 FROM {$CFG->prefix}glossary_entries
1860 WHERE glossaryid = '$glossaryid'
1861 AND userid <> '$userid' ")) {
1862
1863 if ($rated = get_record_sql("SELECT count(*) as num
1864 FROM {$CFG->prefix}glossary_entries e,
1865 {$CFG->prefix}glossary_ratings r
1866 WHERE e.glossaryid = '$glossaryid'
1867 AND e.id = r.entryid
1868 AND r.userid = '$userid'")) {
1869 $difference = $entries->num - $rated->num;
1870 if ($difference > 0) {
1871 return $difference;
1872 } else {
1873 return 0; // Just in case there was a counting error
1874 }
1875 } else {
1876 return $entries->num;
1877 }
1878 } else {
1879 return 0;
1880 }
1881}
1882
1883function glossary_print_ratings_mean($entryid, $scale) {
1884/// Print the multiple ratings on a entry given to the current user by others.
1885/// Scale is an array of ratings
1886
1887 static $strrate;
1888
1889 $mean = glossary_get_ratings_mean($entryid, $scale);
1890
1891 if ($mean !== "") {
1892
1893 if (empty($strratings)) {
1894 $strratings = get_string("ratings", "glossary");
1895 }
1896
37d543b5 1897 echo "<font size=-1>$strratings: ";
63dd5fb2 1898 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
37d543b5 1899 echo "</font>";
63dd5fb2 1900 }
1901}
1902
1903
1904function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1905/// Return the mean rating of a entry given to the current user by others.
1906/// Scale is an array of possible ratings in the scale
1907/// Ratings is an optional simple array of actual ratings (just integers)
1908
1909 if (!$ratings) {
1910 $ratings = array();
1911 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1912 foreach ($rates as $rate) {
1913 $ratings[] = $rate->rating;
1914 }
1915 }
1916 }
1917
1918 $count = count($ratings);
1919
1920 if ($count == 0) {
1921 return "";
1922
1923 } else if ($count == 1) {
1924 return $scale[$ratings[0]];
1925
1926 } else {
1927 $total = 0;
1928 foreach ($ratings as $rating) {
1929 $total += $rating;
1930 }
1931 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1932
1933 if (isset($scale[$mean])) {
1934 return $scale[$mean]." ($count)";
1935 } else {
1936 return "$mean ($count)"; // Should never happen, hopefully
1937 }
1938 }
1939}
1940
1941function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1942/// Return a summary of entry ratings given to the current user by others.
1943/// Scale is an array of possible ratings in the scale
1944/// Ratings is an optional simple array of actual ratings (just integers)
1945
1946 if (!$ratings) {
1947 $ratings = array();
1948 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1949 foreach ($rates as $rate) {
1950 $rating[] = $rate->rating;
1951 }
1952 }
1953 }
1954
1955
1956 if (!$count = count($ratings)) {
1957 return "";
1958 }
1959
1960
1961 foreach ($scale as $key => $scaleitem) {
1962 $sumrating[$key] = 0;
1963 }
1964
1965 foreach ($ratings as $rating) {
1966 $sumrating[$rating]++;
1967 }
1968
1969 $summary = "";
1970 foreach ($scale as $key => $scaleitem) {
1971 $summary = $sumrating[$key].$summary;
1972 if ($key > 1) {
1973 $summary = "/$summary";
1974 }
1975 }
1976 return $summary;
1977}
1978
1979function glossary_print_rating_menu($entryid, $userid, $scale) {
1980/// Print the menu of ratings as part of a larger form.
1981/// If the entry has already been - set that value.
1982/// Scale is an array of ratings
1983
1984 static $strrate;
1985
1986 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
1987 $rating->rating = 0;
1988 }
1989
1990 if (empty($strrate)) {
1991 $strrate = get_string("rate", "glossary");
1992 }
1993
1994 choose_from_menu($scale, $entryid, $rating->rating, "$strrate...");
1995}
1996
cc41c129 1997?>