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
25 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
26 print_error('invalidcoursemodule');
28 if (! $course = $DB->get_record("course", array("id"=>$cm->course
))) {
29 print_error('coursemisconf');
31 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance
))) {
32 print_error('invalidid', 'glossary');
35 } else if (!empty($g)) {
36 if (! $glossary = $DB->get_record("glossary", array("id"=>$g))) {
37 print_error('invalidid', 'glossary');
39 if (! $course = $DB->get_record("course", array("id"=>$glossary->course
))) {
40 print_error('invalidcourseid');
42 if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id
, $course->id
)) {
43 print_error('invalidcoursemodule');
47 print_error('invalidid', 'glossary');
50 require_course_login($course->id
, true, $cm);
51 $context = context_module
::instance($cm->id
);
52 require_capability('mod/glossary:view', $context);
54 // Prepare format_string/text options
56 'context' => $context);
58 require_once($CFG->dirroot
. '/comment/lib.php');
61 /// redirecting if adding a new entry
62 if ($tab == GLOSSARY_ADDENTRY_VIEW
) {
63 redirect("edit.php?cmid=$cm->id&mode=$mode");
66 /// setting the defaut number of entries per page if not set
67 if ( !$entriesbypage = $glossary->entbypage
) {
68 $entriesbypage = $CFG->glossary_entbypage
;
71 // If we have received a page, recalculate offset and page size.
72 $pagelimit = $entriesbypage;
73 if ($page > 0 && $offset == 0) {
74 $offset = $page * $entriesbypage;
75 } else if ($page < 0) {
80 /// setting the default values for the display mode of the current glossary
81 /// only if the glossary is viewed by the first time
82 if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat
)) ) {
83 /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
84 $showtabs = glossary_get_visible_tabs($dp);
85 switch ($dp->defaultmode
) {
87 $defaulttab = GLOSSARY_CATEGORY_VIEW
;
89 // Handle defaultmode if 'category' tab is disabled. Fallback to 'standard' tab.
90 if (!in_array(GLOSSARY_CATEGORY
, $showtabs)) {
91 $defaulttab = GLOSSARY_STANDARD_VIEW
;
96 $defaulttab = GLOSSARY_DATE_VIEW
;
98 // Handle defaultmode if 'date' tab is disabled. Fallback to 'standard' tab.
99 if (!in_array(GLOSSARY_DATE
, $showtabs)) {
100 $defaulttab = GLOSSARY_STANDARD_VIEW
;
105 $defaulttab = GLOSSARY_AUTHOR_VIEW
;
107 // Handle defaultmode if 'author' tab is disabled. Fallback to 'standard' tab.
108 if (!in_array(GLOSSARY_AUTHOR
, $showtabs)) {
109 $defaulttab = GLOSSARY_STANDARD_VIEW
;
114 $defaulttab = GLOSSARY_STANDARD_VIEW
;
116 /// Fetch the rest of variables
117 $printpivot = $dp->showgroup
;
118 if ( $mode == '' and $hook == '' and $show == '') {
119 $mode = $dp->defaultmode
;
120 $hook = $dp->defaulthook
;
121 $sortkey = $dp->sortkey
;
122 $sortorder = $dp->sortorder
;
125 $defaulttab = GLOSSARY_STANDARD_VIEW
;
126 $showtabs = array($defaulttab);
128 if ( $mode == '' and $hook == '' and $show == '') {
134 if ( $displayformat == -1 ) {
135 $displayformat = $glossary->displayformat
;
144 /// stablishing flag variables
145 if ( $sortorder = strtolower($sortorder) ) {
146 if ($sortorder != 'asc' and $sortorder != 'desc') {
150 if ( $sortkey = strtoupper($sortkey) ) {
151 if ($sortkey != 'CREATION' and
152 $sortkey != 'UPDATE' and
153 $sortkey != 'FIRSTNAME' and
154 $sortkey != 'LASTNAME'
160 switch ( $mode = strtolower($mode) ) {
161 case 'search': /// looking for terms containing certain word(s)
162 $tab = GLOSSARY_STANDARD_VIEW
;
164 //Clean a bit the search string
165 $hook = trim(strip_tags($hook));
169 case 'entry': /// Looking for a certain entry id
170 $tab = GLOSSARY_STANDARD_VIEW
;
171 if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat
)) ) {
172 $displayformat = $dp->popupformatname
;
176 case 'cat': /// Looking for a certain cat
177 $tab = GLOSSARY_CATEGORY_VIEW
;
179 // Validation - we don't want to display 'category' tab if it is disabled.
180 if (!in_array(GLOSSARY_CATEGORY
, $showtabs)) {
181 $tab = GLOSSARY_STANDARD_VIEW
;
185 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
189 case 'approval': /// Looking for entries waiting for approval
190 $tab = GLOSSARY_APPROVAL_VIEW
;
191 // Override the display format with the approvaldisplayformat
192 if ($glossary->approvaldisplayformat
!== 'default' && ($df = $DB->get_record("glossary_formats",
193 array("name" => $glossary->approvaldisplayformat
)))) {
194 $displayformat = $df->popupformatname
;
196 if ( !$hook and !$sortkey and !$sortorder) {
201 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
202 $tab = GLOSSARY_STANDARD_VIEW
;
206 $tab = GLOSSARY_DATE_VIEW
;
208 // Validation - we dont want to display 'date' tab if it is disabled.
209 if (!in_array(GLOSSARY_DATE
, $showtabs)) {
210 $tab = GLOSSARY_STANDARD_VIEW
;
221 case 'author': /// Looking for entries, browsed by author
222 $tab = GLOSSARY_AUTHOR_VIEW
;
224 // Validation - we dont want to display 'author' tab if it is disabled.
225 if (!in_array(GLOSSARY_AUTHOR
, $showtabs)) {
226 $tab = GLOSSARY_STANDARD_VIEW
;
233 $sortkey = 'FIRSTNAME';
240 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
242 $tab = GLOSSARY_STANDARD_VIEW
;
250 case GLOSSARY_IMPORT_VIEW
:
251 case GLOSSARY_EXPORT_VIEW
:
252 case GLOSSARY_APPROVAL_VIEW
:
253 $showcommonelements = 0;
257 $showcommonelements = 1;
261 // Trigger module viewed event.
262 glossary_view($glossary, $course, $cm, $context, $mode);
264 /// Printing the heading
265 $strglossaries = get_string("modulenameplural", "glossary");
266 $strglossary = get_string("modulename", "glossary");
267 $strallcategories = get_string("allcategories", "glossary");
268 $straddentry = get_string("addentry", "glossary");
269 $strnoentries = get_string("noentries", "glossary");
270 $strsearchindefinition = get_string("searchindefinition", "glossary");
271 $strsearch = get_string("search");
272 $strwaitingapproval = get_string('waitingapproval', 'glossary');
274 /// If we are in approval mode, prit special header
275 $PAGE->set_title($glossary->name
);
276 $PAGE->set_heading($course->fullname
);
277 $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id
));
279 $url->param('mode', $mode);
281 $PAGE->set_url($url);
283 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->glossary_enablerssfeeds
)
284 && $glossary->rsstype
&& $glossary->rssarticles
) {
286 $rsstitle = format_string($course->shortname
, true, array('context' => context_course
::instance($course->id
))) . ': '. format_string($glossary->name
);
287 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
290 if ($tab == GLOSSARY_APPROVAL_VIEW
) {
291 require_capability('mod/glossary:approve', $context);
292 $PAGE->navbar
->add($strwaitingapproval);
293 echo $OUTPUT->header();
294 echo $OUTPUT->heading($strwaitingapproval);
295 } else { /// Print standard header
296 echo $OUTPUT->header();
298 echo $OUTPUT->heading(format_string($glossary->name
), 2);
300 /// All this depends if whe have $showcommonelements
301 if ($showcommonelements) {
302 /// To calculate available options
303 $availableoptions = '';
305 /// Decide about to print the import link
306 /*if (has_capability('mod/glossary:import', $context)) {
307 $availableoptions = '<span class="helplink">' .
308 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
309 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
310 get_string('importentries', 'glossary') . '</a>' .
313 /// Decide about to print the export link
314 if (has_capability('mod/glossary:export', $context)) {
315 if ($availableoptions) {
316 $availableoptions .= ' / ';
318 $availableoptions .='<span class="helplink">' .
319 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
320 '&mode='.$mode . '&hook=' . urlencode($hook) . '"' .
321 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
322 get_string('exportentries', 'glossary') . '</a>' .
326 /// Decide about to print the approval link
327 if (has_capability('mod/glossary:approve', $context)) {
328 /// Check we have pending entries
329 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id
, 'approved'=>0))) {
330 if ($availableoptions) {
331 $availableoptions .= '<br />';
333 $availableoptions .='<span class="helplink">' .
334 '<a href="' . $CFG->wwwroot
. '/mod/glossary/view.php?id=' . $cm->id
.
335 '&mode=approval' . '"' .
336 ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
337 get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
342 /// Start to print glossary controls
343 // print_box_start('glossarycontrol clearfix');
344 echo '<div class="glossarycontrol" style="text-align: right">';
345 echo $availableoptions;
348 if ( $showcommonelements and $mode != 'search') {
349 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview
) {
354 'sortkey' => $sortkey,
355 'sortorder' => $sortorder,
357 'pagelimit' => $pagelimit
359 $printurl = new moodle_url('/mod/glossary/print.php', $params);
360 $printtitle = get_string('printerfriendly', 'glossary');
361 $printattributes = array(
362 'class' => 'printicon',
363 'title' => $printtitle
365 echo html_writer
::link($printurl, $printtitle, $printattributes);
368 /// End glossary controls
369 // print_box_end(); /// glossarycontrol
372 // print_box(' ', 'clearer');
376 if ($glossary->intro
&& $showcommonelements) {
377 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id
), 'generalbox', 'intro');
381 if ($showcommonelements ) {
382 echo '<form method="post" class="form form-inline m-b-1" action="' . $CFG->wwwroot
. '/mod/glossary/view.php">';
385 if ($mode == 'search') {
386 echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" class="form-control"/> ';
388 echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" class="form-control"/> ';
390 echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" class="btn btn-secondary m-r-1"/> ';
391 if ($fullsearch ||
$mode != 'search') {
392 $fullsearchchecked = 'checked="checked"';
394 $fullsearchchecked = '';
396 echo '<span class="checkbox"><label for="fullsearch">';
397 echo ' <input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.'/> ';
398 echo '<input type="hidden" name="mode" value="search" />';
399 echo '<input type="hidden" name="id" value="'.$cm->id
.'" />';
400 echo $strsearchindefinition.'</label></span>';
405 /// Show the add entry button if allowed
406 if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
407 echo '<div class="singlebutton glossaryaddentry">';
408 echo "<form class=\"form form-inline m-b-1\" id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
410 echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
411 echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" class="btn btn-secondary" />';
422 /// printing the entries
429 //Decide if we must show the ALL link in the pagebar
431 if ($glossary->showall
) {
432 $specialtext = get_string("allentries","glossary");
436 $baseurl = new moodle_url('/mod/glossary/view.php', ['id' => $id, 'mode' => $mode, 'hook' => $hook,
437 'sortkey' => $sortkey, 'sortorder' => $sortorder, 'fullsearch' => $fullsearch]);
438 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, $baseurl->out() . '&',
439 9999, 10, ' ', $specialtext, -1);
441 echo '<div class="paging">';
446 require_once($CFG->dirroot
.'/rating/lib.php');
447 if ($glossary->assessed
!= RATING_AGGREGATE_NONE
) {
448 $ratingoptions = new stdClass
;
449 $ratingoptions->context
= $context;
450 $ratingoptions->component
= 'mod_glossary';
451 $ratingoptions->ratingarea
= 'entry';
452 $ratingoptions->items
= $allentries;
453 $ratingoptions->aggregate
= $glossary->assessed
;//the aggregation method
454 $ratingoptions->scaleid
= $glossary->scale
;
455 $ratingoptions->userid
= $USER->id
;
456 $ratingoptions->returnurl
= $CFG->wwwroot
.'/mod/glossary/view.php?id='.$cm->id
;
457 $ratingoptions->assesstimestart
= $glossary->assesstimestart
;
458 $ratingoptions->assesstimefinish
= $glossary->assesstimefinish
;
460 $rm = new rating_manager();
461 $allentries = $rm->get_ratings($ratingoptions);
464 foreach ($allentries as $entry) {
466 // Setting the pivot for the current entry
468 $pivot = $entry->{$pivotkey};
469 $upperpivot = core_text
::strtoupper($pivot);
470 $pivottoshow = core_text
::strtoupper(format_string($pivot, true, $fmtoptions));
472 // Reduce pivot to 1cc if necessary.
474 $upperpivot = core_text
::substr($upperpivot, 0, 1);
475 $pivottoshow = core_text
::substr($pivottoshow, 0, 1);
478 // If there's a group break.
479 if ($currentpivot != $upperpivot) {
480 $currentpivot = $upperpivot;
482 // print the group break if apply
485 echo '<table cellspacing="0" class="glossarycategoryheader">';
489 // printing the user icon if defined (only when browsing authors)
490 echo '<th align="left">';
491 $user = mod_glossary_entry_query_builder
::get_user_from_record($entry);
492 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id
));
493 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course
::instance($course->id
)));
498 echo $OUTPUT->heading($pivottoshow, 3);
499 echo "</th></tr></table></div>\n";
503 /// highlight the term if necessary
504 if ($mode == 'search') {
505 //We have to strip any word starting by + and take out words starting by -
506 //to make highlight works properly
507 $searchterms = explode(' ', $hook); // Search for words independently
508 foreach ($searchterms as $key => $searchterm) {
509 if (preg_match('/^\-/',$searchterm)) {
510 unset($searchterms[$key]);
512 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
514 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
515 if (strlen($searchterm) < 2) {
516 unset($searchterms[$key]);
519 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
520 $entry->highlight
= $strippedsearch;
523 /// and finally print the entry.
524 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
527 // The all entries value may be a recordset or an array.
528 if ($allentries instanceof moodle_recordset
) {
529 $allentries->close();
532 if ( !$entriesshown ) {
533 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
536 if (!empty($formsent)) {
537 // close the form properly if used
544 echo '<div class="paging">';
549 glossary_print_tabbed_table_end();
552 echo $OUTPUT->footer();