Another small typo, == instead of =. :-D
[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
7dd88447 11define("GLOSSARY_STANDARD_VIEW", 0);
12define("GLOSSARY_CATEGORY_VIEW", 1);
c197e607 13define("GLOSSARY_DATE_VIEW", 2);
14define("GLOSSARY_APPROVAL_VIEW", 3);
fb443f1a 15
767a31c3 16define("GLOSSARY_FORMAT_SIMPLE", 0);
17define("GLOSSARY_FORMAT_CONTINUOUS", 1);
18
07842023 19function glossary_add_instance($glossary) {
20/// Given an object containing all the necessary data,
21/// (defined by the form in mod.html) this function
22/// will create a new instance and return the id number
23/// of the new instance.
24
0de786f7 25 if ( !isset($glossary->globalglossary) ) {
26 $glossary->globalglossary = 0;
27 } elseif ( !isadmin() ) {
28 $glossary->globalglossary = 0;
29 }
30
07842023 31 $glossary->timecreated = time();
32 $glossary->timemodified = $glossary->timecreated;
33
34 # May have to add extra stuff in here #
35
36 return insert_record("glossary", $glossary);
37}
38
39
40function glossary_update_instance($glossary) {
41/// Given an object containing all the necessary data,
42/// (defined by the form in mod.html) this function
43/// will update an existing instance with new data.
5eaa2d34 44global $CFG;
0de786f7 45 if ( !isadmin() ) {
46 unset($glossary->globalglossary);
47 }
48
07842023 49 $glossary->timemodified = time();
50 $glossary->id = $glossary->instance;
51
5eaa2d34 52 $return = update_record("glossary", $glossary);
53 if ($return and $glossary->defaultapproval) {
54 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
55 }
07842023 56
5eaa2d34 57 return $return;
07842023 58}
59
60
61function glossary_delete_instance($id) {
62/// Given an ID of an instance of this module,
63/// this function will permanently delete the instance
64/// and any data that depends on it.
65
66 if (! $glossary = get_record("glossary", "id", "$id")) {
67 return false;
68 }
69
70 $result = true;
71
72 # Delete any dependent records here #
73
74 if (! delete_records("glossary", "id", "$glossary->id")) {
75 $result = false;
76 }
77 delete_records("glossary_entries", "glossaryid", "$glossary->id");
78
79 return $result;
80}
81
82function glossary_user_outline($course, $user, $mod, $glossary) {
83/// Return a small object with summary information about what a
84/// user has done with a given particular instance of this module
85/// Used for user activity reports.
86/// $return->time = the time they did it
87/// $return->info = a short text description
88
89 return $return;
90}
91
92function glossary_user_complete($course, $user, $mod, $glossary) {
93/// Print a detailed representation of what a user has done with
94/// a given particular instance of this module, for user activity reports.
95
96 return true;
97}
98
99function glossary_print_recent_activity($course, $isteacher, $timestart) {
100/// Given a course and a time, this module should find recent activity
101/// that has occurred in glossary activities and print it out.
102/// Return true if there was output, or false is there was none.
103
104 global $CFG, $THEME;
105
106 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
107 "course = '$course->id' AND ".
108 "module = 'glossary' AND ".
109 "action = 'add %' ", "time ASC")) {
110 return false;
111 }
112
07842023 113 foreach ($logs as $log) {
114 //Create a temp valid module structure (course,id)
115 $tempmod->course = $log->course;
116 $tempmod->id = $log->info;
117 //Obtain the visible property from the instance
118 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 119
120 //Only if the mod is visible
121 if ($modvisible) {
122 $entries[$log->info] = glossary_log_info($log);
123 $entries[$log->info]->time = $log->time;
124 $entries[$log->info]->url = $log->url;
125 }
07842023 126 }
127
07842023 128 $content = false;
129 if ($entries) {
130 $strftimerecent = get_string("strftimerecent");
131 $content = true;
132 print_headline(get_string("newentries", "glossary").":");
133 foreach ($entries as $entry) {
134 $date = userdate($entry->timemodified, $strftimerecent);
135 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
136 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
137 echo "$entry->concept";
138 echo "</a>\"</font></p>";
139 }
140 }
141
142 return $content;
143}
144
145function glossary_cron () {
146/// Function to be run periodically according to the moodle cron
147/// This function searches for things that need to be done, such
148/// as sending out mail, toggling flags etc ...
149
150 global $CFG;
151
152 return true;
153}
154
155function glossary_grades($glossaryid) {
156/// Must return an array of grades for a given instance of this module,
157/// indexed by user. It also returns a maximum allowed grade.
158
159 $return->grades = NULL;
160 $return->maxgrade = NULL;
161
162 return $return;
163}
164
05855091 165function glossary_get_participants($glossaryid) {
166//Returns the users with data in one glossary
167//(users with records in glossary_entries, students)
168
169 global $CFG;
170
171 //Get students
172 $students = get_records_sql("SELECT DISTINCT u.*
173 FROM {$CFG->prefix}user u,
174 {$CFG->prefix}glossary_entries g
175 WHERE g.glossaryid = '$glossaryid' and
176 u.id = g.userid");
177
178 //Return students array (it contains an array of unique users)
179 return ($students);
180}
07842023 181
182//////////////////////////////////////////////////////////////////////////////////////
183/// Any other glossary functions go here. Each of them must have a name that
184/// starts with glossary_
185
186function glossary_log_info($log) {
187 global $CFG;
188 return get_record_sql("SELECT g.*, u.firstname, u.lastname
189 FROM {$CFG->prefix}glossary_entries g,
190 {$CFG->prefix}user u
191 WHERE g.glossaryid = '$log->info'
192 AND u.id = '$log->userid'");
193}
194
195function glossary_get_entries($glossaryid, $entrylist) {
196 global $CFG;
197
198 return get_records_sql("SELECT id,userid,concept,definition,format
199 FROM {$CFG->prefix}glossary_entries
200 WHERE glossaryid = '$glossaryid'
201 AND id IN ($entrylist)");
202}
c76e673a 203function glossary_get_entries_sorted($glossary, $where="", $orderby="") {
204global $CFG;
205 if ($where) {
206 $where = " and $where";
207 }
208 if ($orderby) {
209 $orderby = " ORDER BY $orderby";
210 }
211 return get_records_sql("SELECT *
212 FROM {$CFG->prefix}glossary_entries
213 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
214}
215
216function glossary_get_entries_by_category($glossary, $cat, $where="", $orderby="") {
217global $CFG;
218 if ($where) {
219 $where = " and $where";
220 }
221 if ($orderby) {
222 $orderby = " ORDER BY $orderby";
223 }
224 return get_records_sql("SELECT ge.*
225 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
226 WHERE (ge.id = c.entryid and c.categoryid = $cat) and
227 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
228}
07842023 229
c76e673a 230function glossary_print_entry($course, $cm, $glossary, $entry, $tab="",$cat="") {
914cb260 231 global $THEME, $USER, $CFG;
c76e673a 232
c197e607 233 if ($entry->approved or ($USER->id == $entry->userid and !isteacher($course->id)) or $tab == GLOSSARY_APPROVAL_VIEW) {
c76e673a 234 $permissiongranted = 0;
235 $formatfile = "$CFG->dirroot/mod/glossary/formats/$glossary->displayformat.php";
236 $functionname = "glossary_print_entry_by_format";
237
238 $basicformat = ($glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
239 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS);
240 if ( !$basicformat ) {
241 if ( file_exists($formatfile) ) {
242 include_once($formatfile);
243 if (function_exists($functionname) ) {
244 $permissiongranted = 1;
245 }
246 }
247 } else {
248 $permissiongranted = 1;
e179048e 249 }
06d94a52 250
c76e673a 251 if ( !$basicformat and $permissiongranted ) {
252 glossary_print_entry_by_format($course, $cm, $glossary, $entry,$tab,$cat);
253 } else {
254 switch ( $glossary->displayformat ) {
255 case GLOSSARY_FORMAT_SIMPLE:
256 glossary_print_entry_by_default($course, $cm, $glossary, $entry,$tab,$cat);
257 break;
258 case GLOSSARY_FORMAT_CONTINUOUS:
259 glossary_print_entry_continuous($course, $cm, $glossary, $entry,$tab,$cat);
260 break;
261 }
767a31c3 262 }
07842023 263 }
07842023 264}
1d9ddaaf 265function glossary_print_entry_concept($entry) {
266 echo $entry->concept;
267}
268
269function glossary_print_entry_definition($entry) {
270 $definition = str_ireplace($entry->concept,"<nolink>$entry->concept</nolink>",$entry->definition);
271 echo format_text($definition, $entry->format);
272}
273
274function glossary_print_entry_attachment($entry,$format,$align) {
275/// valid format values: html : Return the HTML link for the attachment as an icon
276/// text : Return the HTML link for tha attachment as text
277/// blank : Print the output to the screen
278 if ($entry->attachment) {
279 $glossary = get_record("glossary","id",$entry->glossaryid);
280 $entry->course = $glossary->course; //used inside print_attachment
281 echo "<table border=0 align=$align><tr><td>";
282 echo glossary_print_attachments($entry,$format,$align);
283 echo "</td></tr></table>";
284 }
285}
286
287function glossary_print_entry_approval($cm, $entry, $tab) {
288 if ( $tab == GLOSSARY_APPROVAL_VIEW ) {
289 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&eid=$entry->id&tab=$tab\"><IMG align=\"right\" src=\"check.gif\" border=0 width=\"34\" height=\"34\"></a>";
290 }
291}
07842023 292
c76e673a 293function glossary_print_entry_by_default($course, $cm, $glossary, $entry,$tab="",$cat="") {
07842023 294 global $THEME, $USER;
295
296 $colour = $THEME->cellheading2;
297
298 echo "\n<TR>";
1d9ddaaf 299 echo "<TD WIDTH=100% class=\"generalbox\" valign=\"top\" BGCOLOR=\"#FFFFFF\">";
300 glossary_print_entry_approval($cm, $entry, $tab);
301 glossary_print_entry_attachment($entry,"html","right");
302 echo "<b>";
303 glossary_print_entry_concept($entry);
304 echo ":</b> ";
305 glossary_print_entry_definition($entry);
306 glossary_print_entry_icons($course, $cm, $glossary, $entry,$tab,$cat);
07842023 307 echo "</td>";
308 echo "</TR>";
309}
310
c76e673a 311function glossary_print_entry_continuous($course, $cm, $glossary, $entry,$tab="",$cat="") {
767a31c3 312 global $THEME, $USER;
313 if ($entry) {
1d9ddaaf 314 glossary_print_entry_approval($cm, $entry, $tab);
315 glossary_print_entry_attachment($entry,"html","right");
316 glossary_print_entry_concept($entry);
317 echo " ";
318 glossary_print_entry_definition($entry);
c76e673a 319 glossary_print_entry_icons($course, $cm, $glossary, $entry, $tab, $cat);
767a31c3 320 }
321}
1d9ddaaf 322
c76e673a 323function glossary_print_entry_icons($course, $cm, $glossary, $entry,$tab="",$cat="") {
07842023 324 global $THEME, $USER;
325
7dd88447 326 $importedentry = ($entry->sourceglossaryid == $glossary->id);
d88824c4 327 $isteacher = isteacher($course->id);
7dd88447 328 $ismainglossary = $glossary->mainglossary;
891ae55e 329
c76e673a 330 echo "<p align=\"right\"><font size=1>";
891ae55e 331
c76e673a 332 if (!$entry->approved) {
333 echo get_string("entryishidden","glossary");
334 }
dc36351d 335 $count = count_records("glossary_comments","entryid",$entry->id);
c1e97fe5 336 if ($count) {
c76e673a 337 echo " <a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
c1e97fe5 338 if ($count == 1) {
339 print_string("comment", "glossary");
340 } else {
341 print_string("comments", "glossary");
342 }
c76e673a 343 echo "</a>";
dc36351d 344 }
c76e673a 345 echo "</font>";
c197e607 346 if ( $glossary->allowcomments and !isguest()) {
c76e673a 347 echo " <a href=\"comment.php?id=$cm->id&eid=$entry->id\"><img alt=\"" . get_string("addcomment","glossary") . "\" src=\"comment.gif\" height=16 width=16 border=0></a> ";
ed0201dd 348 }
dc36351d 349
ed0201dd 350 if ($isteacher or $glossary->studentcanpost and $entry->userid == $USER->id) {
351 // only teachers can export entries so check it out
ed0201dd 352 if ($isteacher and !$ismainglossary and !$importedentry) {
353 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
354 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
355
c76e673a 356 echo " <a href=\"exportentry.php?id=$cm->id&entry=$entry->id&tab=$tab&cat=$cat\"><img alt=\"" . get_string("exporttomainglossary","glossary") . "\"src=\"export.gif\" height=11 width=11 border=0></a> ";
07842023 357
ed0201dd 358 }
359 }
360
ad58adac 361 if ( $entry->sourceglossaryid ) {
362 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
363 } else {
364 $icon = "../../pix/t/delete.gif";
365 }
366
ed0201dd 367 // Exported entries can be updated/deleted only by teachers in the main glossary
7dd88447 368 if ( !$importedentry and ($isteacher or !$ismainglossary) ) {
c76e673a 369 echo " <a href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&tab=$tab&cat=$cat\"><img alt=\"" . get_string("delete") . "\"src=\"";
ad58adac 370 echo $icon;
371 echo "\" height=11 width=11 border=0></a> ";
372
c76e673a 373 echo " <a href=\"edit.php?id=$cm->id&e=$entry->id&tab=$tab&cat=$cat\"><img alt=\"" . get_string("edit") . "\" src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a>";
7dd88447 374 } elseif ( $importedentry ) {
c76e673a 375 echo " <font size=-1>" . get_string("exportedentry","glossary") . "</font>";
ad58adac 376 }
ed0201dd 377 }
07842023 378}
379
380function glossary_search_entries($searchterms, $glossary, $includedefinition) {
381/// Returns a list of entries found using an array of search terms
382/// eg word +word -word
383///
384
385 global $CFG;
386
387 if (!isteacher($glossary->course)) {
388 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 389 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 390 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
391 } else {
392
393 $onlyvisible = "";
394 $onlyvisibletable = "";
395 }
396
397 /// Some differences in syntax for PostgreSQL
398 if ($CFG->dbtype == "postgres7") {
399 $LIKE = "ILIKE"; // case-insensitive
400 $NOTLIKE = "NOT ILIKE"; // case-insensitive
401 $REGEXP = "~*";
402 $NOTREGEXP = "!~*";
403 } else {
404 $LIKE = "LIKE";
405 $NOTLIKE = "NOT LIKE";
406 $REGEXP = "REGEXP";
407 $NOTREGEXP = "NOT REGEXP";
408 }
409
410 $conceptsearch = "";
411 $definitionsearch = "";
412
413
414 foreach ($searchterms as $searchterm) {
07842023 415 if ($conceptsearch) {
416 $conceptsearch.= " OR ";
417 }
418 if ($definitionsearch) {
419 $definitionsearch.= " OR ";
420 }
421
422 if (substr($searchterm,0,1) == "+") {
423 $searchterm = substr($searchterm,1);
424 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
425 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
426 } else if (substr($searchterm,0,1) == "-") {
427 $searchterm = substr($searchterm,1);
428 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
429 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
430 } else {
431 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
432 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
433 }
434 }
435
ed0201dd 436 if ( !$includedefinition ) {
437 $definitionsearch = "0";
438 }
07842023 439
440 $selectsql = "{$CFG->prefix}glossary_entries e,
441 {$CFG->prefix}glossary g $onlyvisibletable
442 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 443 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c76e673a 444 AND g.id = $glossary->id AND e.approved != 0";
07842023 445
4d4c38f3 446 return get_records_sql("SELECT e.*
447 FROM $selectsql ORDER BY e.concept ASC");
07842023 448}
449
e179048e 450function glossary_file_area_name($entry) {
451// Creates a directory file name, suitable for make_upload_directory()
452 global $CFG;
453
454 return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
455}
456
457function glossary_file_area($entry) {
458 return make_upload_directory( glossary_file_area_name($entry) );
459}
460
461function glossary_delete_old_attachments($entry, $exception="") {
462// Deletes all the user files in the attachments area for a entry
463// EXCEPT for any file named $exception
464
465 if ($basedir = glossary_file_area($entry)) {
466 if ($files = get_directory_list($basedir)) {
467 foreach ($files as $file) {
468 if ($file != $exception) {
469 unlink("$basedir/$file");
470// notify("Existing file '$file' has been deleted!");
471 }
472 }
473 }
474 if (!$exception) { // Delete directory as well, if empty
475 rmdir("$basedir");
476 }
477 }
478}
479
480function glossary_copy_attachments($entry, $newentry) {
481/// Given a entry object that is being copied to glossaryid,
482/// this function checks that entry
483/// for attachments, and if any are found, these are
484/// copied to the new glossary directory.
485
486 global $CFG;
487
488 $return = true;
489
490 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
491 foreach ($entries as $curentry) {
492 $oldentry->id = $entry->id;
493 $oldentry->course = $entry->course;
494 $oldentry->glossaryid = $curentry->glossaryid;
495 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
496 if (is_dir($oldentrydir)) {
497
498 $newentrydir = glossary_file_area($newentry);
499 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
500 $return = false;
501 }
502 }
503 }
504 }
505 return $return;
506}
507
508function glossary_move_attachments($entry, $glossaryid) {
509/// Given a entry object that is being moved to glossaryid,
510/// this function checks that entry
511/// for attachments, and if any are found, these are
512/// moved to the new glossary directory.
513
514 global $CFG;
515
516 $return = true;
517
518 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
519 foreach ($entries as $entry) {
520 $oldentry->course = $entry->course;
521 $oldentry->glossaryid = $entry->glossaryid;
522 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
523 if (is_dir($oldentrydir)) {
524 $newentry = $oldentry;
525 $newentry->glossaryid = $glossaryid;
526 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
527 if (! @rename($oldentrydir, $newentrydir)) {
528 $return = false;
529 }
530 }
531 }
532 }
533 return $return;
534}
535
536function glossary_add_attachment($entry, $newfile) {
537// $entry is a full entry record, including course and glossary
538// $newfile is a full upload array from $_FILES
539// If successful, this function returns the name of the file
540
541 global $CFG;
542
543 if (empty($newfile['name'])) {
544 return "";
545 }
546
547 $newfile_name = clean_filename($newfile['name']);
548
549 if (valid_uploaded_file($newfile)) {
550 if (! $newfile_name) {
551 notify("This file had a wierd filename and couldn't be uploaded");
552
553 } else if (! $dir = glossary_file_area($entry)) {
554 notify("Attachment could not be stored");
555 $newfile_name = "";
556
557 } else {
558 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
559 chmod("$dir/$newfile_name", $CFG->directorypermissions);
560 glossary_delete_old_attachments($entry, $newfile_name);
561 } else {
562 notify("An error happened while saving the file on the server");
563 $newfile_name = "";
564 }
565 }
566 } else {
567 $newfile_name = "";
568 }
569
570 return $newfile_name;
571}
572
de53b9ac 573function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 574// if return=html, then return a html string.
575// if return=text, then return a text-only string.
576// otherwise, print HTML for non-images, and return image HTML
b764feaa 577// if attachment is an image, $align set its aligment.
e179048e 578 global $CFG;
b764feaa 579
580 $newentry = $entry;
581 if ( $newentry->sourceglossaryid ) {
582 $newentry->glossaryid = $newentry->sourceglossaryid;
583 }
e179048e 584
b764feaa 585 $filearea = glossary_file_area_name($newentry);
e179048e 586
587 $imagereturn = "";
588 $output = "";
589
b764feaa 590 if ($basedir = glossary_file_area($newentry)) {
e179048e 591 if ($files = get_directory_list($basedir)) {
592 $strattachment = get_string("attachment", "glossary");
593 $strpopupwindow = get_string("popupwindow");
594 foreach ($files as $file) {
595 $icon = mimeinfo("icon", $file);
596 if ($CFG->slasharguments) {
597 $ffurl = "file.php/$filearea/$file";
598 } else {
599 $ffurl = "file.php?file=/$filearea/$file";
600 }
601 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
602
603 if ($return == "html") {
604 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
605 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
606 } else if ($return == "text") {
607 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
608
609 } else {
610 if ($icon == "image.gif") { // Image attachments don't get printed as links
de53b9ac 611 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\" align=$align>";
e179048e 612 } else {
613 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
614 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
615 echo "<br />";
616 }
617 }
618 }
619 }
620 }
621
622 if ($return) {
623 return $output;
624 }
625
626 return $imagereturn;
627}
628
7dd88447 629function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 630
631if ( !$tTHEME ) {
632 global $THEME;
633 $tTHEME = $THEME;
634}
635
7dd88447 636$tablecolor = $tTHEME->TabTableBGColor;
637$currenttabcolor = $tTHEME->ActiveTabColor;
638$tabcolor = $tTHEME->InactiveTabColor;
639$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 640
7dd88447 641$tablewidth = $tTHEME->TabTableWidth;
642$tabsperrow = $tTHEME->TabsPerRow;
643$tabseparation = $tTHEME->TabSeparation;
06d94a52 644
7dd88447 645$tabs = count($data);
646$tabwidth = (int) (100 / $tabsperrow);
06d94a52 647
7dd88447 648$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
649
650$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 651
652?>
653 <center>
7dd88447 654 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 655 <tr>
656 <td width="100%">
657
658 <table border="0" cellpadding="0" cellspacing="0" width="100%">
659
ad58adac 660<?php
7dd88447 661$tabproccessed = 0;
662for ($row = 0; $row < $numrows; $row++) {
06d94a52 663 echo "<tr>\n";
7dd88447 664 if ( $row != $currentrow ) {
665 for ($col = 0; $col < $tabsperrow; $col++) {
666 if ( $tabproccessed < $tabs ) {
ad58adac 667 if ( $col == 0 ) {
7dd88447 668 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 669 }
7dd88447 670 if ($tabproccessed == $currenttab) {
671 $currentcolor = $currenttabcolor;
e94c179a 672 $currentstyle = 'generaltabselected';
06d94a52 673 } else {
7dd88447 674 $currentcolor = $tabcolor;
e94c179a 675 $currentstyle = 'generaltab';
06d94a52 676 }
e94c179a 677 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 678 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
679 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 680 }
7dd88447 681
ed0201dd 682 if ( !$data[$tabproccessed]->link ) {
683 echo "<font color=\"$inactivefontcolor\">";
684 }
7dd88447 685 echo $data[$tabproccessed]->caption;
ed0201dd 686 if ( !$data[$tabproccessed]->link ) {
687 echo "</font>";
688 }
7dd88447 689
690 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 691 echo "</a>";
692 }
693 echo "</b></td>";
694
7dd88447 695 if ( $col < $tabsperrow ) {
696 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 697 }
06d94a52 698 } else {
7dd88447 699 $currentcolor = "";
06d94a52 700 }
7dd88447 701 $tabproccessed++;
06d94a52 702 }
703 } else {
7dd88447 704 $firsttabincurrentrow = $tabproccessed;
705 $tabproccessed += $tabsperrow;
06d94a52 706 }
7dd88447 707 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 708}
709 echo "<tr>\n";
7dd88447 710 $tabproccessed = $firsttabincurrentrow;
711 for ($col = 0; $col < $tabsperrow; $col++) {
712 if ( $tabproccessed < $tabs ) {
ad58adac 713 if ( $col == 0 ) {
7dd88447 714 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 715 }
7dd88447 716 if ($tabproccessed == $currenttab) {
717 $currentcolor = $currenttabcolor;
e8ff59d6 718 $currentstyle = 'generaltabselected';
06d94a52 719 } else {
7dd88447 720 $currentcolor = $tabcolor;
e8ff59d6 721 $currentstyle = 'generaltab';
06d94a52 722 }
e8ff59d6 723 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 724 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
725 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 726 }
7dd88447 727
ed0201dd 728 if ( !$data[$tabproccessed]->link ) {
729 echo "<font color=\"$inactivefontcolor\">";
730 }
7dd88447 731 echo $data[$tabproccessed]->caption;
ed0201dd 732 if ( !$data[$tabproccessed]->link ) {
733 echo "</font>";
734 }
7dd88447 735
736 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 737 echo "</a>";
738 }
739 echo "</b></td>";
740
7dd88447 741 if ($col < $tabsperrow) {
742 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 743 }
06d94a52 744 } else {
7dd88447 745 if ($numrows > 1) {
746 $currentcolor = $tabcolor;
06d94a52 747 } else {
7dd88447 748 $currentcolor = "";
06d94a52 749 }
7dd88447 750 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 751 echo "</td>";
752
7dd88447 753 $col = $tabsperrow;
06d94a52 754 }
7dd88447 755 $tabproccessed++;
06d94a52 756 }
757 echo "</tr>\n";
758 ?>
759
760 </table>
761 </td>
762 </tr>
763 <tr>
7dd88447 764 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr></td>
06d94a52 765 </tr>
766 <tr>
7dd88447 767 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 768 <center>
ad58adac 769<?php
06d94a52 770}
771
ad58adac 772function glossary_print_tabbed_table_end() {
06d94a52 773 echo "</center><p></td></tr></table></center>";
774}
775
c76e673a 776function glossary_print_approval_menu($cm, $glossary, $l, $sortkey, $sortorder = "",$tab=GLOSSARY_STANDARD_VIEW) {
c76e673a 777 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c197e607 778 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 779 }
c76e673a 780 glossary_print_special_links($cm, $glossary,$l, $tab);
781
782 glossary_print_alphabet_links($cm, $glossary,$l, $tab);
783
784 glossary_print_all_links($cm, $glossary,$l, $tab);
785
786 glossary_print_sorting_links($cm, $sortkey,$sortorder, $tab);
787}
788
789function glossary_print_alphabet_menu($cm, $glossary, $l, $sortkey, $sortorder = "", $tab=GLOSSARY_STANDARD_VIEW) {
c197e607 790 if ( $tab != GLOSSARY_DATE_VIEW ) {
791 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
792 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
793 }
c76e673a 794
c197e607 795 glossary_print_special_links($cm, $glossary,$l, $tab);
c76e673a 796
c197e607 797 glossary_print_alphabet_links($cm, $glossary,$l, $tab);
798
799 glossary_print_all_links($cm, $glossary,$l, $tab);
800 } else {
801 glossary_print_sorting_links($cm, $sortkey,$sortorder, $tab);
802 }
c76e673a 803}
804
805function glossary_print_categories_menu($course, $cm, $glossary, $cat, $category) {
806global $CFG, $THEME;
c197e607 807 echo '<table border=0 width=100%>';
808 echo '<tr>';
c76e673a 809
c197e607 810 echo '<td align=center width=20%>';
c76e673a 811 if ( isteacher($course->id) ) {
812 $options['id'] = $cm->id;
813 $options['cat'] = $cat;
814 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
815 }
c197e607 816 echo '</td>';
c76e673a 817
c197e607 818 echo '<td align=center width=60%>';
819 echo '<b>';
c76e673a 820
821 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
822 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 823
c76e673a 824 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 825 $selected = '';
c76e673a 826 if ( $categories ) {
827 foreach ($categories as $currentcategory) {
828 $url = $currentcategory->id;
829 if ( $category ) {
830 if ($currentcategory->id == $category->id) {
831 $selected = $url;
832 }
833 }
834 $menu[$url] = $currentcategory->name;
835 }
836 }
837 if ( !$selected ) {
838 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
839 }
840
841 if ( $category ) {
842 echo $category->name;
843 } else {
844 if ( $cat == GLOSSARY_SHOW_NOT_CATEGORISED ) {
845
846 echo get_string("entrieswithoutcategory","glossary");
847 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
848
849 } elseif ( $cat == GLOSSARY_SHOW_ALL_CATEGORIES ) {
850
851 echo get_string("allcategories","glossary");
852 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
853
854 }
855 }
c197e607 856 echo '</b></td>';
857 echo '<td align=center width=20%>';
c76e673a 858
859 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&tab=" . GLOSSARY_CATEGORY_VIEW . "&cat=", $menu, "catmenu", $selected, "",
860 "", "", false);
677038ee 861
c197e607 862 echo '</td>';
863 echo '</tr>';
c76e673a 864
c197e607 865 echo '</table>';
c76e673a 866}
867
868function glossary_print_all_links($cm, $glossary, $l, $tab) {
869global $CFG;
870 if ( $glossary->showall and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
871 $strallentries = get_string("allentries", "glossary");
c197e607 872 if ( $l == 'ALL' ) {
c76e673a 873 echo "<b>$strallentries</b>";
874 } else {
875 $strexplainall = strip_tags(get_string("explainall","glossary"));
876 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&l=ALL&tab=$tab\">$strallentries</a>";
877 }
878 }
879}
880
881function glossary_print_special_links($cm, $glossary, $l, $tab) {
882global $CFG;
883 if ( $glossary->showspecial and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
884 $strspecial = get_string("special", "glossary");
c197e607 885 if ( $l == 'SPECIAL' ) {
677038ee 886 echo "<b>$strspecial</b> | ";
887 } else {
888 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
c76e673a 889 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&l=SPECIAL&tab=$tab\">$strspecial</a> | ";
677038ee 890 }
914cb260 891 }
c76e673a 892}
677038ee 893
c76e673a 894function glossary_print_alphabet_links($cm, $glossary,$l, $tab) {
895global $CFG;
896 if ( $glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
677038ee 897 $alphabet = explode("|", get_string("alphabet","glossary"));
898 $letters_by_line = 14;
899 for ($i = 0; $i < count($alphabet); $i++) {
900 if ( $l == $alphabet[$i] and $l) {
901 echo "<b>$alphabet[$i]</b>";
902 } else {
c76e673a 903 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&l=$alphabet[$i]&tab=$tab\">$alphabet[$i]</a>";
677038ee 904 }
905 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 906 echo ' | ';
677038ee 907 } else {
c197e607 908 echo '<br>';
677038ee 909 }
767a31c3 910 }
677038ee 911 }
c76e673a 912}
913
914function glossary_print_sorting_links($cm, $sortkey,$sortorder, $tab) {
915global $CFG;
916 $strsort = get_string("sortchronogically", "glossary");
917 $strsortbycreation = get_string("sortbycreation", "glossary");
918 $strsortbylastupdate = get_string("sortbylastupdate", "glossary");
677038ee 919
c197e607 920 $neworder = '';
677038ee 921 if ( $sortorder ) {
c197e607 922 if ( $sortorder == 'asc' ) {
923 $neworder = '&sortorder=desc';
677038ee 924 $ordertitle = get_string("descending","glossary");
925 } else {
c197e607 926 $neworder = '&sortorder=asc';
677038ee 927 $ordertitle = get_string("ascending","glossary");
928 }
929 $icon = " <img src=\"$sortorder.gif\" border=0 width=16 height=16>";
930 } else {
c197e607 931 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' ) {
677038ee 932 $icon = "";
933 $ordertitle = get_string("ascending","glossary");
934 } else {
935 $ordertitle = get_string("descending","glossary");
c197e607 936 $neworder = '&sortorder=desc';
937 $icon = ' <img src="asc.gif" border=0 width=16 height=16>';
677038ee 938 }
939 }
c197e607 940 $cicon = '';
941 $cneworder = '';
942 $cbtag = '';
943 $cendbtag = '';
677038ee 944
c197e607 945 $uicon = '';
946 $uneworder = '';
947 $ubtag = '';
948 $uendbtag = '';
677038ee 949
c197e607 950 if ( $sortkey == 'CREATION' ) {
677038ee 951 $cicon = $icon;
952 $cneworder = $neworder;
953 $cordertitle = $ordertitle;
954 $uordertitle = get_string("ascending","glossary");
c197e607 955 $cbtag = '<b>';
956 $cendbtag = '</b>';
957 } elseif ($sortkey == 'UPDATE') {
677038ee 958 $uicon = $icon;
959 $uneworder = $neworder;
960 $cordertitle = get_string("ascending","glossary");
961 $uordertitle = $ordertitle;
c197e607 962 $ubtag = '<b>';
963 $uendbtag = '</b>';
677038ee 964 } else {
965 $cordertitle = get_string("ascending","glossary");
966 $uordertitle = get_string("ascending","glossary");
967 }
c76e673a 968 echo "<br>$strsort: $ubtag<a title=\"$strsortbylastupdate $uordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=UPDATE$uneworder&tab=$tab\">$strsortbylastupdate$uicon</a>$uendbtag | ".
969 "$cbtag<a title=\"$strsortbycreation $cordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=CREATION$cneworder&tab=$tab\">$strsortbycreation$cicon</a>$cendbtag</p>";
fb443f1a 970}
ad58adac 971
972function glossary_sort_entries ( $entry0, $entry1 ) {
973 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
974 return -1;
975 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
976 return 1;
977 } else {
978 return 0;
979 }
980}
981
ed0201dd 982function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
983 global $THEME, $CFG, $USER;
984
1d9ddaaf 985 $colour = $THEME->cellheading2;
ed0201dd 986
987 $user = get_record("user", "id", $comment->userid);
988 $strby = get_string("writtenby","glossary");
989
990 echo "<table class=\"generalbox\" BORDER=1 CELLSPACING=0 valign=top cellpadding=0 width=70% border=0><tr><td>";
991
992 echo "\n<TABLE width=\"100%\" BORDER=0 CELLSPACING=0 valign=top cellpadding=5><tr>";
993
994 echo "\n<TD BGCOLOR=\"$THEME->cellheading\" WIDTH=25% VALIGN=TOP align=right >";
995 print_user_picture($user->id, $course->id, $user->picture);
996 echo "<br><FONT SIZE=2>$strby $user->firstname $user->lastname</font>";
997 echo "<br><FONT SIZE=1>(".get_string("lastedited").": ".userdate($comment->timemodified).")</FONT></small><br>";
998 echo "</TD>";
c197e607 999
1d9ddaaf 1000 echo "<TD valign=top WIDTH=75% BGCOLOR=\"$THEME->cellcontent\">";
ed0201dd 1001 if ($comment) {
c197e607 1002 echo format_text($comment->comment, $comment->format);
ed0201dd 1003 } else {
c197e607 1004 echo "<center>";
ed0201dd 1005 print_string("nocomment", "glossary");
c197e607 1006 echo "</center>";
ed0201dd 1007 }
1008
1009 echo "<p align=right>";
c197e607 1010 if ( (time() - $comment->timemodified < $CFG->maxeditingtime and $USER->id == $comment->userid) or isteacher($course->id) ) {
ed0201dd 1011 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=edit\"><img alt=\"" . get_string("edit") . "\" src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a> ";
c197e607 1012 }
1013 if ( $USER->id == $comment->userid or isteacher($course->id) ) {
ed0201dd 1014 echo "<a href=\"comment.php?id=$cm->id&eid=$entry->id&cid=$comment->id&action=delete\"><img alt=\"" . get_string("delete") . "\" src=\"../../pix/t/delete.gif\" height=11 width=11 border=0></a>";
c197e607 1015 }
ed0201dd 1016 echo "</td>";
c197e607 1017
ed0201dd 1018 echo "</tr></TABLE>\n";
1019
1020 echo "</td></tr></table>";
1021}
1022
1d9ddaaf 1023function glossary_print_dynaentry($courseid, $entries) {
1024 global $THEME, $USER;
cca6f7f1 1025
1d9ddaaf 1026 $colour = $THEME->cellheading2;
cca6f7f1 1027
1d9ddaaf 1028 echo "\n<center><table width=95% border=0><tr>";
1029 echo "<td width=100%\">";
1030 if ( $entries ) {
1031 foreach ( $entries as $entry ) {
1032 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1033 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1034 }
1d9ddaaf 1035 if (! $course = get_record("course", "id", $glossary->course)) {
1036 error("Glossary is misconfigured - don't know what course it's from");
1037 }
0de786f7 1038 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1039 error("Glossary is misconfigured - don't know what course module it is ");
1040 }
1041 glossary_print_entry($course, $cm, $glossary, $entry);
cca6f7f1 1042 }
cca6f7f1 1043 }
1d9ddaaf 1044 echo "</td>";
1045 echo "</tr></table></center>";
1046}
e179048e 1047?>