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