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