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