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