MDL-81574 mod_forum: Fix deprecated usage of format_text
[moodle.git] / mod / glossary / view.php
blobea61737e3219f4ef79bf095bb11bf1c297c4986e
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);
300 $hassecondary = $PAGE->has_secondary_navigation();
301 if ($tab == GLOSSARY_APPROVAL_VIEW && !$hassecondary && $PAGE->activityheader->is_title_allowed()) {
302 $PAGE->activityheader->set_title(
303 $OUTPUT->heading($strwaitingapproval) .
304 $OUTPUT->heading(format_string($glossary->name))
308 if ($tab == GLOSSARY_APPROVAL_VIEW || !($glossary->intro && $showcommonelements)) {
309 $PAGE->activityheader->set_description('');
312 echo $OUTPUT->header();
313 if ($showcommonelements) {
314 echo $renderer->main_action_bar($actionbar);
317 /// All this depends if whe have $showcommonelements
318 if ($showcommonelements) {
319 /// To calculate available options
320 $availableoptions = '';
322 /// Decide about to print the import link
323 /*if (has_capability('mod/glossary:import', $context)) {
324 $availableoptions = '<span class="helplink">' .
325 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
326 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
327 get_string('importentries', 'glossary') . '</a>' .
328 '</span>';
330 /// Decide about to print the export link
331 if (has_capability('mod/glossary:export', $context)) {
332 if ($availableoptions) {
333 $availableoptions .= '&nbsp;/&nbsp;';
335 $availableoptions .='<span class="helplink">' .
336 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
337 '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
338 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
339 get_string('exportentries', 'glossary') . '</a>' .
340 '</span>';
343 /// Decide about to print the approval link
344 if (has_capability('mod/glossary:approve', $context) && !$hassecondary) {
345 /// Check we have pending entries
346 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
347 if ($availableoptions) {
348 $availableoptions .= '<br />';
350 $availableoptions .='<span class="helplink">' .
351 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
352 '&amp;mode=approval' . '"' .
353 ' title="' . s($strwaitingapproval) . '">' .
354 $strwaitingapproval . ' ('.$hiddenentries.')</a>' .
355 '</span>';
359 /// Start to print glossary controls
360 // print_box_start('glossarycontrol clearfix');
361 echo '<div class="glossarycontrol" style="text-align: right">';
362 echo $availableoptions;
364 /// End glossary controls
365 // print_box_end(); /// glossarycontrol
366 echo '</div><br />';
368 // print_box('&nbsp;', 'clearer');
371 require("tabs.php");
373 require("sql.php");
375 /// printing the entries
376 $entriesshown = 0;
377 $currentpivot = '';
378 $paging = NULL;
380 if ($allentries) {
382 //Decide if we must show the ALL link in the pagebar
383 $specialtext = '';
384 if ($glossary->showall) {
385 $specialtext = get_string("allentries","glossary");
388 //Build paging bar
389 $baseurl = new moodle_url('/mod/glossary/view.php', ['id' => $id, 'mode' => $mode, 'hook' => $hook,
390 'sortkey' => $sortkey, 'sortorder' => $sortorder, 'fullsearch' => $fullsearch]);
391 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, $baseurl->out() . '&amp;',
392 9999, 10, '&nbsp;&nbsp;', $specialtext, -1);
394 echo '<div class="paging">';
395 echo $paging;
396 echo '</div>';
398 //load ratings
399 require_once($CFG->dirroot.'/rating/lib.php');
400 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
401 $ratingoptions = new stdClass;
402 $ratingoptions->context = $context;
403 $ratingoptions->component = 'mod_glossary';
404 $ratingoptions->ratingarea = 'entry';
405 $ratingoptions->items = $allentries;
406 $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
407 $ratingoptions->scaleid = $glossary->scale;
408 $ratingoptions->userid = $USER->id;
409 $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
410 $ratingoptions->assesstimestart = $glossary->assesstimestart;
411 $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
413 $rm = new rating_manager();
414 $allentries = $rm->get_ratings($ratingoptions);
417 foreach ($allentries as $entry) {
419 // Setting the pivot for the current entry
420 if ($printpivot) {
421 $pivot = $entry->{$pivotkey};
422 $upperpivot = core_text::strtoupper($pivot);
423 $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
425 // Reduce pivot to 1cc if necessary.
426 if (!$fullpivot) {
427 $upperpivot = core_text::substr($upperpivot, 0, 1);
428 $pivottoshow = core_text::substr($pivottoshow, 0, 1);
431 // If there's a group break.
432 if ($currentpivot != $upperpivot) {
433 $currentpivot = $upperpivot;
435 // print the group break if apply
437 echo '<div>';
438 echo '<table cellspacing="0" class="glossarycategoryheader">';
440 echo '<tr>';
441 if ($userispivot) {
442 // printing the user icon if defined (only when browsing authors)
443 echo '<th align="left">';
444 $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
445 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
446 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
447 } else {
448 echo '<th >';
451 echo $OUTPUT->heading($pivottoshow, 3);
452 echo "</th></tr></table></div>\n";
456 /// highlight the term if necessary
457 if ($mode == 'search') {
458 //We have to strip any word starting by + and take out words starting by -
459 //to make highlight works properly
460 $searchterms = explode(' ', $hook); // Search for words independently
461 foreach ($searchterms as $key => $searchterm) {
462 if (preg_match('/^\-/',$searchterm)) {
463 unset($searchterms[$key]);
464 } else {
465 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
467 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
468 if (strlen($searchterm) < 2) {
469 unset($searchterms[$key]);
472 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
473 $entry->highlight = $strippedsearch;
476 /// and finally print the entry.
477 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
478 $entriesshown++;
480 // The all entries value may be a recordset or an array.
481 if ($allentries instanceof moodle_recordset) {
482 $allentries->close();
485 if ( !$entriesshown ) {
486 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
489 if (!empty($formsent)) {
490 // close the form properly if used
491 echo "</div>";
492 echo "</form>";
495 if ( $paging ) {
496 echo '<hr />';
497 echo '<div class="paging">';
498 echo $paging;
499 echo '</div>';
501 echo '<br />';
503 /// Finish the page
504 echo $OUTPUT->footer();