MDL-1883 improved perf when editing scales - new function xxx_scale_used_anywhere()
[moodle-pu.git] / mod / glossary / lib.php
blob395b67944c0a7c518ea78ed12e1ebe7b39a564b0
1 <?php // $Id$
3 /// Library of functions and constants for module glossary
4 /// (replace glossary with the name of your module and delete this line)
6 require_once($CFG->libdir.'/filelib.php');
8 define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
9 define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
11 define("GLOSSARY_NO_VIEW", -1);
12 define("GLOSSARY_STANDARD_VIEW", 0);
13 define("GLOSSARY_CATEGORY_VIEW", 1);
14 define("GLOSSARY_DATE_VIEW", 2);
15 define("GLOSSARY_AUTHOR_VIEW", 3);
16 define("GLOSSARY_ADDENTRY_VIEW", 4);
17 define("GLOSSARY_IMPORT_VIEW", 5);
18 define("GLOSSARY_EXPORT_VIEW", 6);
19 define("GLOSSARY_APPROVAL_VIEW", 7);
21 //Check for global configure default variables
23 if (!isset($CFG->glossary_studentspost)) {
24 set_config("glossary_studentspost", 1); // Students can post entries.
27 if (!isset($CFG->glossary_dupentries)) {
28 set_config("glossary_dupentries", 0); // Duplicate entries are not allowed.
31 if (!isset($CFG->glossary_allowcomments)) {
32 set_config("glossary_allowcomments", 0); // Comments are not allowed.
35 if (!isset($CFG->glossary_linkbydefault)) {
36 set_config("glossary_linkbydefault", 1); // Linking entries is enabled.
39 if (!isset($CFG->glossary_defaultapproval)) {
40 set_config("glossary_defaultapproval", 1); // Entries are approved.
43 if (!isset($CFG->glossary_entbypage)) {
44 set_config("glossary_entbypage", 10); // 10 entries are showed.
47 /// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
49 function 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.
55 if (empty($glossary->userating)) {
56 $glossary->assessed = 0;
59 if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
60 $glossary->assesstimestart = 0;
61 $glossary->assesstimefinish = 0;
64 if (empty($glossary->globalglossary) ) {
65 $glossary->globalglossary = 0;
68 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
69 $glossary->globalglossary = 0;
72 $glossary->timecreated = time();
73 $glossary->timemodified = $glossary->timecreated;
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!");
81 if ($returnid = insert_record("glossary", $glossary)) {
82 $glossary->id = $returnid;
83 $glossary = stripslashes_recursive($glossary);
84 glossary_grade_item_update($glossary);
87 return $returnid;
91 function 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.
95 global $CFG;
97 if (empty($glossary->globalglossary)) {
98 $glossary->globalglossary = 0;
101 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
102 // keep previous
103 unset($glossary->globalglossary);
106 $glossary->timemodified = time();
107 $glossary->id = $glossary->instance;
109 if (empty($glossary->userating)) {
110 $glossary->assessed = 0;
113 if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
114 $glossary->assesstimestart = 0;
115 $glossary->assesstimefinish = 0;
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!");
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);
128 $glossary = stripslashes_recursive($glossary);
129 glossary_grade_item_update($glossary);
132 return $return;
136 function 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.
141 if (! $glossary = get_record("glossary", "id", "$id")) {
142 return false;
145 $result = true;
147 # Delete any dependent records here #
149 if (! delete_records("glossary", "id", "$glossary->id")) {
150 $result = false;
151 } else {
152 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
153 $cats = "";
154 foreach ( $categories as $cat ) {
155 $cats .= "$cat->id,";
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);
163 if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
164 $ents = "";
165 foreach ( $entries as $entry ) {
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,";
174 $ents = substr($ents,0,-1);
175 if ($ents) {
176 delete_records_select("glossary_comments", "entryid in ($ents)");
177 delete_records_select("glossary_alias", "entryid in ($ents)");
178 delete_records_select("glossary_ratings", "entryid in ($ents)");
181 glossary_delete_attachments($glossary);
182 delete_records("glossary_entries", "glossaryid", "$glossary->id");
184 glossary_grade_item_delete($glossary);
186 return $result;
189 function 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
196 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
197 $result = new object();
198 $result->info = count($entries) . ' ' . get_string("entries", "glossary");
200 $lastentry = array_pop($entries);
201 $result->time = $lastentry->timemodified;
202 return $result;
204 return NULL;
207 function glossary_get_user_entries($glossaryid, $userid) {
208 /// Get all the entries for a user in a glossary
209 global $CFG;
211 return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
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'
218 AND e.userid = u.id
219 ORDER BY e.timemodified ASC");
222 function 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.
225 global $CFG;
227 if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
228 echo '<table width="95%" border="0"><tr><td>';
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>';
234 echo '</td></tr></table>';
238 function 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.
243 global $CFG;
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')) {
250 return false;
253 $entries = array();
255 foreach ($logs as $log) {
256 //Create a temp valid module structure (course,id)
257 $tempmod = new object();
258 $tempmod->course = $log->course;
259 $entry = get_record('glossary_entries','id',$log->info);
260 if (!$entry) {
261 continue;
263 $tempmod->id = $entry->glossaryid;
264 //Obtain the visible property from the instance
265 $modvisible = instance_is_visible($log->module,$tempmod);
267 //Only if the mod is visible
268 if ($modvisible and $entry->approved) {
269 $entries[$log->info] = glossary_log_info($log);
270 $entries[$log->info]->time = $log->time;
271 $entries[$log->info]->url = str_replace('&', '&amp;', $log->url);
275 $content = false;
276 if ($entries) {
277 $content = true;
278 print_headline(get_string('newentries', 'glossary').':');
279 foreach ($entries as $entry) {
280 $user = get_record('user','id',$entry->userid, '','', '','', 'firstname,lastname');
282 print_recent_activity_note($entry->timemodified, $user, $entry->concept,
283 $CFG->wwwroot.'/mod/glossary/view.php?g='.$entry->glossaryid.
284 '&amp;mode=entry&amp;hook='.$entry->id);
288 return $content;
292 function glossary_log_info($log) {
293 global $CFG;
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'");
302 function 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 ...
307 global $CFG;
309 return true;
313 * Return grade for given user or all users.
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
319 function glossary_get_user_grades($glossary, $userid=0) {
320 global $CFG;
322 $user = $userid ? "AND u.id = $userid" : "";
324 $sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS rawgrade
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
328 AND gr.userid != u.id AND ge.glossaryid = $glossary->id
329 $user
330 GROUP BY u.id";
332 return get_records_sql($sql);
336 * Update grades by firing grade_updated event
338 * @param object $glossary null means all glossaries
339 * @param int $userid specific user only, 0 mean all
341 function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
342 global $CFG;
343 require_once($CFG->libdir.'/gradelib.php');
345 if ($glossary != null) {
346 if ($grades = glossary_get_user_grades($glossary, $userid)) {
347 grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades);
349 } else if ($userid and $nullifnone) {
350 $grade = new object();
351 $grade->userid = $userid;
352 $grade->rawgrade = NULL;
353 grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade);
356 } else {
357 $sql = "SELECT g.*, cm.idnumber as cmidnumber
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";
360 if ($rs = get_recordset_sql($sql)) {
361 if ($rs->RecordCount() > 0) {
362 while ($glossary = rs_fetch_next_record($rs)) {
363 glossary_grade_item_update($glossary);
364 if ($glossary->assessed) {
365 glossary_update_grades($glossary, 0, false);
369 rs_close($rs);
375 * Create/update grade item for given glossary
377 * @param object $glossary object with extra cmidnumber
378 * @return int, 0 if ok, error code otherwise
380 function glossary_grade_item_update($glossary) {
381 global $CFG;
382 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
383 require_once($CFG->libdir.'/gradelib.php');
386 $params = array('itemname'=>$glossary->name, 'idnumber'=>$glossary->cmidnumber);
388 if (!$glossary->assessed or $glossary->scale == 0) {
389 $params['gradetype'] = GRADE_TYPE_NONE;
391 } else if ($glossary->scale > 0) {
392 $params['gradetype'] = GRADE_TYPE_VALUE;
393 $params['grademax'] = $glossary->scale;
394 $params['grademin'] = 0;
396 } else if ($glossary->scale < 0) {
397 $params['gradetype'] = GRADE_TYPE_SCALE;
398 $params['scaleid'] = -$glossary->scale;
401 return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params);
405 * Delete grade item for given glossary
407 * @param object $glossary object
409 function glossary_grade_item_delete($glossary) {
410 global $CFG;
411 require_once($CFG->libdir.'/gradelib.php');
413 return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1));
416 function glossary_get_participants($glossaryid) {
417 //Returns the users with data in one glossary
418 //(users with records in glossary_entries, students)
420 global $CFG;
422 //Get students
423 $students = get_records_sql("SELECT DISTINCT u.id, u.id
424 FROM {$CFG->prefix}user u,
425 {$CFG->prefix}glossary_entries g
426 WHERE g.glossaryid = '$glossaryid' and
427 u.id = g.userid");
429 //Return students array (it contains an array of unique users)
430 return ($students);
433 function glossary_scale_used ($glossaryid,$scaleid) {
434 //This function returns if a scale is being used by one glossary
436 $return = false;
438 $rec = get_record("glossary","id","$glossaryid","scale","-$scaleid");
440 if (!empty($rec) && !empty($scaleid)) {
441 $return = true;
444 return $return;
448 * Checks if scale is being used by any instance of glossary
450 * This is used to find out if scale used anywhere
451 * @param $scaleid int
452 * @return boolean True if the scale is used by any glossary
454 function glossary_scale_used_anywhere($scaleid) {
455 if ($scaleid and record_exists('glossary', 'scale', -$scaleid)) {
456 return true;
457 } else {
458 return false;
462 //////////////////////////////////////////////////////////////////////////////////////
463 /// Any other glossary functions go here. Each of them must have a name that
464 /// starts with glossary_
466 //This function return an array of valid glossary_formats records
467 //Everytime it's called, every existing format is checked, new formats
468 //are included if detected and old formats are deleted and any glossary
469 //using an invalid format is updated to the default (dictionary).
470 function glossary_get_available_formats() {
472 global $CFG;
474 //Get available formats (plugin) and insert (if necessary) them into glossary_formats
475 $formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
476 $pluginformats = array();
477 foreach ($formats as $format) {
478 //If the format file exists
479 if (file_exists($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
480 include_once($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
481 //If the function exists
482 if (function_exists('glossary_show_entry_'.$format)) {
483 //Acummulate it as a valid format
484 $pluginformats[] = $format;
485 //If the format doesn't exist in the table
486 if (!$rec = get_record('glossary_formats','name',$format)) {
487 //Insert the record in glossary_formats
488 $gf = new object();
489 $gf->name = $format;
490 $gf->popupformatname = $format;
491 $gf->visible = 1;
492 insert_record("glossary_formats",$gf);
498 //Delete non_existent formats from glossary_formats table
499 $formats = get_records("glossary_formats");
500 foreach ($formats as $format) {
501 $todelete = false;
502 //If the format in DB isn't a valid previously detected format then delete the record
503 if (!in_array($format->name,$pluginformats)) {
504 $todelete = true;
507 if ($todelete) {
508 //Delete the format
509 delete_records('glossary_formats','name',$format->name);
510 //Reasign existing glossaries to default (dictionary) format
511 if ($glossaries = get_records('glossary','displayformat',$format->name)) {
512 foreach($glossaries as $glossary) {
513 set_field('glossary','displayformat','dictionary','id',$glossary->id);
519 //Now everything is ready in glossary_formats table
520 $formats = get_records("glossary_formats");
522 return $formats;
525 function glossary_debug($debug,$text,$br=1) {
526 if ( $debug ) {
527 echo '<font color="red">' . $text . '</font>';
528 if ( $br ) {
529 echo '<br />';
534 function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
535 global $CFG;
536 if ($pivot) {
537 $pivot .= ",";
540 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
541 FROM {$CFG->prefix}glossary_entries
542 WHERE glossaryid = '$glossaryid'
543 AND id IN ($entrylist)");
546 function glossary_get_entries_search($concept, $courseid) {
548 global $CFG;
550 //Check if the user is an admin
551 $bypassadmin = 1; //This means NO (by default)
552 if (has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
553 $bypassadmin = 0; //This means YES
556 //Check if the user is a teacher
557 $bypassteacher = 1; //This means NO (by default)
558 if (has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE, $courseid))) {
559 $bypassteacher = 0; //This means YES
562 $conceptlower = moodle_strtolower(trim($concept));
564 return get_records_sql("SELECT e.*, g.name as glossaryname, cm.id as cmid, cm.course as courseid
565 FROM {$CFG->prefix}glossary_entries e,
566 {$CFG->prefix}glossary g,
567 {$CFG->prefix}course_modules cm,
568 {$CFG->prefix}modules m
569 WHERE m.name = 'glossary' AND
570 cm.module = m.id AND
571 (cm.visible = 1 OR cm.visible = $bypassadmin OR
572 (cm.course = '$courseid' AND cm.visible = $bypassteacher)) AND
573 g.id = cm.instance AND
574 e.glossaryid = g.id AND
575 ( (e.casesensitive != 0 AND LOWER(concept) = '$conceptlower') OR
576 (e.casesensitive = 0 and concept = '$concept')) AND
577 (g.course = '$courseid' OR g.globalglossary = 1) AND
578 e.usedynalink != 0 AND
579 g.usedynalink != 0");
582 function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
583 global $CFG;
584 if ($where) {
585 $where = " and $where";
587 if ($orderby) {
588 $orderby = " ORDER BY $orderby";
590 if ($pivot) {
591 $pivot .= ",";
593 return get_records_sql("SELECT $pivot *
594 FROM {$CFG->prefix}glossary_entries
595 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
598 function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
599 global $CFG;
600 if ($where) {
601 $where = " and $where";
603 if ($orderby) {
604 $orderby = " ORDER BY $orderby";
606 if ($pivot) {
607 $pivot .= ",";
609 return get_records_sql("SELECT $pivot ge.*
610 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
611 WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
612 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
615 function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) {
616 global $USER, $CFG;
617 $return = false;
618 if ( $displayformat < 0 ) {
619 $displayformat = $glossary->displayformat;
621 if ($entry->approved or ($USER->id == $entry->userid) or ($mode == 'approval' and !$entry->approved) ) {
622 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
623 if ($printview) {
624 $functionname = 'glossary_print_entry_'.$displayformat;
625 } else {
626 $functionname = 'glossary_show_entry_'.$displayformat;
629 if (file_exists($formatfile)) {
630 include_once($formatfile);
631 if (function_exists($functionname)) {
632 $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
633 } else if ($printview) {
634 //If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format
635 $return = glossary_print_entry_default($entry);
639 return $return;
642 //Default (old) print format used if custom function doesn't exist in format
643 function glossary_print_entry_default ($entry) {
644 echo '<b>'. strip_tags($entry->concept) . ': </b>';
646 $definition = $entry->definition;
648 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
649 if (trusttext_present($definition)) {
650 $ttpresent = true;
651 $definition = trusttext_strip($definition);
652 } else {
653 $ttpresent = false;
656 $definition = '<span class="nolink">' . strip_tags($definition) . '</span>';
658 // reconstruct the TRUSTTEXT properly after processing
659 if ($ttpresent) {
660 $definition = trusttext_mark($definition);
661 } else {
662 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
665 $options = new object();
666 $options->para = false;
667 $options->trusttext = true;
668 $definition = format_text($definition, $entry->format, $options);
669 echo ($definition);
670 echo '<br /><br />';
673 function glossary_print_entry_concept($entry) {
674 $options = new object();
675 $options->para = false;
676 $text = format_text('<span class="nolink">' . $entry->concept . '</span>', FORMAT_MOODLE, $options);
677 if (!empty($entry->highlight)) {
678 $text = highlight($entry->highlight, $text);
680 echo $text;
683 function glossary_print_entry_definition($entry) {
685 $definition = $entry->definition;
687 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
688 if (trusttext_present($definition)) {
689 $ttpresent = true;
690 $definition = trusttext_strip($definition);
691 } else {
692 $ttpresent = false;
695 $links = array();
696 $tags = array();
697 $urls = array();
698 $addrs = array();
700 //Calculate all the strings to be no-linked
701 //First, the concept
702 $term = preg_quote(trim($entry->concept),'/');
703 $pat = '/('.$term.')/is';
704 $doNolinks[] = $pat;
705 //Now the aliases
706 if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
707 foreach ($aliases as $alias) {
708 $term = preg_quote(trim($alias->alias),'/');
709 $pat = '/('.$term.')/is';
710 $doNolinks[] = $pat;
715 //Extract <a>..><a> tags from definition
716 preg_match_all('/<a[^>]+?>(.*?)<\/a>/is',$definition,$list_of_a);
718 //Save them into links array to use them later
719 foreach (array_unique($list_of_a[0]) as $key=>$value) {
720 $links['<#'.$key.'#>'] = $value;
722 //Take off every link from definition
723 if ( $links ) {
724 $definition = str_replace($links,array_keys($links),$definition);
728 //Extract all tags from definition
729 preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
731 //Save them into tags array to use them later
732 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
733 $tags['<@'.$key.'@>'] = $value;
735 //Take off every tag from definition
736 if ( $tags ) {
737 $definition = str_replace($tags,array_keys($tags),$definition);
741 //Extract all URLS with protocol (http://domain.com) from definition
742 preg_match_all('/([[:space:]]|^|\(|\[)([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_urls);
744 //Save them into urls array to use them later
745 foreach (array_unique($list_of_urls[0]) as $key=>$value) {
746 $urls['<*'.$key.'*>'] = $value;
748 //Take off every url from definition
749 if ( $urls ) {
750 $definition = str_replace($urls,array_keys($urls),$definition);
754 //Extract all WEB ADDRESSES (www.domain.com) from definition
755 preg_match_all('/([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_addresses);
757 //Save them into addrs array to use them later
758 foreach (array_unique($list_of_addresses[0]) as $key=>$value) {
759 $addrs['<+'.$key.'+>'] = $value;
761 //Take off every addr from definition
762 if ( $addrs ) {
763 $definition = str_replace($addrs,array_keys($addrs),$definition);
767 //Put doNolinks (concept + aliases) enclosed by <nolink> tag
768 $definition= preg_replace($doNolinks,'<span class="nolink">$1</span>',$definition);
770 //Restore addrs
771 if ( $addrs ) {
772 $definition = str_replace(array_keys($addrs),$addrs,$definition);
775 //Restore urls
776 if ( $urls ) {
777 $definition = str_replace(array_keys($urls),$urls,$definition);
780 //Restore tags
781 if ( $tags ) {
782 $definition = str_replace(array_keys($tags),$tags,$definition);
785 //Restore links
786 if ( $links ) {
787 $definition = str_replace(array_keys($links),$links,$definition);
790 $options = new object();
791 $options->para = false;
792 $options->trusttext = true;
794 // reconstruct the TRUSTTEXT properly after processing
795 if ($ttpresent) {
796 $definition = trusttext_mark($definition);
797 } else {
798 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
801 $text = format_text($definition, $entry->format, $options);
802 if (!empty($entry->highlight)) {
803 $text = highlight($entry->highlight, $text);
805 if (isset($entry->footer)) { // Unparsed footer info
806 $text .= $entry->footer;
808 echo $text;
811 function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode='',$hook='', $type = 'print') {
812 $return = '';
813 if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
814 foreach ($aliases as $alias) {
815 if (trim($alias->alias)) {
816 if ($return == '') {
817 $return = '<select style="font-size:8pt">';
819 $return .= "<option>$alias->alias</option>";
822 if ($return != '') {
823 $return .= '</select>';
826 if ($type == 'print') {
827 echo $return;
828 } else {
829 return $return;
833 function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$hook='', $type = 'print') {
834 global $USER, $CFG;
836 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
838 $output = false; //To decide if we must really return text in "return". Activate when needed only!
839 $importedentry = ($entry->sourceglossaryid == $glossary->id);
840 $ismainglossary = $glossary->mainglossary;
843 $return = '<span class="commands">';
844 if (!$entry->approved) {
845 $output = true;
846 $return .= get_string('entryishidden','glossary');
848 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
850 if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments) {
851 $output = true;
852 $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').'" /></a>';
856 if (has_capability('mod/glossary:manageentries', $context) or (!empty($USER->id) and has_capability('mod/glossary:write', $context) and $entry->userid == $USER->id)) {
857 // only teachers can export entries so check it out
858 if (has_capability('mod/glossary:export', $context) and !$ismainglossary and !$importedentry) {
859 $mainglossary = get_record('glossary','mainglossary',1,'course',$course->id);
860 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
861 $output = true;
862 $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').'" /></a>';
866 if ( $entry->sourceglossaryid ) {
867 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
868 } else {
869 $icon = "$CFG->pixpath/t/delete.gif";
872 //Decide if an entry is editable:
873 // -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
874 // -The user is teacher or he is a student with time permissions (edit period or editalways defined).
875 $ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
876 if ( !$importedentry and (has_capability('mod/glossary:manageentries', $context) or ($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context))))) {
877 $output = true;
878 $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=\"";
879 $return .= $icon;
880 $return .= "\" class=\"iconsmall\" alt=\"" . get_string("delete") . "\" /></a> ";
882 $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") . "\" /></a>";
883 } elseif ( $importedentry ) {
884 $return .= " <font size=\"-1\">" . get_string("exportedentry","glossary") . "</font>";
887 $return .= "&nbsp;&nbsp;"; // just to make up a little the output in Mozilla ;)
889 $return .= '</span>';
891 //If we haven't calculated any REAL thing, delete result ($return)
892 if (!$output) {
893 $return = '';
895 //Print or get
896 if ($type == 'print') {
897 echo $return;
898 } else {
899 return $return;
903 function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
904 $return = '';
906 $count = count_records('glossary_comments','entryid',$entry->id);
907 if ($count) {
908 $return = '';
909 $return .= "<a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$count ";
910 if ($count == 1) {
911 $return .= get_string('comment', 'glossary');
912 } else {
913 $return .= get_string('comments', 'glossary');
915 $return .= '</a>';
918 if ($type == 'print') {
919 echo $return;
920 } else {
921 return $return;
925 function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
927 if ($aliases) {
928 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,'html');
930 $icons = '';
931 $return = '';
932 if ( $printicons ) {
933 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,'html');
935 if ($aliases || $icons || $ratings) {
936 echo '<table>';
937 if ( $aliases ) {
938 echo '<tr valign="top"><td class="aliases">' .
939 get_string('aliases','glossary').': '.$aliases . '</td></tr>';
941 if ($icons) {
942 echo '<tr valign="top"><td class="icons">'.$icons.'</td></tr>';
944 if ($ratings) {
945 echo '<tr valign="top"><td class="ratings">';
946 $return = glossary_print_entry_ratings($course, $entry, $ratings);
947 echo '</td></tr>';
949 echo '</table>';
951 return $return;
954 function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
955 /// valid format values: html : Return the HTML link for the attachment as an icon
956 /// text : Return the HTML link for tha attachment as text
957 /// blank : Print the output to the screen
958 if ($entry->attachment) {
959 $glossary = get_record("glossary","id",$entry->glossaryid);
960 $entry->course = $glossary->course; //used inside print_attachment
961 if ($insidetable) {
962 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap=\"nowrap\">\n";
964 echo glossary_print_attachments($entry,$format,$align);
965 if ($insidetable) {
966 echo "</td></tr></table>\n";
971 function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
972 if ( $mode == 'approval' and !$entry->approved ) {
973 if ($insidetable) {
974 echo '<table class="glossaryapproval" align="'.$align.'"><tr><td align="'.$align.'">';
976 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>';
977 if ($insidetable) {
978 echo '</td></tr></table>';
983 function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {
984 // It returns all entries from all glossaries that matches the specified criteria
985 // within a given $course. It performs an $extended search if necessary.
986 // It restrict the search to only one $glossary if the $glossary parameter is set.
988 global $CFG;
989 if ( !$glossary ) {
990 if ( $glossaries = get_records("glossary", "course", $course->id) ) {
991 $glos = "";
992 foreach ( $glossaries as $glossary ) {
993 $glos .= "$glossary->id,";
995 $glos = substr($glos,0,-1);
997 } else {
998 $glos = $glossary->id;
1001 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE, $glossary->course))) {
1002 $glossarymodule = get_record("modules", "name", "glossary");
1003 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
1004 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
1005 } else {
1007 $onlyvisible = "";
1008 $onlyvisibletable = "";
1011 /// Some differences in syntax for entrygreSQL
1012 switch ($CFG->dbfamily) {
1013 case 'postgres':
1014 $LIKE = "ILIKE"; // case-insensitive
1015 $NOTLIKE = "NOT ILIKE"; // case-insensitive
1016 $REGEXP = "~*";
1017 $NOTREGEXP = "!~*";
1018 break;
1019 case 'mysql':
1020 default:
1021 $LIKE = "LIKE";
1022 $NOTLIKE = "NOT LIKE";
1023 $REGEXP = "REGEXP";
1024 $NOTREGEXP = "NOT REGEXP";
1025 break;
1028 $conceptsearch = "";
1029 $definitionsearch = "";
1032 foreach ($searchterms as $searchterm) {
1033 if ($conceptsearch) {
1034 $conceptsearch.= " OR ";
1036 if ($definitionsearch) {
1037 $definitionsearch.= " OR ";
1040 /// Under Oracle and MSSQL, trim the + and - operators and perform
1041 /// simpler LIKE search
1042 if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
1043 $searchterm = trim($searchterm, '+-');
1046 if (substr($searchterm,0,1) == "+") {
1047 $searchterm = substr($searchterm,1);
1048 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1049 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1050 } else if (substr($searchterm,0,1) == "-") {
1051 $searchterm = substr($searchterm,1);
1052 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1053 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1054 } else {
1055 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
1056 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
1060 if ( !$extended ) {
1061 $definitionsearch = "0";
1064 $selectsql = "{$CFG->prefix}glossary_entries e,
1065 {$CFG->prefix}glossary g $onlyvisibletable
1066 WHERE ($conceptsearch OR $definitionsearch)
1067 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
1068 AND g.id IN ($glos) AND e.approved != 0";
1070 return get_records_sql("SELECT e.*
1071 FROM $selectsql ORDER BY e.concept ASC");
1074 function glossary_search_entries($searchterms, $glossary, $extended) {
1075 $course = get_record("course","id",$glossary->course);
1076 return glossary_search($course,$searchterms,$extended,$glossary);
1079 function glossary_file_area_name($entry) {
1080 global $CFG;
1081 // Creates a directory file name, suitable for make_upload_directory()
1083 // I'm doing this workaround for make it works for delete_instance also
1084 // (when called from delete_instance, glossary is already deleted so
1085 // getting the course from mdl_glossary does not work)
1086 $module = get_record("modules","name","glossary");
1087 $cm = get_record("course_modules","module",$module->id,"instance",$entry->glossaryid);
1088 return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
1091 function glossary_file_area($entry) {
1092 return make_upload_directory( glossary_file_area_name($entry) );
1095 function glossary_main_file_area($glossary) {
1096 $modarea = glossary_mod_file_area($glossary);
1097 return "$modarea/$glossary->id";
1100 function glossary_mod_file_area($glossary) {
1101 global $CFG;
1103 return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
1106 function glossary_delete_old_attachments($entry, $exception="") {
1107 // Deletes all the user files in the attachments area for a entry
1108 // EXCEPT for any file named $exception
1110 if ($basedir = glossary_file_area($entry)) {
1111 if ($files = get_directory_list($basedir)) {
1112 foreach ($files as $file) {
1113 if ($file != $exception) {
1114 unlink("$basedir/$file");
1115 // notify("Existing file '$file' has been deleted!");
1119 if (!$exception) { // Delete directory as well, if empty
1120 rmdir("$basedir");
1124 function glossary_delete_attachments($glossary) {
1125 // Deletes all the user files in the attachments area for the glossary
1126 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1127 $deleted = 0;
1128 foreach ($entries as $entry) {
1129 if ( $entry->attachment ) {
1130 if ($basedir = glossary_file_area($entry)) {
1131 if ($files = get_directory_list($basedir)) {
1132 foreach ($files as $file) {
1133 unlink("$basedir/$file");
1136 rmdir("$basedir");
1137 $deleted++;
1141 if ( $deleted ) {
1142 $attachmentdir = glossary_main_file_area($glossary);
1143 $glossarydir = glossary_mod_file_area($glossary);
1145 rmdir("$attachmentdir");
1146 if (!$files = get_directory_list($glossarydir) ) {
1147 rmdir( "$glossarydir" );
1153 function glossary_copy_attachments($entry, $newentry) {
1154 /// Given a entry object that is being copied to glossaryid,
1155 /// this function checks that entry
1156 /// for attachments, and if any are found, these are
1157 /// copied to the new glossary directory.
1159 global $CFG;
1161 $return = true;
1163 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
1164 foreach ($entries as $curentry) {
1165 $oldentry = new object();
1166 $oldentry->id = $entry->id;
1167 $oldentry->course = $entry->course;
1168 $oldentry->glossaryid = $curentry->glossaryid;
1169 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1170 if (is_dir($oldentrydir)) {
1172 $newentrydir = glossary_file_area($newentry);
1173 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
1174 $return = false;
1179 return $return;
1182 function glossary_move_attachments($entry, $glossaryid) {
1183 /// Given a entry object that is being moved to glossaryid,
1184 /// this function checks that entry
1185 /// for attachments, and if any are found, these are
1186 /// moved to the new glossary directory.
1188 global $CFG;
1190 require_once($CFG->dirroot.'/lib/uploadlib.php');
1192 $return = true;
1194 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
1195 foreach ($entries as $entry) {
1196 $oldentry = new object();
1197 $oldentry->course = $entry->course;
1198 $oldentry->glossaryid = $entry->glossaryid;
1199 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1200 if (is_dir($oldentrydir)) {
1201 $newentry = $oldentry;
1202 $newentry->glossaryid = $glossaryid;
1203 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
1204 $files = get_directory_list($oldentrydir); // get it before we rename it.
1205 if (! @rename($oldentrydir, $newentrydir)) {
1206 $return = false;
1208 foreach ($files as $file) {
1209 // this is not tested as I can't find anywhere that calls this function, grepping the source.
1210 clam_change_log($oldentrydir.'/'.$file,$newentrydir.'/'.$file);
1215 return $return;
1218 function glossary_print_attachments($entry, $return=NULL, $align="left") {
1219 // if return=html, then return a html string.
1220 // if return=text, then return a text-only string.
1221 // otherwise, print HTML for non-images, and return image HTML
1222 // if attachment is an image, $align set its aligment.
1223 global $CFG;
1225 $newentry = $entry;
1226 if ( $newentry->sourceglossaryid ) {
1227 $newentry->glossaryid = $newentry->sourceglossaryid;
1230 $filearea = glossary_file_area_name($newentry);
1232 $imagereturn = "";
1233 $output = "";
1235 if ($basedir = glossary_file_area($newentry)) {
1236 if ($files = get_directory_list($basedir)) {
1237 $strattachment = get_string("attachment", "glossary");
1238 foreach ($files as $file) {
1239 $icon = mimeinfo("icon", $file);
1240 if ($CFG->slasharguments) {
1241 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
1242 } else {
1243 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
1245 $image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
1247 if ($return == "html") {
1248 $output .= "<a href=\"$ffurl\">$image</a> ";
1249 $output .= "<a href=\"$ffurl\">$file</a><br />";
1251 } else if ($return == "text") {
1252 $output .= "$strattachment $file:\n$ffurl\n";
1254 } else {
1255 if ($icon == "image.gif") { // Image attachments don't get printed as links
1256 $imagereturn .= "<img src=\"$ffurl\" align=\"$align\" alt=\"\" />";
1257 } else {
1258 echo "<a href=\"$ffurl\">$image</a> ";
1259 echo "<a href=\"$ffurl\">$file</a><br />";
1266 if ($return) {
1267 return $output;
1270 return $imagereturn;
1273 function glossary_print_tabbed_table_end() {
1274 echo "</div></div>";
1277 function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1278 if ($glossary->showalphabet) {
1279 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1281 glossary_print_special_links($cm, $glossary, $mode, $hook);
1283 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
1285 glossary_print_all_links($cm, $glossary, $mode, $hook);
1287 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
1290 function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1291 echo '<div class="glossaryexplain">' . get_string("explainimport","glossary") . '</div>';
1294 function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1295 echo '<div class="glossaryexplain">' . get_string("explainexport","glossary") . '</div>';
1298 function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1299 if ( $mode != 'date' ) {
1300 if ($glossary->showalphabet) {
1301 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1304 glossary_print_special_links($cm, $glossary, $mode, $hook);
1306 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1308 glossary_print_all_links($cm, $glossary, $mode, $hook);
1309 } else {
1310 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1314 function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1315 if ($glossary->showalphabet) {
1316 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1319 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1320 glossary_print_all_links($cm, $glossary, $mode, $hook);
1321 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1324 function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
1326 global $CFG;
1328 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1330 echo '<table border="0" width="100%">';
1331 echo '<tr>';
1333 echo '<td align="center" style="width:20%">';
1334 if (has_capability('mod/glossary:managecategories', $context)) {
1335 $options['id'] = $cm->id;
1336 $options['mode'] = 'cat';
1337 $options['hook'] = $hook;
1338 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1340 echo '</td>';
1342 echo '<td align="center" style="width:60%">';
1343 echo '<b>';
1345 $menu[GLOSSARY_SHOW_ALL_CATEGORIES] = get_string("allcategories","glossary");
1346 $menu[GLOSSARY_SHOW_NOT_CATEGORISED] = get_string("notcategorised","glossary");
1348 $categories = get_records("glossary_categories", "glossaryid", $glossary->id, "name ASC");
1349 $selected = '';
1350 if ( $categories ) {
1351 foreach ($categories as $currentcategory) {
1352 $url = $currentcategory->id;
1353 if ( $category ) {
1354 if ($currentcategory->id == $category->id) {
1355 $selected = $url;
1358 $menu[$url] = clean_text($currentcategory->name); //Only clean, not filters
1361 if ( !$selected ) {
1362 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1365 if ( $category ) {
1366 echo format_text($category->name, FORMAT_PLAIN);
1367 } else {
1368 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
1370 echo get_string("entrieswithoutcategory","glossary");
1371 $selected = GLOSSARY_SHOW_NOT_CATEGORISED;
1373 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
1375 echo get_string("allcategories","glossary");
1376 $selected = GLOSSARY_SHOW_ALL_CATEGORIES;
1380 echo '</b></td>';
1381 echo '<td align="center" style="width:20%">';
1383 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=cat&amp;hook=", $menu, "catmenu", $selected, "",
1384 "", "", false);
1386 echo '</td>';
1387 echo '</tr>';
1389 echo '</table>';
1392 function glossary_print_all_links($cm, $glossary, $mode, $hook) {
1393 global $CFG;
1394 if ( $glossary->showall) {
1395 $strallentries = get_string("allentries", "glossary");
1396 if ( $hook == 'ALL' ) {
1397 echo "<b>$strallentries</b>";
1398 } else {
1399 $strexplainall = strip_tags(get_string("explainall","glossary"));
1400 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=ALL\">$strallentries</a>";
1405 function glossary_print_special_links($cm, $glossary, $mode, $hook) {
1406 global $CFG;
1407 if ( $glossary->showspecial) {
1408 $strspecial = get_string("special", "glossary");
1409 if ( $hook == 'SPECIAL' ) {
1410 echo "<b>$strspecial</b> | ";
1411 } else {
1412 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1413 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&amp;mode=$mode&amp;hook=SPECIAL\">$strspecial</a> | ";
1418 function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
1419 global $CFG;
1420 if ( $glossary->showalphabet) {
1421 $alphabet = explode(",", get_string("alphabet"));
1422 $letters_by_line = 14;
1423 for ($i = 0; $i < count($alphabet); $i++) {
1424 if ( $hook == $alphabet[$i] and $hook) {
1425 echo "<b>$alphabet[$i]</b>";
1426 } else {
1427 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>";
1429 if ((int) ($i % $letters_by_line) != 0 or $i == 0) {
1430 echo ' | ';
1431 } else {
1432 echo '<br />';
1438 function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
1439 global $CFG;
1441 $asc = get_string("ascending","glossary");
1442 $desc = get_string("descending","glossary");
1443 $bopen = '<b>';
1444 $bclose = '</b>';
1446 $neworder = '';
1447 if ( $sortorder ) {
1448 if ( $sortorder == 'asc' ) {
1449 $neworder = '&amp;sortorder=desc';
1450 $newordertitle = $desc;
1451 } else {
1452 $neworder = '&amp;sortorder=asc';
1453 $newordertitle = $asc;
1455 $icon = " <img src=\"$sortorder.gif\" class=\"icon\" alt=\"". get_string($sortorder)."\" />";
1456 } else {
1457 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1458 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
1459 $icon = "";
1460 $newordertitle = $asc;
1461 } else {
1462 $newordertitle = $desc;
1463 $neworder = '&amp;sortorder=desc';
1464 $icon = ' <img src="asc.gif" border="0" class="icon" alt="" />';
1467 $ficon = '';
1468 $fneworder = '';
1469 $fbtag = '';
1470 $fendbtag = '';
1472 $sicon = '';
1473 $sneworder = '';
1475 $sbtag = '';
1476 $fbtag = '';
1477 $fendbtag = '';
1478 $sendbtag = '';
1480 $sendbtag = '';
1482 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1483 $ficon = $icon;
1484 $fneworder = $neworder;
1485 $fordertitle = $newordertitle;
1486 $sordertitle = $asc;
1487 $fbtag = $bopen;
1488 $fendbtag = $bclose;
1489 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1490 $sicon = $icon;
1491 $sneworder = $neworder;
1492 $fordertitle = $asc;
1493 $sordertitle = $newordertitle;
1494 $sbtag = $bopen;
1495 $sendbtag = $bclose;
1496 } else {
1497 $fordertitle = $asc;
1498 $sordertitle = $asc;
1501 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1502 $forder = 'CREATION';
1503 $sorder = 'UPDATE';
1504 $fsort = get_string("sortbycreation", "glossary");
1505 $ssort = get_string("sortbylastupdate", "glossary");
1507 $sort = get_string("sortchronogically", "glossary");
1508 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1509 $forder = 'FIRSTNAME';
1510 $sorder = 'LASTNAME';
1511 $fsort = get_string("firstname");
1512 $ssort = get_string("lastname");
1514 $sort = get_string("sortby", "glossary");
1517 echo "<br />$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 | ".
1518 "$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 />";
1521 function glossary_sort_entries ( $entry0, $entry1 ) {
1523 if ( moodle_strtolower(ltrim($entry0->concept)) < moodle_strtolower(ltrim($entry1->concept)) ) {
1524 return -1;
1525 } elseif ( moodle_strtolower(ltrim($entry0->concept)) > moodle_strtolower(ltrim($entry1->concept)) ) {
1526 return 1;
1527 } else {
1528 return 0;
1532 function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1533 global $CFG, $USER;
1535 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1537 $user = get_record('user', 'id', $comment->userid);
1538 $strby = get_string('writtenby','glossary');
1539 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
1541 echo '<div class="boxaligncenter">';
1542 echo '<table class="glossarycomment" cellspacing="0">';
1543 echo '<tr valign="top">';
1544 echo '<td class="left picture">';
1545 print_user_picture($user->id, $course->id, $user->picture);
1546 echo '</td>';
1547 echo '<td class="entryheader">';
1549 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
1550 $by = new object();
1551 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
1552 $by->date = userdate($comment->timemodified);
1553 echo '<span class="author">'.get_string('bynameondate', 'forum', $by).'</span>';
1555 echo '</td></tr>';
1557 echo '<tr valign="top"><td class="left side">';
1558 echo '&nbsp;';
1559 echo '</td><td class="entry">';
1561 $options = new object();
1562 $options->trusttext = true;
1563 echo format_text($comment->entrycomment, $comment->format, $options);
1565 echo '<div class="icons commands">';
1567 $ineditperiod = ((time() - $comment->timemodified < $CFG->maxeditingtime) || $glossary->editalways);
1568 if ( ($glossary->allowcomments && $ineditperiod && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context)) {
1569 echo "<a href=\"comment.php?cid=$comment->id&amp;action=edit\"><img
1570 alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" /></a> ";
1572 if ( ($glossary->allowcomments && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context) ) {
1573 echo "<a href=\"comment.php?cid=$comment->id&amp;action=delete\"><img
1574 alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";
1577 echo '</div></td></tr>';
1578 echo '</table></div>';
1582 function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
1584 global $USER, $CFG;
1586 $glossary = get_record('glossary', 'id', $entry->glossaryid);
1587 $glossarymod = get_record('modules','name','glossary');
1588 $cm = get_record_sql("select * from {$CFG->prefix}course_modules where course = $course->id
1589 and module = $glossarymod->id and instance = $glossary->id");
1591 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1593 $ratingsmenuused = false;
1594 if (!empty($ratings) and !empty($USER->id)) {
1595 $useratings = true;
1596 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1597 if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) {
1598 $useratings = false;
1601 if ($useratings) {
1602 if (has_capability('mod/glossary:viewrating', $context)) {
1603 glossary_print_ratings_mean($entry->id, $ratings->scale);
1604 if ($USER->id != $entry->userid) {
1605 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1606 $ratingsmenuused = true;
1608 } else if ($USER->id == $entry->userid) {
1609 glossary_print_ratings_mean($entry->id, $ratings->scale);
1610 } else if (!empty($ratings->allow) ) {
1611 glossary_print_rating_menu($entry->id, $USER->id, $ratings->scale);
1612 $ratingsmenuused = true;
1616 return $ratingsmenuused;
1619 function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
1620 global $USER,$CFG;
1622 echo '<div class="boxaligncenter">';
1623 echo '<table class="glossarypopup" cellspacing="0"><tr>';
1624 echo '<td>';
1625 if ( $entries ) {
1626 foreach ( $entries as $entry ) {
1627 if (! $glossary = get_record('glossary', 'id', $entry->glossaryid)) {
1628 error('Glossary ID was incorrect or no longer exists');
1630 if (! $course = get_record('course', 'id', $glossary->course)) {
1631 error('Glossary is misconfigured - don\'t know what course it\'s from');
1633 if (!$cm = get_coursemodule_from_instance('glossary', $entry->glossaryid, $glossary->course) ) {
1634 error('Glossary is misconfigured - don\'t know what course module it is');
1637 //If displayformat is present, override glossary->displayformat
1638 if ($displayformat < 0) {
1639 $dp = $glossary->displayformat;
1640 } else {
1641 $dp = $displayformat;
1644 //Get popupformatname
1645 $format = get_record('glossary_formats','name',$dp);
1646 $displayformat = $format->popupformatname;
1648 //Check displayformat variable and set to default if necessary
1649 if (!$displayformat) {
1650 $displayformat = 'dictionary';
1653 $formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
1654 $functionname = 'glossary_show_entry_'.$displayformat;
1656 if (file_exists($formatfile)) {
1657 include_once($formatfile);
1658 if (function_exists($functionname)) {
1659 $functionname($course, $cm, $glossary, $entry,'','','','');
1664 echo '</td>';
1665 echo '</tr></table></div>';
1668 function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
1669 global $CFG;
1671 $co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1673 $co .= glossary_start_tag("GLOSSARY",0,true);
1674 $co .= glossary_start_tag("INFO",1,true);
1675 $co .= glossary_full_tag("NAME",2,false,$glossary->name);
1676 $co .= glossary_full_tag("INTRO",2,false,$glossary->intro);
1677 $co .= glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries);
1678 $co .= glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat);
1679 $co .= glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial);
1680 $co .= glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet);
1681 $co .= glossary_full_tag("SHOWALL",2,false,$glossary->showall);
1682 $co .= glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments);
1683 $co .= glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink);
1684 $co .= glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval);
1685 $co .= glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary);
1686 $co .= glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage);
1688 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
1689 $co .= glossary_start_tag("ENTRIES",2,true);
1690 foreach ($entries as $entry) {
1691 $permissiongranted = 1;
1692 if ( $hook ) {
1693 switch ( $hook ) {
1694 case "ALL":
1695 case "SPECIAL":
1696 break;
1697 default:
1698 $permissiongranted = ($entry->concept[ strlen($hook)-1 ] == $hook);
1699 break;
1702 if ( $hook ) {
1703 switch ( $hook ) {
1704 case GLOSSARY_SHOW_ALL_CATEGORIES:
1705 break;
1706 case GLOSSARY_SHOW_NOT_CATEGORISED:
1707 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id);
1708 break;
1709 default:
1710 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id, "categoryid",$hook);
1711 break;
1714 if ( $entry->approved and $permissiongranted ) {
1715 $co .= glossary_start_tag("ENTRY",3,true);
1716 $co .= glossary_full_tag("CONCEPT",4,false,trim($entry->concept));
1717 $co .= glossary_full_tag("DEFINITION",4,false,trusttext_strip($entry->definition));
1718 $co .= glossary_full_tag("FORMAT",4,false,$entry->format);
1719 $co .= glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink);
1720 $co .= glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive);
1721 $co .= glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch);
1722 $co .= glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry);
1724 if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
1725 $co .= glossary_start_tag("ALIASES",4,true);
1726 foreach ($aliases as $alias) {
1727 $co .= glossary_start_tag("ALIAS",5,true);
1728 $co .= glossary_full_tag("NAME",6,false,trim($alias->alias));
1729 $co .= glossary_end_tag("ALIAS",5,true);
1731 $co .= glossary_end_tag("ALIASES",4,true);
1733 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
1734 $co .= glossary_start_tag("CATEGORIES",4,true);
1735 foreach ($catentries as $catentry) {
1736 $category = get_record("glossary_categories","id",$catentry->categoryid);
1738 $co .= glossary_start_tag("CATEGORY",5,true);
1739 $co .= glossary_full_tag("NAME",6,false,$category->name);
1740 $co .= glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink);
1741 $co .= glossary_end_tag("CATEGORY",5,true);
1743 $co .= glossary_end_tag("CATEGORIES",4,true);
1746 $co .= glossary_end_tag("ENTRY",3,true);
1749 $co .= glossary_end_tag("ENTRIES",2,true);
1754 $co .= glossary_end_tag("INFO",1,true);
1755 $co .= glossary_end_tag("GLOSSARY",0,true);
1757 return $co;
1759 /// Functions designed by Eloy Lafuente
1760 /// Functions to create, open and write header of the xml file
1762 // Read import file and convert to current charset
1763 function glossary_read_imported_file($file) {
1764 require_once "../../lib/xmlize.php";
1765 global $CFG;
1767 $h = fopen($file,"r");
1768 $line = '';
1769 if ($h) {
1770 while ( !feof($h) ) {
1771 $char = fread($h,1024);
1772 $line .= $char;
1774 fclose($h);
1776 return xmlize($line, 0);
1779 //Return the xml start tag
1780 function glossary_start_tag($tag,$level=0,$endline=false) {
1781 if ($endline) {
1782 $endchar = "\n";
1783 } else {
1784 $endchar = "";
1786 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1789 //Return the xml end tag
1790 function glossary_end_tag($tag,$level=0,$endline=true) {
1791 if ($endline) {
1792 $endchar = "\n";
1793 } else {
1794 $endchar = "";
1796 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1799 //Return the start tag, the contents and the end tag
1800 function glossary_full_tag($tag,$level=0,$endline=true,$content) {
1801 global $CFG;
1803 $st = glossary_start_tag($tag,$level,$endline);
1804 $co = preg_replace("/\r\n|\r/", "\n", s($content));
1805 $et = glossary_end_tag($tag,0,true);
1806 return $st.$co.$et;
1810 * Adding grading functions
1813 function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1814 /// Returns a list of ratings for a particular entry - sorted.
1815 global $CFG;
1816 return get_records_sql("SELECT u.*, r.rating, r.time
1817 FROM {$CFG->prefix}glossary_ratings r,
1818 {$CFG->prefix}user u
1819 WHERE r.entryid = '$entryid'
1820 AND r.userid = u.id
1821 ORDER BY $sort");
1824 function glossary_count_unrated_entries($glossaryid, $userid) {
1825 // How many unrated entries are in the given glossary for a given user?
1826 global $CFG;
1827 if ($entries = get_record_sql("SELECT count(*) as num
1828 FROM {$CFG->prefix}glossary_entries
1829 WHERE glossaryid = '$glossaryid'
1830 AND userid <> '$userid' ")) {
1832 if ($rated = get_record_sql("SELECT count(*) as num
1833 FROM {$CFG->prefix}glossary_entries e,
1834 {$CFG->prefix}glossary_ratings r
1835 WHERE e.glossaryid = '$glossaryid'
1836 AND e.id = r.entryid
1837 AND r.userid = '$userid'")) {
1838 $difference = $entries->num - $rated->num;
1839 if ($difference > 0) {
1840 return $difference;
1841 } else {
1842 return 0; // Just in case there was a counting error
1844 } else {
1845 return $entries->num;
1847 } else {
1848 return 0;
1852 function glossary_print_ratings_mean($entryid, $scale) {
1853 /// Print the multiple ratings on a entry given to the current user by others.
1854 /// Scale is an array of ratings
1856 static $strrate;
1858 $mean = glossary_get_ratings_mean($entryid, $scale);
1860 if ($mean !== "") {
1862 if (empty($strratings)) {
1863 $strratings = get_string("ratings", "glossary");
1866 echo "$strratings: ";
1867 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
1872 function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1873 /// Return the mean rating of a entry given to the current user by others.
1874 /// Scale is an array of possible ratings in the scale
1875 /// Ratings is an optional simple array of actual ratings (just integers)
1877 if (!$ratings) {
1878 $ratings = array();
1879 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1880 foreach ($rates as $rate) {
1881 $ratings[] = $rate->rating;
1886 $count = count($ratings);
1888 if ($count == 0) {
1889 return "";
1891 } else if ($count == 1) {
1892 return $scale[$ratings[0]];
1894 } else {
1895 $total = 0;
1896 foreach ($ratings as $rating) {
1897 $total += $rating;
1899 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1901 if (isset($scale[$mean])) {
1902 return $scale[$mean]." ($count)";
1903 } else {
1904 return "$mean ($count)"; // Should never happen, hopefully
1909 function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1910 /// Return a summary of entry ratings given to the current user by others.
1911 /// Scale is an array of possible ratings in the scale
1912 /// Ratings is an optional simple array of actual ratings (just integers)
1914 if (!$ratings) {
1915 $ratings = array();
1916 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1917 foreach ($rates as $rate) {
1918 $rating[] = $rate->rating;
1924 if (!$count = count($ratings)) {
1925 return "";
1929 foreach ($scale as $key => $scaleitem) {
1930 $sumrating[$key] = 0;
1933 foreach ($ratings as $rating) {
1934 $sumrating[$rating]++;
1937 $summary = "";
1938 foreach ($scale as $key => $scaleitem) {
1939 $summary = $sumrating[$key].$summary;
1940 if ($key > 1) {
1941 $summary = "/$summary";
1944 return $summary;
1947 function glossary_print_rating_menu($entryid, $userid, $scale) {
1948 /// Print the menu of ratings as part of a larger form.
1949 /// If the entry has already been - set that value.
1950 /// Scale is an array of ratings
1952 static $strrate;
1954 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
1955 $rating->rating = -999;
1958 if (empty($strrate)) {
1959 $strrate = get_string("rate", "glossary");
1962 choose_from_menu($scale, $entryid, $rating->rating, "$strrate...",'',-999);
1966 function glossary_get_paging_bar($totalcount, $page, $perpage, $baseurl, $maxpageallowed=99999, $maxdisplay=20, $separator="&nbsp;", $specialtext="", $specialvalue=-1, $previousandnext = true) {
1967 // Returns the html code to represent any pagging bar. Paramenters are:
1969 // Mandatory:
1970 // $totalcount: total number of records to be displayed
1971 // $page: page currently selected (0 based)
1972 // $perpage: number of records per page
1973 // $baseurl: url to link in each page, the string 'page=XX' will be added automatically.
1974 // Optional:
1975 // $maxpageallowed: maximum number of page allowed.
1976 // $maxdisplay: maximum number of page links to show in the bar
1977 // $separator: string to be used between pages in the bar
1978 // $specialtext: string to be showed as an special link
1979 // $specialvalue: value (page) to be used in the special link
1980 // $previousandnext: to decide if we want the previous and next links
1982 // The function dinamically show the first and last pages, and "scroll" over pages.
1983 // Fully compatible with Moodle's print_paging_bar() function. Perhaps some day this
1984 // could replace the general one. ;-)
1986 $code = '';
1988 $showspecial = false;
1989 $specialselected = false;
1991 //Check if we have to show the special link
1992 if (!empty($specialtext)) {
1993 $showspecial = true;
1995 //Check if we are with the special link selected
1996 if ($showspecial && $page == $specialvalue) {
1997 $specialselected = true;
2000 //If there are results (more than 1 page)
2001 if ($totalcount > $perpage) {
2002 $code .= "<div style=\"text-align:center\">";
2003 $code .= "<p>".get_string("page").":";
2005 $maxpage = (int)(($totalcount-1)/$perpage);
2007 //Lower and upper limit of page
2008 if ($page < 0) {
2009 $page = 0;
2011 if ($page > $maxpageallowed) {
2012 $page = $maxpageallowed;
2014 if ($page > $maxpage) {
2015 $page = $maxpage;
2018 //Calculate the window of pages
2019 $pagefrom = $page - ((int)($maxdisplay / 2));
2020 if ($pagefrom < 0) {
2021 $pagefrom = 0;
2023 $pageto = $pagefrom + $maxdisplay - 1;
2024 if ($pageto > $maxpageallowed) {
2025 $pageto = $maxpageallowed;
2027 if ($pageto > $maxpage) {
2028 $pageto = $maxpage;
2031 //Some movements can be necessary if don't see enought pages
2032 if ($pageto - $pagefrom < $maxdisplay - 1) {
2033 if ($pageto - $maxdisplay + 1 > 0) {
2034 $pagefrom = $pageto - $maxdisplay + 1;
2038 //Calculate first and last if necessary
2039 $firstpagecode = '';
2040 $lastpagecode = '';
2041 if ($pagefrom > 0) {
2042 $firstpagecode = "$separator<a href=\"{$baseurl}page=0\">1</a>";
2043 if ($pagefrom > 1) {
2044 $firstpagecode .= "$separator...";
2047 if ($pageto < $maxpage) {
2048 if ($pageto < $maxpage -1) {
2049 $lastpagecode = "$separator...";
2051 $lastpagecode .= "$separator<a href=\"{$baseurl}page=$maxpage\">".($maxpage+1)."</a>";
2054 //Previous
2055 if ($page > 0 && $previousandnext) {
2056 $pagenum = $page - 1;
2057 $code .= "&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>)&nbsp;";
2060 //Add first
2061 $code .= $firstpagecode;
2063 $pagenum = $pagefrom;
2065 //List of maxdisplay pages
2066 while ($pagenum <= $pageto) {
2067 $pagetoshow = $pagenum +1;
2068 if ($pagenum == $page && !$specialselected) {
2069 $code .= "$separator$pagetoshow";
2070 } else {
2071 $code .= "$separator<a href=\"{$baseurl}page=$pagenum\">$pagetoshow</a>";
2073 $pagenum++;
2076 //Add last
2077 $code .= $lastpagecode;
2079 //Next
2080 if ($page < $maxpage && $page < $maxpageallowed && $previousandnext) {
2081 $pagenum = $page + 1;
2082 $code .= "$separator(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
2085 //Add special
2086 if ($showspecial) {
2087 $code .= '<br />';
2088 if ($specialselected) {
2089 $code .= $specialtext;
2090 } else {
2091 $code .= "$separator<a href=\"{$baseurl}page=$specialvalue\">$specialtext</a>";
2095 //End html
2096 $code .= "</p>";
2097 $code .= "</div>";
2100 return $code;
2103 function glossary_get_view_actions() {
2104 return array('view','view all','view entry');
2107 function glossary_get_post_actions() {
2108 return array('add category','add comment','add entry','approve entry','delete category','delete comment','delete entry','edit category','update comment','update entry');