MDL-45144 convert book events to create_from_xxx() helpers
[moodle.git] / mod / glossary / view.php
blob43a7ba89de3a387ad55e3ee6ec896f400dda3d30
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 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');
45 $id = $cm->id;
46 } else {
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
55 $fmtoptions = array(
56 'context' => $context);
58 require_once($CFG->dirroot . '/comment/lib.php');
59 comment::init();
61 /// redirecting if adding a new entry
62 if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
63 redirect("edit.php?cmid=$cm->id&amp;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
72 if ($page != 0 && $offset == 0) {
73 $offset = $page * $entriesbypage;
76 /// setting the default values for the display mode of the current glossary
77 /// only if the glossary is viewed by the first time
78 if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
79 /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
80 switch ($dp->defaultmode) {
81 case 'cat':
82 $defaulttab = GLOSSARY_CATEGORY_VIEW;
83 break;
84 case 'date':
85 $defaulttab = GLOSSARY_DATE_VIEW;
86 break;
87 case 'author':
88 $defaulttab = GLOSSARY_AUTHOR_VIEW;
89 break;
90 default:
91 $defaulttab = GLOSSARY_STANDARD_VIEW;
93 /// Fetch the rest of variables
94 $printpivot = $dp->showgroup;
95 if ( $mode == '' and $hook == '' and $show == '') {
96 $mode = $dp->defaultmode;
97 $hook = $dp->defaulthook;
98 $sortkey = $dp->sortkey;
99 $sortorder = $dp->sortorder;
101 } else {
102 $defaulttab = GLOSSARY_STANDARD_VIEW;
103 $printpivot = 1;
104 if ( $mode == '' and $hook == '' and $show == '') {
105 $mode = 'letter';
106 $hook = 'ALL';
110 if ( $displayformat == -1 ) {
111 $displayformat = $glossary->displayformat;
114 if ( $show ) {
115 $mode = 'term';
116 $hook = $show;
117 $show = '';
119 /// Processing standard security processes
120 if ($course->id != SITEID) {
121 require_login($course);
123 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
124 echo $OUTPUT->header();
125 notice(get_string("activityiscurrentlyhidden"));
128 /// stablishing flag variables
129 if ( $sortorder = strtolower($sortorder) ) {
130 if ($sortorder != 'asc' and $sortorder != 'desc') {
131 $sortorder = '';
134 if ( $sortkey = strtoupper($sortkey) ) {
135 if ($sortkey != 'CREATION' and
136 $sortkey != 'UPDATE' and
137 $sortkey != 'FIRSTNAME' and
138 $sortkey != 'LASTNAME'
140 $sortkey = '';
144 switch ( $mode = strtolower($mode) ) {
145 case 'search': /// looking for terms containing certain word(s)
146 $tab = GLOSSARY_STANDARD_VIEW;
148 //Clean a bit the search string
149 $hook = trim(strip_tags($hook));
151 break;
153 case 'entry': /// Looking for a certain entry id
154 $tab = GLOSSARY_STANDARD_VIEW;
155 if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
156 $displayformat = $dp->popupformatname;
158 break;
160 case 'cat': /// Looking for a certain cat
161 $tab = GLOSSARY_CATEGORY_VIEW;
162 if ( $hook > 0 ) {
163 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
165 break;
167 case 'approval': /// Looking for entries waiting for approval
168 $tab = GLOSSARY_APPROVAL_VIEW;
169 // Override the display format with the approvaldisplayformat
170 if ($glossary->approvaldisplayformat !== 'default' && ($df = $DB->get_record("glossary_formats",
171 array("name" => $glossary->approvaldisplayformat)))) {
172 $displayformat = $df->popupformatname;
174 if ( !$hook and !$sortkey and !$sortorder) {
175 $hook = 'ALL';
177 break;
179 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
180 $tab = GLOSSARY_STANDARD_VIEW;
181 break;
183 case 'date':
184 $tab = GLOSSARY_DATE_VIEW;
185 if ( !$sortkey ) {
186 $sortkey = 'UPDATE';
188 if ( !$sortorder ) {
189 $sortorder = 'desc';
191 break;
193 case 'author': /// Looking for entries, browsed by author
194 $tab = GLOSSARY_AUTHOR_VIEW;
195 if ( !$hook ) {
196 $hook = 'ALL';
198 if ( !$sortkey ) {
199 $sortkey = 'FIRSTNAME';
201 if ( !$sortorder ) {
202 $sortorder = 'asc';
204 break;
206 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
207 default:
208 $tab = GLOSSARY_STANDARD_VIEW;
209 if ( !$hook ) {
210 $hook = 'ALL';
212 break;
215 switch ( $tab ) {
216 case GLOSSARY_IMPORT_VIEW:
217 case GLOSSARY_EXPORT_VIEW:
218 case GLOSSARY_APPROVAL_VIEW:
219 $showcommonelements = 0;
220 break;
222 default:
223 $showcommonelements = 1;
224 break;
227 // Trigger module viewed event.
228 $event = \mod_glossary\event\course_module_viewed::create(array(
229 'objectid' => $glossary->id,
230 'context' => $context,
231 'other' => array('mode' => $mode)
233 $event->add_record_snapshot('course', $course);
234 $event->add_record_snapshot('course_modules', $cm);
235 $event->add_record_snapshot('glossary', $glossary);
236 $event->trigger();
238 // Mark as viewed
239 $completion = new completion_info($course);
240 $completion->set_module_viewed($cm);
242 /// Printing the heading
243 $strglossaries = get_string("modulenameplural", "glossary");
244 $strglossary = get_string("modulename", "glossary");
245 $strallcategories = get_string("allcategories", "glossary");
246 $straddentry = get_string("addentry", "glossary");
247 $strnoentries = get_string("noentries", "glossary");
248 $strsearchindefinition = get_string("searchindefinition", "glossary");
249 $strsearch = get_string("search");
250 $strwaitingapproval = get_string('waitingapproval', 'glossary');
252 /// If we are in approval mode, prit special header
253 $PAGE->set_title($glossary->name);
254 $PAGE->set_heading($course->fullname);
255 $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
256 if (isset($mode)) {
257 $url->param('mode', $mode);
259 $PAGE->set_url($url);
261 if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
262 && $glossary->rsstype && $glossary->rssarticles) {
264 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': '. format_string($glossary->name);
265 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
268 if ($tab == GLOSSARY_APPROVAL_VIEW) {
269 require_capability('mod/glossary:approve', $context);
270 $PAGE->navbar->add($strwaitingapproval);
271 echo $OUTPUT->header();
272 echo $OUTPUT->heading($strwaitingapproval);
273 } else { /// Print standard header
274 echo $OUTPUT->header();
276 echo $OUTPUT->heading(format_string($glossary->name), 2);
278 /// All this depends if whe have $showcommonelements
279 if ($showcommonelements) {
280 /// To calculate available options
281 $availableoptions = '';
283 /// Decide about to print the import link
284 /*if (has_capability('mod/glossary:import', $context)) {
285 $availableoptions = '<span class="helplink">' .
286 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
287 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
288 get_string('importentries', 'glossary') . '</a>' .
289 '</span>';
291 /// Decide about to print the export link
292 if (has_capability('mod/glossary:export', $context)) {
293 if ($availableoptions) {
294 $availableoptions .= '&nbsp;/&nbsp;';
296 $availableoptions .='<span class="helplink">' .
297 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
298 '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
299 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
300 get_string('exportentries', 'glossary') . '</a>' .
301 '</span>';
304 /// Decide about to print the approval link
305 if (has_capability('mod/glossary:approve', $context)) {
306 /// Check we have pending entries
307 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
308 if ($availableoptions) {
309 $availableoptions .= '<br />';
311 $availableoptions .='<span class="helplink">' .
312 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
313 '&amp;mode=approval' . '"' .
314 ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
315 get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
316 '</span>';
320 /// Start to print glossary controls
321 // print_box_start('glossarycontrol clearfix');
322 echo '<div class="glossarycontrol" style="text-align: right">';
323 echo $availableoptions;
325 /// The print icon
326 if ( $showcommonelements and $mode != 'search') {
327 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
328 echo " <a class='printicon' title =\"". get_string("printerfriendly","glossary") ."\" href=\"print.php?id=$cm->id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;offset=$offset\">" . get_string("printerfriendly","glossary")."</a>";
331 /// End glossary controls
332 // print_box_end(); /// glossarycontrol
333 echo '</div>';
335 // print_box('&nbsp;', 'clearer');
338 /// Info box
339 if ($glossary->intro && $showcommonelements) {
340 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
343 /// Search box
344 if ($showcommonelements ) {
345 echo '<form method="post" action="view.php">';
347 echo '<table class="boxaligncenter" width="70%" border="0">';
348 echo '<tr><td align="center" class="glossarysearchbox">';
350 echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
351 if ($mode == 'search') {
352 echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
353 } else {
354 echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
356 if ($fullsearch || $mode != 'search') {
357 $fullsearchchecked = 'checked="checked"';
358 } else {
359 $fullsearchchecked = '';
361 echo '<input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.' />';
362 echo '<input type="hidden" name="mode" value="search" />';
363 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
364 echo '<label for="fullsearch">'.$strsearchindefinition.'</label>';
365 echo '</td></tr></table>';
367 echo '</form>';
369 echo '<br />';
372 /// Show the add entry button if allowed
373 if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
374 echo '<div class="singlebutton glossaryaddentry">';
375 echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
376 echo '<div>';
377 echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
378 echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" />';
379 echo '</div>';
380 echo '</form>';
381 echo "</div>\n";
384 echo '<br />';
386 require("tabs.php");
388 require("sql.php");
390 /// printing the entries
391 $entriesshown = 0;
392 $currentpivot = '';
393 $paging = NULL;
395 if ($allentries) {
397 //Decide if we must show the ALL link in the pagebar
398 $specialtext = '';
399 if ($glossary->showall) {
400 $specialtext = get_string("allentries","glossary");
403 //Build paging bar
404 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, "view.php?id=$id&amp;mode=$mode&amp;hook=".urlencode($hook)."&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;fullsearch=$fullsearch&amp;",9999,10,'&nbsp;&nbsp;', $specialtext, -1);
406 echo '<div class="paging">';
407 echo $paging;
408 echo '</div>';
410 //load ratings
411 require_once($CFG->dirroot.'/rating/lib.php');
412 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
413 $ratingoptions = new stdClass;
414 $ratingoptions->context = $context;
415 $ratingoptions->component = 'mod_glossary';
416 $ratingoptions->ratingarea = 'entry';
417 $ratingoptions->items = $allentries;
418 $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
419 $ratingoptions->scaleid = $glossary->scale;
420 $ratingoptions->userid = $USER->id;
421 $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
422 $ratingoptions->assesstimestart = $glossary->assesstimestart;
423 $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
425 $rm = new rating_manager();
426 $allentries = $rm->get_ratings($ratingoptions);
429 foreach ($allentries as $entry) {
431 // Setting the pivot for the current entry
432 $pivot = $entry->glossarypivot;
433 $upperpivot = core_text::strtoupper($pivot);
434 $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
435 // Reduce pivot to 1cc if necessary
436 if ( !$fullpivot ) {
437 $upperpivot = core_text::substr($upperpivot, 0, 1);
438 $pivottoshow = core_text::substr($pivottoshow, 0, 1);
441 // if there's a group break
442 if ( $currentpivot != $upperpivot ) {
444 // print the group break if apply
445 if ( $printpivot ) {
446 $currentpivot = $upperpivot;
448 echo '<div>';
449 echo '<table cellspacing="0" class="glossarycategoryheader">';
451 echo '<tr>';
452 if ( isset($entry->userispivot) ) {
453 // printing the user icon if defined (only when browsing authors)
454 echo '<th align="left">';
456 $user = $DB->get_record("user", array("id"=>$entry->userid));
457 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
458 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
459 } else {
460 echo '<th >';
463 echo $OUTPUT->heading($pivottoshow, 3);
464 echo "</th></tr></table></div>\n";
469 /// highlight the term if necessary
470 if ($mode == 'search') {
471 //We have to strip any word starting by + and take out words starting by -
472 //to make highlight works properly
473 $searchterms = explode(' ', $hook); // Search for words independently
474 foreach ($searchterms as $key => $searchterm) {
475 if (preg_match('/^\-/',$searchterm)) {
476 unset($searchterms[$key]);
477 } else {
478 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
480 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
481 if (strlen($searchterm) < 2) {
482 unset($searchterms[$key]);
485 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
486 $entry->highlight = $strippedsearch;
489 /// and finally print the entry.
490 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
491 $entriesshown++;
494 if ( !$entriesshown ) {
495 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
498 if (!empty($formsent)) {
499 // close the form properly if used
500 echo "</div>";
501 echo "</form>";
504 if ( $paging ) {
505 echo '<hr />';
506 echo '<div class="paging">';
507 echo $paging;
508 echo '</div>';
510 echo '<br />';
511 glossary_print_tabbed_table_end();
513 /// Finish the page
514 echo $OUTPUT->footer();