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