*** empty log message ***
[moodle.git] / mod / glossary / lib.php
CommitLineData
07842023 1<?PHP // $Id$
2
3/// Library of functions and constants for module glossary
4/// (replace glossary with the name of your module and delete this line)
5
e179048e 6require_once("$CFG->dirroot/files/mimetypes.php");
07842023 7
7dd88447 8define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
9define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
c76e673a 10
1ac87c73 11define("GLOSSARY_NO_VIEW", -1);
7dd88447 12define("GLOSSARY_STANDARD_VIEW", 0);
13define("GLOSSARY_CATEGORY_VIEW", 1);
c197e607 14define("GLOSSARY_DATE_VIEW", 2);
1ac87c73 15define("GLOSSARY_AUTHOR_VIEW", 3);
16define("GLOSSARY_ADDENTRY_VIEW", 4);
17define("GLOSSARY_IMPORT_VIEW", 5);
18define("GLOSSARY_EXPORT_VIEW", 6);
19define("GLOSSARY_APPROVAL_VIEW", 7);
fb443f1a 20
767a31c3 21define("GLOSSARY_FORMAT_SIMPLE", 0);
22define("GLOSSARY_FORMAT_CONTINUOUS", 1);
23
07842023 24function glossary_add_instance($glossary) {
25/// Given an object containing all the necessary data,
26/// (defined by the form in mod.html) this function
27/// will create a new instance and return the id number
28/// of the new instance.
29
0de786f7 30 if ( !isset($glossary->globalglossary) ) {
31 $glossary->globalglossary = 0;
32 } elseif ( !isadmin() ) {
33 $glossary->globalglossary = 0;
34 }
35
07842023 36 $glossary->timecreated = time();
37 $glossary->timemodified = $glossary->timecreated;
38
39 # May have to add extra stuff in here #
40
41 return insert_record("glossary", $glossary);
42}
43
44
45function glossary_update_instance($glossary) {
46/// Given an object containing all the necessary data,
47/// (defined by the form in mod.html) this function
48/// will update an existing instance with new data.
5eaa2d34 49global $CFG;
0de786f7 50 if ( !isadmin() ) {
51 unset($glossary->globalglossary);
52 }
53
07842023 54 $glossary->timemodified = time();
55 $glossary->id = $glossary->instance;
56
5eaa2d34 57 $return = update_record("glossary", $glossary);
58 if ($return and $glossary->defaultapproval) {
59 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
60 }
07842023 61
5eaa2d34 62 return $return;
07842023 63}
64
65
66function glossary_delete_instance($id) {
67/// Given an ID of an instance of this module,
68/// this function will permanently delete the instance
69/// and any data that depends on it.
70
71 if (! $glossary = get_record("glossary", "id", "$id")) {
72 return false;
73 }
74
75 $result = true;
76
77 # Delete any dependent records here #
78
79 if (! delete_records("glossary", "id", "$glossary->id")) {
80 $result = false;
49210b90 81 } else {
82 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
83 $cats = "";
1ac87c73 84 foreach ( $categories as $hook ) {
49210b90 85 $cats .= "$cat->id,";
86 }
87 $cats = substr($cats,0,-1);
88 if ($cats) {
89 delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
90 delete_records("glossary_categories", "glossaryid", $glossary->id);
91 }
92 }
93 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
94 $ents = "";
95 foreach ( $entries as $entry ) {
96 $ents .= "$entry->id,";
97 }
98 $ents = substr($ents,0,-1);
99 if ($ents) {
100 delete_records_select("glossary_comments", "entryid in ($ents)");
ea14e783 101 delete_records_select("glossary_alias", "entryid in ($ents)");
49210b90 102 }
103 }
104 delete_records("glossary_entries", "glossaryid", "$glossary->id");
07842023 105 }
07842023 106
107 return $result;
108}
109
110function glossary_user_outline($course, $user, $mod, $glossary) {
111/// Return a small object with summary information about what a
112/// user has done with a given particular instance of this module
113/// Used for user activity reports.
114/// $return->time = the time they did it
115/// $return->info = a short text description
116
1ac87c73 117 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
118 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
119
120 $lastpost = array_pop($entries);
121 $result->time = $lastpost->timemodified;
122 return $result;
123 }
124 return NULL;
125}
126
127function glossary_get_user_entries($glossaryid, $userid) {
128/// Get all the entries for a user in a glossary
129 global $CFG;
130
131 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
132 FROM {$CFG->prefix}glossary g,
133 {$CFG->prefix}glossary_entries e,
134 {$CFG->prefix}user u
135 WHERE g.id = '$glossaryid'
136 AND e.glossaryid = g.id
137 AND e.userid = '$userid'
138 AND e.userid = u.id
139 ORDER BY e.timemodified ASC");
07842023 140}
141
142function glossary_user_complete($course, $user, $mod, $glossary) {
143/// Print a detailed representation of what a user has done with
144/// a given particular instance of this module, for user activity reports.
1ac87c73 145 global $CFG;
07842023 146
1ac87c73 147 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
148 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
149 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
150 print_simple_box_start("center","70%");
151 } else {
152 echo '<table width="95%" boder="0"><tr><td>';
153 }
154 foreach ($entries as $entry) {
155 $cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
156 glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
157 echo '<p>';
158 }
159 if ( $glossary->displayformat == GLOSSARY_FORMAT_SIMPLE or
160 $glossary->displayformat == GLOSSARY_FORMAT_CONTINUOUS ) {
161 print_simple_box_end();
162 } else {
163 echo '</td></tr></table>';
164 }
165 }
07842023 166}
167
168function glossary_print_recent_activity($course, $isteacher, $timestart) {
169/// Given a course and a time, this module should find recent activity
170/// that has occurred in glossary activities and print it out.
171/// Return true if there was output, or false is there was none.
172
173 global $CFG, $THEME;
174
175 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
176 "course = '$course->id' AND ".
177 "module = 'glossary' AND ".
c4a35419 178 "(action = 'add entry' OR ".
179 " action = 'approve entry')", "time ASC")) {
07842023 180 return false;
181 }
182
07842023 183 foreach ($logs as $log) {
184 //Create a temp valid module structure (course,id)
1ac87c73 185 $tempmod->course = $log->course;
186 $entry = get_record("glossary_entries","id",$log->info);
187 $tempmod->id = $entry->glossaryid;
188 //Obtain the visible property from the instance
07842023 189 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 190
191 //Only if the mod is visible
c4a35419 192 if ($modvisible and $entry->approved) {
07842023 193 $entries[$log->info] = glossary_log_info($log);
194 $entries[$log->info]->time = $log->time;
195 $entries[$log->info]->url = $log->url;
196 }
07842023 197 }
198
07842023 199 $content = false;
200 if ($entries) {
201 $strftimerecent = get_string("strftimerecent");
202 $content = true;
203 print_headline(get_string("newentries", "glossary").":");
204 foreach ($entries as $entry) {
205 $date = userdate($entry->timemodified, $strftimerecent);
206 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
207 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
208 echo "$entry->concept";
209 echo "</a>\"</font></p>";
210 }
211 }
212
213 return $content;
214}
215
1ac87c73 216function glossary_log_info($log) {
217 global $CFG;
218
219 return get_record_sql("SELECT e.*, u.firstname, u.lastname
220 FROM {$CFG->prefix}glossary_entries e,
221 {$CFG->prefix}user u
222 WHERE e.id = '$log->info'
223 AND u.id = '$log->userid'");
224}
225
07842023 226function glossary_cron () {
227/// Function to be run periodically according to the moodle cron
228/// This function searches for things that need to be done, such
229/// as sending out mail, toggling flags etc ...
230
231 global $CFG;
232
233 return true;
234}
235
236function glossary_grades($glossaryid) {
237/// Must return an array of grades for a given instance of this module,
238/// indexed by user. It also returns a maximum allowed grade.
239
240 $return->grades = NULL;
241 $return->maxgrade = NULL;
242
243 return $return;
244}
245
05855091 246function glossary_get_participants($glossaryid) {
247//Returns the users with data in one glossary
248//(users with records in glossary_entries, students)
249
250 global $CFG;
251
252 //Get students
253 $students = get_records_sql("SELECT DISTINCT u.*
254 FROM {$CFG->prefix}user u,
255 {$CFG->prefix}glossary_entries g
256 WHERE g.glossaryid = '$glossaryid' and
257 u.id = g.userid");
258
259 //Return students array (it contains an array of unique users)
260 return ($students);
261}
07842023 262
263//////////////////////////////////////////////////////////////////////////////////////
264/// Any other glossary functions go here. Each of them must have a name that
265/// starts with glossary_
266
1ac87c73 267function glossary_debug($debug,$text,$br=1) {
268 if ( $debug ) {
269 echo '<font color=red>' . $text . '</font>';
270 if ( $br ) {
271 echo '<br>';
272 }
273 }
07842023 274}
275
ea14e783 276function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
07842023 277 global $CFG;
ea14e783 278 if ($pivot) {
279 $pivot .= ",";
280 }
07842023 281
ea14e783 282 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
07842023 283 FROM {$CFG->prefix}glossary_entries
284 WHERE glossaryid = '$glossaryid'
285 AND id IN ($entrylist)");
286}
ea14e783 287function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
c76e673a 288global $CFG;
289 if ($where) {
290 $where = " and $where";
291 }
292 if ($orderby) {
293 $orderby = " ORDER BY $orderby";
294 }
ea14e783 295 if ($pivot) {
296 $pivot .= ",";
297 }
298 return get_records_sql("SELECT $pivot *
c76e673a 299 FROM {$CFG->prefix}glossary_entries
300 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
301}
302
1ac87c73 303function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
c76e673a 304global $CFG;
305 if ($where) {
306 $where = " and $where";
307 }
308 if ($orderby) {
309 $orderby = " ORDER BY $orderby";
310 }
ea14e783 311 if ($pivot) {
312 $pivot .= ",";
313 }
314 return get_records_sql("SELECT $pivot ge.*
c76e673a 315 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
1ac87c73 316 WHERE (ge.id = c.entryid and c.categoryid = $hook) and
c76e673a 317 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
318}
07842023 319
b1918034 320function glossary_print_entry($course, $cm, $glossary, $entry, $mode="",$hook="",$printicons = 1, $displayformat = -1) {
914cb260 321 global $THEME, $USER, $CFG;
b1918034 322 if ( $displayformat < 0 ) {
323 $displayformat = $glossary->displayformat;
324 }
1ac87c73 325 if ($entry->approved or ($USER->id == $entry->userid and !isteacher($course->id)) or $mode == 'approval') {
c76e673a 326 $permissiongranted = 0;
b1918034 327 $formatfile = "$CFG->dirroot/mod/glossary/formats/$displayformat.php";
c76e673a 328 $functionname = "glossary_print_entry_by_format";
329
b1918034 330 $basicformat = ($displayformat == GLOSSARY_FORMAT_SIMPLE or
331 $displayformat == GLOSSARY_FORMAT_CONTINUOUS);
c76e673a 332 if ( !$basicformat ) {
333 if ( file_exists($formatfile) ) {
334 include_once($formatfile);
335 if (function_exists($functionname) ) {
336 $permissiongranted = 1;
337 }
338 }
339 } else {
340 $permissiongranted = 1;
e179048e 341 }
06d94a52 342
b1918034 343 if ( !$basicformat and $permissiongranted or $displayformat >= 2) {
1ac87c73 344 glossary_print_entry_by_format($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
c76e673a 345 } else {
b1918034 346 switch ( $displayformat ) {
1ac87c73 347 case GLOSSARY_FORMAT_SIMPLE:
348 glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
349 break;
350 case GLOSSARY_FORMAT_CONTINUOUS:
351 glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
352 break;
c76e673a 353 }
767a31c3 354 }
07842023 355 }
07842023 356}
81bdc9e9 357function glossary_print_entry_concept($entry) {
ea14e783 358 echo $entry->concept;
81bdc9e9 359}
1d9ddaaf 360
361function glossary_print_entry_definition($entry) {
f287e69c 362 $definition = $entry->definition;
363
364 //getting ride of A tags
365 $links = array();
366 preg_match_all('/<img (.+?)>/is',$definition,$list_of_links);
367
368 foreach (array_unique($list_of_links[0]) as $key=>$value) {
369 $links['<@'.$key.'@>'] = $value;
370 }
371 if ( $links ) {
372 $definition = str_replace($links,array_keys($links),$definition);
373 }
374
375 //getting ride of IMG tags
376 $images = array();
377 preg_match_all('/<A (.+?)>/is',$definition,$list_of_images);
378
379 foreach (array_unique($list_of_images[0]) as $key=>$value) {
380 $images['<@'.$key.'@>'] = $value;
381 }
382 if ( $images ) {
383 $definition = str_replace($images,array_keys($images),$definition);
384 }
385
386 $definition = str_ireplace($entry->concept,"<nolink>$entry->concept</nolink>",$definition);
387
388 //restoring A tags
389 if ( $links ) {
390 $definition = str_replace(array_keys($links),$links,$definition);
391 }
392
393 //restoring IMG tags
394 if ( $images ) {
395 $definition = str_replace(array_keys($images),$images,$definition);
396 }
397
584dcac9 398/* if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
399 foreach ($aliases as $alias) {
400 echo "<small><font color=red>$alias->alias: ";
401 $definition = str_ireplace($alias->alias,"<nolink>$alias->alias</nolink>",$definition);
402 echo "<b>" . format_text($definition,2) . "</b>";
403 echo "</font></small><br>";
404 }
405 }
406*/
1d9ddaaf 407 echo format_text($definition, $entry->format);
408}
409
1ac87c73 410function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 411 $return = '';
412 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
413 foreach ($aliases as $alias) {
414 if ($alias->alias) {
415 if ($return == '') {
416 $return = '<select style="font-size:8pt">';
417 }
418 $return .= "<option>$alias->alias</option>";
419 }
420 }
421 if ($return != '') {
422 $return .= '</select>';
423// $return = "<table border=0 align=$align><tr><td>$return</td></tr></table>";
424 }
425 }
1ac87c73 426 if ($type == 'print') {
81bdc9e9 427 echo $return;
428 } else {
429 return $return;
430 }
431}
432
1ac87c73 433function glossary_print_entry_icons($course, $cm, $glossary, $entry,$mode="",$hook="", $type = 'print') {
81bdc9e9 434 global $THEME, $USER;
435
436 $importedentry = ($entry->sourceglossaryid == $glossary->id);
437 $isteacher = isteacher($course->id);
438 $ismainglossary = $glossary->mainglossary;
439
440 $return = "<font size=1>";
81bdc9e9 441 if (!$entry->approved) {
442 $return .= get_string("entryishidden","glossary");
443 }
a0d1e2bb 444
66d7ae88 445 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
446
447 $return .= "</font> ";
a0d1e2bb 448
449
81bdc9e9 450 if ( $glossary->allowcomments and !isguest()) {
451 $return .= " <a title=\"" . get_string("addcomment","glossary") . "\" href=\"comment.php?id=$cm->id&eid=$entry->id\"><img src=\"comment.gif\" height=16 width=16 border=0></a> ";
452 }
453
454 if ($isteacher or $glossary->studentcanpost and $entry->userid == $USER->id) {
455 // only teachers can export entries so check it out
456 if ($isteacher and !$ismainglossary and !$importedentry) {
457 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
458 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
459
1ac87c73 460 $return .= " <a title=\"" . get_string("exporttomainglossary","glossary") . "\" href=\"exportentry.php?id=$cm->id&entry=$entry->id&mode=$mode&hook=$hook\"><img src=\"export.gif\" height=11 width=11 border=0></a> ";
81bdc9e9 461
462 }
463 }
464
465 if ( $entry->sourceglossaryid ) {
466 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
467 } else {
468 $icon = "../../pix/t/delete.gif";
469 }
470
471 // Exported entries can be updated/deleted only by teachers in the main glossary
472 if ( !$importedentry and ($isteacher or !$ismainglossary) ) {
1ac87c73 473 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
81bdc9e9 474 $return .= $icon;
475 $return .= "\" height=11 width=11 border=0></a> ";
476
1ac87c73 477 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a>";
81bdc9e9 478 } elseif ( $importedentry ) {
479 $return .= " <font size=-1>" . get_string("exportedentry","glossary") . "</font>";
480 }
481 }
482 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
1ac87c73 483 if ($type == 'print') {
81bdc9e9 484 echo $return;
485 } else {
486 return $return;
a0d1e2bb 487 }
488}
489
490function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
491 $return = '';
492
493 $count = count_records("glossary_comments","entryid",$entry->id);
494 if ($count) {
495 $return = "<font size=1>";
496 $return .= "<a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
497 if ($count == 1) {
498 $return .= get_string("comment", "glossary");
499 } else {
500 $return .= get_string("comments", "glossary");
501 }
502 $return .= "</a>";
503 $return .= "</font>";
504 }
505
506 if ($type == 'print') {
507 echo $return;
508 } else {
509 return $return;
81bdc9e9 510 }
511}
512
1ac87c73 513function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons) {
81bdc9e9 514
1ac87c73 515 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,"html");
516 $icons = "";
517 if ( $printicons ) {
518 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
519 }
81bdc9e9 520 if ( $aliases ) {
521 echo '<table border="0" width="100%" align="center"><tr>' .
522 '<td align="right" width="50%" valign=top><font size=1>' .
523 get_string("aliases","glossary") . ': ' . $aliases . '</td>' .
524 '<td align=right width="50%" valign=top>'.
525 $icons .
526 '</td></tr></table>';
527 } else {
528 echo "<p align=right>$icons";
529 }
530}
531
4f4ca7b5 532function glossary_print_entry_attachment($entry,$format=NULL,$align="right") {
1d9ddaaf 533/// valid format values: html : Return the HTML link for the attachment as an icon
534/// text : Return the HTML link for tha attachment as text
535/// blank : Print the output to the screen
536 if ($entry->attachment) {
537 $glossary = get_record("glossary","id",$entry->glossaryid);
538 $entry->course = $glossary->course; //used inside print_attachment
539 echo "<table border=0 align=$align><tr><td>";
540 echo glossary_print_attachments($entry,$format,$align);
541 echo "</td></tr></table>";
542 }
543}
544
1ac87c73 545function glossary_print_entry_approval($cm, $entry, $mode) {
546 if ( $mode == 'approval' and !$entry->approved ) {
547 echo "<a title=\"" . get_string("approve","glossary"). "\" href=\"approve.php?id=$cm->id&eid=$entry->id&mode=$mode\"><IMG align=\"right\" src=\"check.gif\" border=0 width=\"34\" height=\"34\"></a>";
1d9ddaaf 548 }
549}
07842023 550
1ac87c73 551function glossary_print_entry_by_default($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1) {
07842023 552 global $THEME, $USER;
553
554 $colour = $THEME->cellheading2;
555
556 echo "\n<TR>";
1d9ddaaf 557 echo "<TD WIDTH=100% class=\"generalbox\" valign=\"top\" BGCOLOR=\"#FFFFFF\">";
1ac87c73 558 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 559 glossary_print_entry_attachment($entry,"html","right");
560 echo "<b>";
561 glossary_print_entry_concept($entry);
562 echo ":</b> ";
563 glossary_print_entry_definition($entry);
1ac87c73 564 glossary_print_entry_lower_section($course, $cm, $glossary, $entry,$mode,$hook,$printicons);
07842023 565 echo "</td>";
566 echo "</TR>";
567}
568
1ac87c73 569function glossary_print_entry_continuous($course, $cm, $glossary, $entry,$mode="",$hook="",$printicons=1) {
767a31c3 570 global $THEME, $USER;
571 if ($entry) {
1ac87c73 572 glossary_print_entry_approval($cm, $entry, $mode);
1d9ddaaf 573 glossary_print_entry_attachment($entry,"html","right");
574 glossary_print_entry_concept($entry);
575 echo " ";
1ac87c73 576
1d9ddaaf 577 glossary_print_entry_definition($entry);
1ac87c73 578 if ( $printicons ) {
579 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,"html");
580 echo "($icons)";
581 }
ed0201dd 582 }
07842023 583}
584
585function glossary_search_entries($searchterms, $glossary, $includedefinition) {
586/// Returns a list of entries found using an array of search terms
587/// eg word +word -word
588///
589
590 global $CFG;
591
592 if (!isteacher($glossary->course)) {
593 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 594 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 595 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
596 } else {
597
598 $onlyvisible = "";
599 $onlyvisibletable = "";
600 }
601
602 /// Some differences in syntax for PostgreSQL
603 if ($CFG->dbtype == "postgres7") {
604 $LIKE = "ILIKE"; // case-insensitive
605 $NOTLIKE = "NOT ILIKE"; // case-insensitive
606 $REGEXP = "~*";
607 $NOTREGEXP = "!~*";
608 } else {
609 $LIKE = "LIKE";
610 $NOTLIKE = "NOT LIKE";
611 $REGEXP = "REGEXP";
612 $NOTREGEXP = "NOT REGEXP";
613 }
614
615 $conceptsearch = "";
616 $definitionsearch = "";
617
618
619 foreach ($searchterms as $searchterm) {
07842023 620 if ($conceptsearch) {
621 $conceptsearch.= " OR ";
622 }
623 if ($definitionsearch) {
624 $definitionsearch.= " OR ";
625 }
626
627 if (substr($searchterm,0,1) == "+") {
628 $searchterm = substr($searchterm,1);
629 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
630 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
631 } else if (substr($searchterm,0,1) == "-") {
632 $searchterm = substr($searchterm,1);
633 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
634 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
635 } else {
636 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
637 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
638 }
639 }
640
ed0201dd 641 if ( !$includedefinition ) {
642 $definitionsearch = "0";
643 }
07842023 644
645 $selectsql = "{$CFG->prefix}glossary_entries e,
646 {$CFG->prefix}glossary g $onlyvisibletable
647 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 648 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c76e673a 649 AND g.id = $glossary->id AND e.approved != 0";
07842023 650
4d4c38f3 651 return get_records_sql("SELECT e.*
652 FROM $selectsql ORDER BY e.concept ASC");
07842023 653}
654
e179048e 655function glossary_file_area_name($entry) {
656// Creates a directory file name, suitable for make_upload_directory()
657 global $CFG;
658
659 return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
660}
661
662function glossary_file_area($entry) {
663 return make_upload_directory( glossary_file_area_name($entry) );
664}
665
666function glossary_delete_old_attachments($entry, $exception="") {
667// Deletes all the user files in the attachments area for a entry
668// EXCEPT for any file named $exception
669
670 if ($basedir = glossary_file_area($entry)) {
671 if ($files = get_directory_list($basedir)) {
672 foreach ($files as $file) {
673 if ($file != $exception) {
674 unlink("$basedir/$file");
675// notify("Existing file '$file' has been deleted!");
676 }
677 }
678 }
679 if (!$exception) { // Delete directory as well, if empty
680 rmdir("$basedir");
681 }
682 }
683}
684
685function glossary_copy_attachments($entry, $newentry) {
686/// Given a entry object that is being copied to glossaryid,
687/// this function checks that entry
688/// for attachments, and if any are found, these are
689/// copied to the new glossary directory.
690
691 global $CFG;
692
693 $return = true;
694
695 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
696 foreach ($entries as $curentry) {
697 $oldentry->id = $entry->id;
698 $oldentry->course = $entry->course;
699 $oldentry->glossaryid = $curentry->glossaryid;
700 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
701 if (is_dir($oldentrydir)) {
702
703 $newentrydir = glossary_file_area($newentry);
704 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
705 $return = false;
706 }
707 }
708 }
709 }
710 return $return;
711}
712
713function glossary_move_attachments($entry, $glossaryid) {
714/// Given a entry object that is being moved to glossaryid,
715/// this function checks that entry
716/// for attachments, and if any are found, these are
717/// moved to the new glossary directory.
718
719 global $CFG;
720
721 $return = true;
722
723 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
724 foreach ($entries as $entry) {
725 $oldentry->course = $entry->course;
726 $oldentry->glossaryid = $entry->glossaryid;
727 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
728 if (is_dir($oldentrydir)) {
729 $newentry = $oldentry;
730 $newentry->glossaryid = $glossaryid;
731 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
732 if (! @rename($oldentrydir, $newentrydir)) {
733 $return = false;
734 }
735 }
736 }
737 }
738 return $return;
739}
740
741function glossary_add_attachment($entry, $newfile) {
742// $entry is a full entry record, including course and glossary
743// $newfile is a full upload array from $_FILES
744// If successful, this function returns the name of the file
745
746 global $CFG;
747
748 if (empty($newfile['name'])) {
749 return "";
750 }
751
752 $newfile_name = clean_filename($newfile['name']);
753
754 if (valid_uploaded_file($newfile)) {
755 if (! $newfile_name) {
756 notify("This file had a wierd filename and couldn't be uploaded");
757
758 } else if (! $dir = glossary_file_area($entry)) {
759 notify("Attachment could not be stored");
760 $newfile_name = "";
761
762 } else {
763 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
764 chmod("$dir/$newfile_name", $CFG->directorypermissions);
765 glossary_delete_old_attachments($entry, $newfile_name);
766 } else {
767 notify("An error happened while saving the file on the server");
768 $newfile_name = "";
769 }
770 }
771 } else {
772 $newfile_name = "";
773 }
774
775 return $newfile_name;
776}
777
de53b9ac 778function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 779// if return=html, then return a html string.
780// if return=text, then return a text-only string.
781// otherwise, print HTML for non-images, and return image HTML
b764feaa 782// if attachment is an image, $align set its aligment.
e179048e 783 global $CFG;
b764feaa 784
785 $newentry = $entry;
786 if ( $newentry->sourceglossaryid ) {
787 $newentry->glossaryid = $newentry->sourceglossaryid;
788 }
e179048e 789
b764feaa 790 $filearea = glossary_file_area_name($newentry);
e179048e 791
792 $imagereturn = "";
793 $output = "";
794
b764feaa 795 if ($basedir = glossary_file_area($newentry)) {
e179048e 796 if ($files = get_directory_list($basedir)) {
797 $strattachment = get_string("attachment", "glossary");
798 $strpopupwindow = get_string("popupwindow");
799 foreach ($files as $file) {
800 $icon = mimeinfo("icon", $file);
801 if ($CFG->slasharguments) {
802 $ffurl = "file.php/$filearea/$file";
803 } else {
804 $ffurl = "file.php?file=/$filearea/$file";
805 }
806 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
807
808 if ($return == "html") {
809 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
810 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
811 } else if ($return == "text") {
812 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
813
814 } else {
815 if ($icon == "image.gif") { // Image attachments don't get printed as links
de53b9ac 816 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\" align=$align>";
e179048e 817 } else {
818 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
819 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
820 echo "<br />";
821 }
822 }
823 }
824 }
825 }
826
827 if ($return) {
828 return $output;
829 }
830
831 return $imagereturn;
832}
833
7dd88447 834function glossary_print_tabbed_table_start($data, $currenttab, $tTHEME = NULL) {
06d94a52 835
836if ( !$tTHEME ) {
837 global $THEME;
838 $tTHEME = $THEME;
839}
840
7dd88447 841$tablecolor = $tTHEME->TabTableBGColor;
842$currenttabcolor = $tTHEME->ActiveTabColor;
843$tabcolor = $tTHEME->InactiveTabColor;
844$inactivefontcolor = $tTHEME->InactiveFontColor;
06d94a52 845
7dd88447 846$tablewidth = $tTHEME->TabTableWidth;
847$tabsperrow = $tTHEME->TabsPerRow;
848$tabseparation = $tTHEME->TabSeparation;
06d94a52 849
7dd88447 850$tabs = count($data);
851$tabwidth = (int) (100 / $tabsperrow);
06d94a52 852
7dd88447 853$currentrow = ( $currenttab - ( $currenttab % $tabsperrow) ) / $tabsperrow;
854
855$numrows = (int) ( $tabs / $tabsperrow ) + 1;
06d94a52 856
857?>
858 <center>
7dd88447 859 <table border="0" cellpadding="0" cellspacing="0" width="<?php p($tablewidth) ?>">
06d94a52 860 <tr>
861 <td width="100%">
862
863 <table border="0" cellpadding="0" cellspacing="0" width="100%">
864
ad58adac 865<?php
7dd88447 866$tabproccessed = 0;
867for ($row = 0; $row < $numrows; $row++) {
06d94a52 868 echo "<tr>\n";
7dd88447 869 if ( $row != $currentrow ) {
870 for ($col = 0; $col < $tabsperrow; $col++) {
871 if ( $tabproccessed < $tabs ) {
ad58adac 872 if ( $col == 0 ) {
7dd88447 873 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 874 }
7dd88447 875 if ($tabproccessed == $currenttab) {
876 $currentcolor = $currenttabcolor;
e94c179a 877 $currentstyle = 'generaltabselected';
06d94a52 878 } else {
7dd88447 879 $currentcolor = $tabcolor;
e94c179a 880 $currentstyle = 'generaltab';
06d94a52 881 }
505eec9c 882 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 883 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
884 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 885 }
7dd88447 886
ed0201dd 887 if ( !$data[$tabproccessed]->link ) {
888 echo "<font color=\"$inactivefontcolor\">";
889 }
7dd88447 890 echo $data[$tabproccessed]->caption;
ed0201dd 891 if ( !$data[$tabproccessed]->link ) {
892 echo "</font>";
893 }
7dd88447 894
895 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 896 echo "</a>";
897 }
898 echo "</b></td>";
899
7dd88447 900 if ( $col < $tabsperrow ) {
901 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 902 }
06d94a52 903 } else {
7dd88447 904 $currentcolor = "";
06d94a52 905 }
7dd88447 906 $tabproccessed++;
06d94a52 907 }
908 } else {
7dd88447 909 $firsttabincurrentrow = $tabproccessed;
910 $tabproccessed += $tabsperrow;
06d94a52 911 }
7dd88447 912 echo "</tr><tr><td colspan=" . (2* $tabsperrow) . " ></td></tr>\n";
06d94a52 913}
914 echo "<tr>\n";
7dd88447 915 $tabproccessed = $firsttabincurrentrow;
916 for ($col = 0; $col < $tabsperrow; $col++) {
917 if ( $tabproccessed < $tabs ) {
ad58adac 918 if ( $col == 0 ) {
7dd88447 919 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
ad58adac 920 }
7dd88447 921 if ($tabproccessed == $currenttab) {
922 $currentcolor = $currenttabcolor;
e8ff59d6 923 $currentstyle = 'generaltabselected';
06d94a52 924 } else {
7dd88447 925 $currentcolor = $tabcolor;
e8ff59d6 926 $currentstyle = 'generaltab';
06d94a52 927 }
505eec9c 928 echo "<td class=\"$currentstyle\" width=\"$tabwidth%\" bgcolor=\"$currentcolor\" align=\"center\"><b>";
7dd88447 929 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
930 echo "<a href=\"" . $data[$tabproccessed]->link . "\">";
914cb260 931 }
7dd88447 932
ed0201dd 933 if ( !$data[$tabproccessed]->link ) {
934 echo "<font color=\"$inactivefontcolor\">";
935 }
7dd88447 936 echo $data[$tabproccessed]->caption;
ed0201dd 937 if ( !$data[$tabproccessed]->link ) {
938 echo "</font>";
939 }
7dd88447 940
941 if ($tabproccessed != $currenttab and $data[$tabproccessed]->link) {
914cb260 942 echo "</a>";
943 }
944 echo "</b></td>";
945
7dd88447 946 if ($col < $tabsperrow) {
947 echo "<td width=\"$tabseparation\" align=\"center\">&nbsp;</td>";
914cb260 948 }
06d94a52 949 } else {
7dd88447 950 if ($numrows > 1) {
951 $currentcolor = $tabcolor;
06d94a52 952 } else {
7dd88447 953 $currentcolor = "";
06d94a52 954 }
7dd88447 955 echo "<td colspan = " . (2 * ($tabsperrow - $col)) . " bgcolor=\"$currentcolor\" align=\"center\">";
06d94a52 956 echo "</td>";
957
7dd88447 958 $col = $tabsperrow;
06d94a52 959 }
7dd88447 960 $tabproccessed++;
06d94a52 961 }
962 echo "</tr>\n";
963 ?>
964
965 </table>
966 </td>
967 </tr>
968 <tr>
7dd88447 969 <td width="100%" bgcolor="<?php p($tablecolor) ?>"><hr></td>
06d94a52 970 </tr>
971 <tr>
7dd88447 972 <td width="100%" bgcolor="<?php p($tablecolor) ?>">
06d94a52 973 <center>
ad58adac 974<?php
06d94a52 975}
976
ad58adac 977function glossary_print_tabbed_table_end() {
06d94a52 978 echo "</center><p></td></tr></table></center>";
979}
980
1ac87c73 981function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
c76e673a 982 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c197e607 983 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
677038ee 984 }
1ac87c73 985 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 986
c4a35419 987 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 988
1ac87c73 989 glossary_print_all_links($cm, $glossary, $mode, $hook);
c76e673a 990
db87439a 991 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
c76e673a 992}
993
1ac87c73 994function glossary_print_addentry_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
ea14e783 995 echo '<center>' . get_string("explainaddentry","glossary") . '<p>';
996}
997
1ac87c73 998function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 999 echo '<center>' . get_string("explainimport","glossary") . '<p>';
1000}
1001
1ac87c73 1002function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
748b1932 1003 echo '<center>' . get_string("explainexport","glossary") . '<p>';
1004}
1005
1ac87c73 1006function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1007 if ( $mode != 'date' ) {
c197e607 1008 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
1009 echo '<center>' . get_string("explainalphabet","glossary") . '<p>';
1010 }
c76e673a 1011
1ac87c73 1012 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1013
c4a35419 1014 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 1015
1ac87c73 1016 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 1017 } else {
1ac87c73 1018 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1019 }
1020}
1021
1022function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1023 if ($glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
c4a35419 1024 echo '<center>' . get_string("explainalphabet","glossary") . '<br \>';
c197e607 1025 }
1ac87c73 1026
c4a35419 1027 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1028 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 1029 glossary_print_all_links($cm, $glossary, $mode, $hook);
c4a35419 1030// echo "<br />";
c76e673a 1031}
1032
1ac87c73 1033function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
c76e673a 1034global $CFG, $THEME;
c197e607 1035 echo '<table border=0 width=100%>';
1036 echo '<tr>';
c76e673a 1037
c197e607 1038 echo '<td align=center width=20%>';
840d3ba8 1039 if ( isteacher($glossary->course) ) {
c76e673a 1040 $options['id'] = $cm->id;
1ac87c73 1041 $options['mode'] = 'cat';
1042 $options['hook'] = $hook;
c76e673a 1043 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1044 }
c197e607 1045 echo '</td>';
c76e673a 1046
c197e607 1047 echo '<td align=center width=60%>';
1048 echo '<b>';
c76e673a 1049
1050 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1051 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1052
c76e673a 1053 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1054 $selected = '';
c76e673a 1055 if ( $categories ) {
1056 foreach ($categories as $currentcategory) {
1057 $url = $currentcategory->id;
1058 if ( $category ) {
1059 if ($currentcategory->id == $category->id) {
1060 $selected = $url;
1061 }
1062 }
1063 $menu[$url] = $currentcategory->name;
1064 }
1065 }
1066 if ( !$selected ) {
1067 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1068 }
1069
1070 if ( $category ) {
1071 echo $category->name;
1072 } else {
1ac87c73 1073 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1074
1075 echo get_string("entrieswithoutcategory","glossary");
1076 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1077
1ac87c73 1078 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1079
1080 echo get_string("allcategories","glossary");
1081 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1082
1083 }
1084 }
c197e607 1085 echo '</b></td>';
1086 echo '<td align=center width=20%>';
c76e673a 1087
1ac87c73 1088 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=cat&hook=", $menu, "catmenu", $selected, "",
c76e673a 1089 "", "", false);
677038ee 1090
c197e607 1091 echo '</td>';
1092 echo '</tr>';
c76e673a 1093
c197e607 1094 echo '</table>';
c76e673a 1095}
1096
1ac87c73 1097function glossary_print_all_links($cm, $glossary, $mode, $hook) {
c76e673a 1098global $CFG;
1099 if ( $glossary->showall and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS) {
1100 $strallentries = get_string("allentries", "glossary");
1ac87c73 1101 if ( $hook == 'ALL' ) {
c76e673a 1102 echo "<b>$strallentries</b>";
1103 } else {
1104 $strexplainall = strip_tags(get_string("explainall","glossary"));
1ac87c73 1105 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=ALL\">$strallentries</a>";
c76e673a 1106 }
1107 }
1108}
1109
1ac87c73 1110function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1111global $CFG;
1112 if ( $glossary->showspecial and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
1113 $strspecial = get_string("special", "glossary");
1ac87c73 1114 if ( $hook == 'SPECIAL' ) {
677038ee 1115 echo "<b>$strspecial</b> | ";
1116 } else {
1117 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1ac87c73 1118 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=SPECIAL\">$strspecial</a> | ";
677038ee 1119 }
914cb260 1120 }
c76e673a 1121}
677038ee 1122
c4a35419 1123function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1124global $CFG;
1125 if ( $glossary->showalphabet and $glossary->displayformat != GLOSSARY_FORMAT_CONTINUOUS ) {
c4274149 1126 $alphabet = explode(",", get_string("alphabet"));
677038ee 1127 $letters_by_line = 14;
1128 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1129 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1130 echo "<b>$alphabet[$i]</b>";
1131 } else {
c4a35419 1132 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=$alphabet[$i]&sortkey=$sortkey&sortorder=$sortorder\">$alphabet[$i]</a>";
677038ee 1133 }
1134 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1135 echo ' | ';
677038ee 1136 } else {
c197e607 1137 echo '<br>';
677038ee 1138 }
767a31c3 1139 }
677038ee 1140 }
c76e673a 1141}
1142
1ac87c73 1143function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1144global $CFG;
677038ee 1145
c4a35419 1146 $asc = get_string("ascending","glossary");
1147 $desc = get_string("descending","glossary");
1148 $bopen = '<b>';
1149 $bclose = '</b>';
1150
c197e607 1151 $neworder = '';
677038ee 1152 if ( $sortorder ) {
c197e607 1153 if ( $sortorder == 'asc' ) {
1154 $neworder = '&sortorder=desc';
c4a35419 1155 $newordertitle = $desc;
677038ee 1156 } else {
c197e607 1157 $neworder = '&sortorder=asc';
c4a35419 1158 $newordertitle = $asc;
677038ee 1159 }
1160 $icon = " <img src=\"$sortorder.gif\" border=0 width=16 height=16>";
1161 } else {
c4a35419 1162 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1163 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1164 $icon = "";
c4a35419 1165 $newordertitle = $asc;
677038ee 1166 } else {
c4a35419 1167 $newordertitle = $desc;
c197e607 1168 $neworder = '&sortorder=desc';
1169 $icon = ' <img src="asc.gif" border=0 width=16 height=16>';
677038ee 1170 }
1171 }
c4a35419 1172 $ficon = '';
1173 $fneworder = '';
1174 $fbtag = '';
1175 $fendbtag = '';
1176
1177 $sicon = '';
1178 $sneworder = '';
ae078733 1179
1180 $sbtag = '';
1181 $fbtag = '';
1182 $fendbtag = '';
1183 $sendbtag = '';
1184
c4a35419 1185 $sendbtag = '';
1186
1187 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1188 $ficon = $icon;
1189 $fneworder = $neworder;
1190 $fordertitle = $newordertitle;
1191 $sordertitle = $asc;
1192 $fbtag = $bopen;
1193 $fendbtag = $bclose;
1194 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1195 $sicon = $icon;
1196 $sneworder = $neworder;
1197 $fordertitle = $asc;
1198 $sordertitle = $newordertitle;
1199 $sbtag = $bopen;
1200 $sendbtag = $bclose;
677038ee 1201 } else {
c4a35419 1202 $fordertitle = $asc;
1203 $sordertitle = $asc;
677038ee 1204 }
c4a35419 1205
1206 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1207 $forder = 'CREATION';
1208 $sorder = 'UPDATE';
1209 $fsort = get_string("sortbycreation", "glossary");
1210 $ssort = get_string("sortbylastupdate", "glossary");
1211
1212 $sort = get_string("sortchronogically", "glossary");
1213 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1214 $forder = 'FIRSTNAME';
1215 $sorder = 'LASTNAME';
1216 $fsort = get_string("firstname");
1217 $ssort = get_string("lastname");
1218
1219 $sort = get_string("sortby", "glossary");
1220 }
1221
1222 echo "<br>$sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$sorder$sneworder&mode=$mode\">$ssort$sicon</a>$sendbtag | ".
1223 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$forder$fneworder&mode=$mode\">$fsort$ficon</a>$fendbtag<br \>";
fb443f1a 1224}
ad58adac 1225
1226function glossary_sort_entries ( $entry0, $entry1 ) {
1227 if ( strtolower(ltrim($entry0->concept)) < strtolower(ltrim($entry1->concept)) ) {
1228 return -1;
1229 } elseif ( strtolower(ltrim($entry0->concept)) > strtolower(ltrim($entry1->concept)) ) {
1230 return 1;
1231 } else {
1232 return 0;
1233 }
1234}
1235
ed0201dd 1236function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1237 global $THEME, $CFG, $USER;
1238
1d9ddaaf 1239 $colour = $THEME->cellheading2;
ed0201dd 1240
1241 $user = get_record("user", "id", $comment->userid);
1242 $strby = get_string("writtenby","glossary");
1243
1244 echo "<table class=\"generalbox\" BORDER=1 CELLSPACING=0 valign=top cellpadding=0 width=70% border=0><tr><td>";
1245
1246 echo "\n<TABLE width=\"100%\" BORDER=0 CELLSPACING=0 valign=top cellpadding=5><tr>";
1247
1248 echo "\n<TD BGCOLOR=\"$THEME->cellheading\" WIDTH=25% VALIGN=TOP align=right >";
1249 print_user_picture($user->id, $course->id, $user->picture);
1250 echo "<br><FONT SIZE=2>$strby $user->firstname $user->lastname</font>";
1251 echo "<br><FONT SIZE=1>(".get_string("lastedited").": ".userdate($comment->timemodified).")</FONT></small><br>";
1252 echo "</TD>";
c197e607 1253
1d9ddaaf 1254 echo "<TD valign=top WIDTH=75% BGCOLOR=\"$THEME->cellcontent\">";
ed0201dd 1255 if ($comment) {
c197e607 1256 echo format_text($comment->comment, $comment->format);
ed0201dd 1257 } else {
c197e607 1258 echo "<center>";
ed0201dd 1259 print_string("nocomment", "glossary");
c197e607 1260 echo "</center>";
ed0201dd 1261 }
1262
1263 echo "<p align=right>";
c197e607 1264 if ( (time() - $comment->timemodified < $CFG->maxeditingtime and $USER->id == $comment->userid) or isteacher($course->id) ) {
ed0201dd 1265 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 1266 }
1267 if ( $USER->id == $comment->userid or isteacher($course->id) ) {
ed0201dd 1268 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 1269 }
ed0201dd 1270 echo "</td>";
c197e607 1271
ed0201dd 1272 echo "</tr></TABLE>\n";
1273
1274 echo "</td></tr></table>";
1275}
1276
b1918034 1277function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
1d9ddaaf 1278 global $THEME, $USER;
cca6f7f1 1279
1d9ddaaf 1280 $colour = $THEME->cellheading2;
cca6f7f1 1281
1d9ddaaf 1282 echo "\n<center><table width=95% border=0><tr>";
1283 echo "<td width=100%\">";
1284 if ( $entries ) {
1285 foreach ( $entries as $entry ) {
1286 if (! $glossary = get_record("glossary", "id", $entry->glossaryid)) {
1287 error("Glossary ID was incorrect or no longer exists");
cca6f7f1 1288 }
1d9ddaaf 1289 if (! $course = get_record("course", "id", $glossary->course)) {
1290 error("Glossary is misconfigured - don't know what course it's from");
1291 }
0de786f7 1292 if (!$cm = get_coursemodule_from_instance("glossary", $entry->glossaryid, $glossary->course) ) {
1d9ddaaf 1293 error("Glossary is misconfigured - don't know what course module it is ");
1294 }
584dcac9 1295 $dp = $displayformat;
1296 // Hard-coded until the Display formats manager is done.
b6862360 1297 if ( $dprecord = get_record("glossary_displayformats","fid", $glossary->displayformat) ) {
1298 if ( $dprecord->relatedview >= 0 ) {
1299 $dp = $dprecord->relatedview;
1300 }
584dcac9 1301 }
1302 glossary_print_entry($course, $cm, $glossary, $entry, "","",0,$dp);
cca6f7f1 1303 }
cca6f7f1 1304 }
1d9ddaaf 1305 echo "</td>";
1306 echo "</tr></table></center>";
1307}
4f4ca7b5 1308
1ac87c73 1309function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
4f4ca7b5 1310global $CFG;
1311 glossary_check_moddata_dir($glossary);
1312 $h = glossary_open_xml($glossary);
1313
1314 $status = fwrite ($h,glossary_start_tag("INFO",1,true));
1315 fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name));
1316 fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro));
1317 fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost));
1318 fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries));
1319 fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial));
1320 fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet));
1321 fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall));
1322 fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments));
1323 fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink));
1324 fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval));
1325 fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary));
1ac87c73 1326 fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage));
4f4ca7b5 1327
748b1932 1328 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1329 $status = fwrite ($h,glossary_start_tag("ENTRIES",2,true));
1330 foreach ($entries as $entry) {
046a797c 1331 $permissiongranted = 1;
1ac87c73 1332 if ( $hook ) {
1333 switch ( $hook ) {
046a797c 1334 case "ALL":
1335 case "SPECIAL":
1336 break;
1337 default:
1ac87c73 1338 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1339 break;
1340 }
1341 }
1ac87c73 1342 if ( $hook ) {
1343 switch ( $hook ) {
046a797c 1344 case GLOSSARY_SHOW_ALL_CATEGORIES:
1345 break;
1346 case GLOSSARY_SHOW_NOT_CATEGORISED:
1347 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1348 break;
1349 default:
1ac87c73 1350 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1351 break;
1352 }
1353 }
1354 if ( $entry->approved and $permissiongranted ) {
748b1932 1355 $status = fwrite($h,glossary_start_tag("ENTRY",3,true));
1356 fwrite($h,glossary_full_tag("CONCEPT",4,false,$entry->concept));
1357 fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition));
1358 fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format));
1359 fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink));
1360 fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive));
1361 fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch));
1362 fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry));
1363
7965be79 1364 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1365 $status = fwrite ($h,glossary_start_tag("ALIASES",4,true));
1366 foreach ($aliases as $alias) {
1367 $status = fwrite ($h,glossary_start_tag("ALIAS",5,true));
1368 fwrite($h,glossary_full_tag("NAME",6,false,$alias->alias));
1369 $status = fwrite($h,glossary_end_tag("ALIAS",5,true));
1370 }
00758851 1371 $status = fwrite($h,glossary_end_tag("ALIASES",4,true));
7965be79 1372 }
748b1932 1373 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1374 $status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true));
1375 foreach ($catentries as $catentry) {
1376 $category = get_record("glossary_categories","id",$catentry->categoryid);
1377
1378 $status = fwrite ($h,glossary_start_tag("CATEGORY",5,true));
1379 fwrite($h,glossary_full_tag("NAME",6,false,$category->name));
1ac87c73 1380 fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink));
748b1932 1381 $status = fwrite($h,glossary_end_tag("CATEGORY",5,true));
1382 }
1383 $status = fwrite($h,glossary_end_tag("CATEGORIES",4,true));
1384 }
4f4ca7b5 1385
748b1932 1386 $status =fwrite($h,glossary_end_tag("ENTRY",3,true));
4f4ca7b5 1387 }
1388 }
748b1932 1389 $status =fwrite ($h,glossary_end_tag("ENTRIES",2,true));
1390
4f4ca7b5 1391 }
748b1932 1392
1393
1394 $status =fwrite ($h,glossary_end_tag("INFO",1,true));
1395
4f4ca7b5 1396 $h = glossary_close_xml($h);
1397}
1398// Functions designed by Eloy Lafuente
1399//
1400//Function to create, open and write header of the xml file
1401function glossary_open_xml($glossary) {
1402
1403 global $CFG;
1404
1405 $status = true;
1406
1407 //Open for writing
1408
1409 $file = $CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name) ."/glossary.xml";
1410 $h = fopen($file,"w");
1411 //Writes the header
1412 $status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1413 if ($status) {
1414 $status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
1415 }
1416 if ($status) {
1417 return $h;
1418 } else {
1419 return false;
1420 }
1421}
1422
748b1932 1423function glossary_read_imported_file($file) {
1424require_once "../../lib/xmlize.php";
1425 $h = fopen($file,"r");
1426 $line = '';
1427 if ($h) {
1428 while ( !feof($h) ) {
1429 $char = fread($h,1024);
1430 $line .= $char;
1431 }
1432 fclose($h);
1433 }
1434 return xmlize($line);
1435}
4f4ca7b5 1436//Close the file
1437function glossary_close_xml($h) {
1438 $status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
1439 return fclose($h);
1440}
1441
1442//Return the xml start tag
1443function glossary_start_tag($tag,$level=0,$endline=false) {
1444 if ($endline) {
1445 $endchar = "\n";
1446 } else {
1447 $endchar = "";
1448 }
1449 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1450}
1451
1452//Return the xml end tag
1453function glossary_end_tag($tag,$level=0,$endline=true) {
1454 if ($endline) {
1455 $endchar = "\n";
1456 } else {
1457 $endchar = "";
1458 }
1459 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1460}
1461
1462//Return the start tag, the contents and the end tag
1463function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
1464 $st = glossary_start_tag($tag,$level,$endline);
1465 $co="";
1466 if ($to_utf) {
1467 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
1468 } else {
1469 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
1470 }
1471 $et = glossary_end_tag($tag,0,true);
1472 return $st.$co.$et;
1473}
1474
1475 //Function to check and create the needed moddata dir to
1476 //save all the mod backup files. We always name it moddata
1477 //to be able to restore it, but in restore we check for
1478 //$CFG->moddata !!
1479function glossary_check_moddata_dir($glossary) {
1480
1481 global $CFG;
1482
1483 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
1484 if ( $status ) {
1485 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
1486 if ( $status ) {
1487 $status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename($glossary->name),true);
1488 }
1489 }
1490 return $status;
1491}
1492
1493//Function to check if a directory exists
1494//and, optionally, create it
1495function glossary_check_dir_exists($dir,$create=false) {
1496
1497 global $CFG;
1498
1499 $status = true;
1500 if(!is_dir($dir)) {
1501 if (!$create) {
1502 $status = false;
1503 } else {
1504 umask(0000);
1505 $status = mkdir ($dir,$CFG->directorypermissions);
1506 }
1507 }
1508 return $status;
1509}
cc41c129 1510?>