MDL-49231 mod_glossary: Invalid pivot when printing entries
[moodle.git] / mod / glossary / print.php
CommitLineData
5fa0208e 1<?php
2
3global $CFG;
4
5require_once("../../config.php");
6require_once("lib.php");
7
8$id = required_param('id', PARAM_INT); // Course Module ID
9$sortorder = optional_param('sortorder', 'asc', PARAM_ALPHA); // Sorting order
10$offset = optional_param('offset', 0, PARAM_INT); // number of entries to bypass
11$displayformat = optional_param('displayformat',-1, PARAM_INT);
12
13$mode = required_param('mode', PARAM_ALPHA); // mode to show the entries
f97aee76 14$hook = optional_param('hook','ALL', PARAM_CLEAN); // what to show
5fa0208e 15$sortkey = optional_param('sortkey','UPDATE', PARAM_ALPHA); // Sorting key
16
a6855934 17$url = new moodle_url('/mod/glossary/print.php', array('id'=>$id));
5fa0208e 18if ($sortorder !== 'asc') {
19 $url->param('sortorder', $sortorder);
20}
21if ($offset !== 0) {
22 $url->param('offset', $offset);
23}
24if ($displayformat !== -1) {
25 $url->param('displayformat', $displayformat);
26}
27if ($sortkey !== 'UPDATE') {
28 $url->param('sortkey', $sortkey);
29}
30if ($mode !== 'letter') {
31 $url->param('mode', $mode);
32}
33if ($hook !== 'ALL') {
34 $url->param('hook', $hook);
35}
36$PAGE->set_url($url);
37
38if (! $cm = get_coursemodule_from_id('glossary', $id)) {
39 print_error('invalidcoursemodule');
40}
41
42if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
43 print_error('coursemisconf');
44}
45
46if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
47 print_error('invalidid', 'glossary');
48}
49
50if ( !$entriesbypage = $glossary->entbypage ) {
51 $entriesbypage = $CFG->glossary_entbypage;
52}
53
5fa0208e 54require_course_login($course, true, $cm);
e0a91e11 55$context = context_module::instance($cm->id);
4f4ca7b5 56
bce3e3dc
EL
57// Prepare format_string/text options
58$fmtoptions = array(
59 'context' => $context);
60
ed5d13c0 61$PAGE->set_pagelayout('print');
2e4fd166
RW
62$PAGE->set_title(get_string("modulenameplural", "glossary"));
63$PAGE->set_heading($course->fullname);
1f1cae83
EL
64echo $OUTPUT->header();
65
5fa0208e 66if (!has_capability('mod/glossary:manageentries', $context) and !$glossary->allowprintview) {
67 notice(get_string('printviewnotallowed', 'glossary'));
68}
ec81373f 69
5fa0208e 70/// setting the default values for the display mode of the current glossary
71/// only if the glossary is viewed by the first time
72if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
73 $printpivot = $dp->showgroup;
74 if ( $mode == '' and $hook == '' and $show == '') {
75 $mode = $dp->defaultmode;
76 $hook = $dp->defaulthook;
77 $sortkey = $dp->sortkey;
78 $sortorder = $dp->sortorder;
79 }
80} else {
81 $printpivot = 1;
82 if ( $mode == '' and $hook == '' and $show == '') {
83 $mode = 'letter';
84 $hook = 'ALL';
85 }
86}
4f4ca7b5 87
5fa0208e 88if ( $displayformat == -1 ) {
89 $displayformat = $glossary->displayformat;
90}
2a252525 91
5fa0208e 92/// stablishing flag variables
93if ( $sortorder = strtolower($sortorder) ) {
94 if ($sortorder != 'asc' and $sortorder != 'desc') {
95 $sortorder = '';
ec81373f 96 }
5fa0208e 97}
98if ( $sortkey = strtoupper($sortkey) ) {
99 if ($sortkey != 'CREATION' and
100 $sortkey != 'UPDATE' and
101 $sortkey != 'FIRSTNAME' and
102 $sortkey != 'LASTNAME'
103 ) {
104 $sortkey = '';
ec81373f 105 }
5fa0208e 106}
ec81373f 107
5fa0208e 108switch ( $mode = strtolower($mode) ) {
109case 'entry': /// Looking for a certain entry id
110 $tab = GLOSSARY_STANDARD_VIEW;
111break;
ec81373f 112
5fa0208e 113case 'cat': /// Looking for a certain cat
114 $tab = GLOSSARY_CATEGORY_VIEW;
115 if ( $hook > 0 ) {
116 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
4f3e59c3 117 }
5fa0208e 118break;
4f3e59c3 119
5fa0208e 120case 'approval': /// Looking for entries waiting for approval
121 $tab = GLOSSARY_APPROVAL_VIEW;
122 if ( !$hook and !$sortkey and !$sortorder) {
123 $hook = 'ALL';
124 }
125break;
2b99ea9e 126
5fa0208e 127case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
128 $tab = GLOSSARY_STANDARD_VIEW;
129break;
71235154 130
5fa0208e 131case 'date':
132 $tab = GLOSSARY_DATE_VIEW;
133 if ( !$sortkey ) {
134 $sortkey = 'UPDATE';
41c98028 135 }
5fa0208e 136 if ( !$sortorder ) {
137 $sortorder = 'desc';
a359c29b 138 }
5fa0208e 139break;
a359c29b 140
5fa0208e 141case 'author': /// Looking for entries, browsed by author
142 $tab = GLOSSARY_AUTHOR_VIEW;
143 if ( !$hook ) {
144 $hook = 'ALL';
a359c29b 145 }
5fa0208e 146 if ( !$sortkey ) {
147 $sortkey = 'FIRSTNAME';
ea14e783 148 }
5fa0208e 149 if ( !$sortorder ) {
150 $sortorder = 'asc';
4f3e59c3 151 }
5fa0208e 152break;
4f4ca7b5 153
5fa0208e 154case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
155default:
156 $tab = GLOSSARY_STANDARD_VIEW;
157 if ( !$hook ) {
158 $hook = 'ALL';
159 }
160break;
161}
162
163include_once("sql.php");
164
165$entriesshown = 0;
166$currentpivot = '';
5fa0208e 167
168$site = $DB->get_record("course", array("id"=>1));
2d5fb2a6
RW
169
170// Print dialog link.
171$printtext = get_string('print', 'glossary');
be27bd59
RW
172$printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'glossary_no_print printicon');
173$printiconlink = html_writer::link('#', $printtext, $printlinkatt);
174echo html_writer::tag('div', $printiconlink, array('class' => 'displayprinticon'));
2d5fb2a6 175
ac9cd092
RW
176echo html_writer::tag('div', userdate(time()), array('class' => 'displaydate'));
177
178$sitename = get_string("site") . ': <span class="strong">' . format_string($site->fullname) . '</span>';
179echo html_writer::tag('div', $sitename, array('class' => 'sitename'));
180
181$coursename = get_string("course") . ': <span class="strong">' . format_string($course->fullname) . ' ('. format_string($course->shortname) . ')</span>';
182echo html_writer::tag('div', $coursename, array('class' => 'coursename'));
183
184$modname = get_string("modulename","glossary") . ': <span class="strong">' . format_string($glossary->name, true) . '</span>';
185echo html_writer::tag('div', $modname, array('class' => 'modname'));
186
5fa0208e 187if ( $allentries ) {
188 foreach ($allentries as $entry) {
189
70d20d08
FM
190 // Setting the pivot for the current entry.
191 if ($printpivot) {
192
0c48895b 193 $pivot = $entry->{$pivotkey};
70d20d08
FM
194 $upperpivot = core_text::strtoupper($pivot);
195 $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
4f4ca7b5 196
70d20d08
FM
197 // Reduce pivot to 1cc if necessary.
198 if (!$fullpivot) {
199 $upperpivot = core_text::substr($upperpivot, 0, 1);
200 $pivottoshow = core_text::substr($pivottoshow, 0, 1);
201 }
4f4ca7b5 202
70d20d08
FM
203 // If there's a group break.
204 if ($currentpivot != $upperpivot) {
5fa0208e 205 $currentpivot = $upperpivot;
ec81373f 206
70d20d08
FM
207 if ($userispivot) {
208 // Printing the user icon if defined (only when browsing authors).
209 $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
5fa0208e 210 $pivottoshow = fullname($user);
4f3e59c3 211 }
be27bd59 212 echo html_writer::tag('div', clean_text($pivottoshow), array('class' => 'mdl-align strong'));
5fa0208e 213 }
4f3e59c3 214 }
5fa0208e 215
0c758f82 216 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook, 1, $displayformat, true);
4f4ca7b5 217 }
5fa0208e 218}
2a252525 219
5fa0208e 220echo $OUTPUT->footer();