- Minor change (aliases by Keywords, more intuitive, I think)
[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
767a31c3 21define("GLOSSARY_FORMAT_SIMPLE", 0);
22define("GLOSSARY_FORMAT_CONTINUOUS", 1);
23
07842023 24function glossary_add_instance($glossary) {
25/// Given an object containing all the necessary data,
26/// (defined by the form in mod.html) this function
27/// will create a new instance and return the id number
28/// of the new instance.
29
63dd5fb2 30 if (!$glossary->userating) {
31 $glossary->assessed = 0;
32 }
33
34 if (!empty($glossary->ratingtime)) {
35 $glossary->assesstimestart = make_timestamp($glossary->startyear, $glossary->startmonth, $glossary->startday,
36 $glossary->starthour, $glossary->startminute, 0);
37 $glossary->assesstimefinish = make_timestamp($glossary->finishyear, $glossary->finishmonth, $glossary->finishday,
38 $glossary->finishhour, $glossary->finishminute, 0);
39 } else {
40 $glossary->assesstimestart = 0;
41 $glossary->assesstimefinish = 0;
42 }
43
0de786f7 44 if ( !isset($glossary->globalglossary) ) {
45 $glossary->globalglossary = 0;
46 } elseif ( !isadmin() ) {
47 $glossary->globalglossary = 0;
48 }
49
07842023 50 $glossary->timecreated = time();
51 $glossary->timemodified = $glossary->timecreated;
52
53 # May have to add extra stuff in here #
54
55 return insert_record("glossary", $glossary);
56}
57
58
59function glossary_update_instance($glossary) {
60/// Given an object containing all the necessary data,
61/// (defined by the form in mod.html) this function
62/// will update an existing instance with new data.
5eaa2d34 63global $CFG;
0de786f7 64 if ( !isadmin() ) {
65 unset($glossary->globalglossary);
66 }
67
07842023 68 $glossary->timemodified = time();
69 $glossary->id = $glossary->instance;
70
63dd5fb2 71 if (!$glossary->userating) {
72 $glossary->assessed = 0;
73 }
74
75 if (!empty($glossary->ratingtime)) {
76 $glossary->assesstimestart = make_timestamp($glossary->startyear, $glossary->startmonth, $glossary->startday,
77 $glossary->starthour, $glossary->startminute, 0);
78 $glossary->assesstimefinish = make_timestamp($glossary->finishyear, $glossary->finishmonth, $glossary->finishday,
79 $glossary->finishhour, $glossary->finishminute, 0);
80 } else {
81 $glossary->assesstimestart = 0;
82 $glossary->assesstimefinish = 0;
83 }
84
5eaa2d34 85 $return = update_record("glossary", $glossary);
86 if ($return and $glossary->defaultapproval) {
87 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
88 }
07842023 89
5eaa2d34 90 return $return;
07842023 91}
92
93
94function glossary_delete_instance($id) {
95/// Given an ID of an instance of this module,
96/// this function will permanently delete the instance
97/// and any data that depends on it.
98
99 if (! $glossary = get_record("glossary", "id", "$id")) {
100 return false;
101 }
102
103 $result = true;
104
105 # Delete any dependent records here #
106
107 if (! delete_records("glossary", "id", "$glossary->id")) {
108 $result = false;
49210b90 109 } else {
110 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
111 $cats = "";
1ac87c73 112 foreach ( $categories as $hook ) {
49210b90 113 $cats .= "$cat->id,";
114 }
115 $cats = substr($cats,0,-1);
116 if ($cats) {
117 delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
118 delete_records("glossary_categories", "glossaryid", $glossary->id);
119 }
120 }
121 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
122 $ents = "";
123 foreach ( $entries as $entry ) {
124 $ents .= "$entry->id,";
125 }
126 $ents = substr($ents,0,-1);
127 if ($ents) {
128 delete_records_select("glossary_comments", "entryid in ($ents)");
ea14e783 129 delete_records_select("glossary_alias", "entryid in ($ents)");
49210b90 130 }
131 }
132 delete_records("glossary_entries", "glossaryid", "$glossary->id");
07842023 133 }
07842023 134
135 return $result;
136}
137
138function glossary_user_outline($course, $user, $mod, $glossary) {
139/// Return a small object with summary information about what a
140/// user has done with a given particular instance of this module
141/// Used for user activity reports.
142/// $return->time = the time they did it
143/// $return->info = a short text description
144
1ac87c73 145 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
146 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
147
63dd5fb2 148 $lastentry = array_pop($entries);
149 $result->time = $lastentry->timemodified;
1ac87c73 150 return $result;
151 }
152 return NULL;
153}
154
155function glossary_get_user_entries($glossaryid, $userid) {
156/// Get all the entries for a user in a glossary
157 global $CFG;
158
159 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
160 FROM {$CFG->prefix}glossary g,
161 {$CFG->prefix}glossary_entries e,
162 {$CFG->prefix}user u
163 WHERE g.id = '$glossaryid'
164 AND e.glossaryid = g.id
165 AND e.userid = '$userid'
166 AND e.userid = u.id
167 ORDER BY e.timemodified ASC");
07842023 168}
169
170function glossary_user_complete($course, $user, $mod, $glossary) {
171/// Print a detailed representation of what a user has done with
172/// a given particular instance of this module, for user activity reports.
1ac87c73 173 global $CFG;
07842023 174
1ac87c73 175 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
176 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
177 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
178 print_simple_box_start("center","70%");
179 } else {
180 echo '<table width="95%" boder="0"><tr><td>';
181 }
182 foreach ($entries as $entry) {
183 $cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
184 glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
185 echo '<p>';
186 }
187 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
188 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
189 print_simple_box_end();
190 } else {
191 echo '</td></tr></table>';
192 }
193 }
07842023 194}
195
196function glossary_print_recent_activity($course, $isteacher, $timestart) {
197/// Given a course and a time, this module should find recent activity
198/// that has occurred in glossary activities and print it out.
199/// Return true if there was output, or false is there was none.
200
201 global $CFG, $THEME;
202
203 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
204 "course = '$course->id' AND ".
205 "module = 'glossary' AND ".
c4a35419 206 "(action = 'add entry' OR ".
207 " action = 'approve entry')", "time ASC")) {
07842023 208 return false;
209 }
210
07842023 211 foreach ($logs as $log) {
212 //Create a temp valid module structure (course,id)
1ac87c73 213 $tempmod->course = $log->course;
214 $entry = get_record("glossary_entries","id",$log->info);
215 $tempmod->id = $entry->glossaryid;
216 //Obtain the visible property from the instance
07842023 217 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 218
219 //Only if the mod is visible
c4a35419 220 if ($modvisible and $entry->approved) {
07842023 221 $entries[$log->info] = glossary_log_info($log);
222 $entries[$log->info]->time = $log->time;
223 $entries[$log->info]->url = $log->url;
224 }
07842023 225 }
226
07842023 227 $content = false;
228 if ($entries) {
229 $strftimerecent = get_string("strftimerecent");
230 $content = true;
231 print_headline(get_string("newentries", "glossary").":");
232 foreach ($entries as $entry) {
233 $date = userdate($entry->timemodified, $strftimerecent);
234 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
235 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
236 echo "$entry->concept";
237 echo "</a>\"</font></p>";
238 }
239 }
240
241 return $content;
242}
243
1ac87c73 244function glossary_log_info($log) {
245 global $CFG;
246
247 return get_record_sql("SELECT e.*, u.firstname, u.lastname
248 FROM {$CFG->prefix}glossary_entries e,
249 {$CFG->prefix}user u
250 WHERE e.id = '$log->info'
251 AND u.id = '$log->userid'");
252}
253
07842023 254function glossary_cron () {
255/// Function to be run periodically according to the moodle cron
256/// This function searches for things that need to be done, such
257/// as sending out mail, toggling flags etc ...
258
259 global $CFG;
260
261 return true;
262}
263
264function glossary_grades($glossaryid) {
265/// Must return an array of grades for a given instance of this module,
266/// indexed by user. It also returns a maximum allowed grade.
63dd5fb2 267 if (!$glossary = get_record("glossary", "id", $glossaryid)) {
268 return false;
269 }
270 if (!$glossary->assessed) {
271 return false;
272 }
273 $scalemenu = make_grades_menu($glossary->scale);
274
275 $currentuser = 0;
276 $ratingsuser = array();
277
278 if ($ratings = glossary_get_user_grades($glossaryid)) {
279 foreach ($ratings as $rating) { // Ordered by user
280 if ($currentuser and $rating->userid != $currentuser) {
281 if (!empty($ratingsuser)) {
282 if ($glossary->scale < 0) {
283 $return->grades[$currentuser] = glossary_get_ratings_mean(0, $scalemenu, $ratingsuser);
284 $return->grades[$currentuser] .= "<br />".glossary_get_ratings_summary(0, $scalemenu, $ratingsuser);
285 } else {
286 $total = 0;
287 $count = 0;
288 foreach ($ratingsuser as $ra) {
289 $total += $ra;
290 $count ++;
291 }
292 $return->grades[$currentuser] = format_float($total/$count, 2);
293 }
294 } else {
295 $return->grades[$currentuser] = "";
296 }
297 $ratingsuser = array();
298 }
299 $ratingsuser[] = $rating->rating;
300 $currentuser = $rating->userid;
301 }
302 if (!empty($ratingsuser)) {
303 if ($glossary->scale < 0) {
304 $return->grades[$currentuser] = glossary_get_ratings_mean(0, $scalemenu, $ratingsuser);
305 $return->grades[$currentuser] .= "<br />".glossary_get_ratings_summary(0, $scalemenu, $ratingsuser);
306 } else {
307 $total = 0;
308 $count = 0;
309 foreach ($ratingsuser as $ra) {
310 $total += $ra;
311 $count ++;
312 }
313 $return->grades[$currentuser] = format_float((float)$total/(float)$count, 2);
314 }
315 } else {
316 $return->grades[$currentuser] = "";
317 }
318 } else {
319 $return->grades = array();
320 }
07842023 321
63dd5fb2 322 if ($glossary->scale < 0) {
323 $return->maxgrade = "";
324 } else {
325 $return->maxgrade = $glossary->scale;
326 }
07842023 327 return $return;
328}
329
05855091 330function glossary_get_participants($glossaryid) {
331//Returns the users with data in one glossary
332//(users with records in glossary_entries, students)
333
334 global $CFG;
335
336 //Get students
337 $students = get_records_sql("SELECT DISTINCT u.*
338 FROM {$CFG->prefix}user u,
339 {$CFG->prefix}glossary_entries g
340 WHERE g.glossaryid = '$glossaryid' and
341 u.id = g.userid");
342
343 //Return students array (it contains an array of unique users)
344 return ($students);
345}
07842023 346
347//////////////////////////////////////////////////////////////////////////////////////
348/// Any other glossary functions go here. Each of them must have a name that
349/// starts with glossary_
350
1ac87c73 351function glossary_debug($debug,$text,$br=1) {
352 if ( $debug ) {
353 echo '<font color=red>' . $text . '</font>';
354 if ( $br ) {
355 echo '<br>';
356 }
357 }
07842023 358}
359
ea14e783 360function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
07842023 361 global $CFG;
ea14e783 362 if ($pivot) {
363 $pivot .= ",";
364 }
07842023 365
ea14e783 366 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
07842023 367 FROM {$CFG->prefix}glossary_entries
368 WHERE glossaryid = '$glossaryid'
369 AND id IN ($entrylist)");
370}
ea14e783 371function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
c76e673a 372global $CFG;
373 if ($where) {
374 $where = " and $where";
375 }
376 if ($orderby) {
377 $orderby = " ORDER BY $orderby";
378 }
ea14e783 379 if ($pivot) {
380 $pivot .= ",";
381 }
382 return get_records_sql("SELECT $pivot *
c76e673a 383 FROM {$CFG->prefix}glossary_entries
384 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
385}
386
1ac87c73 387function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
c76e673a 388global $CFG;
389 if ($where) {
390 $where = " and $where";
391 }
392 if ($orderby) {
393 $orderby = " ORDER BY $orderby";
394 }
ea14e783 395 if ($pivot) {
396 $pivot .= ",";
397 }
398 return get_records_sql("SELECT $pivot ge.*
c76e673a 399 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
63dd5fb2 400 WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
c76e673a 401 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
402}
07842023 403
63dd5fb2 404function glossary_print_entry($course, $cm, $glossary, $entry, $mode="",$hook="",$printicons = 1, $displayformat = -1, $ratings = NULL) {
914cb260 405 global $THEME, $USER, $CFG;
b1918034 406 if ( $displayformat < 0 ) {
407 $displayformat = $glossary->displayformat;
408 }
1ac87c73 409 if ($entry->approved or ($USER->id == $entry->userid and !isteacher($course->id)) or $mode == 'approval') {
c76e673a 410 $permissiongranted = 0;
b1918034 411 $formatfile = "$CFG->dirroot/mod/glossary/formats/$displayformat.php";
c76e673a 412 $functionname = "glossary_print_entry_by_format";
413
b1918034 414 $basicformat = ($displayformat == GLOSSARY_FORMAT_SIMPLE or
415 $displayformat == GLOSSARY_FORMAT_CONTINUOUS);
c76e673a 416 if ( !$basicformat ) {
417 if ( file_exists($formatfile) ) {
418 include_once($formatfile);
419 if (function_exists($functionname) ) {
420 $permissiongranted = 1;
421 }
422 }
423 } else {
424 $permissiongranted = 1;
e179048e 425 }
06d94a52 426
b1918034 427 if ( !$basicformat and $permissiongranted or $displayformat >= 2) {
63dd5fb2 428 return glossary_print_entry_by_format($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
c76e673a 429 } else {
b1918034 430 switch ( $displayformat ) {
1ac87c73 431 case GLOSSARY_FORMAT_SIMPLE:
63dd5fb2 432 return glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
1ac87c73 433 break;
434 case GLOSSARY_FORMAT_CONTINUOUS:
63dd5fb2 435 return glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
1ac87c73 436 break;
c76e673a 437 }
767a31c3 438 }
07842023 439 }
07842023 440}
81bdc9e9 441function glossary_print_entry_concept($entry) {
ea14e783 442 echo $entry->concept;
81bdc9e9 443}
1d9ddaaf 444
445function glossary_print_entry_definition($entry) {
f287e69c 446 $definition = $entry->definition;
447
448 //getting ride of A tags
449 $links = array();
450 preg_match_all('/<img (.+?)>/is',$definition,$list_of_links);
451
452 foreach (array_unique($list_of_links[0]) as $key=>$value) {
453 $links['<@'.$key.'@>'] = $value;
454 }
455 if ( $links ) {
456 $definition = str_replace($links,array_keys($links),$definition);
457 }
458
459 //getting ride of IMG tags
460 $images = array();
461 preg_match_all('/<A (.+?)>/is',$definition,$list_of_images);
462
463 foreach (array_unique($list_of_images[0]) as $key=>$value) {
464 $images['<@'.$key.'@>'] = $value;
465 }
466 if ( $images ) {
467 $definition = str_replace($images,array_keys($images),$definition);
468 }
469
470 $definition = str_ireplace($entry->concept,"<nolink>$entry->concept</nolink>",$definition);
471
472 //restoring A tags
473 if ( $links ) {
474 $definition = str_replace(array_keys($links),$links,$definition);
475 }
476
477 //restoring IMG tags
478 if ( $images ) {
479 $definition = str_replace(array_keys($images),$images,$definition);
480 }
481
584dcac9 482/* if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
483 foreach ($aliases as $alias) {
484 echo "<small><font color=red>$alias->alias: ";
485 $definition = str_ireplace($alias->alias,"<nolink>$alias->alias</nolink>",$definition);
486 echo "<b>" . format_text($definition,2) . "</b>";
487 echo "</font></small><br>";
488 }
489 }
490*/
1d9ddaaf 491 echo format_text($definition, $entry->format);
492}
493
1ac87c73 494function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 495 $return = '';
496 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
497 foreach ($aliases as $alias) {
498 if ($alias->alias) {
499 if ($return == '') {
500 $return = '<select style="font-size:8pt">';
501 }
502 $return .= "<option>$alias->alias</option>";
503 }
504 }
505 if ($return != '') {
506 $return .= '</select>';
507// $return = "<table border=0 align=$align><tr><td>$return</td></tr></table>";
508 }
509 }
1ac87c73 510 if ($type == 'print') {
81bdc9e9 511 echo $return;
512 } else {
513 return $return;
514 }
515}
516
1ac87c73 517function glossary_print_entry_icons($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 518 global $THEME, $USER;
519
520 $importedentry = ($entry->sourceglossaryid == $glossary->id);
521 $isteacher = isteacher($course->id);
522 $ismainglossary = $glossary->mainglossary;
523
524 $return = "<font size=1>";
81bdc9e9 525 if (!$entry->approved) {
526 $return .= get_string("entryishidden","glossary");
527 }
a0d1e2bb 528
66d7ae88 529 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
530
531 $return .= "</font> ";
a0d1e2bb 532
533
81bdc9e9 534 if ( $glossary->allowcomments and !isguest()) {
535 $return .= " <a title=\"" . get_string("addcomment","glossary") . "\" href=\"comment.php?id=$cm->id&eid=$entry->id\"><img src=\"comment.gif\" height=16 width=16 border=0></a> ";
536 }
537
63dd5fb2 538 if ($isteacher or $glossary->studentcanentry and $entry->userid == $USER->id) {
81bdc9e9 539 // only teachers can export entries so check it out
540 if ($isteacher and !$ismainglossary and !$importedentry) {
541 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
542 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
543
1ac87c73 544 $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 545
546 }
547 }
548
549 if ( $entry->sourceglossaryid ) {
550 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
551 } else {
552 $icon = "../../pix/t/delete.gif";
553 }
554
555 // Exported entries can be updated/deleted only by teachers in the main glossary
556 if ( !$importedentry and ($isteacher or !$ismainglossary) ) {
1ac87c73 557 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
81bdc9e9 558 $return .= $icon;
559 $return .= "\" height=11 width=11 border=0></a> ";
560
1ac87c73 561 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a>";
81bdc9e9 562 } elseif ( $importedentry ) {
563 $return .= " <font size=-1>" . get_string("exportedentry","glossary") . "</font>";
564 }
565 }
566 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
1ac87c73 567 if ($type == 'print') {
81bdc9e9 568 echo $return;
569 } else {
570 return $return;
a0d1e2bb 571 }
572}
573
574function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
575 $return = '';
576
577 $count = count_records("glossary_comments","entryid",$entry->id);
578 if ($count) {
579 $return = "<font size=1>";
580 $return .= "<a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
581 if ($count == 1) {
582 $return .= get_string("comment", "glossary");
583 } else {
584 $return .= get_string("comments", "glossary");
585 }
586 $return .= "</a>";
587 $return .= "</font>";
588 }
589
590 if ($type == 'print') {
591 echo $return;
592 } else {
593 return $return;
81bdc9e9 594 }
595}
596
1ac87c73 597function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons) {
81bdc9e9 598
1ac87c73 599 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,"html");
600 $icons = "";
601 if ( $printicons ) {
602 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
603 }
81bdc9e9 604 if ( $aliases ) {
605 echo '<table border="0" width="100%" align="center"><tr>' .
606 '<td align="right" width="50%" valign=top><font size=1>' .
607 get_string("aliases","glossary") . ': ' . $aliases . '</td>' .
608 '<td align=right width="50%" valign=top>'.
609 $icons .
610 '</td></tr></table>';
611 } else {
612 echo "<p align=right>$icons";
613 }
614}
615
4f4ca7b5 616function glossary_print_entry_attachment($entry,$format=NULL,$align="right") {
1d9ddaaf 617/// valid format values: html : Return the HTML link for the attachment as an icon
618/// text : Return the HTML link for tha attachment as text
619/// blank : Print the output to the screen
620 if ($entry->attachment) {
621 $glossary = get_record("glossary","id",$entry->glossaryid);
622 $entry->course = $glossary->course; //used inside print_attachment
623 echo "<table border=0 align=$align><tr><td>";
624 echo glossary_print_attachments($entry,$format,$align);
625 echo "</td></tr></table>";
626 }
627}
628
1ac87c73 629function glossary_print_entry_approval($cm, $entry, $mode) {
630 if ( $mode == 'approval' and !$entry->approved ) {
631 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&eid=$entry->id&mode=$mode\"><IMG align=\"right\" src=\"check.gif\" border=0 width=\"34\" height=\"34\"></a>";
1d9ddaaf 632 }
633}
07842023 634
63dd5fb2 635function glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1, $ratings=NULL) {
07842023 636 global $THEME, $USER;
637
638 $colour = $THEME->cellheading2;
639
640 echo "\n<TR>";
1d9ddaaf 641 echo "<TD WIDTH=100% class=\"generalbox\" valign=\"top\" BGCOLOR=\"#FFFFFF\">";
1ac87c73 642 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 643 glossary_print_entry_attachment($entry,"html","right");
644 echo "<b>";
645 glossary_print_entry_concept($entry);
646 echo ":</b> ";
647 glossary_print_entry_definition($entry);
1ac87c73 648 glossary_print_entry_lower_section($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
63dd5fb2 649 echo ' ';
650 $return = glossary_print_entry_ratings($course, $entry, $ratings);
07842023 651 echo "</td>";
652 echo "</TR>";
63dd5fb2 653 return $return;
07842023 654}
655
63dd5fb2 656function glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1, $ratings = NULL) {
767a31c3 657 global $THEME, $USER;
63dd5fb2 658 $return = false;
767a31c3 659 if ($entry) {
1ac87c73 660 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 661 glossary_print_entry_attachment($entry,"html","right");
662 glossary_print_entry_concept($entry);
663 echo " ";
1ac87c73 664
1d9ddaaf 665 glossary_print_entry_definition($entry);
1ac87c73 666 if ( $printicons ) {
667 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
668 echo "($icons)";
669 }
63dd5fb2 670 $return = glossary_print_entry_ratings($course, $entry, $ratings);
ed0201dd 671 }
63dd5fb2 672 return $return;
07842023 673}
674
675function glossary_search_entries($searchterms, $glossary, $includedefinition) {
676/// Returns a list of entries found using an array of search terms
677/// eg word +word -word
678///
679
680 global $CFG;
681
682 if (!isteacher($glossary->course)) {
683 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 684 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 685 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
686 } else {
687
688 $onlyvisible = "";
689 $onlyvisibletable = "";
690 }
691
63dd5fb2 692 /// Some differences in syntax for entrygreSQL
693 if ($CFG->dbtype == "entrygres7") {
07842023 694 $LIKE = "ILIKE"; // case-insensitive
695 $NOTLIKE = "NOT ILIKE"; // case-insensitive
696 $REGEXP = "~*";
697 $NOTREGEXP = "!~*";
698 } else {
699 $LIKE = "LIKE";
700 $NOTLIKE = "NOT LIKE";
701 $REGEXP = "REGEXP";
702 $NOTREGEXP = "NOT REGEXP";
703 }
704
705 $conceptsearch = "";
706 $definitionsearch = "";
707
708
709 foreach ($searchterms as $searchterm) {
07842023 710 if ($conceptsearch) {
711 $conceptsearch.= " OR ";
712 }
713 if ($definitionsearch) {
714 $definitionsearch.= " OR ";
715 }
716
717 if (substr($searchterm,0,1) == "+") {
718 $searchterm = substr($searchterm,1);
719 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
720 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
721 } else if (substr($searchterm,0,1) == "-") {
722 $searchterm = substr($searchterm,1);
723 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
724 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
725 } else {
726 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
727 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
728 }
729 }
730
ed0201dd 731 if ( !$includedefinition ) {
732 $definitionsearch = "0";
733 }
07842023 734
735 $selectsql = "{$CFG->prefix}glossary_entries e,
736 {$CFG->prefix}glossary g $onlyvisibletable
737 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 738 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c76e673a 739 AND g.id = $glossary->id AND e.approved != 0";
07842023 740
4d4c38f3 741 return get_records_sql("SELECT e.*
742 FROM $selectsql ORDER BY e.concept ASC");
07842023 743}
744
e179048e 745function glossary_file_area_name($entry) {
746// Creates a directory file name, suitable for make_upload_directory()
747 global $CFG;
748
749 return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
750}
751
752function glossary_file_area($entry) {
753 return make_upload_directory( glossary_file_area_name($entry) );
754}
755
756function glossary_delete_old_attachments($entry, $exception="") {
757// Deletes all the user files in the attachments area for a entry
758// EXCEPT for any file named $exception
759
760 if ($basedir = glossary_file_area($entry)) {
761 if ($files = get_directory_list($basedir)) {
762 foreach ($files as $file) {
763 if ($file != $exception) {
764 unlink("$basedir/$file");
765// notify("Existing file '$file' has been deleted!");
766 }
767 }
768 }
769 if (!$exception) { // Delete directory as well, if empty
770 rmdir("$basedir");
771 }
772 }
773}
774
775function glossary_copy_attachments($entry, $newentry) {
776/// Given a entry object that is being copied to glossaryid,
777/// this function checks that entry
778/// for attachments, and if any are found, these are
779/// copied to the new glossary directory.
780
781 global $CFG;
782
783 $return = true;
784
785 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
786 foreach ($entries as $curentry) {
787 $oldentry->id = $entry->id;
788 $oldentry->course = $entry->course;
789 $oldentry->glossaryid = $curentry->glossaryid;
790 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
791 if (is_dir($oldentrydir)) {
792
793 $newentrydir = glossary_file_area($newentry);
794 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
795 $return = false;
796 }
797 }
798 }
799 }
800 return $return;
801}
802
803function glossary_move_attachments($entry, $glossaryid) {
804/// Given a entry object that is being moved to glossaryid,
805/// this function checks that entry
806/// for attachments, and if any are found, these are
807/// moved to the new glossary directory.
808
809 global $CFG;
810
811 $return = true;
812
813 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
814 foreach ($entries as $entry) {
815 $oldentry->course = $entry->course;
816 $oldentry->glossaryid = $entry->glossaryid;
817 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
818 if (is_dir($oldentrydir)) {
819 $newentry = $oldentry;
820 $newentry->glossaryid = $glossaryid;
821 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
822 if (! @rename($oldentrydir, $newentrydir)) {
823 $return = false;
824 }
825 }
826 }
827 }
828 return $return;
829}
830
831function glossary_add_attachment($entry, $newfile) {
832// $entry is a full entry record, including course and glossary
833// $newfile is a full upload array from $_FILES
834// If successful, this function returns the name of the file
835
836 global $CFG;
837
838 if (empty($newfile['name'])) {
839 return "";
840 }
841
842 $newfile_name = clean_filename($newfile['name']);
843
844 if (valid_uploaded_file($newfile)) {
845 if (! $newfile_name) {
846 notify("This file had a wierd filename and couldn't be uploaded");
847
848 } else if (! $dir = glossary_file_area($entry)) {
849 notify("Attachment could not be stored");
850 $newfile_name = "";
851
852 } else {
853 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
854 chmod("$dir/$newfile_name", $CFG->directorypermissions);
855 glossary_delete_old_attachments($entry, $newfile_name);
856 } else {
857 notify("An error happened while saving the file on the server");
858 $newfile_name = "";
859 }
860 }
861 } else {
862 $newfile_name = "";
863 }
864
865 return $newfile_name;
866}
867
de53b9ac 868function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 869// if return=html, then return a html string.
870// if return=text, then return a text-only string.
871// otherwise, print HTML for non-images, and return image HTML
b764feaa 872// if attachment is an image, $align set its aligment.
e179048e 873 global $CFG;
b764feaa 874
875 $newentry = $entry;
876 if ( $newentry->sourceglossaryid ) {
877 $newentry->glossaryid = $newentry->sourceglossaryid;
878 }
e179048e 879
b764feaa 880 $filearea = glossary_file_area_name($newentry);
e179048e 881
882 $imagereturn = "";
883 $output = "";
884
b764feaa 885 if ($basedir = glossary_file_area($newentry)) {
e179048e 886 if ($files = get_directory_list($basedir)) {
887 $strattachment = get_string("attachment", "glossary");
888 $strpopupwindow = get_string("popupwindow");
889 foreach ($files as $file) {
890 $icon = mimeinfo("icon", $file);
891 if ($CFG->slasharguments) {
892 $ffurl = "file.php/$filearea/$file";
893 } else {
894 $ffurl = "file.php?file=/$filearea/$file";
895 }
896 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
897
898 if ($return == "html") {
899 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
900 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
901 } else if ($return == "text") {
902 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
903
904 } else {
905 if ($icon == "image.gif") { // Image attachments don't get printed as links
de53b9ac 906 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\" align=$align>";
e179048e 907 } else {
908 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
909 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
910 echo "<br />";
911 }
912 }
913 }
914 }
915 }
916
917 if ($return) {
918 return $output;
919 }
920
921 return $imagereturn;
922}
923
7dd88447 924function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 925
926if ( !$tTHEME ) {
927 global $THEME;
928 $tTHEME = $THEME;
929}
930
7dd88447 931$tablecolor = $tTHEME->TabTableBGColor;
932$currenttabcolor = $tTHEME->ActiveTabColor;
933$tabcolor = $tTHEME->InactiveTabColor;
934$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 935
7dd88447 936$tablewidth = $tTHEME->TabTableWidth;
937$tabsperrow = $tTHEME->TabsPerRow;
938$tabseparation = $tTHEME->TabSeparation;
06d94a52 939
7dd88447 940$tabs = count($data);
941$tabwidth = (int) (100 / $tabsperrow);
06d94a52 942
7dd88447 943$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
944
945$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 946
947?>
948 <center>
7dd88447 949 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 950 <tr>
951 <td width="100%">
952
953 <table border="0" cellpadding="0" cellspacing="0" width="100%">
954
ad58adac 955<?php
7dd88447 956$tabproccessed = 0;
957for ($row = 0; $row < $numrows; $row++) {
06d94a52 958 echo "<tr>\n";
7dd88447 959 if ( $row != $currentrow ) {
960 for ($col = 0; $col < $tabsperrow; $col++) {
961 if ( $tabproccessed < $tabs ) {
ad58adac 962 if ( $col == 0 ) {
7dd88447 963 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 964 }
7dd88447 965 if ($tabproccessed == $currenttab) {
966 $currentcolor = $currenttabcolor;
e94c179a 967 $currentstyle = 'generaltabselected';
06d94a52 968 } else {
7dd88447 969 $currentcolor = $tabcolor;
e94c179a 970 $currentstyle = 'generaltab';
06d94a52 971 }
505eec9c 972 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 973 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
974 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 975 }
7dd88447 976
ed0201dd 977 if ( !$data[$tabproccessed]->link ) {
978 echo "<font color=\"$inactivefontcolor\">";
979 }
7dd88447 980 echo $data[$tabproccessed]->caption;
ed0201dd 981 if ( !$data[$tabproccessed]->link ) {
982 echo "</font>";
983 }
7dd88447 984
985 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 986 echo "</a>";
987 }
988 echo "</b></td>";
989
7dd88447 990 if ( $col < $tabsperrow ) {
991 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 992 }
06d94a52 993 } else {
7dd88447 994 $currentcolor = "";
06d94a52 995 }
7dd88447 996 $tabproccessed++;
06d94a52 997 }
998 } else {
7dd88447 999 $firsttabincurrentrow = $tabproccessed;
1000 $tabproccessed += $tabsperrow;
06d94a52 1001 }
7dd88447 1002 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 1003}
1004 echo "<tr>\n";
7dd88447 1005 $tabproccessed = $firsttabincurrentrow;
1006 for ($col = 0; $col < $tabsperrow; $col++) {
1007 if ( $tabproccessed < $tabs ) {
ad58adac 1008 if ( $col == 0 ) {
7dd88447 1009 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 1010 }
7dd88447 1011 if ($tabproccessed == $currenttab) {
1012 $currentcolor = $currenttabcolor;
e8ff59d6 1013 $currentstyle = 'generaltabselected';
06d94a52 1014 } else {
7dd88447 1015 $currentcolor = $tabcolor;
e8ff59d6 1016 $currentstyle = 'generaltab';
06d94a52 1017 }
505eec9c 1018 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 1019 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
1020 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 1021 }
7dd88447 1022
ed0201dd 1023 if ( !$data[$tabproccessed]->link ) {
1024 echo "<font color=\"$inactivefontcolor\">";
1025 }
7dd88447 1026 echo $data[$tabproccessed]->caption;
ed0201dd 1027 if ( !$data[$tabproccessed]->link ) {
1028 echo "</font>";
1029 }
7dd88447 1030
1031 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 1032 echo "</a>";
1033 }
1034 echo "</b></td>";
1035
7dd88447 1036 if ($col < $tabsperrow) {
1037 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 1038 }
06d94a52 1039 } else {
7dd88447 1040 if ($numrows > 1) {
1041 $currentcolor = $tabcolor;
06d94a52 1042 } else {
7dd88447 1043 $currentcolor = "";
06d94a52 1044 }
7dd88447 1045 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 1046 echo "</td>";
1047
7dd88447 1048 $col = $tabsperrow;
06d94a52 1049 }
7dd88447 1050 $tabproccessed++;
06d94a52 1051 }
1052 echo "</tr>\n";
1053 ?>
1054
1055 </table>
1056 </td>
1057 </tr>
1058 <tr>
7dd88447 1059 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr></td>
06d94a52 1060 </tr>
1061 <tr>
7dd88447 1062 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 1063 <center>
ad58adac 1064<?php
06d94a52 1065}
1066
ad58adac 1067function glossary_print_tabbed_table_end() {
06d94a52 1068 echo "</center><p></td></tr></table></center>";
1069}
1070
1ac87c73 1071function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
c76e673a 1072 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c197e607 1073 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 1074 }
1ac87c73 1075 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1076
c4a35419 1077 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 1078
1ac87c73 1079 glossary_print_all_links($cm, $glossary, $mode, $hook);
c76e673a 1080
db87439a 1081 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
c76e673a 1082}
1083
1ac87c73 1084function glossary_print_addentry_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
ea14e783 1085 echo '<center>' . get_string("explainaddentry","glossary") . '<p>';
1086}
1087
1ac87c73 1088function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1089 echo '<center>' . get_string("explainimport","glossary") . '<p>';
1090}
1091
1ac87c73 1092function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1093 echo '<center>' . get_string("explainexport","glossary") . '<p>';
1094}
1095
1ac87c73 1096function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1097 if ( $mode != 'date' ) {
c197e607 1098 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
1099 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
1100 }
c76e673a 1101
1ac87c73 1102 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1103
c4a35419 1104 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 1105
1ac87c73 1106 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 1107 } else {
1ac87c73 1108 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1109 }
1110}
1111
1112function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1113 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c4a35419 1114 echo '<center>' . get_string("explainalphabet","glossary") . '<br \>';
c197e607 1115 }
1ac87c73 1116
c4a35419 1117 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1118 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 1119 glossary_print_all_links($cm, $glossary, $mode, $hook);
c4a35419 1120// echo "<br />";
c76e673a 1121}
1122
1ac87c73 1123function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
c76e673a 1124global $CFG, $THEME;
c197e607 1125 echo '<table border=0 width=100%>';
1126 echo '<tr>';
c76e673a 1127
c197e607 1128 echo '<td align=center width=20%>';
840d3ba8 1129 if ( isteacher($glossary->course) ) {
c76e673a 1130 $options['id'] = $cm->id;
1ac87c73 1131 $options['mode'] = 'cat';
1132 $options['hook'] = $hook;
c76e673a 1133 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1134 }
c197e607 1135 echo '</td>';
c76e673a 1136
c197e607 1137 echo '<td align=center width=60%>';
1138 echo '<b>';
c76e673a 1139
1140 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1141 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1142
c76e673a 1143 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1144 $selected = '';
c76e673a 1145 if ( $categories ) {
1146 foreach ($categories as $currentcategory) {
1147 $url = $currentcategory->id;
1148 if ( $category ) {
1149 if ($currentcategory->id == $category->id) {
1150 $selected = $url;
1151 }
1152 }
1153 $menu[$url] = $currentcategory->name;
1154 }
1155 }
1156 if ( !$selected ) {
1157 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1158 }
1159
1160 if ( $category ) {
1161 echo $category->name;
1162 } else {
1ac87c73 1163 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1164
1165 echo get_string("entrieswithoutcategory","glossary");
1166 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1167
1ac87c73 1168 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1169
1170 echo get_string("allcategories","glossary");
1171 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1172
1173 }
1174 }
c197e607 1175 echo '</b></td>';
1176 echo '<td align=center width=20%>';
c76e673a 1177
1ac87c73 1178 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=cat&hook=", $menu, "catmenu", $selected, "",
c76e673a 1179 "", "", false);
677038ee 1180
c197e607 1181 echo '</td>';
1182 echo '</tr>';
c76e673a 1183
c197e607 1184 echo '</table>';
c76e673a 1185}
1186
1ac87c73 1187function glossary_print_all_links($cm, $glossary, $mode, $hook) {
c76e673a 1188global $CFG;
1189 if ( $glossary->showall and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
1190 $strallentries = get_string("allentries", "glossary");
1ac87c73 1191 if ( $hook == 'ALL' ) {
c76e673a 1192 echo "<b>$strallentries</b>";
1193 } else {
1194 $strexplainall = strip_tags(get_string("explainall","glossary"));
1ac87c73 1195 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=ALL\">$strallentries</a>";
c76e673a 1196 }
1197 }
1198}
1199
1ac87c73 1200function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1201global $CFG;
1202 if ( $glossary->showspecial and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
1203 $strspecial = get_string("special", "glossary");
1ac87c73 1204 if ( $hook == 'SPECIAL' ) {
677038ee 1205 echo "<b>$strspecial</b> | ";
1206 } else {
1207 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1ac87c73 1208 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=SPECIAL\">$strspecial</a> | ";
677038ee 1209 }
914cb260 1210 }
c76e673a 1211}
677038ee 1212
c4a35419 1213function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1214global $CFG;
1215 if ( $glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
c4274149 1216 $alphabet = explode(",", get_string("alphabet"));
677038ee 1217 $letters_by_line = 14;
1218 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1219 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1220 echo "<b>$alphabet[$i]</b>";
1221 } else {
c4a35419 1222 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 1223 }
1224 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1225 echo ' | ';
677038ee 1226 } else {
c197e607 1227 echo '<br>';
677038ee 1228 }
767a31c3 1229 }
677038ee 1230 }
c76e673a 1231}
1232
1ac87c73 1233function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1234global $CFG;
677038ee 1235
c4a35419 1236 $asc = get_string("ascending","glossary");
1237 $desc = get_string("descending","glossary");
1238 $bopen = '<b>';
1239 $bclose = '</b>';
1240
c197e607 1241 $neworder = '';
677038ee 1242 if ( $sortorder ) {
c197e607 1243 if ( $sortorder == 'asc' ) {
1244 $neworder = '&sortorder=desc';
c4a35419 1245 $newordertitle = $desc;
677038ee 1246 } else {
c197e607 1247 $neworder = '&sortorder=asc';
c4a35419 1248 $newordertitle = $asc;
677038ee 1249 }
1250 $icon = " <img src=\"$sortorder.gif\" border=0 width=16 height=16>";
1251 } else {
c4a35419 1252 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1253 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1254 $icon = "";
c4a35419 1255 $newordertitle = $asc;
677038ee 1256 } else {
c4a35419 1257 $newordertitle = $desc;
c197e607 1258 $neworder = '&sortorder=desc';
1259 $icon = ' <img src="asc.gif" border=0 width=16 height=16>';
677038ee 1260 }
1261 }
c4a35419 1262 $ficon = '';
1263 $fneworder = '';
1264 $fbtag = '';
1265 $fendbtag = '';
1266
1267 $sicon = '';
1268 $sneworder = '';
ae078733 1269
1270 $sbtag = '';
1271 $fbtag = '';
1272 $fendbtag = '';
1273 $sendbtag = '';
1274
c4a35419 1275 $sendbtag = '';
1276
1277 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1278 $ficon = $icon;
1279 $fneworder = $neworder;
1280 $fordertitle = $newordertitle;
1281 $sordertitle = $asc;
1282 $fbtag = $bopen;
1283 $fendbtag = $bclose;
1284 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1285 $sicon = $icon;
1286 $sneworder = $neworder;
1287 $fordertitle = $asc;
1288 $sordertitle = $newordertitle;
1289 $sbtag = $bopen;
1290 $sendbtag = $bclose;
677038ee 1291 } else {
c4a35419 1292 $fordertitle = $asc;
1293 $sordertitle = $asc;
677038ee 1294 }
c4a35419 1295
1296 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1297 $forder = 'CREATION';
1298 $sorder = 'UPDATE';
1299 $fsort = get_string("sortbycreation", "glossary");
1300 $ssort = get_string("sortbylastupdate", "glossary");
1301
1302 $sort = get_string("sortchronogically", "glossary");
1303 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1304 $forder = 'FIRSTNAME';
1305 $sorder = 'LASTNAME';
1306 $fsort = get_string("firstname");
1307 $ssort = get_string("lastname");
1308
1309 $sort = get_string("sortby", "glossary");
1310 }
1311
1312 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 | ".
1313 "$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 1314}
ad58adac 1315
1316function glossary_sort_entries ( $entry0, $entry1 ) {
1317 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
1318 return -1;
1319 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
1320 return 1;
1321 } else {
1322 return 0;
1323 }
1324}
1325
ed0201dd 1326function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1327 global $THEME, $CFG, $USER;
1328
1d9ddaaf 1329 $colour = $THEME->cellheading2;
ed0201dd 1330
1331 $user = get_record("user", "id", $comment->userid);
1332 $strby = get_string("writtenby","glossary");
1333
1334 echo "<table class=\"generalbox\" BORDER=1 CELLSPACING=0 valign=top cellpadding=0 width=70% border=0><tr><td>";
1335
1336 echo "\n<TABLE width=\"100%\" BORDER=0 CELLSPACING=0 valign=top cellpadding=5><tr>";
1337
1338 echo "\n<TD BGCOLOR=\"$THEME->cellheading\" WIDTH=25% VALIGN=TOP align=right >";
1339 print_user_picture($user->id, $course->id, $user->picture);
1340 echo "<br><FONT SIZE=2>$strby $user->firstname $user->lastname</font>";
1341 echo "<br><FONT SIZE=1>(".get_string("lastedited").": ".userdate($comment->timemodified).")</FONT></small><br>";
1342 echo "</TD>";
c197e607 1343
1d9ddaaf 1344 echo "<TD valign=top WIDTH=75% BGCOLOR=\"$THEME->cellcontent\">";
ed0201dd 1345 if ($comment) {
c197e607 1346 echo format_text($comment->comment, $comment->format);
ed0201dd 1347 } else {
c197e607 1348 echo "<center>";
ed0201dd 1349 print_string("nocomment", "glossary");
c197e607 1350 echo "</center>";
ed0201dd 1351 }
1352
1353 echo "<p align=right>";
c197e607 1354 if ( (time() - $comment->timemodified < $CFG->maxeditingtime and $USER->id == $comment->userid) or isteacher($course->id) ) {
ed0201dd 1355 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=edit\"><img alt=\"" . get_string("edit") . "\" src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a> ";
c197e607 1356 }
1357 if ( $USER->id == $comment->userid or isteacher($course->id) ) {
ed0201dd 1358 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=delete\"><img alt=\"" . get_string("delete") . "\" src=\"../../pix/t/delete.gif\" height=11 width=11 border=0></a>";
c197e607 1359 }
ed0201dd 1360 echo "</td>";
c197e607 1361
ed0201dd 1362 echo "</tr></TABLE>\n";
1363
1364 echo "</td></tr></table>";
1365}
1366
63dd5fb2 1367function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
1368global $USER;
1369 $ratingsmenuused = false;
1370 if (!empty($ratings) and !empty($USER->id)) {
1371 $useratings = true;
1372 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1373 if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) {
1374 $useratings = false;
1375 }
1376 }
1377 if ($useratings) {
1378 if (isteacher($course->id)) {
1379 glossary_print_ratings_mean($entry->id, $ratings->scale);
1380 if ($USER->id != $entry->userid) {
1381 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1382 $ratingsmenuused = true;
1383 }
1384 } else if ($USER->id == $entry->userid) {
1385 glossary_print_ratings_mean($entry->id, $ratings->scale);
1386 } else if (!empty($ratings->allow) ) {
1387 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1388 $ratingsmenuused = true;
1389 }
1390 }
1391 }
1392 return $ratingsmenuused;
1393}
1394
b1918034 1395function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
1d9ddaaf 1396 global $THEME, $USER;
cca6f7f1 1397
1d9ddaaf 1398 $colour = $THEME->cellheading2;
cca6f7f1 1399
1d9ddaaf 1400 echo "\n<center><table width=95% border=0><tr>";
1401 echo "<td width=100%\">";
1402 if ( $entries ) {
1403 foreach ( $entries as $entry ) {
1404 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1405 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1406 }
1d9ddaaf 1407 if (! $course = get_record("course", "id", $glossary->course)) {
1408 error("Glossary is misconfigured - don't know what course it's from");
1409 }
0de786f7 1410 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1411 error("Glossary is misconfigured - don't know what course module it is ");
1412 }
584dcac9 1413 $dp = $displayformat;
1414 // Hard-coded until the Display formats manager is done.
b6862360 1415 if ( $dprecord = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
1416 if ( $dprecord->relatedview >= 0 ) {
1417 $dp = $dprecord->relatedview;
1418 }
584dcac9 1419 }
1420 glossary_print_entry($course, $cm, $glossary, $entry, "","",0,$dp);
cca6f7f1 1421 }
cca6f7f1 1422 }
1d9ddaaf 1423 echo "</td>";
1424 echo "</tr></table></center>";
1425}
4f4ca7b5 1426
1ac87c73 1427function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
4f4ca7b5 1428global $CFG;
1429 glossary_check_moddata_dir($glossary);
1430 $h = glossary_open_xml($glossary);
1431
1432 $status = fwrite ($h,glossary_start_tag("INFO",1,true));
1433 fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
1434 fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
63dd5fb2 1435 fwrite ($h,glossary_full_tag("STUDENTCANentry",2,false,$glossary->studentcanentry));
4f4ca7b5 1436 fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
1437 fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
1438 fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
1439 fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
1440 fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
1441 fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
1442 fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
1443 fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
1ac87c73 1444 fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
4f4ca7b5 1445
748b1932 1446 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1447 $status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
1448 foreach ($entries as $entry) {
046a797c 1449 $permissiongranted = 1;
1ac87c73 1450 if ( $hook ) {
1451 switch ( $hook ) {
046a797c 1452 case "ALL":
1453 case "SPECIAL":
1454 break;
1455 default:
1ac87c73 1456 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1457 break;
1458 }
1459 }
1ac87c73 1460 if ( $hook ) {
1461 switch ( $hook ) {
046a797c 1462 case GLOSSARY_SHOW_ALL_CATEGORIES:
1463 break;
1464 case GLOSSARY_SHOW_NOT_CATEGORISED:
1465 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1466 break;
1467 default:
1ac87c73 1468 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1469 break;
1470 }
1471 }
1472 if ( $entry->approved and $permissiongranted ) {
748b1932 1473 $status = fwrite($h,glossary_start_tag("ENTRY",3,true));
1474 fwrite($h,glossary_full_tag("CONCEPT",4,false,$entry->concept));
1475 fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
1476 fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
1477 fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
1478 fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
1479 fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
1480 fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
1481
7965be79 1482 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1483 $status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
1484 foreach ($aliases as $alias) {
1485 $status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
1486 fwrite($h,glossary_full_tag("NAME",6,false,$alias->alias));
1487 $status = fwrite($h,glossary_end_tag("ALIAS",5,true));
1488 }
00758851 1489 $status = fwrite($h,glossary_end_tag("ALIASES",4,true));
7965be79 1490 }
748b1932 1491 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1492 $status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
1493 foreach ($catentries as $catentry) {
1494 $category = get_record("glossary_categories","id",$catentry->categoryid);
1495
1496 $status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
1497 fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
1ac87c73 1498 fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
748b1932 1499 $status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
1500 }
1501 $status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
1502 }
4f4ca7b5 1503
748b1932 1504 $status =fwrite($h,glossary_end_tag("ENTRY",3,true));
4f4ca7b5 1505 }
1506 }
748b1932 1507 $status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
1508
4f4ca7b5 1509 }
748b1932 1510
1511
1512 $status =fwrite ($h,glossary_end_tag("INFO",1,true));
1513
4f4ca7b5 1514 $h = glossary_close_xml($h);
1515}
1516// Functions designed by Eloy Lafuente
1517//
1518//Function to create, open and write header of the xml file
1519function glossary_open_xml($glossary) {
1520
1521 global $CFG;
1522
1523 $status = true;
1524
1525 //Open for writing
1526
1527 $file = $CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name) ."/glossary.xml";
1528 $h = fopen($file,"w");
1529 //Writes the header
1530 $status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1531 if ($status) {
1532 $status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
1533 }
1534 if ($status) {
1535 return $h;
1536 } else {
1537 return false;
1538 }
1539}
1540
748b1932 1541function glossary_read_imported_file($file) {
1542require_once "../../lib/xmlize.php";
1543 $h = fopen($file,"r");
1544 $line = '';
1545 if ($h) {
1546 while ( !feof($h) ) {
1547 $char = fread($h,1024);
1548 $line .= $char;
1549 }
1550 fclose($h);
1551 }
1552 return xmlize($line);
1553}
4f4ca7b5 1554//Close the file
1555function glossary_close_xml($h) {
1556 $status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
1557 return fclose($h);
1558}
1559
1560//Return the xml start tag
1561function glossary_start_tag($tag,$level=0,$endline=false) {
1562 if ($endline) {
1563 $endchar = "\n";
1564 } else {
1565 $endchar = "";
1566 }
1567 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1568}
1569
1570//Return the xml end tag
1571function glossary_end_tag($tag,$level=0,$endline=true) {
1572 if ($endline) {
1573 $endchar = "\n";
1574 } else {
1575 $endchar = "";
1576 }
1577 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1578}
1579
1580//Return the start tag, the contents and the end tag
1581function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
1582 $st = glossary_start_tag($tag,$level,$endline);
1583 $co="";
1584 if ($to_utf) {
1585 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
1586 } else {
1587 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
1588 }
1589 $et = glossary_end_tag($tag,0,true);
1590 return $st.$co.$et;
1591}
1592
1593 //Function to check and create the needed moddata dir to
1594 //save all the mod backup files. We always name it moddata
1595 //to be able to restore it, but in restore we check for
1596 //$CFG->moddata !!
1597function glossary_check_moddata_dir($glossary) {
1598
1599 global $CFG;
1600
1601 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
1602 if ( $status ) {
1603 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
1604 if ( $status ) {
1605 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name),true);
1606 }
1607 }
1608 return $status;
1609}
1610
1611//Function to check if a directory exists
1612//and, optionally, create it
1613function glossary_check_dir_exists($dir,$create=false) {
1614
1615 global $CFG;
1616
1617 $status = true;
1618 if(!is_dir($dir)) {
1619 if (!$create) {
1620 $status = false;
1621 } else {
1622 umask(0000);
1623 $status = mkdir ($dir,$CFG->directorypermissions);
1624 }
1625 }
1626 return $status;
1627}
63dd5fb2 1628/*
1629* Adding grading functions
1630*/
1631
1632function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1633/// Returns a list of ratings for a particular entry - sorted.
1634 global $CFG;
1635 return get_records_sql("SELECT u.*, r.rating, r.time
1636 FROM {$CFG->prefix}glossary_ratings r,
1637 {$CFG->prefix}user u
1638 WHERE r.entryid = '$entryid'
1639 AND r.userid = u.id
1640 ORDER BY $sort");
1641}
1642
1643function glossary_get_user_grades($glossaryid) {
1644/// Get all user grades for a glossary
1645 global $CFG;
1646
1647 return get_records_sql("SELECT r.id, e.userid, r.rating
1648 FROM {$CFG->prefix}glossary_entries e,
1649 {$CFG->prefix}glossary_ratings r
1650 WHERE e.glossaryid = '$glossaryid'
1651 AND r.entryid = e.id
1652 ORDER by e.userid ");
1653}
1654
1655function glossary_count_unrated_entries($glossaryid, $userid) {
1656// How many unrated entries are in the given glossary for a given user?
1657 global $CFG;
1658 if ($entries = get_record_sql("SELECT count(*) as num
1659 FROM {$CFG->prefix}glossary_entries
1660 WHERE glossaryid = '$glossaryid'
1661 AND userid <> '$userid' ")) {
1662
1663 if ($rated = get_record_sql("SELECT count(*) as num
1664 FROM {$CFG->prefix}glossary_entries e,
1665 {$CFG->prefix}glossary_ratings r
1666 WHERE e.glossaryid = '$glossaryid'
1667 AND e.id = r.entryid
1668 AND r.userid = '$userid'")) {
1669 $difference = $entries->num - $rated->num;
1670 if ($difference > 0) {
1671 return $difference;
1672 } else {
1673 return 0; // Just in case there was a counting error
1674 }
1675 } else {
1676 return $entries->num;
1677 }
1678 } else {
1679 return 0;
1680 }
1681}
1682
1683function glossary_print_ratings_mean($entryid, $scale) {
1684/// Print the multiple ratings on a entry given to the current user by others.
1685/// Scale is an array of ratings
1686
1687 static $strrate;
1688
1689 $mean = glossary_get_ratings_mean($entryid, $scale);
1690
1691 if ($mean !== "") {
1692
1693 if (empty($strratings)) {
1694 $strratings = get_string("ratings", "glossary");
1695 }
1696
1697 echo "$strratings: ";
1698 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
1699 }
1700}
1701
1702
1703function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1704/// Return the mean rating of a entry given to the current user by others.
1705/// Scale is an array of possible ratings in the scale
1706/// Ratings is an optional simple array of actual ratings (just integers)
1707
1708 if (!$ratings) {
1709 $ratings = array();
1710 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1711 foreach ($rates as $rate) {
1712 $ratings[] = $rate->rating;
1713 }
1714 }
1715 }
1716
1717 $count = count($ratings);
1718
1719 if ($count == 0) {
1720 return "";
1721
1722 } else if ($count == 1) {
1723 return $scale[$ratings[0]];
1724
1725 } else {
1726 $total = 0;
1727 foreach ($ratings as $rating) {
1728 $total += $rating;
1729 }
1730 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1731
1732 if (isset($scale[$mean])) {
1733 return $scale[$mean]." ($count)";
1734 } else {
1735 return "$mean ($count)"; // Should never happen, hopefully
1736 }
1737 }
1738}
1739
1740function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1741/// Return a summary of entry ratings given to the current user by others.
1742/// Scale is an array of possible ratings in the scale
1743/// Ratings is an optional simple array of actual ratings (just integers)
1744
1745 if (!$ratings) {
1746 $ratings = array();
1747 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1748 foreach ($rates as $rate) {
1749 $rating[] = $rate->rating;
1750 }
1751 }
1752 }
1753
1754
1755 if (!$count = count($ratings)) {
1756 return "";
1757 }
1758
1759
1760 foreach ($scale as $key => $scaleitem) {
1761 $sumrating[$key] = 0;
1762 }
1763
1764 foreach ($ratings as $rating) {
1765 $sumrating[$rating]++;
1766 }
1767
1768 $summary = "";
1769 foreach ($scale as $key => $scaleitem) {
1770 $summary = $sumrating[$key].$summary;
1771 if ($key > 1) {
1772 $summary = "/$summary";
1773 }
1774 }
1775 return $summary;
1776}
1777
1778function glossary_print_rating_menu($entryid, $userid, $scale) {
1779/// Print the menu of ratings as part of a larger form.
1780/// If the entry has already been - set that value.
1781/// Scale is an array of ratings
1782
1783 static $strrate;
1784
1785 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
1786 $rating->rating = 0;
1787 }
1788
1789 if (empty($strrate)) {
1790 $strrate = get_string("rate", "glossary");
1791 }
1792
1793 choose_from_menu($scale, $entryid, $rating->rating, "$strrate...");
1794}
1795
cc41c129 1796?>