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