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