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