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