weekly release 4.2.1+
[moodle.git] / mod / glossary / view.php
blob6160f954e4e6b4f20a9721043fa59590d4ff2dce
1 <?php
3 /// This page prints a particular instance of glossary
4 require_once("../../config.php");
5 require_once("lib.php");
6 require_once($CFG->libdir . '/completionlib.php');
7 require_once("$CFG->libdir/rsslib.php");
9 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
10 $g = optional_param('g', 0, PARAM_INT); // Glossary ID
12 $tab = optional_param('tab', GLOSSARY_NO_VIEW, PARAM_ALPHA); // browsing entries by categories?
13 $displayformat = optional_param('displayformat',-1, PARAM_INT); // override of the glossary display format
15 $mode = optional_param('mode', '', PARAM_ALPHA); // term entry cat date letter search author approval
16 $hook = optional_param('hook', '', PARAM_CLEAN); // the term, entry, cat, etc... to look for based on mode
17 $fullsearch = optional_param('fullsearch', 0,PARAM_INT); // full search (concept and definition) when searching?
18 $sortkey = optional_param('sortkey', '', PARAM_ALPHA);// Sorted view: CREATION | UPDATE | FIRSTNAME | LASTNAME...
19 $sortorder = optional_param('sortorder', 'ASC', PARAM_ALPHA); // it defines the order of the sorting (ASC or DESC)
20 $offset = optional_param('offset', 0,PARAM_INT); // entries to bypass (for paging purposes)
21 $page = optional_param('page', 0,PARAM_INT); // Page to show (for paging purposes)
22 $show = optional_param('show', '', PARAM_ALPHA); // [ concept | alias ] => mode=term hook=$show
24 if (!empty($id)) {
25 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
26 throw new \moodle_exception('invalidcoursemodule');
28 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
29 throw new \moodle_exception('coursemisconf');
31 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
32 throw new \moodle_exception('invalidid', 'glossary');
35 } else if (!empty($g)) {
36 if (! $glossary = $DB->get_record("glossary", array("id"=>$g))) {
37 throw new \moodle_exception('invalidid', 'glossary');
39 if (! $course = $DB->get_record("course", array("id"=>$glossary->course))) {
40 throw new \moodle_exception('invalidcourseid');
42 if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
43 throw new \moodle_exception('invalidcoursemodule');
45 $id = $cm->id;
46 } else {
47 throw new \moodle_exception('invalidid', 'glossary');
49 $cm = cm_info::create($cm);
51 require_course_login($course->id, true, $cm);
52 $context = context_module::instance($cm->id);
53 require_capability('mod/glossary:view', $context);
54 $hassecondary = $PAGE->has_secondary_navigation();
56 // Prepare format_string/text options
57 $fmtoptions = array(
58 'context' => $context);
60 require_once($CFG->dirroot . '/comment/lib.php');
61 comment::init();
63 /// redirecting if adding a new entry
64 if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
65 redirect("edit.php?cmid=$cm->id&amp;mode=$mode");
68 /// setting the defaut number of entries per page if not set
69 if ( !$entriesbypage = $glossary->entbypage ) {
70 $entriesbypage = $CFG->glossary_entbypage;
73 // If we have received a page, recalculate offset and page size.
74 $pagelimit = $entriesbypage;
75 if ($page > 0 && $offset == 0) {
76 $offset = $page * $entriesbypage;
77 } else if ($page < 0) {
78 $offset = 0;
79 $pagelimit = 0;
82 /// setting the default values for the display mode of the current glossary
83 /// only if the glossary is viewed by the first time
84 if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
85 /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
86 $showtabs = glossary_get_visible_tabs($dp);
87 switch ($dp->defaultmode) {
88 case 'cat':
89 $defaulttab = GLOSSARY_CATEGORY_VIEW;
91 // Handle defaultmode if 'category' tab is disabled. Fallback to 'standard' tab.
92 if (!in_array(GLOSSARY_CATEGORY, $showtabs)) {
93 $defaulttab = GLOSSARY_STANDARD_VIEW;
96 break;
97 case 'date':
98 $defaulttab = GLOSSARY_DATE_VIEW;
100 // Handle defaultmode if 'date' tab is disabled. Fallback to 'standard' tab.
101 if (!in_array(GLOSSARY_DATE, $showtabs)) {
102 $defaulttab = GLOSSARY_STANDARD_VIEW;
105 break;
106 case 'author':
107 $defaulttab = GLOSSARY_AUTHOR_VIEW;
109 // Handle defaultmode if 'author' tab is disabled. Fallback to 'standard' tab.
110 if (!in_array(GLOSSARY_AUTHOR, $showtabs)) {
111 $defaulttab = GLOSSARY_STANDARD_VIEW;
114 break;
115 default:
116 $defaulttab = GLOSSARY_STANDARD_VIEW;
118 /// Fetch the rest of variables
119 $printpivot = $dp->showgroup;
120 if ( $mode == '' and $hook == '' and $show == '') {
121 $mode = $dp->defaultmode;
122 $hook = $dp->defaulthook;
123 $sortkey = $dp->sortkey;
124 $sortorder = $dp->sortorder;
126 } else {
127 $defaulttab = GLOSSARY_STANDARD_VIEW;
128 $showtabs = array($defaulttab);
129 $printpivot = 1;
130 if ( $mode == '' and $hook == '' and $show == '') {
131 $mode = 'letter';
132 $hook = 'ALL';
136 if ( $displayformat == -1 ) {
137 $displayformat = $glossary->displayformat;
140 if ( $show ) {
141 $mode = 'term';
142 $hook = $show;
143 $show = '';
146 /// stablishing flag variables
147 if ( $sortorder = strtolower($sortorder) ) {
148 if ($sortorder != 'asc' and $sortorder != 'desc') {
149 $sortorder = '';
152 if ( $sortkey = strtoupper($sortkey) ) {
153 if ($sortkey != 'CREATION' and
154 $sortkey != 'UPDATE' and
155 $sortkey != 'FIRSTNAME' and
156 $sortkey != 'LASTNAME'
158 $sortkey = '';
162 switch ( $mode = strtolower($mode) ) {
163 case 'search': /// looking for terms containing certain word(s)
164 $tab = GLOSSARY_STANDARD_VIEW;
166 //Clean a bit the search string
167 $hook = trim(strip_tags($hook));
169 break;
171 case 'entry': /// Looking for a certain entry id
172 $tab = GLOSSARY_STANDARD_VIEW;
173 if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
174 $displayformat = $dp->popupformatname;
176 break;
178 case 'cat': /// Looking for a certain cat
179 $tab = GLOSSARY_CATEGORY_VIEW;
181 // Validation - we don't want to display 'category' tab if it is disabled.
182 if (!in_array(GLOSSARY_CATEGORY, $showtabs)) {
183 $tab = GLOSSARY_STANDARD_VIEW;
186 if ( $hook > 0 ) {
187 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
189 break;
191 case 'approval': /// Looking for entries waiting for approval
192 $tab = GLOSSARY_APPROVAL_VIEW;
193 // Override the display format with the approvaldisplayformat
194 if ($glossary->approvaldisplayformat !== 'default' && ($df = $DB->get_record("glossary_formats",
195 array("name" => $glossary->approvaldisplayformat)))) {
196 $displayformat = $df->popupformatname;
198 if ( !$hook and !$sortkey and !$sortorder) {
199 $hook = 'ALL';
201 break;
203 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
204 $tab = GLOSSARY_STANDARD_VIEW;
205 break;
207 case 'date':
208 $tab = GLOSSARY_DATE_VIEW;
210 // Validation - we dont want to display 'date' tab if it is disabled.
211 if (!in_array(GLOSSARY_DATE, $showtabs)) {
212 $tab = GLOSSARY_STANDARD_VIEW;
215 if ( !$sortkey ) {
216 $sortkey = 'UPDATE';
218 if ( !$sortorder ) {
219 $sortorder = 'desc';
221 break;
223 case 'author': /// Looking for entries, browsed by author
224 $tab = GLOSSARY_AUTHOR_VIEW;
226 // Validation - we dont want to display 'author' tab if it is disabled.
227 if (!in_array(GLOSSARY_AUTHOR, $showtabs)) {
228 $tab = GLOSSARY_STANDARD_VIEW;
231 if ( !$hook ) {
232 $hook = 'ALL';
234 if ( !$sortkey ) {
235 $sortkey = 'FIRSTNAME';
237 if ( !$sortorder ) {
238 $sortorder = 'asc';
240 break;
242 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
243 default:
244 $tab = GLOSSARY_STANDARD_VIEW;
245 if ( !$hook ) {
246 $hook = 'ALL';
248 break;
251 switch ( $tab ) {
252 case GLOSSARY_IMPORT_VIEW:
253 case GLOSSARY_EXPORT_VIEW:
254 case GLOSSARY_APPROVAL_VIEW:
255 $showcommonelements = 0;
256 break;
258 default:
259 $showcommonelements = 1;
260 break;
263 // Trigger module viewed event.
264 glossary_view($glossary, $course, $cm, $context, $mode);
266 /// Printing the heading
267 $strglossaries = get_string("modulenameplural", "glossary");
268 $strglossary = get_string("modulename", "glossary");
269 $strallcategories = get_string("allcategories", "glossary");
270 $straddentry = get_string("addentry", "glossary");
271 $strnoentries = get_string("noentries", "glossary");
272 $strsearchindefinition = get_string("searchindefinition", "glossary");
273 $strsearch = get_string("search");
274 $strwaitingapproval = get_string('pendingapproval', 'glossary');
276 /// If we are in approval mode, prit special header
277 $PAGE->set_title($glossary->name);
278 $PAGE->set_heading($course->fullname);
279 $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
280 if (isset($mode) && $mode) {
281 $url->param('mode', $mode);
283 $PAGE->set_url($url);
285 $renderer = $PAGE->get_renderer('mod_glossary');
286 $actionbar = new \mod_glossary\output\standard_action_bar($cm, $glossary, $dp, $mode, $hook,
287 $sortkey, $sortorder, $offset, $pagelimit, $fullsearch, $tab, $defaulttab);
288 $PAGE->force_settings_menu();
290 if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
291 && $glossary->rsstype && $glossary->rssarticles) {
293 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': '. format_string($glossary->name);
294 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
296 if ($tab == GLOSSARY_APPROVAL_VIEW) {
297 require_capability('mod/glossary:approve', $context);
298 $PAGE->navbar->add($strwaitingapproval);
301 $hassecondary = $PAGE->has_secondary_navigation();
302 if ($tab == GLOSSARY_APPROVAL_VIEW && !$hassecondary && $PAGE->activityheader->is_title_allowed()) {
303 $PAGE->activityheader->set_title(
304 $OUTPUT->heading($strwaitingapproval) .
305 $OUTPUT->heading(format_string($glossary->name))
309 if ($tab == GLOSSARY_APPROVAL_VIEW || !($glossary->intro && $showcommonelements)) {
310 $PAGE->activityheader->set_description('');
313 echo $OUTPUT->header();
314 if ($showcommonelements) {
315 echo $renderer->main_action_bar($actionbar);
318 /// All this depends if whe have $showcommonelements
319 if ($showcommonelements) {
320 /// To calculate available options
321 $availableoptions = '';
323 /// Decide about to print the import link
324 /*if (has_capability('mod/glossary:import', $context)) {
325 $availableoptions = '<span class="helplink">' .
326 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
327 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
328 get_string('importentries', 'glossary') . '</a>' .
329 '</span>';
331 /// Decide about to print the export link
332 if (has_capability('mod/glossary:export', $context)) {
333 if ($availableoptions) {
334 $availableoptions .= '&nbsp;/&nbsp;';
336 $availableoptions .='<span class="helplink">' .
337 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
338 '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
339 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
340 get_string('exportentries', 'glossary') . '</a>' .
341 '</span>';
344 /// Decide about to print the approval link
345 if (has_capability('mod/glossary:approve', $context) && !$hassecondary) {
346 /// Check we have pending entries
347 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
348 if ($availableoptions) {
349 $availableoptions .= '<br />';
351 $availableoptions .='<span class="helplink">' .
352 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
353 '&amp;mode=approval' . '"' .
354 ' title="' . s($strwaitingapproval) . '">' .
355 $strwaitingapproval . ' ('.$hiddenentries.')</a>' .
356 '</span>';
360 /// Start to print glossary controls
361 // print_box_start('glossarycontrol clearfix');
362 echo '<div class="glossarycontrol" style="text-align: right">';
363 echo $availableoptions;
365 /// End glossary controls
366 // print_box_end(); /// glossarycontrol
367 echo '</div><br />';
369 // print_box('&nbsp;', 'clearer');
372 require("tabs.php");
374 require("sql.php");
376 /// printing the entries
377 $entriesshown = 0;
378 $currentpivot = '';
379 $paging = NULL;
381 if ($allentries) {
383 //Decide if we must show the ALL link in the pagebar
384 $specialtext = '';
385 if ($glossary->showall) {
386 $specialtext = get_string("allentries","glossary");
389 //Build paging bar
390 $baseurl = new moodle_url('/mod/glossary/view.php', ['id' => $id, 'mode' => $mode, 'hook' => $hook,
391 'sortkey' => $sortkey, 'sortorder' => $sortorder, 'fullsearch' => $fullsearch]);
392 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, $baseurl->out() . '&amp;',
393 9999, 10, '&nbsp;&nbsp;', $specialtext, -1);
395 echo '<div class="paging">';
396 echo $paging;
397 echo '</div>';
399 //load ratings
400 require_once($CFG->dirroot.'/rating/lib.php');
401 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
402 $ratingoptions = new stdClass;
403 $ratingoptions->context = $context;
404 $ratingoptions->component = 'mod_glossary';
405 $ratingoptions->ratingarea = 'entry';
406 $ratingoptions->items = $allentries;
407 $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
408 $ratingoptions->scaleid = $glossary->scale;
409 $ratingoptions->userid = $USER->id;
410 $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
411 $ratingoptions->assesstimestart = $glossary->assesstimestart;
412 $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
414 $rm = new rating_manager();
415 $allentries = $rm->get_ratings($ratingoptions);
418 foreach ($allentries as $entry) {
420 // Setting the pivot for the current entry
421 if ($printpivot) {
422 $pivot = $entry->{$pivotkey};
423 $upperpivot = core_text::strtoupper($pivot);
424 $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
426 // Reduce pivot to 1cc if necessary.
427 if (!$fullpivot) {
428 $upperpivot = core_text::substr($upperpivot, 0, 1);
429 $pivottoshow = core_text::substr($pivottoshow, 0, 1);
432 // If there's a group break.
433 if ($currentpivot != $upperpivot) {
434 $currentpivot = $upperpivot;
436 // print the group break if apply
438 echo '<div>';
439 echo '<table cellspacing="0" class="glossarycategoryheader">';
441 echo '<tr>';
442 if ($userispivot) {
443 // printing the user icon if defined (only when browsing authors)
444 echo '<th align="left">';
445 $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
446 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
447 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
448 } else {
449 echo '<th >';
452 echo $OUTPUT->heading($pivottoshow, 3);
453 echo "</th></tr></table></div>\n";
457 /// highlight the term if necessary
458 if ($mode == 'search') {
459 //We have to strip any word starting by + and take out words starting by -
460 //to make highlight works properly
461 $searchterms = explode(' ', $hook); // Search for words independently
462 foreach ($searchterms as $key => $searchterm) {
463 if (preg_match('/^\-/',$searchterm)) {
464 unset($searchterms[$key]);
465 } else {
466 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
468 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
469 if (strlen($searchterm) < 2) {
470 unset($searchterms[$key]);
473 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
474 $entry->highlight = $strippedsearch;
477 /// and finally print the entry.
478 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
479 $entriesshown++;
481 // The all entries value may be a recordset or an array.
482 if ($allentries instanceof moodle_recordset) {
483 $allentries->close();
486 if ( !$entriesshown ) {
487 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
490 if (!empty($formsent)) {
491 // close the form properly if used
492 echo "</div>";
493 echo "</form>";
496 if ( $paging ) {
497 echo '<hr />';
498 echo '<div class="paging">';
499 echo $paging;
500 echo '</div>';
502 echo '<br />';
504 /// Finish the page
505 echo $OUTPUT->footer();