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);
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.
97 if (empty($glossary->globalglossary
)) {
98 $glossary->globalglossary
= 0;
101 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
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);
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")) {
147 # Delete any dependent records here #
149 if (! delete_records("glossary", "id", "$glossary->id")) {
152 if ($categories = get_records("glossary_categories","glossaryid",$glossary->id
)) {
154 foreach ( $categories as $cat ) {
155 $cats .= "$cat->id,";
157 $cats = substr($cats,0,-1);
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
) ) {
165 foreach ( $entries as $entry ) {
166 if ( $entry->sourceglossaryid
) {
167 $entry->glossaryid
= $entry->sourceglossaryid
;
168 $entry->sourceglossaryid
= 0;
169 update_record("glossary_entries",$entry);
171 $ents .= "$entry->id,";
174 $ents = substr($ents,0,-1);
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);
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
;
207 function glossary_get_user_entries($glossaryid, $userid) {
208 /// Get all the entries for a user in a glossary
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,
215 WHERE g.id = '$glossaryid'
216 AND e.glossaryid = g.id
217 AND e.userid = '$userid'
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.
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);
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.
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')) {
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
);
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('&', '&', $log->url
);
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 '&mode=entry&hook='.$entry->id
);
292 function glossary_log_info($log) {
295 return get_record_sql("SELECT e.*, u.firstname, u.lastname
296 FROM {$CFG->prefix}glossary_entries e,
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 ...
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) {
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
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) {
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->itemid
= $glossary->id
;
352 $grade->userid
= $userid;
353 $grade->rawgrade
= NULL;
354 grade_update('mod/glossary', $glossary->course
, 'mod', 'glossary', $glossary->id
, 0, $grade);
358 $sql = "SELECT g.*, cm.idnumber as cmidnumber
359 FROM {$CFG->prefix}glossary g, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
360 WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id";
361 if ($rs = get_recordset_sql($sql)) {
362 if ($rs->RecordCount() > 0) {
363 while ($glossary = rs_fetch_next_record($rs)) {
364 glossary_grade_item_update($glossary);
365 if ($glossary->assessed
) {
366 glossary_update_grades($glossary, 0, false);
376 * Create/update grade item for given glossary
378 * @param object $glossary object with extra cmidnumber
379 * @return int, 0 if ok, error code otherwise
381 function glossary_grade_item_update($glossary) {
383 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
384 require_once($CFG->libdir
.'/gradelib.php');
387 $params = array('itemname'=>$glossary->name
, 'idnumber'=>$glossary->cmidnumber
);
389 if (!$glossary->assessed
or $glossary->scale
== 0) {
390 $params['gradetype'] = GRADE_TYPE_NONE
;
392 } else if ($glossary->scale
> 0) {
393 $params['gradetype'] = GRADE_TYPE_VALUE
;
394 $params['grademax'] = $glossary->scale
;
395 $params['grademin'] = 0;
397 } else if ($glossary->scale
< 0) {
398 $params['gradetype'] = GRADE_TYPE_SCALE
;
399 $params['scaleid'] = -$glossary->scale
;
402 return grade_update('mod/glossary', $glossary->course
, 'mod', 'glossary', $glossary->id
, 0, NULL, $params);
406 * Delete grade item for given glossary
408 * @param object $glossary object
410 function glossary_grade_item_delete($glossary) {
412 require_once($CFG->libdir
.'/gradelib.php');
414 return grade_update('mod/glossary', $glossary->course
, 'mod', 'glossary', $glossary->id
, 0, NULL, array('deleted'=>1));
417 function glossary_get_participants($glossaryid) {
418 //Returns the users with data in one glossary
419 //(users with records in glossary_entries, students)
424 $students = get_records_sql("SELECT DISTINCT u.id, u.id
425 FROM {$CFG->prefix}user u,
426 {$CFG->prefix}glossary_entries g
427 WHERE g.glossaryid = '$glossaryid' and
430 //Return students array (it contains an array of unique users)
434 function glossary_scale_used ($glossaryid,$scaleid) {
435 //This function returns if a scale is being used by one glossary
439 $rec = get_record("glossary","id","$glossaryid","scale","-$scaleid");
441 if (!empty($rec) && !empty($scaleid)) {
448 //////////////////////////////////////////////////////////////////////////////////////
449 /// Any other glossary functions go here. Each of them must have a name that
450 /// starts with glossary_
452 //This function return an array of valid glossary_formats records
453 //Everytime it's called, every existing format is checked, new formats
454 //are included if detected and old formats are deleted and any glossary
455 //using an invalid format is updated to the default (dictionary).
456 function glossary_get_available_formats() {
460 //Get available formats (plugin) and insert (if necessary) them into glossary_formats
461 $formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
462 $pluginformats = array();
463 foreach ($formats as $format) {
464 //If the format file exists
465 if (file_exists($CFG->dirroot
.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
466 include_once($CFG->dirroot
.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
467 //If the function exists
468 if (function_exists('glossary_show_entry_'.$format)) {
469 //Acummulate it as a valid format
470 $pluginformats[] = $format;
471 //If the format doesn't exist in the table
472 if (!$rec = get_record('glossary_formats','name',$format)) {
473 //Insert the record in glossary_formats
476 $gf->popupformatname
= $format;
478 insert_record("glossary_formats",$gf);
484 //Delete non_existent formats from glossary_formats table
485 $formats = get_records("glossary_formats");
486 foreach ($formats as $format) {
488 //If the format in DB isn't a valid previously detected format then delete the record
489 if (!in_array($format->name
,$pluginformats)) {
495 delete_records('glossary_formats','name',$format->name
);
496 //Reasign existing glossaries to default (dictionary) format
497 if ($glossaries = get_records('glossary','displayformat',$format->name
)) {
498 foreach($glossaries as $glossary) {
499 set_field('glossary','displayformat','dictionary','id',$glossary->id
);
505 //Now everything is ready in glossary_formats table
506 $formats = get_records("glossary_formats");
511 function glossary_debug($debug,$text,$br=1) {
513 echo '<font color="red">' . $text . '</font>';
520 function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
526 return get_records_sql("SELECT $pivot id,userid,concept,definition,format
527 FROM {$CFG->prefix}glossary_entries
528 WHERE glossaryid = '$glossaryid'
529 AND id IN ($entrylist)");
532 function glossary_get_entries_search($concept, $courseid) {
536 //Check if the user is an admin
537 $bypassadmin = 1; //This means NO (by default)
538 if (has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
539 $bypassadmin = 0; //This means YES
542 //Check if the user is a teacher
543 $bypassteacher = 1; //This means NO (by default)
544 if (has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE
, $courseid))) {
545 $bypassteacher = 0; //This means YES
548 $conceptlower = moodle_strtolower(trim($concept));
550 return get_records_sql("SELECT e.*, g.name as glossaryname, cm.id as cmid, cm.course as courseid
551 FROM {$CFG->prefix}glossary_entries e,
552 {$CFG->prefix}glossary g,
553 {$CFG->prefix}course_modules cm,
554 {$CFG->prefix}modules m
555 WHERE m.name = 'glossary' AND
557 (cm.visible = 1 OR cm.visible = $bypassadmin OR
558 (cm.course = '$courseid' AND cm.visible = $bypassteacher)) AND
559 g.id = cm.instance AND
560 e.glossaryid = g.id AND
561 ( (e.casesensitive != 0 AND LOWER(concept) = '$conceptlower') OR
562 (e.casesensitive = 0 and concept = '$concept')) AND
563 (g.course = '$courseid' OR g.globalglossary = 1) AND
564 e.usedynalink != 0 AND
565 g.usedynalink != 0");
568 function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
571 $where = " and $where";
574 $orderby = " ORDER BY $orderby";
579 return get_records_sql("SELECT $pivot *
580 FROM {$CFG->prefix}glossary_entries
581 WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
584 function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
587 $where = " and $where";
590 $orderby = " ORDER BY $orderby";
595 return get_records_sql("SELECT $pivot ge.*
596 FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
597 WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
598 (ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
601 function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) {
604 if ( $displayformat < 0 ) {
605 $displayformat = $glossary->displayformat
;
607 if ($entry->approved
or ($USER->id
== $entry->userid
) or ($mode == 'approval' and !$entry->approved
) ) {
608 $formatfile = $CFG->dirroot
.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
610 $functionname = 'glossary_print_entry_'.$displayformat;
612 $functionname = 'glossary_show_entry_'.$displayformat;
615 if (file_exists($formatfile)) {
616 include_once($formatfile);
617 if (function_exists($functionname)) {
618 $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
619 } else if ($printview) {
620 //If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format
621 $return = glossary_print_entry_default($entry);
628 //Default (old) print format used if custom function doesn't exist in format
629 function glossary_print_entry_default ($entry) {
630 echo '<b>'. strip_tags($entry->concept
) . ': </b>';
632 $definition = $entry->definition
;
634 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
635 if (trusttext_present($definition)) {
637 $definition = trusttext_strip($definition);
642 $definition = '<span class="nolink">' . strip_tags($definition) . '</span>';
644 // reconstruct the TRUSTTEXT properly after processing
646 $definition = trusttext_mark($definition);
648 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
651 $options = new object();
652 $options->para
= false;
653 $options->trusttext
= true;
654 $definition = format_text($definition, $entry->format
, $options);
659 function glossary_print_entry_concept($entry) {
660 $options = new object();
661 $options->para
= false;
662 $text = format_text('<span class="nolink">' . $entry->concept
. '</span>', FORMAT_MOODLE
, $options);
663 if (!empty($entry->highlight
)) {
664 $text = highlight($entry->highlight
, $text);
669 function glossary_print_entry_definition($entry) {
671 $definition = $entry->definition
;
673 // always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
674 if (trusttext_present($definition)) {
676 $definition = trusttext_strip($definition);
686 //Calculate all the strings to be no-linked
688 $term = preg_quote(trim($entry->concept
),'/');
689 $pat = '/('.$term.')/is';
692 if ( $aliases = get_records('glossary_alias','entryid',$entry->id
) ) {
693 foreach ($aliases as $alias) {
694 $term = preg_quote(trim($alias->alias
),'/');
695 $pat = '/('.$term.')/is';
701 //Extract <a>..><a> tags from definition
702 preg_match_all('/<a[^>]+?>(.*?)<\/a>/is',$definition,$list_of_a);
704 //Save them into links array to use them later
705 foreach (array_unique($list_of_a[0]) as $key=>$value) {
706 $links['<#'.$key.'#>'] = $value;
708 //Take off every link from definition
710 $definition = str_replace($links,array_keys($links),$definition);
714 //Extract all tags from definition
715 preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
717 //Save them into tags array to use them later
718 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
719 $tags['<@'.$key.'@>'] = $value;
721 //Take off every tag from definition
723 $definition = str_replace($tags,array_keys($tags),$definition);
727 //Extract all URLS with protocol (http://domain.com) from definition
728 preg_match_all('/([[:space:]]|^|\(|\[)([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_urls);
730 //Save them into urls array to use them later
731 foreach (array_unique($list_of_urls[0]) as $key=>$value) {
732 $urls['<*'.$key.'*>'] = $value;
734 //Take off every url from definition
736 $definition = str_replace($urls,array_keys($urls),$definition);
740 //Extract all WEB ADDRESSES (www.domain.com) from definition
741 preg_match_all('/([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_addresses);
743 //Save them into addrs array to use them later
744 foreach (array_unique($list_of_addresses[0]) as $key=>$value) {
745 $addrs['<+'.$key.'+>'] = $value;
747 //Take off every addr from definition
749 $definition = str_replace($addrs,array_keys($addrs),$definition);
753 //Put doNolinks (concept + aliases) enclosed by <nolink> tag
754 $definition= preg_replace($doNolinks,'<span class="nolink">$1</span>',$definition);
758 $definition = str_replace(array_keys($addrs),$addrs,$definition);
763 $definition = str_replace(array_keys($urls),$urls,$definition);
768 $definition = str_replace(array_keys($tags),$tags,$definition);
773 $definition = str_replace(array_keys($links),$links,$definition);
776 $options = new object();
777 $options->para
= false;
778 $options->trusttext
= true;
780 // reconstruct the TRUSTTEXT properly after processing
782 $definition = trusttext_mark($definition);
784 $definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
787 $text = format_text($definition, $entry->format
, $options);
788 if (!empty($entry->highlight
)) {
789 $text = highlight($entry->highlight
, $text);
791 if (isset($entry->footer
)) { // Unparsed footer info
792 $text .= $entry->footer
;
797 function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode='',$hook='', $type = 'print') {
799 if ( $aliases = get_records('glossary_alias','entryid',$entry->id
) ) {
800 foreach ($aliases as $alias) {
801 if (trim($alias->alias
)) {
803 $return = '<select style="font-size:8pt">';
805 $return .= "<option>$alias->alias</option>";
809 $return .= '</select>';
812 if ($type == 'print') {
819 function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$hook='', $type = 'print') {
822 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
824 $output = false; //To decide if we must really return text in "return". Activate when needed only!
825 $importedentry = ($entry->sourceglossaryid
== $glossary->id
);
826 $ismainglossary = $glossary->mainglossary
;
829 $return = '<span class="commands">';
830 if (!$entry->approved
) {
832 $return .= get_string('entryishidden','glossary');
834 $return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
836 if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments
) {
838 $return .= ' <a title="' . get_string('addcomment','glossary') . '" href="comment.php?action=add&eid='.$entry->id
.'"><img src="comment.gif" class="iconsmall" alt="'.get_string('addcomment','glossary').'" /></a>';
842 if (has_capability('mod/glossary:manageentries', $context) or (!empty($USER->id
) and has_capability('mod/glossary:write', $context) and $entry->userid
== $USER->id
)) {
843 // only teachers can export entries so check it out
844 if (has_capability('mod/glossary:export', $context) and !$ismainglossary and !$importedentry) {
845 $mainglossary = get_record('glossary','mainglossary',1,'course',$course->id
);
846 if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
848 $return .= ' <a title="'.get_string('exporttomainglossary','glossary') . '" href="exportentry.php?id='.$cm->id
.'&entry='.$entry->id
.'&mode='.$mode.'&hook='.$hook.'"><img src="export.gif" class="iconsmall" alt="'.get_string('exporttomainglossary','glossary').'" /></a>';
852 if ( $entry->sourceglossaryid
) {
853 $icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
855 $icon = "$CFG->pixpath/t/delete.gif";
858 //Decide if an entry is editable:
859 // -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
860 // -The user is teacher or he is a student with time permissions (edit period or editalways defined).
861 $ineditperiod = ((time() - $entry->timecreated
< $CFG->maxeditingtime
) ||
$glossary->editalways
);
862 if ( !$importedentry and (has_capability('mod/glossary:manageentries', $context) or ($entry->userid
== $USER->id
and ($ineditperiod and has_capability('mod/glossary:write', $context))))) {
864 $return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
866 $return .= "\" class=\"iconsmall\" alt=\"" . get_string("delete") . "\" /></a> ";
868 $return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"" . get_string("edit") . "\" /></a>";
869 } elseif ( $importedentry ) {
870 $return .= " <font size=\"-1\">" . get_string("exportedentry","glossary") . "</font>";
873 $return .= " "; // just to make up a little the output in Mozilla ;)
875 $return .= '</span>';
877 //If we haven't calculated any REAL thing, delete result ($return)
882 if ($type == 'print') {
889 function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
892 $count = count_records('glossary_comments','entryid',$entry->id
);
895 $return .= "<a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
897 $return .= get_string('comment', 'glossary');
899 $return .= get_string('comments', 'glossary');
904 if ($type == 'print') {
911 function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
914 $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,'html');
919 $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,'html');
921 if ($aliases ||
$icons ||
$ratings) {
924 echo '<tr valign="top"><td class="aliases">' .
925 get_string('aliases','glossary').': '.$aliases . '</td></tr>';
928 echo '<tr valign="top"><td class="icons">'.$icons.'</td></tr>';
931 echo '<tr valign="top"><td class="ratings">';
932 $return = glossary_print_entry_ratings($course, $entry, $ratings);
940 function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
941 /// valid format values: html : Return the HTML link for the attachment as an icon
942 /// text : Return the HTML link for tha attachment as text
943 /// blank : Print the output to the screen
944 if ($entry->attachment
) {
945 $glossary = get_record("glossary","id",$entry->glossaryid
);
946 $entry->course
= $glossary->course
; //used inside print_attachment
948 echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap=\"nowrap\">\n";
950 echo glossary_print_attachments($entry,$format,$align);
952 echo "</td></tr></table>\n";
957 function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
958 if ( $mode == 'approval' and !$entry->approved
) {
960 echo '<table class="glossaryapproval" align="'.$align.'"><tr><td align="'.$align.'">';
962 echo '<a title="'.get_string('approve','glossary').'" href="approve.php?id='.$cm->id
.'&eid='.$entry->id
.'&mode='.$mode.'"><img align="'.$align.'" src="check.gif" style="border:0px; width:34px; height:34px" alt="'.get_string('approve','glossary').'" /></a>';
964 echo '</td></tr></table>';
969 function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {
970 // It returns all entries from all glossaries that matches the specified criteria
971 // within a given $course. It performs an $extended search if necessary.
972 // It restrict the search to only one $glossary if the $glossary parameter is set.
976 if ( $glossaries = get_records("glossary", "course", $course->id
) ) {
978 foreach ( $glossaries as $glossary ) {
979 $glos .= "$glossary->id,";
981 $glos = substr($glos,0,-1);
984 $glos = $glossary->id
;
987 if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE
, $glossary->course
))) {
988 $glossarymodule = get_record("modules", "name", "glossary");
989 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
990 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
994 $onlyvisibletable = "";
997 /// Some differences in syntax for entrygreSQL
998 switch ($CFG->dbfamily
) {
1000 $LIKE = "ILIKE"; // case-insensitive
1001 $NOTLIKE = "NOT ILIKE"; // case-insensitive
1008 $NOTLIKE = "NOT LIKE";
1010 $NOTREGEXP = "NOT REGEXP";
1014 $conceptsearch = "";
1015 $definitionsearch = "";
1018 foreach ($searchterms as $searchterm) {
1019 if ($conceptsearch) {
1020 $conceptsearch.= " OR ";
1022 if ($definitionsearch) {
1023 $definitionsearch.= " OR ";
1026 /// Under Oracle and MSSQL, trim the + and - operators and perform
1027 /// simpler LIKE search
1028 if ($CFG->dbfamily
== 'oracle' ||
$CFG->dbfamily
== 'mssql') {
1029 $searchterm = trim($searchterm, '+-');
1032 if (substr($searchterm,0,1) == "+") {
1033 $searchterm = substr($searchterm,1);
1034 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1035 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1036 } else if (substr($searchterm,0,1) == "-") {
1037 $searchterm = substr($searchterm,1);
1038 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1039 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
1041 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
1042 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
1047 $definitionsearch = "0";
1050 $selectsql = "{$CFG->prefix}glossary_entries e,
1051 {$CFG->prefix}glossary g $onlyvisibletable
1052 WHERE ($conceptsearch OR $definitionsearch)
1053 AND (e.glossaryid = g.id or e.sourceglossaryid = g.id) $onlyvisible
1054 AND g.id IN ($glos) AND e.approved != 0";
1056 return get_records_sql("SELECT e.*
1057 FROM $selectsql ORDER BY e.concept ASC");
1060 function glossary_search_entries($searchterms, $glossary, $extended) {
1061 $course = get_record("course","id",$glossary->course
);
1062 return glossary_search($course,$searchterms,$extended,$glossary);
1065 function glossary_file_area_name($entry) {
1067 // Creates a directory file name, suitable for make_upload_directory()
1069 // I'm doing this workaround for make it works for delete_instance also
1070 // (when called from delete_instance, glossary is already deleted so
1071 // getting the course from mdl_glossary does not work)
1072 $module = get_record("modules","name","glossary");
1073 $cm = get_record("course_modules","module",$module->id
,"instance",$entry->glossaryid
);
1074 return "$cm->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
1077 function glossary_file_area($entry) {
1078 return make_upload_directory( glossary_file_area_name($entry) );
1081 function glossary_main_file_area($glossary) {
1082 $modarea = glossary_mod_file_area($glossary);
1083 return "$modarea/$glossary->id";
1086 function glossary_mod_file_area($glossary) {
1089 return make_upload_directory( "$glossary->course/$CFG->moddata/glossary" );
1092 function glossary_delete_old_attachments($entry, $exception="") {
1093 // Deletes all the user files in the attachments area for a entry
1094 // EXCEPT for any file named $exception
1096 if ($basedir = glossary_file_area($entry)) {
1097 if ($files = get_directory_list($basedir)) {
1098 foreach ($files as $file) {
1099 if ($file != $exception) {
1100 unlink("$basedir/$file");
1101 // notify("Existing file '$file' has been deleted!");
1105 if (!$exception) { // Delete directory as well, if empty
1110 function glossary_delete_attachments($glossary) {
1111 // Deletes all the user files in the attachments area for the glossary
1112 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id
) ) {
1114 foreach ($entries as $entry) {
1115 if ( $entry->attachment
) {
1116 if ($basedir = glossary_file_area($entry)) {
1117 if ($files = get_directory_list($basedir)) {
1118 foreach ($files as $file) {
1119 unlink("$basedir/$file");
1128 $attachmentdir = glossary_main_file_area($glossary);
1129 $glossarydir = glossary_mod_file_area($glossary);
1131 rmdir("$attachmentdir");
1132 if (!$files = get_directory_list($glossarydir) ) {
1133 rmdir( "$glossarydir" );
1139 function glossary_copy_attachments($entry, $newentry) {
1140 /// Given a entry object that is being copied to glossaryid,
1141 /// this function checks that entry
1142 /// for attachments, and if any are found, these are
1143 /// copied to the new glossary directory.
1149 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
1150 foreach ($entries as $curentry) {
1151 $oldentry = new object();
1152 $oldentry->id
= $entry->id
;
1153 $oldentry->course
= $entry->course
;
1154 $oldentry->glossaryid
= $curentry->glossaryid
;
1155 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1156 if (is_dir($oldentrydir)) {
1158 $newentrydir = glossary_file_area($newentry);
1159 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
1168 function glossary_move_attachments($entry, $glossaryid) {
1169 /// Given a entry object that is being moved to glossaryid,
1170 /// this function checks that entry
1171 /// for attachments, and if any are found, these are
1172 /// moved to the new glossary directory.
1176 require_once($CFG->dirroot
.'/lib/uploadlib.php');
1180 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
1181 foreach ($entries as $entry) {
1182 $oldentry = new object();
1183 $oldentry->course
= $entry->course
;
1184 $oldentry->glossaryid
= $entry->glossaryid
;
1185 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
1186 if (is_dir($oldentrydir)) {
1187 $newentry = $oldentry;
1188 $newentry->glossaryid
= $glossaryid;
1189 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
1190 $files = get_directory_list($oldentrydir); // get it before we rename it.
1191 if (! @rename
($oldentrydir, $newentrydir)) {
1194 foreach ($files as $file) {
1195 // this is not tested as I can't find anywhere that calls this function, grepping the source.
1196 clam_change_log($oldentrydir.'/'.$file,$newentrydir.'/'.$file);
1204 function glossary_print_attachments($entry, $return=NULL, $align="left") {
1205 // if return=html, then return a html string.
1206 // if return=text, then return a text-only string.
1207 // otherwise, print HTML for non-images, and return image HTML
1208 // if attachment is an image, $align set its aligment.
1212 if ( $newentry->sourceglossaryid
) {
1213 $newentry->glossaryid
= $newentry->sourceglossaryid
;
1216 $filearea = glossary_file_area_name($newentry);
1221 if ($basedir = glossary_file_area($newentry)) {
1222 if ($files = get_directory_list($basedir)) {
1223 $strattachment = get_string("attachment", "glossary");
1224 foreach ($files as $file) {
1225 $icon = mimeinfo("icon", $file);
1226 if ($CFG->slasharguments
) {
1227 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
1229 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
1231 $image = "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"\" />";
1233 if ($return == "html") {
1234 $output .= "<a href=\"$ffurl\">$image</a> ";
1235 $output .= "<a href=\"$ffurl\">$file</a><br />";
1237 } else if ($return == "text") {
1238 $output .= "$strattachment $file:\n$ffurl\n";
1241 if ($icon == "image.gif") { // Image attachments don't get printed as links
1242 $imagereturn .= "<img src=\"$ffurl\" align=\"$align\" alt=\"\" />";
1244 echo "<a href=\"$ffurl\">$image</a> ";
1245 echo "<a href=\"$ffurl\">$file</a><br />";
1256 return $imagereturn;
1259 function glossary_print_tabbed_table_end() {
1260 echo "</div></div>";
1263 function glossary_print_approval_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1264 if ($glossary->showalphabet
) {
1265 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1267 glossary_print_special_links($cm, $glossary, $mode, $hook);
1269 glossary_print_alphabet_links($cm, $glossary, $mode, $hook,$sortkey, $sortorder);
1271 glossary_print_all_links($cm, $glossary, $mode, $hook);
1273 glossary_print_sorting_links($cm, $mode, 'CREATION', 'asc');
1276 function glossary_print_import_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1277 echo '<div class="glossaryexplain">' . get_string("explainimport","glossary") . '</div>';
1280 function glossary_print_export_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1281 echo '<div class="glossaryexplain">' . get_string("explainexport","glossary") . '</div>';
1284 function glossary_print_alphabet_menu($cm, $glossary, $mode, $hook, $sortkey='', $sortorder = '') {
1285 if ( $mode != 'date' ) {
1286 if ($glossary->showalphabet
) {
1287 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1290 glossary_print_special_links($cm, $glossary, $mode, $hook);
1292 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1294 glossary_print_all_links($cm, $glossary, $mode, $hook);
1296 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1300 function glossary_print_author_menu($cm, $glossary,$mode, $hook, $sortkey = '', $sortorder = '') {
1301 if ($glossary->showalphabet
) {
1302 echo '<div class="glossaryexplain">' . get_string("explainalphabet","glossary") . '</div><br />';
1305 glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder);
1306 glossary_print_all_links($cm, $glossary, $mode, $hook);
1307 glossary_print_sorting_links($cm, $mode, $sortkey,$sortorder);
1310 function glossary_print_categories_menu($cm, $glossary, $hook, $category) {
1314 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
1316 echo '<table border="0" width="100%">';
1319 echo '<td align="center" style="width:20%">';
1320 if (has_capability('mod/glossary:managecategories', $context)) {
1321 $options['id'] = $cm->id
;
1322 $options['mode'] = 'cat';
1323 $options['hook'] = $hook;
1324 echo print_single_button("editcategories.php", $options, get_string("editcategories","glossary"), "get");
1328 echo '<td align="center" style="width:60%">';
1331 $menu[GLOSSARY_SHOW_ALL_CATEGORIES
] = get_string("allcategories","glossary");
1332 $menu[GLOSSARY_SHOW_NOT_CATEGORISED
] = get_string("notcategorised","glossary");
1334 $categories = get_records("glossary_categories", "glossaryid", $glossary->id
, "name ASC");
1336 if ( $categories ) {
1337 foreach ($categories as $currentcategory) {
1338 $url = $currentcategory->id
;
1340 if ($currentcategory->id
== $category->id
) {
1344 $menu[$url] = clean_text($currentcategory->name
); //Only clean, not filters
1348 $selected = GLOSSARY_SHOW_NOT_CATEGORISED
;
1352 echo format_text($category->name
, FORMAT_PLAIN
);
1354 if ( $hook == GLOSSARY_SHOW_NOT_CATEGORISED
) {
1356 echo get_string("entrieswithoutcategory","glossary");
1357 $selected = GLOSSARY_SHOW_NOT_CATEGORISED
;
1359 } elseif ( $hook == GLOSSARY_SHOW_ALL_CATEGORIES
) {
1361 echo get_string("allcategories","glossary");
1362 $selected = GLOSSARY_SHOW_ALL_CATEGORIES
;
1367 echo '<td align="center" style="width:20%">';
1369 echo popup_form("$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=cat&hook=", $menu, "catmenu", $selected, "",
1378 function glossary_print_all_links($cm, $glossary, $mode, $hook) {
1380 if ( $glossary->showall
) {
1381 $strallentries = get_string("allentries", "glossary");
1382 if ( $hook == 'ALL' ) {
1383 echo "<b>$strallentries</b>";
1385 $strexplainall = strip_tags(get_string("explainall","glossary"));
1386 echo "<a title=\"$strexplainall\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=ALL\">$strallentries</a>";
1391 function glossary_print_special_links($cm, $glossary, $mode, $hook) {
1393 if ( $glossary->showspecial
) {
1394 $strspecial = get_string("special", "glossary");
1395 if ( $hook == 'SPECIAL' ) {
1396 echo "<b>$strspecial</b> | ";
1398 $strexplainspecial = strip_tags(get_string("explainspecial","glossary"));
1399 echo "<a title=\"$strexplainspecial\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=SPECIAL\">$strspecial</a> | ";
1404 function glossary_print_alphabet_links($cm, $glossary, $mode, $hook, $sortkey, $sortorder) {
1406 if ( $glossary->showalphabet
) {
1407 $alphabet = explode(",", get_string("alphabet"));
1408 $letters_by_line = 14;
1409 for ($i = 0; $i < count($alphabet); $i++
) {
1410 if ( $hook == $alphabet[$i] and $hook) {
1411 echo "<b>$alphabet[$i]</b>";
1413 echo "<a href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&mode=$mode&hook=".urlencode($alphabet[$i])."&sortkey=$sortkey&sortorder=$sortorder\">$alphabet[$i]</a>";
1415 if ((int) ($i %
$letters_by_line) != 0 or $i == 0) {
1424 function glossary_print_sorting_links($cm, $mode, $sortkey = '',$sortorder = '') {
1427 $asc = get_string("ascending","glossary");
1428 $desc = get_string("descending","glossary");
1434 if ( $sortorder == 'asc' ) {
1435 $neworder = '&sortorder=desc';
1436 $newordertitle = $desc;
1438 $neworder = '&sortorder=asc';
1439 $newordertitle = $asc;
1441 $icon = " <img src=\"$sortorder.gif\" class=\"icon\" alt=\"". get_string($sortorder)."\" />";
1443 if ( $sortkey != 'CREATION' and $sortkey != 'UPDATE' and
1444 $sortkey != 'FIRSTNAME' and $sortkey != 'LASTNAME' ) {
1446 $newordertitle = $asc;
1448 $newordertitle = $desc;
1449 $neworder = '&sortorder=desc';
1450 $icon = ' <img src="asc.gif" border="0" class="icon" alt="" />';
1468 if ( $sortkey == 'CREATION' or $sortkey == 'FIRSTNAME' ) {
1470 $fneworder = $neworder;
1471 $fordertitle = $newordertitle;
1472 $sordertitle = $asc;
1474 $fendbtag = $bclose;
1475 } elseif ($sortkey == 'UPDATE' or $sortkey == 'LASTNAME') {
1477 $sneworder = $neworder;
1478 $fordertitle = $asc;
1479 $sordertitle = $newordertitle;
1481 $sendbtag = $bclose;
1483 $fordertitle = $asc;
1484 $sordertitle = $asc;
1487 if ( $sortkey == 'CREATION' or $sortkey == 'UPDATE' ) {
1488 $forder = 'CREATION';
1490 $fsort = get_string("sortbycreation", "glossary");
1491 $ssort = get_string("sortbylastupdate", "glossary");
1493 $sort = get_string("sortchronogically", "glossary");
1494 } elseif ( $sortkey == 'FIRSTNAME' or $sortkey == 'LASTNAME') {
1495 $forder = 'FIRSTNAME';
1496 $sorder = 'LASTNAME';
1497 $fsort = get_string("firstname");
1498 $ssort = get_string("lastname");
1500 $sort = get_string("sortby", "glossary");
1503 echo "<br />$sort: $sbtag<a title=\"$ssort $sordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$sorder$sneworder&mode=$mode\">$ssort$sicon</a>$sendbtag | ".
1504 "$fbtag<a title=\"$fsort $fordertitle\" href=\"$CFG->wwwroot/mod/glossary/view.php?id=$cm->id&sortkey=$forder$fneworder&mode=$mode\">$fsort$ficon</a>$fendbtag<br />";
1507 function glossary_sort_entries ( $entry0, $entry1 ) {
1509 if ( moodle_strtolower(ltrim($entry0->concept
)) < moodle_strtolower(ltrim($entry1->concept
)) ) {
1511 } elseif ( moodle_strtolower(ltrim($entry0->concept
)) > moodle_strtolower(ltrim($entry1->concept
)) ) {
1518 function glossary_print_comment($course, $cm, $glossary, $entry, $comment) {
1521 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
1523 $user = get_record('user', 'id', $comment->userid
);
1524 $strby = get_string('writtenby','glossary');
1525 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE
, $course->id
)));
1527 echo '<div class="boxaligncenter">';
1528 echo '<table class="glossarycomment" cellspacing="0">';
1529 echo '<tr valign="top">';
1530 echo '<td class="left picture">';
1531 print_user_picture($user->id
, $course->id
, $user->picture
);
1533 echo '<td class="entryheader">';
1535 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE
, $course->id
)));
1537 $by->name
= '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$course->id
.'">'.$fullname.'</a>';
1538 $by->date
= userdate($comment->timemodified
);
1539 echo '<span class="author">'.get_string('bynameondate', 'forum', $by).'</span>';
1543 echo '<tr valign="top"><td class="left side">';
1545 echo '</td><td class="entry">';
1547 $options = new object();
1548 $options->trusttext
= true;
1549 echo format_text($comment->entrycomment
, $comment->format
, $options);
1551 echo '<div class="icons commands">';
1553 $ineditperiod = ((time() - $comment->timemodified
< $CFG->maxeditingtime
) ||
$glossary->editalways
);
1554 if ( ($glossary->allowcomments
&& $ineditperiod && $USER->id
== $comment->userid
) ||
has_capability('mod/glossary:managecomments', $context)) {
1555 echo "<a href=\"comment.php?cid=$comment->id&action=edit\"><img
1556 alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" /></a> ";
1558 if ( ($glossary->allowcomments
&& $USER->id
== $comment->userid
) ||
has_capability('mod/glossary:managecomments', $context) ) {
1559 echo "<a href=\"comment.php?cid=$comment->id&action=delete\"><img
1560 alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";
1563 echo '</div></td></tr>';
1564 echo '</table></div>';
1568 function glossary_print_entry_ratings($course, $entry, $ratings = NULL) {
1572 $glossary = get_record('glossary', 'id', $entry->glossaryid
);
1573 $glossarymod = get_record('modules','name','glossary');
1574 $cm = get_record_sql("select * from {$CFG->prefix}course_modules where course = $course->id
1575 and module = $glossarymod->id and instance = $glossary->id");
1577 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
1579 $ratingsmenuused = false;
1580 if (!empty($ratings) and !empty($USER->id
)) {
1582 if ($ratings->assesstimestart
and $ratings->assesstimefinish
) {
1583 if ($entry->timecreated
< $ratings->assesstimestart
or $entry->timecreated
> $ratings->assesstimefinish
) {
1584 $useratings = false;
1588 if (has_capability('mod/glossary:viewrating', $context)) {
1589 glossary_print_ratings_mean($entry->id
, $ratings->scale
);
1590 if ($USER->id
!= $entry->userid
) {
1591 glossary_print_rating_menu($entry->id
, $USER->id
, $ratings->scale
);
1592 $ratingsmenuused = true;
1594 } else if ($USER->id
== $entry->userid
) {
1595 glossary_print_ratings_mean($entry->id
, $ratings->scale
);
1596 } else if (!empty($ratings->allow
) ) {
1597 glossary_print_rating_menu($entry->id
, $USER->id
, $ratings->scale
);
1598 $ratingsmenuused = true;
1602 return $ratingsmenuused;
1605 function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
1608 echo '<div class="boxaligncenter">';
1609 echo '<table class="glossarypopup" cellspacing="0"><tr>';
1612 foreach ( $entries as $entry ) {
1613 if (! $glossary = get_record('glossary', 'id', $entry->glossaryid
)) {
1614 error('Glossary ID was incorrect or no longer exists');
1616 if (! $course = get_record('course', 'id', $glossary->course
)) {
1617 error('Glossary is misconfigured - don\'t know what course it\'s from');
1619 if (!$cm = get_coursemodule_from_instance('glossary', $entry->glossaryid
, $glossary->course
) ) {
1620 error('Glossary is misconfigured - don\'t know what course module it is');
1623 //If displayformat is present, override glossary->displayformat
1624 if ($displayformat < 0) {
1625 $dp = $glossary->displayformat
;
1627 $dp = $displayformat;
1630 //Get popupformatname
1631 $format = get_record('glossary_formats','name',$dp);
1632 $displayformat = $format->popupformatname
;
1634 //Check displayformat variable and set to default if necessary
1635 if (!$displayformat) {
1636 $displayformat = 'dictionary';
1639 $formatfile = $CFG->dirroot
.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
1640 $functionname = 'glossary_show_entry_'.$displayformat;
1642 if (file_exists($formatfile)) {
1643 include_once($formatfile);
1644 if (function_exists($functionname)) {
1645 $functionname($course, $cm, $glossary, $entry,'','','','');
1651 echo '</tr></table></div>';
1654 function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
1657 $co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1659 $co .= glossary_start_tag("GLOSSARY",0,true);
1660 $co .= glossary_start_tag("INFO",1,true);
1661 $co .= glossary_full_tag("NAME",2,false,$glossary->name
);
1662 $co .= glossary_full_tag("INTRO",2,false,$glossary->intro
);
1663 $co .= glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries
);
1664 $co .= glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat
);
1665 $co .= glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial
);
1666 $co .= glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet
);
1667 $co .= glossary_full_tag("SHOWALL",2,false,$glossary->showall
);
1668 $co .= glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments
);
1669 $co .= glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink
);
1670 $co .= glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval
);
1671 $co .= glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary
);
1672 $co .= glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage
);
1674 if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id
) ) {
1675 $co .= glossary_start_tag("ENTRIES",2,true);
1676 foreach ($entries as $entry) {
1677 $permissiongranted = 1;
1684 $permissiongranted = ($entry->concept
[ strlen($hook)-1 ] == $hook);
1690 case GLOSSARY_SHOW_ALL_CATEGORIES
:
1692 case GLOSSARY_SHOW_NOT_CATEGORISED
:
1693 $permissiongranted = !record_exists("glossary_entries_categories","entryid",$entry->id
);
1696 $permissiongranted = record_exists("glossary_entries_categories","entryid",$entry->id
, "categoryid",$hook);
1700 if ( $entry->approved
and $permissiongranted ) {
1701 $co .= glossary_start_tag("ENTRY",3,true);
1702 $co .= glossary_full_tag("CONCEPT",4,false,trim($entry->concept
));
1703 $co .= glossary_full_tag("DEFINITION",4,false,trusttext_strip($entry->definition
));
1704 $co .= glossary_full_tag("FORMAT",4,false,$entry->format
);
1705 $co .= glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink
);
1706 $co .= glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive
);
1707 $co .= glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch
);
1708 $co .= glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry
);
1710 if ( $aliases = get_records("glossary_alias","entryid",$entry->id
) ) {
1711 $co .= glossary_start_tag("ALIASES",4,true);
1712 foreach ($aliases as $alias) {
1713 $co .= glossary_start_tag("ALIAS",5,true);
1714 $co .= glossary_full_tag("NAME",6,false,trim($alias->alias
));
1715 $co .= glossary_end_tag("ALIAS",5,true);
1717 $co .= glossary_end_tag("ALIASES",4,true);
1719 if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id
) ) {
1720 $co .= glossary_start_tag("CATEGORIES",4,true);
1721 foreach ($catentries as $catentry) {
1722 $category = get_record("glossary_categories","id",$catentry->categoryid
);
1724 $co .= glossary_start_tag("CATEGORY",5,true);
1725 $co .= glossary_full_tag("NAME",6,false,$category->name
);
1726 $co .= glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink
);
1727 $co .= glossary_end_tag("CATEGORY",5,true);
1729 $co .= glossary_end_tag("CATEGORIES",4,true);
1732 $co .= glossary_end_tag("ENTRY",3,true);
1735 $co .= glossary_end_tag("ENTRIES",2,true);
1740 $co .= glossary_end_tag("INFO",1,true);
1741 $co .= glossary_end_tag("GLOSSARY",0,true);
1745 /// Functions designed by Eloy Lafuente
1746 /// Functions to create, open and write header of the xml file
1748 // Read import file and convert to current charset
1749 function glossary_read_imported_file($file) {
1750 require_once "../../lib/xmlize.php";
1753 $h = fopen($file,"r");
1756 while ( !feof($h) ) {
1757 $char = fread($h,1024);
1762 return xmlize($line, 0);
1765 //Return the xml start tag
1766 function glossary_start_tag($tag,$level=0,$endline=false) {
1772 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
1775 //Return the xml end tag
1776 function glossary_end_tag($tag,$level=0,$endline=true) {
1782 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
1785 //Return the start tag, the contents and the end tag
1786 function glossary_full_tag($tag,$level=0,$endline=true,$content) {
1789 $st = glossary_start_tag($tag,$level,$endline);
1790 $co = preg_replace("/\r\n|\r/", "\n", s($content));
1791 $et = glossary_end_tag($tag,0,true);
1796 * Adding grading functions
1799 function glossary_get_ratings($entryid, $sort="u.firstname ASC") {
1800 /// Returns a list of ratings for a particular entry - sorted.
1802 return get_records_sql("SELECT u.*, r.rating, r.time
1803 FROM {$CFG->prefix}glossary_ratings r,
1804 {$CFG->prefix}user u
1805 WHERE r.entryid = '$entryid'
1810 function glossary_count_unrated_entries($glossaryid, $userid) {
1811 // How many unrated entries are in the given glossary for a given user?
1813 if ($entries = get_record_sql("SELECT count(*) as num
1814 FROM {$CFG->prefix}glossary_entries
1815 WHERE glossaryid = '$glossaryid'
1816 AND userid <> '$userid' ")) {
1818 if ($rated = get_record_sql("SELECT count(*) as num
1819 FROM {$CFG->prefix}glossary_entries e,
1820 {$CFG->prefix}glossary_ratings r
1821 WHERE e.glossaryid = '$glossaryid'
1822 AND e.id = r.entryid
1823 AND r.userid = '$userid'")) {
1824 $difference = $entries->num
- $rated->num
;
1825 if ($difference > 0) {
1828 return 0; // Just in case there was a counting error
1831 return $entries->num
;
1838 function glossary_print_ratings_mean($entryid, $scale) {
1839 /// Print the multiple ratings on a entry given to the current user by others.
1840 /// Scale is an array of ratings
1844 $mean = glossary_get_ratings_mean($entryid, $scale);
1848 if (empty($strratings)) {
1849 $strratings = get_string("ratings", "glossary");
1852 echo "$strratings: ";
1853 link_to_popup_window ("/mod/glossary/report.php?id=$entryid", "ratings", $mean, 400, 600);
1858 function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) {
1859 /// Return the mean rating of a entry given to the current user by others.
1860 /// Scale is an array of possible ratings in the scale
1861 /// Ratings is an optional simple array of actual ratings (just integers)
1865 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1866 foreach ($rates as $rate) {
1867 $ratings[] = $rate->rating
;
1872 $count = count($ratings);
1877 } else if ($count == 1) {
1878 return $scale[$ratings[0]];
1882 foreach ($ratings as $rating) {
1885 $mean = round( ((float)$total/(float)$count) +
0.001); // Little fudge factor so that 0.5 goes UP
1887 if (isset($scale[$mean])) {
1888 return $scale[$mean]." ($count)";
1890 return "$mean ($count)"; // Should never happen, hopefully
1895 function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) {
1896 /// Return a summary of entry ratings given to the current user by others.
1897 /// Scale is an array of possible ratings in the scale
1898 /// Ratings is an optional simple array of actual ratings (just integers)
1902 if ($rates = get_records("glossary_ratings", "entryid", $entryid)) {
1903 foreach ($rates as $rate) {
1904 $rating[] = $rate->rating
;
1910 if (!$count = count($ratings)) {
1915 foreach ($scale as $key => $scaleitem) {
1916 $sumrating[$key] = 0;
1919 foreach ($ratings as $rating) {
1920 $sumrating[$rating]++
;
1924 foreach ($scale as $key => $scaleitem) {
1925 $summary = $sumrating[$key].$summary;
1927 $summary = "/$summary";
1933 function glossary_print_rating_menu($entryid, $userid, $scale) {
1934 /// Print the menu of ratings as part of a larger form.
1935 /// If the entry has already been - set that value.
1936 /// Scale is an array of ratings
1940 if (!$rating = get_record("glossary_ratings", "userid", $userid, "entryid", $entryid)) {
1941 $rating->rating
= -999;
1944 if (empty($strrate)) {
1945 $strrate = get_string("rate", "glossary");
1948 choose_from_menu($scale, $entryid, $rating->rating
, "$strrate...",'',-999);
1952 function glossary_get_paging_bar($totalcount, $page, $perpage, $baseurl, $maxpageallowed=99999, $maxdisplay=20, $separator=" ", $specialtext="", $specialvalue=-1, $previousandnext = true) {
1953 // Returns the html code to represent any pagging bar. Paramenters are:
1956 // $totalcount: total number of records to be displayed
1957 // $page: page currently selected (0 based)
1958 // $perpage: number of records per page
1959 // $baseurl: url to link in each page, the string 'page=XX' will be added automatically.
1961 // $maxpageallowed: maximum number of page allowed.
1962 // $maxdisplay: maximum number of page links to show in the bar
1963 // $separator: string to be used between pages in the bar
1964 // $specialtext: string to be showed as an special link
1965 // $specialvalue: value (page) to be used in the special link
1966 // $previousandnext: to decide if we want the previous and next links
1968 // The function dinamically show the first and last pages, and "scroll" over pages.
1969 // Fully compatible with Moodle's print_paging_bar() function. Perhaps some day this
1970 // could replace the general one. ;-)
1974 $showspecial = false;
1975 $specialselected = false;
1977 //Check if we have to show the special link
1978 if (!empty($specialtext)) {
1979 $showspecial = true;
1981 //Check if we are with the special link selected
1982 if ($showspecial && $page == $specialvalue) {
1983 $specialselected = true;
1986 //If there are results (more than 1 page)
1987 if ($totalcount > $perpage) {
1988 $code .= "<div style=\"text-align:center\">";
1989 $code .= "<p>".get_string("page").":";
1991 $maxpage = (int)(($totalcount-1)/$perpage);
1993 //Lower and upper limit of page
1997 if ($page > $maxpageallowed) {
1998 $page = $maxpageallowed;
2000 if ($page > $maxpage) {
2004 //Calculate the window of pages
2005 $pagefrom = $page - ((int)($maxdisplay / 2));
2006 if ($pagefrom < 0) {
2009 $pageto = $pagefrom +
$maxdisplay - 1;
2010 if ($pageto > $maxpageallowed) {
2011 $pageto = $maxpageallowed;
2013 if ($pageto > $maxpage) {
2017 //Some movements can be necessary if don't see enought pages
2018 if ($pageto - $pagefrom < $maxdisplay - 1) {
2019 if ($pageto - $maxdisplay +
1 > 0) {
2020 $pagefrom = $pageto - $maxdisplay +
1;
2024 //Calculate first and last if necessary
2025 $firstpagecode = '';
2027 if ($pagefrom > 0) {
2028 $firstpagecode = "$separator<a href=\"{$baseurl}page=0\">1</a>";
2029 if ($pagefrom > 1) {
2030 $firstpagecode .= "$separator...";
2033 if ($pageto < $maxpage) {
2034 if ($pageto < $maxpage -1) {
2035 $lastpagecode = "$separator...";
2037 $lastpagecode .= "$separator<a href=\"{$baseurl}page=$maxpage\">".($maxpage+
1)."</a>";
2041 if ($page > 0 && $previousandnext) {
2042 $pagenum = $page - 1;
2043 $code .= " (<a href=\"{$baseurl}page=$pagenum\">".get_string("previous")."</a>) ";
2047 $code .= $firstpagecode;
2049 $pagenum = $pagefrom;
2051 //List of maxdisplay pages
2052 while ($pagenum <= $pageto) {
2053 $pagetoshow = $pagenum +
1;
2054 if ($pagenum == $page && !$specialselected) {
2055 $code .= "$separator$pagetoshow";
2057 $code .= "$separator<a href=\"{$baseurl}page=$pagenum\">$pagetoshow</a>";
2063 $code .= $lastpagecode;
2066 if ($page < $maxpage && $page < $maxpageallowed && $previousandnext) {
2067 $pagenum = $page +
1;
2068 $code .= "$separator(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
2074 if ($specialselected) {
2075 $code .= $specialtext;
2077 $code .= "$separator<a href=\"{$baseurl}page=$specialvalue\">$specialtext</a>";
2089 function glossary_get_view_actions() {
2090 return array('view','view all','view entry');
2093 function glossary_get_post_actions() {
2094 return array('add category','add comment','add entry','approve entry','delete category','delete comment','delete entry','edit category','update comment','update entry');