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