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