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