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