Possible fix to MDL-12093 : give window without heading and navigation special css...
[moodle.git] / mod / glossary / lib.php
CommitLineData
41905731 1<?php // $Id$
07842023 2
3/// Library of functions and constants for module glossary
4/// (replace glossary with the name of your module and delete this line)
5
f1e0649c 6require_once($CFG->libdir.'/filelib.php');
07842023 7
7dd88447 8define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
9define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
c76e673a 10
1ac87c73 11define("GLOSSARY_NO_VIEW", -1);
7dd88447 12define("GLOSSARY_STANDARD_VIEW", 0);
13define("GLOSSARY_CATEGORY_VIEW", 1);
c197e607 14define("GLOSSARY_DATE_VIEW", 2);
1ac87c73 15define("GLOSSARY_AUTHOR_VIEW", 3);
16define("GLOSSARY_ADDENTRY_VIEW", 4);
17define("GLOSSARY_IMPORT_VIEW", 5);
18define("GLOSSARY_EXPORT_VIEW", 6);
19define("GLOSSARY_APPROVAL_VIEW", 7);
fb443f1a 20
da9c60ec 21//Check for global configure default variables
22
23if (!isset($CFG->glossary_studentspost)) {
24 set_config("glossary_studentspost", 1); // Students can post entries.
25}
26
27if (!isset($CFG->glossary_dupentries)) {
28 set_config("glossary_dupentries", 0); // Duplicate entries are not allowed.
29}
30
31if (!isset($CFG->glossary_allowcomments)) {
32 set_config("glossary_allowcomments", 0); // Comments are not allowed.
33}
34
35if (!isset($CFG->glossary_linkbydefault)) {
36 set_config("glossary_linkbydefault", 1); // Linking entries is enabled.
37}
38
39if (!isset($CFG->glossary_defaultapproval)) {
40 set_config("glossary_defaultapproval", 1); // Entries are approved.
41}
42
43if (!isset($CFG->glossary_entbypage)) {
44 set_config("glossary_entbypage", 10); // 10 entries are showed.
45}
46
47/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
48
07842023 49function glossary_add_instance($glossary) {
50/// Given an object containing all the necessary data,
51/// (defined by the form in mod.html) this function
52/// will create a new instance and return the id number
53/// of the new instance.
54
63e21b9b 55 if (empty($glossary->userating)) {
63dd5fb2 56 $glossary->assessed = 0;
57 }
58
e6d45048 59 if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
63dd5fb2 60 $glossary->assesstimestart = 0;
61 $glossary->assesstimefinish = 0;
62 }
63
63e21b9b 64 if (empty($glossary->globalglossary) ) {
0de786f7 65 $glossary->globalglossary = 0;
63e21b9b 66 }
67
68 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
0de786f7 69 $glossary->globalglossary = 0;
70 }
71
e6d45048 72 $glossary->timecreated = time();
07842023 73 $glossary->timemodified = $glossary->timecreated;
74
ff424012 75 //Check displayformat is a valid one
76 $formats = get_list_of_plugins('mod/glossary/formats','TEMPLATE');
77 if (!in_array($glossary->displayformat, $formats)) {
78 error("This format doesn't exist!");
a02c77dc 79 }
24c1be88 80
e6d45048 81 if ($returnid = insert_record("glossary", $glossary)) {
82 $glossary->id = $returnid;
4e9ba8c0 83 $glossary = stripslashes_recursive($glossary);
612607bd 84 glossary_grade_item_update($glossary);
e6d45048 85 }
86
87 return $returnid;
07842023 88}
89
90
91function glossary_update_instance($glossary) {
92/// Given an object containing all the necessary data,
93/// (defined by the form in mod.html) this function
94/// will update an existing instance with new data.
9c37662f 95 global $CFG;
a02c77dc 96
da5754f8 97 if (empty($glossary->globalglossary)) {
98 $glossary->globalglossary = 0;
99 }
0de786f7 100
63e21b9b 101 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
102 // keep previous
103 unset($glossary->globalglossary);
104 }
105
07842023 106 $glossary->timemodified = time();
e6d45048 107 $glossary->id = $glossary->instance;
07842023 108
63e21b9b 109 if (empty($glossary->userating)) {
63dd5fb2 110 $glossary->assessed = 0;
111 }
112
e6d45048 113 if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
63dd5fb2 114 $glossary->assesstimestart = 0;
115 $glossary->assesstimefinish = 0;
116 }
117
ff424012 118 //Check displayformat is a valid one
119 $formats = get_list_of_plugins('mod/glossary/formats','TEMPLATE');
120 if (!in_array($glossary->displayformat, $formats)) {
121 error("This format doesn't exist!");
122 }
123
e6d45048 124 if ($return = update_record("glossary", $glossary)) {
125 if ($glossary->defaultapproval) {
126 execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
127 }
4e9ba8c0 128 $glossary = stripslashes_recursive($glossary);
e6d45048 129 glossary_grade_item_update($glossary);
5eaa2d34 130 }
07842023 131
5eaa2d34 132 return $return;
07842023 133}
134
135
136function glossary_delete_instance($id) {
137/// Given an ID of an instance of this module,
138/// this function will permanently delete the instance
139/// and any data that depends on it.
140
141 if (! $glossary = get_record("glossary", "id", "$id")) {
142 return false;
143 }
144
145 $result = true;
146
147 # Delete any dependent records here #
148
149 if (! delete_records("glossary", "id", "$glossary->id")) {
150 $result = false;
49210b90 151 } else {
152 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
153 $cats = "";
4070cddf 154 foreach ( $categories as $cat ) {
49210b90 155 $cats .= "$cat->id,";
156 }
157 $cats = substr($cats,0,-1);
158 if ($cats) {
159 delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
160 delete_records("glossary_categories", "glossaryid", $glossary->id);
161 }
162 }
163 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
164 $ents = "";
165 foreach ( $entries as $entry ) {
072f7533 166 if ( $entry->sourceglossaryid ) {
167 $entry->glossaryid = $entry->sourceglossaryid;
168 $entry->sourceglossaryid = 0;
169 update_record("glossary_entries",$entry);
170 } else {
171 $ents .= "$entry->id,";
172 }
49210b90 173 }
174 $ents = substr($ents,0,-1);
175 if ($ents) {
176 delete_records_select("glossary_comments", "entryid in ($ents)");
ea14e783 177 delete_records_select("glossary_alias", "entryid in ($ents)");
e29ad7eb 178 delete_records_select("glossary_ratings", "entryid in ($ents)");
49210b90 179 }
180 }
072f7533 181 glossary_delete_attachments($glossary);
49210b90 182 delete_records("glossary_entries", "glossaryid", "$glossary->id");
07842023 183 }
e6d45048 184 glossary_grade_item_delete($glossary);
07842023 185
186 return $result;
187}
188
189function glossary_user_outline($course, $user, $mod, $glossary) {
190/// Return a small object with summary information about what a
191/// user has done with a given particular instance of this module
192/// Used for user activity reports.
193/// $return->time = the time they did it
194/// $return->info = a short text description
195
1ac87c73 196 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
e6d45048 197 $result = new object();
1ac87c73 198 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
199
63dd5fb2 200 $lastentry = array_pop($entries);
201 $result->time = $lastentry->timemodified;
1ac87c73 202 return $result;
203 }
204 return NULL;
205}
206
207function glossary_get_user_entries($glossaryid, $userid) {
208/// Get all the entries for a user in a glossary
209 global $CFG;
210
211 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
a02c77dc 212 FROM {$CFG->prefix}glossary g,
213 {$CFG->prefix}glossary_entries e,
214 {$CFG->prefix}user u
215 WHERE g.id = '$glossaryid'
216 AND e.glossaryid = g.id
217 AND e.userid = '$userid'
1ac87c73 218 AND e.userid = u.id
219 ORDER BY e.timemodified ASC");
07842023 220}
221
222function glossary_user_complete($course, $user, $mod, $glossary) {
223/// Print a detailed representation of what a user has done with
224/// a given particular instance of this module, for user activity reports.
1ac87c73 225 global $CFG;
07842023 226
1ac87c73 227 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
a359c29b 228 echo '<table width="95%" border="0"><tr><td>';
1ac87c73 229 foreach ($entries as $entry) {
230 $cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
231 glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
232 echo '<p>';
233 }
a359c29b 234 echo '</td></tr></table>';
1ac87c73 235 }
07842023 236}
237
238function glossary_print_recent_activity($course, $isteacher, $timestart) {
239/// Given a course and a time, this module should find recent activity
240/// that has occurred in glossary activities and print it out.
241/// Return true if there was output, or false is there was none.
242
b8340d19 243 global $CFG;
07842023 244
8f7dc7f1 245 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
246 'course = \''.$course->id.'\' AND '.
247 'module = \'glossary\' AND '.
248 '(action = \'add entry\' OR '.
249 ' action = \'approve entry\')', 'time ASC')) {
07842023 250 return false;
251 }
252
a359c29b 253 $entries = array();
254
07842023 255 foreach ($logs as $log) {
256 //Create a temp valid module structure (course,id)
e6d45048 257 $tempmod = new object();
a02c77dc 258 $tempmod->course = $log->course;
8f7dc7f1 259 $entry = get_record('glossary_entries','id',$log->info);
cf8eaebb 260 if (!$entry) {
261 continue;
262 }
1ac87c73 263 $tempmod->id = $entry->glossaryid;
a02c77dc 264 //Obtain the visible property from the instance
07842023 265 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 266
267 //Only if the mod is visible
c4a35419 268 if ($modvisible and $entry->approved) {
07842023 269 $entries[$log->info] = glossary_log_info($log);
270 $entries[$log->info]->time = $log->time;
d7d21c38 271 $entries[$log->info]->url = str_replace('&', '&amp;', $log->url);
07842023 272 }
07842023 273 }
274
07842023 275 $content = false;
276 if ($entries) {
07842023 277 $content = true;
8f7dc7f1 278 print_headline(get_string('newentries', 'glossary').':');
07842023 279 foreach ($entries as $entry) {
8f7dc7f1 280 $user = get_record('user','id',$entry->userid, '','', '','', 'firstname,lastname');
281
583b57b4 282 print_recent_activity_note($entry->timemodified, $user, $entry->concept,
8f7dc7f1 283 $CFG->wwwroot.'/mod/glossary/view.php?g='.$entry->glossaryid.
284 '&amp;mode=entry&amp;hook='.$entry->id);
07842023 285 }
286 }
287
288 return $content;
289}
290
8f7dc7f1 291
1ac87c73 292function glossary_log_info($log) {
293 global $CFG;
294
295 return get_record_sql("SELECT e.*, u.firstname, u.lastname
296 FROM {$CFG->prefix}glossary_entries e,
297 {$CFG->prefix}user u
298 WHERE e.id = '$log->info'
299 AND u.id = '$log->userid'");
300}
301
07842023 302function glossary_cron () {
303/// Function to be run periodically according to the moodle cron
304/// This function searches for things that need to be done, such
305/// as sending out mail, toggling flags etc ...
306
307 global $CFG;
308
309 return true;
310}
311
d31bae70 312/**
313 * Return grade for given user or all users.
314 *
315 * @param int $glossaryid id of glossary
316 * @param int $userid optional user id, 0 means all users
317 * @return array array of grades, false if none
318 */
612607bd 319function glossary_get_user_grades($glossary, $userid=0) {
e6d45048 320 global $CFG;
63dd5fb2 321
e6d45048 322 $user = $userid ? "AND u.id = $userid" : "";
63dd5fb2 323
ac9b0805 324 $sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS rawgrade
e6d45048 325 FROM {$CFG->prefix}user u, {$CFG->prefix}glossary_entries ge,
326 {$CFG->prefix}glossary_ratings gr
327 WHERE u.id = ge.userid AND ge.id = gr.entryid
612607bd 328 AND gr.userid != u.id AND ge.glossaryid = $glossary->id
e6d45048 329 $user
330 GROUP BY u.id";
331
332 return get_records_sql($sql);
333}
334
d31bae70 335/**
336 * Update grades by firing grade_updated event
337 *
612607bd 338 * @param object $glossary null means all glossaries
d31bae70 339 * @param int $userid specific user only, 0 mean all
340 */
612607bd 341function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
e6d45048 342 global $CFG;
adcbb43a 343 require_once($CFG->libdir.'/gradelib.php');
e6d45048 344
612607bd 345 if ($glossary != null) {
346 if ($grades = glossary_get_user_grades($glossary, $userid)) {
b67ec72f 347 grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades);
3e6303b7 348
1970d243 349 } else if ($userid and $nullifnone) {
612607bd 350 $grade = new object();
3bfabe6e 351 $grade->userid = $userid;
ac9b0805 352 $grade->rawgrade = NULL;
b67ec72f 353 grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade);
63dd5fb2 354 }
a02c77dc 355
e6d45048 356 } else {
dd232d01 357 $sql = "SELECT g.*, cm.idnumber as cmidnumber
e8586b5f 358 FROM {$CFG->prefix}glossary g, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
359 WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id";
e6d45048 360 if ($rs = get_recordset_sql($sql)) {
03cedd62 361 while ($glossary = rs_fetch_next_record($rs)) {
362 glossary_grade_item_update($glossary);
363 if ($glossary->assessed) {
364 glossary_update_grades($glossary, 0, false);
37d543b5 365 }
63dd5fb2 366 }
e6d45048 367 rs_close($rs);
368 }
369 }
370}
371
d31bae70 372/**
612607bd 373 * Create/update grade item for given glossary
d31bae70 374 *
dd232d01 375 * @param object $glossary object with extra cmidnumber
612607bd 376 * @return int, 0 if ok, error code otherwise
d31bae70 377 */
e6d45048 378function glossary_grade_item_update($glossary) {
612607bd 379 global $CFG;
380 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
381 require_once($CFG->libdir.'/gradelib.php');
63dd5fb2 382 }
e6d45048 383
612607bd 384 $params = array('itemname'=>$glossary->name, 'idnumber'=>$glossary->cmidnumber);
e6d45048 385
5980d52f 386 if (!$glossary->assessed or $glossary->scale == 0) {
612607bd 387 $params['gradetype'] = GRADE_TYPE_NONE;
34601114 388
389 } else if ($glossary->scale > 0) {
e8586b5f 390 $params['gradetype'] = GRADE_TYPE_VALUE;
e6d45048 391 $params['grademax'] = $glossary->scale;
392 $params['grademin'] = 0;
393
394 } else if ($glossary->scale < 0) {
e8586b5f 395 $params['gradetype'] = GRADE_TYPE_SCALE;
e6d45048 396 $params['scaleid'] = -$glossary->scale;
e6d45048 397 }
398
b67ec72f 399 return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params);
e6d45048 400}
401
d31bae70 402/**
403 * Delete grade item for given glossary
404 *
dd232d01 405 * @param object $glossary object
d31bae70 406 */
e6d45048 407function glossary_grade_item_delete($glossary) {
612607bd 408 global $CFG;
409 require_once($CFG->libdir.'/gradelib.php');
410
b67ec72f 411 return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1));
07842023 412}
413
05855091 414function glossary_get_participants($glossaryid) {
415//Returns the users with data in one glossary
416//(users with records in glossary_entries, students)
417
418 global $CFG;
419
420 //Get students
95e72c12 421 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 422 FROM {$CFG->prefix}user u,
423 {$CFG->prefix}glossary_entries g
424 WHERE g.glossaryid = '$glossaryid' and
425 u.id = g.userid");
426
427 //Return students array (it contains an array of unique users)
428 return ($students);
429}
07842023 430
0f1a97c2 431function glossary_scale_used ($glossaryid,$scaleid) {
432//This function returns if a scale is being used by one glossary
a02c77dc 433
0f1a97c2 434 $return = false;
435
436 $rec = get_record("glossary","id","$glossaryid","scale","-$scaleid");
437
2127fedd 438 if (!empty($rec) && !empty($scaleid)) {
a02c77dc 439 $return = true;
440 }
441
0f1a97c2 442 return $return;
443}
444
85c9ebb9 445/**
446 * Checks if scale is being used by any instance of glossary
447 *
448 * This is used to find out if scale used anywhere
449 * @param $scaleid int
450 * @return boolean True if the scale is used by any glossary
451 */
452function glossary_scale_used_anywhere($scaleid) {
453 if ($scaleid and record_exists('glossary', 'scale', -$scaleid)) {
454 return true;
455 } else {
456 return false;
457 }
458}
459
07842023 460//////////////////////////////////////////////////////////////////////////////////////
461/// Any other glossary functions go here. Each of them must have a name that
462/// starts with glossary_
463
a359c29b 464//This function return an array of valid glossary_formats records
465//Everytime it's called, every existing format is checked, new formats
466//are included if detected and old formats are deleted and any glossary
467//using an invalid format is updated to the default (dictionary).
468function glossary_get_available_formats() {
469
470 global $CFG;
471
472 //Get available formats (plugin) and insert (if necessary) them into glossary_formats
77495793 473 $formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
474 $pluginformats = array();
a359c29b 475 foreach ($formats as $format) {
476 //If the format file exists
477 if (file_exists($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
478 include_once($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
479 //If the function exists
480 if (function_exists('glossary_show_entry_'.$format)) {
77495793 481 //Acummulate it as a valid format
482 $pluginformats[] = $format;
a359c29b 483 //If the format doesn't exist in the table
484 if (!$rec = get_record('glossary_formats','name',$format)) {
485 //Insert the record in glossary_formats
e6d45048 486 $gf = new object();
a359c29b 487 $gf->name = $format;
488 $gf->popupformatname = $format;
489 $gf->visible = 1;
490 insert_record("glossary_formats",$gf);
491 }
492 }
493 }
494 }
495
496 //Delete non_existent formats from glossary_formats table
497 $formats = get_records("glossary_formats");
498 foreach ($formats as $format) {
499 $todelete = false;
77495793 500 //If the format in DB isn't a valid previously detected format then delete the record
501 if (!in_array($format->name,$pluginformats)) {
a359c29b 502 $todelete = true;
a359c29b 503 }
504
505 if ($todelete) {
506 //Delete the format
507 delete_records('glossary_formats','name',$format->name);
508 //Reasign existing glossaries to default (dictionary) format
509 if ($glossaries = get_records('glossary','displayformat',$format->name)) {
510 foreach($glossaries as $glossary) {
511 set_field('glossary','displayformat','dictionary','id',$glossary->id);
512 }
513 }
514 }
515 }
516
517 //Now everything is ready in glossary_formats table
518 $formats = get_records("glossary_formats");
519
520 return $formats;
521}
522
1ac87c73 523function glossary_debug($debug,$text,$br=1) {
524 if ( $debug ) {
41905731 525 echo '<font color="red">' . $text . '</font>';
1ac87c73 526 if ( $br ) {
a9ef4a63 527 echo '<br />';
1ac87c73 528 }
529 }
07842023 530}
531
ea14e783 532function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
07842023 533 global $CFG;
ea14e783 534 if ($pivot) {
535 $pivot .= ",";
536 }
07842023 537
ea14e783 538 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
07842023 539 FROM {$CFG->prefix}glossary_entries
540 WHERE glossaryid = '$glossaryid'
541 AND id IN ($entrylist)");
542}
359f2758 543
544function glossary_get_entries_search($concept, $courseid) {
6c351232 545
359f2758 546 global $CFG;
547
a02c77dc 548 //Check if the user is an admin
6c351232 549 $bypassadmin = 1; //This means NO (by default)
9c37662f 550 if (has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
6c351232 551 $bypassadmin = 0; //This means YES
a02c77dc 552 }
6c351232 553
a02c77dc 554 //Check if the user is a teacher
6c351232 555 $bypassteacher = 1; //This means NO (by default)
81e956b9 556 if (has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE, $courseid))) {
6c351232 557 $bypassteacher = 0; //This means YES
a02c77dc 558 }
6c351232 559
95d56829 560 $conceptlower = moodle_strtolower(trim($concept));
359f2758 561
a736e963 562 return get_records_sql("SELECT e.*, g.name as glossaryname, cm.id as cmid, cm.course as courseid
a02c77dc 563 FROM {$CFG->prefix}glossary_entries e,
6c351232 564 {$CFG->prefix}glossary g,
565 {$CFG->prefix}course_modules cm,
566 {$CFG->prefix}modules m
567 WHERE m.name = 'glossary' AND
568 cm.module = m.id AND
569 (cm.visible = 1 OR cm.visible = $bypassadmin OR
570 (cm.course = '$courseid' AND cm.visible = $bypassteacher)) AND
571 g.id = cm.instance AND
572 e.glossaryid = g.id AND
95d56829 573 ( (e.casesensitive != 0 AND LOWER(concept) = '$conceptlower') OR
6c351232 574 (e.casesensitive = 0 and concept = '$concept')) AND
575 (g.course = '$courseid' OR g.globalglossary = 1) AND
576 e.usedynalink != 0 AND
577 g.usedynalink != 0");
359f2758 578}
579
ea14e783 580function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
c76e673a 581global $CFG;
582 if ($where) {
583 $where = " and $where";
584 }
585 if ($orderby) {
586 $orderby = " ORDER BY $orderby";
587 }
ea14e783 588 if ($pivot) {
589 $pivot .= ",";
590 }
591 return get_records_sql("SELECT $pivot *
a02c77dc 592 FROM {$CFG->prefix}glossary_entries
c76e673a 593 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
594}
595
1ac87c73 596function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
c76e673a 597global $CFG;
598 if ($where) {
599 $where = " and $where";
600 }
601 if ($orderby) {
602 $orderby = " ORDER BY $orderby";
603 }
ea14e783 604 if ($pivot) {
605 $pivot .= ",";
606 }
607 return get_records_sql("SELECT $pivot ge.*
c76e673a 608 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
63dd5fb2 609 WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
c76e673a 610 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
611}
07842023 612
b8340d19 613function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) {
614 global $USER, $CFG;
37d543b5 615 $return = false;
b1918034 616 if ( $displayformat < 0 ) {
617 $displayformat = $glossary->displayformat;
618 }
b620a995 619 if ($entry->approved or ($USER->id == $entry->userid) or ($mode == 'approval' and !$entry->approved) ) {
a359c29b 620 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
ae06e00e 621 if ($printview) {
622 $functionname = 'glossary_print_entry_'.$displayformat;
623 } else {
624 $functionname = 'glossary_show_entry_'.$displayformat;
625 }
a359c29b 626
627 if (file_exists($formatfile)) {
628 include_once($formatfile);
629 if (function_exists($functionname)) {
630 $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
ae06e00e 631 } else if ($printview) {
632 //If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format
633 $return = glossary_print_entry_default($entry);
c76e673a 634 }
767a31c3 635 }
07842023 636 }
a359c29b 637 return $return;
07842023 638}
b8340d19 639
ae06e00e 640 //Default (old) print format used if custom function doesn't exist in format
641function glossary_print_entry_default ($entry) {
097d705e 642 echo '<h3>'. strip_tags($entry->concept) . ': </h3>';
7d8a3cb0 643
644 $definition = $entry->definition;
645
646 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
647 if (trusttext_present($definition)) {
648 $ttpresent = true;
649 $definition = trusttext_strip($definition);
650 } else {
651 $ttpresent = false;
652 }
653
654 $definition = '<span class="nolink">' . strip_tags($definition) . '</span>';
655
656 // reconstruct the TRUSTTEXT properly after processing
657 if ($ttpresent) {
658 $definition = trusttext_mark($definition);
659 } else {
660 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
661 }
662
663 $options = new object();
ae06e00e 664 $options->para = false;
7d8a3cb0 665 $options->trusttext = true;
666 $definition = format_text($definition, $entry->format, $options);
ae06e00e 667 echo ($definition);
668 echo '<br /><br />';
669}
a359c29b 670
120a18f0 671/**
672 * Print glossary concept/term as a heading &lt;h3>
673 */
81bdc9e9 674function glossary_print_entry_concept($entry) {
7d8a3cb0 675 $options = new object();
d34b9c5f 676 $options->para = false;
120a18f0 677 $text = format_text(print_heading($entry->concept, '', 3, 'nolink'), FORMAT_MOODLE, $options);
ff8352de 678 if (!empty($entry->highlight)) {
679 $text = highlight($entry->highlight, $text);
680 }
681 echo $text;
81bdc9e9 682}
1d9ddaaf 683
684function glossary_print_entry_definition($entry) {
701fff21 685
f287e69c 686 $definition = $entry->definition;
687
7d8a3cb0 688 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
689 if (trusttext_present($definition)) {
690 $ttpresent = true;
691 $definition = trusttext_strip($definition);
692 } else {
693 $ttpresent = false;
694 }
695
0f9a668f 696 $links = array();
701fff21 697 $tags = array();
b5937aac 698 $urls = array();
699 $addrs = array();
f287e69c 700
701fff21 701 //Calculate all the strings to be no-linked
702 //First, the concept
b8340d19 703 $term = preg_quote(trim($entry->concept),'/');
701fff21 704 $pat = '/('.$term.')/is';
705 $doNolinks[] = $pat;
706 //Now the aliases
b8340d19 707 if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
701fff21 708 foreach ($aliases as $alias) {
b8340d19 709 $term = preg_quote(trim($alias->alias),'/');
701fff21 710 $pat = '/('.$term.')/is';
711 $doNolinks[] = $pat;
712 }
f287e69c 713 }
714
a02c77dc 715
0f9a668f 716 //Extract <a>..><a> tags from definition
717 preg_match_all('/<a[^>]+?>(.*?)<\/a>/is',$definition,$list_of_a);
a02c77dc 718
0f9a668f 719 //Save them into links array to use them later
720 foreach (array_unique($list_of_a[0]) as $key=>$value) {
721 $links['<#'.$key.'#>'] = $value;
722 }
723 //Take off every link from definition
724 if ( $links ) {
725 $definition = str_replace($links,array_keys($links),$definition);
726 }
727
728
701fff21 729 //Extract all tags from definition
730 preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
f287e69c 731
701fff21 732 //Save them into tags array to use them later
733 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
734 $tags['<@'.$key.'@>'] = $value;
f287e69c 735 }
701fff21 736 //Take off every tag from definition
737 if ( $tags ) {
738 $definition = str_replace($tags,array_keys($tags),$definition);
f287e69c 739 }
7275671a 740
0f9a668f 741
7275671a 742 //Extract all URLS with protocol (http://domain.com) from definition
743 preg_match_all('/([[:space:]]|^|\(|\[)([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_urls);
744
745 //Save them into urls array to use them later
746 foreach (array_unique($list_of_urls[0]) as $key=>$value) {
747 $urls['<*'.$key.'*>'] = $value;
748 }
7275671a 749 //Take off every url from definition
750 if ( $urls ) {
751 $definition = str_replace($urls,array_keys($urls),$definition);
752 }
753
0f9a668f 754
7275671a 755 //Extract all WEB ADDRESSES (www.domain.com) from definition
756 preg_match_all('/([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_addresses);
757
758 //Save them into addrs array to use them later
759 foreach (array_unique($list_of_addresses[0]) as $key=>$value) {
760 $addrs['<+'.$key.'+>'] = $value;
761 }
7275671a 762 //Take off every addr from definition
763 if ( $addrs ) {
764 $definition = str_replace($addrs,array_keys($addrs),$definition);
765 }
766
0f9a668f 767
701fff21 768 //Put doNolinks (concept + aliases) enclosed by <nolink> tag
e19a635c 769 $definition= preg_replace($doNolinks,'<span class="nolink">$1</span>',$definition);
770
7275671a 771 //Restore addrs
772 if ( $addrs ) {
773 $definition = str_replace(array_keys($addrs),$addrs,$definition);
774 }
775
776 //Restore urls
777 if ( $urls ) {
778 $definition = str_replace(array_keys($urls),$urls,$definition);
779 }
780
701fff21 781 //Restore tags
782 if ( $tags ) {
783 $definition = str_replace(array_keys($tags),$tags,$definition);
584dcac9 784 }
ff8352de 785
0f9a668f 786 //Restore links
787 if ( $links ) {
788 $definition = str_replace(array_keys($links),$links,$definition);
789 }
790
7d8a3cb0 791 $options = new object();
3eb2f7ec 792 $options->para = false;
7d8a3cb0 793 $options->trusttext = true;
794
795 // reconstruct the TRUSTTEXT properly after processing
796 if ($ttpresent) {
797 $definition = trusttext_mark($definition);
798 } else {
799 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
800 }
0f9a668f 801
7d8a3cb0 802 $text = format_text($definition, $entry->format, $options);
ff8352de 803 if (!empty($entry->highlight)) {
804 $text = highlight($entry->highlight, $text);
805 }
359f2758 806 if (isset($entry->footer)) { // Unparsed footer info
807 $text .= $entry->footer;
808 }
ff8352de 809 echo $text;
1d9ddaaf 810}
811
b8340d19 812function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode='',$hook='', $type = 'print') {
81bdc9e9 813 $return = '';
b8340d19 814 if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
81bdc9e9 815 foreach ($aliases as $alias) {
33cc423e 816 if (trim($alias->alias)) {
81bdc9e9 817 if ($return == '') {
818 $return = '<select style="font-size:8pt">';
819 }
820 $return .= "<option>$alias->alias</option>";
821 }
822 }
823 if ($return != '') {
824 $return .= '</select>';
81bdc9e9 825 }
a02c77dc 826 }
1ac87c73 827 if ($type == 'print') {
81bdc9e9 828 echo $return;
829 } else {
830 return $return;
831 }
832}
833
b8340d19 834function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$hook='', $type = 'print') {
835 global $USER, $CFG;
a02c77dc 836
dabfd0ed 837 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
81bdc9e9 838
36ce6ea2 839 $output = false; //To decide if we must really return text in "return". Activate when needed only!
81bdc9e9 840 $importedentry = ($entry->sourceglossaryid == $glossary->id);
81bdc9e9 841 $ismainglossary = $glossary->mainglossary;
b8340d19 842
843
9362b9b6 844 $return = '<span class="commands">';
097d705e 845 // Differentiate links for each entry.
846 $altsuffix = ': '.$entry->concept;
81bdc9e9 847 if (!$entry->approved) {
36ce6ea2 848 $output = true;
b8340d19 849 $return .= get_string('entryishidden','glossary');
81bdc9e9 850 }
66d7ae88 851 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
a02c77dc 852
ea16f9b2 853 if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments) {
dabfd0ed 854 $output = true;
097d705e 855 $return .= ' <a title="' . get_string('addcomment','glossary') . '" href="comment.php?action=add&amp;eid='.$entry->id.'"><img src="comment.gif" class="iconsmall" alt="'.get_string('addcomment','glossary').$altsuffix.'" /></a>';
81bdc9e9 856 }
857
b8340d19 858
6cb34d44 859 if (has_capability('mod/glossary:manageentries', $context) or (!empty($USER->id) and has_capability('mod/glossary:write', $context) and $entry->userid == $USER->id)) {
81bdc9e9 860 // only teachers can export entries so check it out
0468976c 861 if (has_capability('mod/glossary:export', $context) and !$ismainglossary and !$importedentry) {
dabfd0ed 862 $mainglossary = get_record('glossary','mainglossary',1,'course',$course->id);
81bdc9e9 863 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
36ce6ea2 864 $output = true;
097d705e 865 $return .= ' <a title="'.get_string('exporttomainglossary','glossary') . '" href="exportentry.php?id='.$cm->id.'&amp;entry='.$entry->id.'&amp;mode='.$mode.'&amp;hook='.$hook.'"><img src="export.gif" class="iconsmall" alt="'.get_string('exporttomainglossary','glossary').$altsuffix.'" /></a>';
81bdc9e9 866 }
867 }
868
869 if ( $entry->sourceglossaryid ) {
870 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
871 } else {
096b5432 872 $icon = "$CFG->pixpath/t/delete.gif";
81bdc9e9 873 }
874
ff1e2621 875 //Decide if an entry is editable:
876 // -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
877 // -The user is teacher or he is a student with time permissions (edit period or editalways defined).
516c7276 878 $ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
6cb34d44 879 if ( !$importedentry and (has_capability('mod/glossary:manageentries', $context) or ($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context))))) {
36ce6ea2 880 $output = true;
839f2456 881 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&amp;mode=delete&amp;entry=$entry->id&amp;prevmode=$mode&amp;hook=$hook\"><img src=\"";
81bdc9e9 882 $return .= $icon;
097d705e 883 $return .= "\" class=\"iconsmall\" alt=\"" . get_string("delete") .$altsuffix."\" /></a> ";
a02c77dc 884
097d705e 885 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&amp;e=$entry->id&amp;mode=$mode&amp;hook=$hook\"><img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"" . get_string("edit") .$altsuffix. "\" /></a>";
81bdc9e9 886 } elseif ( $importedentry ) {
41905731 887 $return .= " <font size=\"-1\">" . get_string("exportedentry","glossary") . "</font>";
81bdc9e9 888 }
889 }
890 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
36ce6ea2 891
9362b9b6 892 $return .= '</span>';
a02c77dc 893
36ce6ea2 894 //If we haven't calculated any REAL thing, delete result ($return)
895 if (!$output) {
896 $return = '';
897 }
898 //Print or get
1ac87c73 899 if ($type == 'print') {
81bdc9e9 900 echo $return;
901 } else {
902 return $return;
a0d1e2bb 903 }
904}
905
906function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
907 $return = '';
908
b8340d19 909 $count = count_records('glossary_comments','entryid',$entry->id);
a0d1e2bb 910 if ($count) {
b8340d19 911 $return = '';
839f2456 912 $return .= "<a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$count ";
a0d1e2bb 913 if ($count == 1) {
b8340d19 914 $return .= get_string('comment', 'glossary');
a0d1e2bb 915 } else {
b8340d19 916 $return .= get_string('comments', 'glossary');
a0d1e2bb 917 }
b8340d19 918 $return .= '</a>';
a0d1e2bb 919 }
920
921 if ($type == 'print') {
922 echo $return;
923 } else {
924 return $return;
81bdc9e9 925 }
926}
927
26983d03 928function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
81bdc9e9 929
26983d03 930 if ($aliases) {
9362b9b6 931 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,'html');
26983d03 932 }
9362b9b6 933 $icons = '';
934 $return = '';
1ac87c73 935 if ( $printicons ) {
9362b9b6 936 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,'html');
1ac87c73 937 }
36ce6ea2 938 if ($aliases || $icons || $ratings) {
9362b9b6 939 echo '<table>';
a359c29b 940 if ( $aliases ) {
9362b9b6 941 echo '<tr valign="top"><td class="aliases">' .
942 get_string('aliases','glossary').': '.$aliases . '</td></tr>';
a359c29b 943 }
944 if ($icons) {
9362b9b6 945 echo '<tr valign="top"><td class="icons">'.$icons.'</td></tr>';
a359c29b 946 }
947 if ($ratings) {
9362b9b6 948 echo '<tr valign="top"><td class="ratings">';
a359c29b 949 $return = glossary_print_entry_ratings($course, $entry, $ratings);
950 echo '</td></tr>';
951 }
952 echo '</table>';
81bdc9e9 953 }
a359c29b 954 return $return;
81bdc9e9 955}
956
77495793 957function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
1d9ddaaf 958/// valid format values: html : Return the HTML link for the attachment as an icon
959/// text : Return the HTML link for tha attachment as text
960/// blank : Print the output to the screen
961 if ($entry->attachment) {
894ff63f 962 $glossary = get_record("glossary","id",$entry->glossaryid);
1d9ddaaf 963 $entry->course = $glossary->course; //used inside print_attachment
77495793 964 if ($insidetable) {
41905731 965 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap=\"nowrap\">\n";
77495793 966 }
1d9ddaaf 967 echo glossary_print_attachments($entry,$format,$align);
77495793 968 if ($insidetable) {
969 echo "</td></tr></table>\n";
970 }
1d9ddaaf 971 }
972}
973
77495793 974function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
1ac87c73 975 if ( $mode == 'approval' and !$entry->approved ) {
77495793 976 if ($insidetable) {
a8466100 977 echo '<table class="glossaryapproval" align="'.$align.'"><tr><td align="'.$align.'">';
77495793 978 }
ff8889d8 979 echo '<a title="'.get_string('approve','glossary').'" href="approve.php?id='.$cm->id.'&amp;eid='.$entry->id.'&amp;mode='.$mode.'"><img align="'.$align.'" src="check.gif" style="border:0px; width:34px; height:34px" alt="'.get_string('approve','glossary').'" /></a>';
77495793 980 if ($insidetable) {
a8466100 981 echo '</td></tr></table>';
77495793 982 }
1d9ddaaf 983 }
984}
07842023 985
c80828fe 986function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {
a02c77dc 987// It returns all entries from all glossaries that matches the specified criteria
c80828fe 988// within a given $course. It performs an $extended search if necessary.
989// It restrict the search to only one $glossary if the $glossary parameter is set.
07842023 990
991 global $CFG;
c80828fe 992 if ( !$glossary ) {
993 if ( $glossaries = get_records("glossary", "course", $course->id) ) {
994 $glos = "";
995 foreach ( $glossaries as $glossary ) {
996 $glos .= "$glossary->id,";
997 }
9cae3799 998 $glos = substr($glos,0,-1);
c80828fe 999 }
1000 } else {
1001 $glos = $glossary->id;
1002 }
a02c77dc 1003
81e956b9 1004 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE, $glossary->course))) {
07842023 1005 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 1006 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 1007 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
1008 } else {
1009
1010 $onlyvisible = "";
1011 $onlyvisibletable = "";
1012 }
1013
63dd5fb2 1014 /// Some differences in syntax for entrygreSQL
a4bad45c 1015 switch ($CFG->dbfamily) {
1016 case 'postgres':
07842023 1017 $LIKE = "ILIKE"; // case-insensitive
1018 $NOTLIKE = "NOT ILIKE"; // case-insensitive
1019 $REGEXP = "~*";
1020 $NOTREGEXP = "!~*";
a5e48d96 1021 break;
1022 case 'mysql':
1023 default:
07842023 1024 $LIKE = "LIKE";
1025 $NOTLIKE = "NOT LIKE";
1026 $REGEXP = "REGEXP";
1027 $NOTREGEXP = "NOT REGEXP";
a5e48d96 1028 break;
a02c77dc 1029 }
07842023 1030
1031 $conceptsearch = "";
1032 $definitionsearch = "";
1033
1034
1035 foreach ($searchterms as $searchterm) {
07842023 1036 if ($conceptsearch) {
1037 $conceptsearch.= " OR ";
1038 }
1039 if ($definitionsearch) {
1040 $definitionsearch.= " OR ";
1041 }
1042
6f4bdb9c 1043 /// Under Oracle and MSSQL, trim the + and - operators and perform
1044 /// simpler LIKE search
a4bad45c 1045 if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
6f4bdb9c 1046 $searchterm = trim($searchterm, '+-');
1047 }
1048
07842023 1049 if (substr($searchterm,0,1) == "+") {
1050 $searchterm = substr($searchterm,1);
1051 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1052 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1053 } else if (substr($searchterm,0,1) == "-") {
1054 $searchterm = substr($searchterm,1);
1055 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1056 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1057 } else {
1058 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
1059 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
1060 }
1061 }
1062
c80828fe 1063 if ( !$extended ) {
ed0201dd 1064 $definitionsearch = "0";
1065 }
07842023 1066
1067 $selectsql = "{$CFG->prefix}glossary_entries e,
1068 {$CFG->prefix}glossary g $onlyvisibletable
1069 WHERE ($conceptsearch OR $definitionsearch)
ad58adac 1070 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
c80828fe 1071 AND g.id IN ($glos) AND e.approved != 0";
07842023 1072
a02c77dc 1073 return get_records_sql("SELECT e.*
4d4c38f3 1074 FROM $selectsql ORDER BY e.concept ASC");
07842023 1075}
1076
c80828fe 1077function glossary_search_entries($searchterms, $glossary, $extended) {
1078 $course = get_record("course","id",$glossary->course);
1079 return glossary_search($course,$searchterms,$extended,$glossary);
1080}
1081
e179048e 1082function glossary_file_area_name($entry) {
e179048e 1083 global $CFG;
e29ad7eb 1084// Creates a directory file name, suitable for make_upload_directory()
e179048e 1085
a02c77dc 1086 // I'm doing this workaround for make it works for delete_instance also
e29ad7eb 1087 // (when called from delete_instance, glossary is already deleted so
1088 // getting the course from mdl_glossary does not work)
1089 $module = get_record("modules","name","glossary");
1090 $cm = get_record("course_modules","module",$module->id,"instance",$entry->glossaryid);
1091 return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
e179048e 1092}
1093
1094function glossary_file_area($entry) {
1095 return make_upload_directory( glossary_file_area_name($entry) );
1096}
1097
e29ad7eb 1098function glossary_main_file_area($glossary) {
1099 $modarea = glossary_mod_file_area($glossary);
1100 return "$modarea/$glossary->id";
1101}
1102
1103function glossary_mod_file_area($glossary) {
1104 global $CFG;
a02c77dc 1105
e29ad7eb 1106 return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
1107}
1108
e179048e 1109function glossary_delete_old_attachments($entry, $exception="") {
1110// Deletes all the user files in the attachments area for a entry
1111// EXCEPT for any file named $exception
1112
1113 if ($basedir = glossary_file_area($entry)) {
1114 if ($files = get_directory_list($basedir)) {
1115 foreach ($files as $file) {
1116 if ($file != $exception) {
1117 unlink("$basedir/$file");
1118// notify("Existing file '$file' has been deleted!");
1119 }
1120 }
1121 }
1122 if (!$exception) { // Delete directory as well, if empty
1123 rmdir("$basedir");
1124 }
1125 }
1126}
072f7533 1127function glossary_delete_attachments($glossary) {
e29ad7eb 1128// Deletes all the user files in the attachments area for the glossary
1129 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1130 $deleted = 0;
1131 foreach ($entries as $entry) {
1132 if ( $entry->attachment ) {
1133 if ($basedir = glossary_file_area($entry)) {
1134 if ($files = get_directory_list($basedir)) {
1135 foreach ($files as $file) {
1136 unlink("$basedir/$file");
1137 }
1138 }
1139 rmdir("$basedir");
1140 $deleted++;
1141 }
a02c77dc 1142 }
e29ad7eb 1143 }
1144 if ( $deleted ) {
1145 $attachmentdir = glossary_main_file_area($glossary);
1146 $glossarydir = glossary_mod_file_area($glossary);
a02c77dc 1147
e29ad7eb 1148 rmdir("$attachmentdir");
1149 if (!$files = get_directory_list($glossarydir) ) {
1150 rmdir( "$glossarydir" );
1151 }
a02c77dc 1152 }
e29ad7eb 1153 }
1154}
e179048e 1155
1156function glossary_copy_attachments($entry, $newentry) {
1157/// Given a entry object that is being copied to glossaryid,
1158/// this function checks that entry
1159/// for attachments, and if any are found, these are
1160/// copied to the new glossary directory.
1161
1162 global $CFG;
1163
1164 $return = true;
1165
1166 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
1167 foreach ($entries as $curentry) {
e6d45048 1168 $oldentry = new object();
e179048e 1169 $oldentry->id = $entry->id;
1170 $oldentry->course = $entry->course;
1171 $oldentry->glossaryid = $curentry->glossaryid;
1172 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1173 if (is_dir($oldentrydir)) {
1174
1175 $newentrydir = glossary_file_area($newentry);
1176 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
1177 $return = false;
1178 }
1179 }
1180 }
1181 }
1182 return $return;
1183}
1184
1185function glossary_move_attachments($entry, $glossaryid) {
1186/// Given a entry object that is being moved to glossaryid,
1187/// this function checks that entry
1188/// for attachments, and if any are found, these are
1189/// moved to the new glossary directory.
1190
1191 global $CFG;
1192
18b8fbfa 1193 require_once($CFG->dirroot.'/lib/uploadlib.php');
1194
e179048e 1195 $return = true;
1196
1197 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
1198 foreach ($entries as $entry) {
e6d45048 1199 $oldentry = new object();
e179048e 1200 $oldentry->course = $entry->course;
1201 $oldentry->glossaryid = $entry->glossaryid;
1202 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1203 if (is_dir($oldentrydir)) {
1204 $newentry = $oldentry;
1205 $newentry->glossaryid = $glossaryid;
1206 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
18b8fbfa 1207 $files = get_directory_list($oldentrydir); // get it before we rename it.
e179048e 1208 if (! @rename($oldentrydir, $newentrydir)) {
1209 $return = false;
1210 }
18b8fbfa 1211 foreach ($files as $file) {
1212 // this is not tested as I can't find anywhere that calls this function, grepping the source.
a02c77dc 1213 clam_change_log($oldentrydir.'/'.$file,$newentrydir.'/'.$file);
18b8fbfa 1214 }
e179048e 1215 }
1216 }
1217 }
1218 return $return;
1219}
1220
de53b9ac 1221function glossary_print_attachments($entry, $return=NULL, $align="left") {
e179048e 1222// if return=html, then return a html string.
1223// if return=text, then return a text-only string.
1224// otherwise, print HTML for non-images, and return image HTML
b764feaa 1225// if attachment is an image, $align set its aligment.
e179048e 1226 global $CFG;
a02c77dc 1227
b764feaa 1228 $newentry = $entry;
1229 if ( $newentry->sourceglossaryid ) {
1230 $newentry->glossaryid = $newentry->sourceglossaryid;
1231 }
e179048e 1232
b764feaa 1233 $filearea = glossary_file_area_name($newentry);
e179048e 1234
1235 $imagereturn = "";
1236 $output = "";
1237
b764feaa 1238 if ($basedir = glossary_file_area($newentry)) {
e179048e 1239 if ($files = get_directory_list($basedir)) {
1240 $strattachment = get_string("attachment", "glossary");
e179048e 1241 foreach ($files as $file) {
1242 $icon = mimeinfo("icon", $file);
1243 if ($CFG->slasharguments) {
d96624ca 1244 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
e179048e 1245 } else {
d96624ca 1246 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
e179048e 1247 }
0d905d9f 1248 $image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
e179048e 1249
1250 if ($return == "html") {
d96624ca 1251 $output .= "<a href=\"$ffurl\">$image</a> ";
1252 $output .= "<a href=\"$ffurl\">$file</a><br />";
24fb69e1 1253
e179048e 1254 } else if ($return == "text") {
d96624ca 1255 $output .= "$strattachment $file:\n$ffurl\n";
e179048e 1256
1257 } else {
1258 if ($icon == "image.gif") { // Image attachments don't get printed as links
d96624ca 1259 $imagereturn .= "<img src=\"$ffurl\" align=\"$align\" alt=\"\" />";
e179048e 1260 } else {
24fb69e1 1261 echo "<a href=\"$ffurl\">$image</a> ";
d96624ca 1262 echo "<a href=\"$ffurl\">$file</a><br />";
e179048e 1263 }
1264 }
1265 }
1266 }
1267 }
1268
1269 if ($return) {
1270 return $output;
1271 }
1272
1273 return $imagereturn;
1274}
1275
ad58adac 1276function glossary_print_tabbed_table_end() {
9ca8cc08 1277 echo "</div></div>";
06d94a52 1278}
1279
1ac87c73 1280function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1281 if ($glossary->showalphabet) {
7a3ec1af 1282 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
677038ee 1283 }
1ac87c73 1284 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1285
c4a35419 1286 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
c76e673a 1287
1ac87c73 1288 glossary_print_all_links($cm, $glossary, $mode, $hook);
894ff63f 1289
db87439a 1290 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
c76e673a 1291}
1292
1ac87c73 1293function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
7a3ec1af 1294 echo '<div class="glossaryexplain">' . get_string("explainimport","glossary") . '</div>';
748b1932 1295}
1296
1ac87c73 1297function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
7a3ec1af 1298 echo '<div class="glossaryexplain">' . get_string("explainexport","glossary") . '</div>';
748b1932 1299}
1300
1ac87c73 1301function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1302 if ( $mode != 'date' ) {
a359c29b 1303 if ($glossary->showalphabet) {
7a3ec1af 1304 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
c197e607 1305 }
c76e673a 1306
1ac87c73 1307 glossary_print_special_links($cm, $glossary, $mode, $hook);
c76e673a 1308
c4a35419 1309 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
c197e607 1310
1ac87c73 1311 glossary_print_all_links($cm, $glossary, $mode, $hook);
c197e607 1312 } else {
1ac87c73 1313 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1314 }
1315}
1316
1317function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
a359c29b 1318 if ($glossary->showalphabet) {
7a3ec1af 1319 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
c197e607 1320 }
1ac87c73 1321
c4a35419 1322 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1ac87c73 1323 glossary_print_all_links($cm, $glossary, $mode, $hook);
7a3ec1af 1324 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
c76e673a 1325}
1326
1ac87c73 1327function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
a02c77dc 1328
dabfd0ed 1329 global $CFG;
a02c77dc 1330
dabfd0ed 1331 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
3a26f0ea 1332
41905731 1333 echo '<table border="0" width="100%">';
c197e607 1334 echo '<tr>';
c76e673a 1335
5bd76d7f 1336 echo '<td align="center" style="width:20%">';
0468976c 1337 if (has_capability('mod/glossary:managecategories', $context)) {
c76e673a 1338 $options['id'] = $cm->id;
1ac87c73 1339 $options['mode'] = 'cat';
1340 $options['hook'] = $hook;
c76e673a 1341 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1342 }
c197e607 1343 echo '</td>';
c76e673a 1344
5bd76d7f 1345 echo '<td align="center" style="width:60%">';
c197e607 1346 echo '<b>';
c76e673a 1347
1348 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1349 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
677038ee 1350
c76e673a 1351 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
c197e607 1352 $selected = '';
c76e673a 1353 if ( $categories ) {
1354 foreach ($categories as $currentcategory) {
1355 $url = $currentcategory->id;
1356 if ( $category ) {
1357 if ($currentcategory->id == $category->id) {
1358 $selected = $url;
1359 }
1360 }
ae06e00e 1361 $menu[$url] = clean_text($currentcategory->name); //Only clean, not filters
c76e673a 1362 }
1363 }
1364 if ( !$selected ) {
1365 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1366 }
1367
1368 if ( $category ) {
5bd76d7f 1369 echo format_text($category->name, FORMAT_PLAIN);
c76e673a 1370 } else {
1ac87c73 1371 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
c76e673a 1372
1373 echo get_string("entrieswithoutcategory","glossary");
1374 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1375
1ac87c73 1376 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
c76e673a 1377
1378 echo get_string("allcategories","glossary");
1379 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1380
1381 }
1382 }
c197e607 1383 echo '</b></td>';
5bd76d7f 1384 echo '<td align="center" style="width:20%">';
c76e673a 1385
839f2456 1386 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=cat&amp;hook=", $menu, "catmenu", $selected, "",
c76e673a 1387 "", "", false);
677038ee 1388
c197e607 1389 echo '</td>';
1390 echo '</tr>';
c76e673a 1391
c197e607 1392 echo '</table>';
c76e673a 1393}
1394
1ac87c73 1395function glossary_print_all_links($cm, $glossary, $mode, $hook) {
a02c77dc 1396global $CFG;
a359c29b 1397 if ( $glossary->showall) {
c76e673a 1398 $strallentries = get_string("allentries", "glossary");
1ac87c73 1399 if ( $hook == 'ALL' ) {
c76e673a 1400 echo "<b>$strallentries</b>";
1401 } else {
1402 $strexplainall = strip_tags(get_string("explainall","glossary"));
839f2456 1403 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=ALL\">$strallentries</a>";
c76e673a 1404 }
1405 }
1406}
1407
1ac87c73 1408function glossary_print_special_links($cm, $glossary, $mode, $hook) {
c76e673a 1409global $CFG;
a359c29b 1410 if ( $glossary->showspecial) {
c76e673a 1411 $strspecial = get_string("special", "glossary");
1ac87c73 1412 if ( $hook == 'SPECIAL' ) {
677038ee 1413 echo "<b>$strspecial</b> | ";
1414 } else {
1415 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
839f2456 1416 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=SPECIAL\">$strspecial</a> | ";
677038ee 1417 }
914cb260 1418 }
c76e673a 1419}
677038ee 1420
c4a35419 1421function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
c76e673a 1422global $CFG;
a359c29b 1423 if ( $glossary->showalphabet) {
c4274149 1424 $alphabet = explode(",", get_string("alphabet"));
677038ee 1425 $letters_by_line = 14;
1426 for ($i = 0; $i < count($alphabet); $i++) {
1ac87c73 1427 if ( $hook == $alphabet[$i] and $hook) {
677038ee 1428 echo "<b>$alphabet[$i]</b>";
1429 } else {
82015ed2 1430 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=".urlencode($alphabet[$i])."&amp;sortkey=$sortkey&amp;sortorder=$sortorder\">$alphabet[$i]</a>";
677038ee 1431 }
1432 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
c197e607 1433 echo ' | ';
677038ee 1434 } else {
a9ef4a63 1435 echo '<br />';
677038ee 1436 }
767a31c3 1437 }
677038ee 1438 }
c76e673a 1439}
1440
1ac87c73 1441function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
c76e673a 1442global $CFG;
677038ee 1443
c4a35419 1444 $asc = get_string("ascending","glossary");
1445 $desc = get_string("descending","glossary");
1446 $bopen = '<b>';
1447 $bclose = '</b>';
a02c77dc 1448
c197e607 1449 $neworder = '';
468c120a 1450 $currentorder = '';
1451 $currentsort = '';
677038ee 1452 if ( $sortorder ) {
c197e607 1453 if ( $sortorder == 'asc' ) {
468c120a 1454 $currentorder = $asc;
839f2456 1455 $neworder = '&amp;sortorder=desc';
468c120a 1456 $newordertitle = get_string('changeto', 'glossary', $desc);
677038ee 1457 } else {
468c120a 1458 $currentorder = $desc;
839f2456 1459 $neworder = '&amp;sortorder=asc';
468c120a 1460 $newordertitle = get_string('changeto', 'glossary', $asc);
677038ee 1461 }
468c120a 1462 $icon = " <img src=\"$sortorder.gif\" class=\"icon\" alt=\"$newordertitle\" />";
677038ee 1463 } else {
c4a35419 1464 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1465 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
677038ee 1466 $icon = "";
c4a35419 1467 $newordertitle = $asc;
677038ee 1468 } else {
c4a35419 1469 $newordertitle = $desc;
839f2456 1470 $neworder = '&amp;sortorder=desc';
468c120a 1471 $icon = ' <img src="asc.gif" class="icon" alt="'.$newordertitle.'" />';
677038ee 1472 }
1473 }
c4a35419 1474 $ficon = '';
1475 $fneworder = '';
1476 $fbtag = '';
1477 $fendbtag = '';
1478
1479 $sicon = '';
1480 $sneworder = '';
ae078733 1481
1482 $sbtag = '';
1483 $fbtag = '';
1484 $fendbtag = '';
1485 $sendbtag = '';
1486
c4a35419 1487 $sendbtag = '';
1488
1489 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1490 $ficon = $icon;
1491 $fneworder = $neworder;
1492 $fordertitle = $newordertitle;
1493 $sordertitle = $asc;
1494 $fbtag = $bopen;
1495 $fendbtag = $bclose;
1496 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1497 $sicon = $icon;
1498 $sneworder = $neworder;
1499 $fordertitle = $asc;
1500 $sordertitle = $newordertitle;
1501 $sbtag = $bopen;
1502 $sendbtag = $bclose;
677038ee 1503 } else {
c4a35419 1504 $fordertitle = $asc;
1505 $sordertitle = $asc;
677038ee 1506 }
c4a35419 1507
1508 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1509 $forder = 'CREATION';
1510 $sorder = 'UPDATE';
1511 $fsort = get_string("sortbycreation", "glossary");
1512 $ssort = get_string("sortbylastupdate", "glossary");
1513
468c120a 1514 $currentsort = $fsort;
1515 if ($sortkey == 'UPDATE') {
1516 $currentsort = $ssort;
1517 }
c4a35419 1518 $sort = get_string("sortchronogically", "glossary");
1519 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1520 $forder = 'FIRSTNAME';
1521 $sorder = 'LASTNAME';
1522 $fsort = get_string("firstname");
1523 $ssort = get_string("lastname");
1524
468c120a 1525 $currentsort = $fsort;
1526 if ($sortkey == 'LASTNAME') {
1527 $currentsort = $ssort;
1528 }
c4a35419 1529 $sort = get_string("sortby", "glossary");
1530 }
468c120a 1531 $current = '<span class="accesshide">'.get_string('current', 'glossary', "$currentsort $currentorder").'</span>';
1532 echo "<br />$current $sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;sortkey=$sorder$sneworder&amp;mode=$mode\">$ssort$sicon</a>$sendbtag | ".
839f2456 1533 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;sortkey=$forder$fneworder&amp;mode=$mode\">$fsort$ficon</a>$fendbtag<br />";
fb443f1a 1534}
ad58adac 1535
1536function glossary_sort_entries ( $entry0, $entry1 ) {
a02c77dc 1537
95d56829 1538 if ( moodle_strtolower(ltrim($entry0->concept)) < moodle_strtolower(ltrim($entry1->concept)) ) {
ad58adac 1539 return -1;
95d56829 1540 } elseif ( moodle_strtolower(ltrim($entry0->concept)) > moodle_strtolower(ltrim($entry1->concept)) ) {
ad58adac 1541 return 1;
1542 } else {
1543 return 0;
1544 }
1545}
1546
ed0201dd 1547function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
3a26f0ea 1548 global $CFG, $USER;
a02c77dc 1549
bbbf2d40 1550 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
ed0201dd 1551
3a26f0ea 1552 $user = get_record('user', 'id', $comment->userid);
1553 $strby = get_string('writtenby','glossary');
26663a23 1554 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
ed0201dd 1555
36a2b6bd 1556 echo '<div class="boxaligncenter">';
3a26f0ea 1557 echo '<table class="glossarycomment" cellspacing="0">';
1558 echo '<tr valign="top">';
eee75231 1559 echo '<td class="left picture">';
79be5e9a 1560 print_user_picture($user->id, $course->id, $user->picture);
3a26f0ea 1561 echo '</td>';
1562 echo '<td class="entryheader">';
c07ca217 1563
26663a23 1564 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
e6d45048 1565 $by = new object();
c07ca217 1566 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
1567 $by->date = userdate($comment->timemodified);
1568 echo '<span class="author">'.get_string('bynameondate', 'forum', $by).'</span>';
1569
3a26f0ea 1570 echo '</td></tr>';
ed0201dd 1571
3a26f0ea 1572 echo '<tr valign="top"><td class="left side">';
1573 echo '&nbsp;';
1574 echo '</td><td class="entry">';
072f7533 1575
7d8a3cb0 1576 $options = new object();
1577 $options->trusttext = true;
0703d22e 1578 echo format_text($comment->entrycomment, $comment->format, $options);
072f7533 1579
3a26f0ea 1580 echo '<div class="icons commands">';
1581
3a512f86 1582 $ineditperiod = ((time() - $comment->timemodified < $CFG->maxeditingtime) || $glossary->editalways);
0468976c 1583 if ( ($glossary->allowcomments && $ineditperiod && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context)) {
a02c77dc 1584 echo "<a href=\"comment.php?cid=$comment->id&amp;action=edit\"><img
0d905d9f 1585 alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" /></a> ";
c197e607 1586 }
0468976c 1587 if ( ($glossary->allowcomments && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context) ) {
a02c77dc 1588 echo "<a href=\"comment.php?cid=$comment->id&amp;action=delete\"><img
0d905d9f 1589 alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";
c197e607 1590 }
a02c77dc 1591
3a26f0ea 1592 echo '</div></td></tr>';
36a2b6bd 1593 echo '</table></div>';
ed0201dd 1594
ed0201dd 1595}
1596
63dd5fb2 1597function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
a02c77dc 1598
bbbf2d40 1599 global $USER, $CFG;
a02c77dc 1600
bbbf2d40 1601 $glossary = get_record('glossary', 'id', $entry->glossaryid);
1602 $glossarymod = get_record('modules','name','glossary');
a02c77dc 1603 $cm = get_record_sql("select * from {$CFG->prefix}course_modules where course = $course->id
dabfd0ed 1604 and module = $glossarymod->id and instance = $glossary->id");
a02c77dc 1605
bbbf2d40 1606 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
a359c29b 1607
63dd5fb2 1608 $ratingsmenuused = false;
1609 if (!empty($ratings) and !empty($USER->id)) {
1610 $useratings = true;
1611 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1612 if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) {
1613 $useratings = false;
1614 }
1615 }
1616 if ($useratings) {
0468976c 1617 if (has_capability('mod/glossary:viewrating', $context)) {
63dd5fb2 1618 glossary_print_ratings_mean($entry->id, $ratings->scale);
1619 if ($USER->id != $entry->userid) {
1620 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1621 $ratingsmenuused = true;
1622 }
1623 } else if ($USER->id == $entry->userid) {
1624 glossary_print_ratings_mean($entry->id, $ratings->scale);
1625 } else if (!empty($ratings->allow) ) {
1626 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1627 $ratingsmenuused = true;
1628 }
1629 }
1630 }
1631 return $ratingsmenuused;
1632}
1633
b1918034 1634function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
a8466100 1635 global $USER,$CFG;
cca6f7f1 1636
36a2b6bd 1637 echo '<div class="boxaligncenter">';
a8466100 1638 echo '<table class="glossarypopup" cellspacing="0"><tr>';
1639 echo '<td>';
1d9ddaaf 1640 if ( $entries ) {
1641 foreach ( $entries as $entry ) {
a8466100 1642 if (! $glossary = get_record('glossary', 'id', $entry->glossaryid)) {
1643 error('Glossary ID was incorrect or no longer exists');
cca6f7f1 1644 }
a8466100 1645 if (! $course = get_record('course', 'id', $glossary->course)) {
1646 error('Glossary is misconfigured - don\'t know what course it\'s from');
1d9ddaaf 1647 }
a8466100 1648 if (!$cm = get_coursemodule_from_instance('glossary', $entry->glossaryid, $glossary->course) ) {
1649 error('Glossary is misconfigured - don\'t know what course module it is');
1d9ddaaf 1650 }
1f63b7c6 1651
1652 //If displayformat is present, override glossary->displayformat
a359c29b 1653 if ($displayformat < 0) {
1f63b7c6 1654 $dp = $glossary->displayformat;
a359c29b 1655 } else {
1f63b7c6 1656 $dp = $displayformat;
1657 }
1658
a359c29b 1659 //Get popupformatname
1660 $format = get_record('glossary_formats','name',$dp);
1661 $displayformat = $format->popupformatname;
1662
1663 //Check displayformat variable and set to default if necessary
1664 if (!$displayformat) {
1665 $displayformat = 'dictionary';
584dcac9 1666 }
1f63b7c6 1667
a359c29b 1668 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
1669 $functionname = 'glossary_show_entry_'.$displayformat;
1670
1671 if (file_exists($formatfile)) {
1672 include_once($formatfile);
1673 if (function_exists($functionname)) {
1674 $functionname($course, $cm, $glossary, $entry,'','','','');
1675 }
1676 }
cca6f7f1 1677 }
cca6f7f1 1678 }
a8466100 1679 echo '</td>';
36a2b6bd 1680 echo '</tr></table></div>';
1d9ddaaf 1681}
4f4ca7b5 1682
1ac87c73 1683function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
212039c0 1684 global $CFG;
1685
1686 $co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1687
1688 $co .= glossary_start_tag("GLOSSARY",0,true);
1689 $co .= glossary_start_tag("INFO",1,true);
1690 $co .= glossary_full_tag("NAME",2,false,$glossary->name);
1691 $co .= glossary_full_tag("INTRO",2,false,$glossary->intro);
212039c0 1692 $co .= glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries);
1693 $co .= glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat);
1694 $co .= glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial);
1695 $co .= glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet);
1696 $co .= glossary_full_tag("SHOWALL",2,false,$glossary->showall);
1697 $co .= glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments);
1698 $co .= glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink);
1699 $co .= glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval);
1700 $co .= glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary);
1701 $co .= glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage);
4f4ca7b5 1702
748b1932 1703 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
212039c0 1704 $co .= glossary_start_tag("ENTRIES",2,true);
748b1932 1705 foreach ($entries as $entry) {
046a797c 1706 $permissiongranted = 1;
1ac87c73 1707 if ( $hook ) {
1708 switch ( $hook ) {
046a797c 1709 case "ALL":
1710 case "SPECIAL":
1711 break;
1712 default:
1ac87c73 1713 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
046a797c 1714 break;
1715 }
1716 }
1ac87c73 1717 if ( $hook ) {
1718 switch ( $hook ) {
046a797c 1719 case GLOSSARY_SHOW_ALL_CATEGORIES:
1720 break;
1721 case GLOSSARY_SHOW_NOT_CATEGORISED:
1722 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1723 break;
1724 default:
1ac87c73 1725 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
046a797c 1726 break;
1727 }
1728 }
1729 if ( $entry->approved and $permissiongranted ) {
212039c0 1730 $co .= glossary_start_tag("ENTRY",3,true);
1731 $co .= glossary_full_tag("CONCEPT",4,false,trim($entry->concept));
7d8a3cb0 1732 $co .= glossary_full_tag("DEFINITION",4,false,trusttext_strip($entry->definition));
212039c0 1733 $co .= glossary_full_tag("FORMAT",4,false,$entry->format);
1734 $co .= glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink);
1735 $co .= glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive);
1736 $co .= glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch);
1737 $co .= glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry);
748b1932 1738
7965be79 1739 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
212039c0 1740 $co .= glossary_start_tag("ALIASES",4,true);
7965be79 1741 foreach ($aliases as $alias) {
212039c0 1742 $co .= glossary_start_tag("ALIAS",5,true);
1743 $co .= glossary_full_tag("NAME",6,false,trim($alias->alias));
1744 $co .= glossary_end_tag("ALIAS",5,true);
7965be79 1745 }
212039c0 1746 $co .= glossary_end_tag("ALIASES",4,true);
7965be79 1747 }
748b1932 1748 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
212039c0 1749 $co .= glossary_start_tag("CATEGORIES",4,true);
748b1932 1750 foreach ($catentries as $catentry) {
1751 $category = get_record("glossary_categories","id",$catentry->categoryid);
1752
212039c0 1753 $co .= glossary_start_tag("CATEGORY",5,true);
1754 $co .= glossary_full_tag("NAME",6,false,$category->name);
1755 $co .= glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink);
1756 $co .= glossary_end_tag("CATEGORY",5,true);
748b1932 1757 }
212039c0 1758 $co .= glossary_end_tag("CATEGORIES",4,true);
748b1932 1759 }
4f4ca7b5 1760
212039c0 1761 $co .= glossary_end_tag("ENTRY",3,true);
4f4ca7b5 1762 }
1763 }
212039c0 1764 $co .= glossary_end_tag("ENTRIES",2,true);
748b1932 1765
4f4ca7b5 1766 }
748b1932 1767
1768
212039c0 1769 $co .= glossary_end_tag("INFO",1,true);
1770 $co .= glossary_end_tag("GLOSSARY",0,true);
4f4ca7b5 1771
212039c0 1772 return $co;
4f4ca7b5 1773}
212039c0 1774/// Functions designed by Eloy Lafuente
1775/// Functions to create, open and write header of the xml file
4f4ca7b5 1776
212039c0 1777// Read import file and convert to current charset
748b1932 1778function glossary_read_imported_file($file) {
9f79b50f 1779 require_once "../../lib/xmlize.php";
1780 global $CFG;
1781
748b1932 1782 $h = fopen($file,"r");
1783 $line = '';
1784 if ($h) {
1785 while ( !feof($h) ) {
b9656030 1786 $char = fread($h,1024);
748b1932 1787 $line .= $char;
1788 }
1789 fclose($h);
894ff63f 1790 }
792a76d8 1791 return xmlize($line, 0);
748b1932 1792}
4f4ca7b5 1793
a02c77dc 1794//Return the xml start tag
4f4ca7b5 1795function glossary_start_tag($tag,$level=0,$endline=false) {
1796 if ($endline) {
1797 $endchar = "\n";
1798 } else {
1799 $endchar = "";
1800 }
1801 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1802}
a02c77dc 1803
1804//Return the xml end tag
4f4ca7b5 1805function glossary_end_tag($tag,$level=0,$endline=true) {
1806 if ($endline) {
1807 $endchar = "\n";
1808 } else {
1809 $endchar = "";
1810 }
1811 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1812}
a02c77dc 1813
4f4ca7b5 1814//Return the start tag, the contents and the end tag
212039c0 1815function glossary_full_tag($tag,$level=0,$endline=true,$content) {
9f79b50f 1816 global $CFG;
a02c77dc 1817
4f4ca7b5 1818 $st = glossary_start_tag($tag,$level,$endline);
212039c0 1819 $co = preg_replace("/\r\n|\r/", "\n", s($content));
4f4ca7b5 1820 $et = glossary_end_tag($tag,0,true);
1821 return $st.$co.$et;
1822}
1823
63dd5fb2 1824/*
a02c77dc 1825* Adding grading functions
63dd5fb2 1826*/
1827
1828function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1829/// Returns a list of ratings for a particular entry - sorted.
1830 global $CFG;
a02c77dc 1831 return get_records_sql("SELECT u.*, r.rating, r.time
1832 FROM {$CFG->prefix}glossary_ratings r,
63dd5fb2 1833 {$CFG->prefix}user u
a02c77dc 1834 WHERE r.entryid = '$entryid'
1835 AND r.userid = u.id
63dd5fb2 1836 ORDER BY $sort");
1837}
1838
63dd5fb2 1839function glossary_count_unrated_entries($glossaryid, $userid) {
1840// How many unrated entries are in the given glossary for a given user?
1841 global $CFG;
1842 if ($entries = get_record_sql("SELECT count(*) as num
1843 FROM {$CFG->prefix}glossary_entries
a02c77dc 1844 WHERE glossaryid = '$glossaryid'
63dd5fb2 1845 AND userid <> '$userid' ")) {
1846
a02c77dc 1847 if ($rated = get_record_sql("SELECT count(*) as num
1848 FROM {$CFG->prefix}glossary_entries e,
63dd5fb2 1849 {$CFG->prefix}glossary_ratings r
1850 WHERE e.glossaryid = '$glossaryid'
a02c77dc 1851 AND e.id = r.entryid
63dd5fb2 1852 AND r.userid = '$userid'")) {
1853 $difference = $entries->num - $rated->num;
1854 if ($difference > 0) {
1855 return $difference;
1856 } else {
1857 return 0; // Just in case there was a counting error
1858 }
1859 } else {
1860 return $entries->num;
1861 }
1862 } else {
1863 return 0;
1864 }
1865}
1866
1867function glossary_print_ratings_mean($entryid, $scale) {
1868/// Print the multiple ratings on a entry given to the current user by others.
1869/// Scale is an array of ratings
1870
1871 static $strrate;
1872
1873 $mean = glossary_get_ratings_mean($entryid, $scale);
a02c77dc 1874
63dd5fb2 1875 if ($mean !== "") {
1876
1877 if (empty($strratings)) {
1878 $strratings = get_string("ratings", "glossary");
1879 }
1880
91b4cb68 1881 echo "$strratings: ";
63dd5fb2 1882 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
1883 }
1884}
1885
1886
1887function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1888/// Return the mean rating of a entry given to the current user by others.
1889/// Scale is an array of possible ratings in the scale
1890/// Ratings is an optional simple array of actual ratings (just integers)
1891
1892 if (!$ratings) {
1893 $ratings = array();
1894 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1895 foreach ($rates as $rate) {
1896 $ratings[] = $rate->rating;
1897 }
1898 }
1899 }
1900
1901 $count = count($ratings);
1902
1903 if ($count == 0) {
1904 return "";
1905
1906 } else if ($count == 1) {
1907 return $scale[$ratings[0]];
1908
1909 } else {
1910 $total = 0;
1911 foreach ($ratings as $rating) {
1912 $total += $rating;
1913 }
1914 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
a02c77dc 1915
63dd5fb2 1916 if (isset($scale[$mean])) {
1917 return $scale[$mean]." ($count)";
1918 } else {
1919 return "$mean ($count)"; // Should never happen, hopefully
1920 }
1921 }
1922}
1923
1924function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1925/// Return a summary of entry ratings given to the current user by others.
1926/// Scale is an array of possible ratings in the scale
1927/// Ratings is an optional simple array of actual ratings (just integers)
1928
1929 if (!$ratings) {
1930 $ratings = array();
1931 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1932 foreach ($rates as $rate) {
1933 $rating[] = $rate->rating;
1934 }
1935 }
1936 }
1937
1938
1939 if (!$count = count($ratings)) {
1940 return "";
1941 }
1942
1943
1944 foreach ($scale as $key => $scaleitem) {
1945 $sumrating[$key] = 0;
1946 }
1947
1948 foreach ($ratings as $rating) {
1949 $sumrating[$rating]++;
1950 }
1951
1952 $summary = "";
1953 foreach ($scale as $key => $scaleitem) {
1954 $summary = $sumrating[$key].$summary;
1955 if ($key > 1) {
1956 $summary = "/$summary";
1957 }
1958 }
1959 return $summary;
1960}
1961
1962function glossary_print_rating_menu($entryid, $userid, $scale) {
a02c77dc 1963/// Print the menu of ratings as part of a larger form.
63dd5fb2 1964/// If the entry has already been - set that value.
1965/// Scale is an array of ratings
1966
1967 static $strrate;
1968
1969 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
92fac4b2 1970 $rating->rating = -999;
63dd5fb2 1971 }
1972
1973 if (empty($strrate)) {
1974 $strrate = get_string("rate", "glossary");
1975 }
1976
92fac4b2 1977 choose_from_menu($scale, $entryid, $rating->rating, "$strrate...",'',-999);
63dd5fb2 1978}
1979
e2cf5316 1980
1981function glossary_get_paging_bar($totalcount, $page, $perpage, $baseurl, $maxpageallowed=99999, $maxdisplay=20, $separator="&nbsp;", $specialtext="", $specialvalue=-1, $previousandnext = true) {
1982// Returns the html code to represent any pagging bar. Paramenters are:
1983//
1984// Mandatory:
1985// $totalcount: total number of records to be displayed
1986// $page: page currently selected (0 based)
1987// $perpage: number of records per page
1988// $baseurl: url to link in each page, the string 'page=XX' will be added automatically.
1989// Optional:
1990// $maxpageallowed: maximum number of page allowed.
1991// $maxdisplay: maximum number of page links to show in the bar
1992// $separator: string to be used between pages in the bar
1993// $specialtext: string to be showed as an special link
1994// $specialvalue: value (page) to be used in the special link
1995// $previousandnext: to decide if we want the previous and next links
1996//
1997// The function dinamically show the first and last pages, and "scroll" over pages.
1998// Fully compatible with Moodle's print_paging_bar() function. Perhaps some day this
1999// could replace the general one. ;-)
2000
2001 $code = '';
2002
2003 $showspecial = false;
2004 $specialselected = false;
2005
2006 //Check if we have to show the special link
2007 if (!empty($specialtext)) {
2008 $showspecial = true;
2009 }
2010 //Check if we are with the special link selected
2011 if ($showspecial && $page == $specialvalue) {
2012 $specialselected = true;
a02c77dc 2013 }
e2cf5316 2014
2015 //If there are results (more than 1 page)
2016 if ($totalcount > $perpage) {
36a2b6bd 2017 $code .= "<div style=\"text-align:center\">";
e2cf5316 2018 $code .= "<p>".get_string("page").":";
2019
2020 $maxpage = (int)(($totalcount-1)/$perpage);
2021
2022 //Lower and upper limit of page
2023 if ($page < 0) {
2024 $page = 0;
2025 }
2026 if ($page > $maxpageallowed) {
2027 $page = $maxpageallowed;
2028 }
2029 if ($page > $maxpage) {
2030 $page = $maxpage;
2031 }
2032
2033 //Calculate the window of pages
2034 $pagefrom = $page - ((int)($maxdisplay / 2));
2035 if ($pagefrom < 0) {
2036 $pagefrom = 0;
2037 }
2038 $pageto = $pagefrom + $maxdisplay - 1;
2039 if ($pageto > $maxpageallowed) {
2040 $pageto = $maxpageallowed;
2041 }
2042 if ($pageto > $maxpage) {
2043 $pageto = $maxpage;
2044 }
2045
2046 //Some movements can be necessary if don't see enought pages
2047 if ($pageto - $pagefrom < $maxdisplay - 1) {
2048 if ($pageto - $maxdisplay + 1 > 0) {
2049 $pagefrom = $pageto - $maxdisplay + 1;
2050 }
2051 }
2052
2053 //Calculate first and last if necessary
2054 $firstpagecode = '';
2055 $lastpagecode = '';
2056 if ($pagefrom > 0) {
2057 $firstpagecode = "$separator<a href=\"{$baseurl}page=0\">1</a>";
2058 if ($pagefrom > 1) {
2059 $firstpagecode .= "$separator...";
2060 }
2061 }
2062 if ($pageto < $maxpage) {
2063 if ($pageto < $maxpage -1) {
2064 $lastpagecode = "$separator...";
2065 }
2066 $lastpagecode .= "$separator<a href=\"{$baseurl}page=$maxpage\">".($maxpage+1)."</a>";
2067 }
2068
2069 //Previous
2070 if ($page > 0 && $previousandnext) {
2071 $pagenum = $page - 1;
2072 $code .= "&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>)&nbsp;";
2073 }
2074
2075 //Add first
2076 $code .= $firstpagecode;
2077
2078 $pagenum = $pagefrom;
2079
2080 //List of maxdisplay pages
2081 while ($pagenum <= $pageto) {
2082 $pagetoshow = $pagenum +1;
2083 if ($pagenum == $page && !$specialselected) {
2084 $code .= "$separator$pagetoshow";
2085 } else {
2086 $code .= "$separator<a href=\"{$baseurl}page=$pagenum\">$pagetoshow</a>";
2087 }
2088 $pagenum++;
2089 }
2090
2091 //Add last
2092 $code .= $lastpagecode;
2093
2094 //Next
2095 if ($page < $maxpage && $page < $maxpageallowed && $previousandnext) {
2096 $pagenum = $page + 1;
2097 $code .= "$separator(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
2098 }
2099
2100 //Add special
2101 if ($showspecial) {
2102 $code .= '<br />';
2103 if ($specialselected) {
2104 $code .= $specialtext;
2105 } else {
2106 $code .= "$separator<a href=\"{$baseurl}page=$specialvalue\">$specialtext</a>";
2107 }
2108 }
2109
2110 //End html
2111 $code .= "</p>";
36a2b6bd 2112 $code .= "</div>";
e2cf5316 2113 }
2114
2115 return $code;
2116}
2117
f3221af9 2118function glossary_get_view_actions() {
2119 return array('view','view all','view entry');
2120}
2121
2122function glossary_get_post_actions() {
2123 return array('add category','add comment','add entry','approve entry','delete category','delete comment','delete entry','edit category','update comment','update entry');
2124}
2125
cc41c129 2126?>