MDL-28048 glossary - add multilang support to categories
[moodle.git] / mod / glossary / view.php
CommitLineData
5fa0208e 1<?php
2
07842023 3/// This page prints a particular instance of glossary
5fa0208e 4require_once("../../config.php");
5require_once("lib.php");
786d886e 6require_once($CFG->libdir . '/completionlib.php');
5fa0208e 7require_once("$CFG->libdir/rsslib.php");
8
9$id = optional_param('id', 0, PARAM_INT); // Course Module ID
10$g = optional_param('g', 0, PARAM_INT); // Glossary ID
11
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
14
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
23
24if (!empty($id)) {
25 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
26 print_error('invalidcoursemodule');
27 }
28 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
29 print_error('coursemisconf');
30 }
31 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
32 print_error('invalidid', 'glossary');
33 }
49bcd737 34
5fa0208e 35} else if (!empty($g)) {
36 if (! $glossary = $DB->get_record("glossary", array("id"=>$g))) {
8a274bcd 37 print_error('invalidid', 'glossary');
67905ace 38 }
5fa0208e 39 if (! $course = $DB->get_record("course", array("id"=>$glossary->course))) {
40 print_error('invalidcourseid');
41 }
42 if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
43 print_error('invalidcoursemodule');
44 }
45 $id = $cm->id;
46} else {
47 print_error('invalidid', 'glossary');
48}
7856dd59 49
5fa0208e 50require_course_login($course->id, true, $cm);
51$context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 52
bce3e3dc
EL
53// Prepare format_string/text options
54$fmtoptions = array(
55 'context' => $context);
56
36051c9e
DC
57require_once($CFG->dirroot . '/comment/lib.php');
58comment::init();
59
a983f05a 60/// Loading the textlib singleton instance. We are going to need it.
5fa0208e 61$textlib = textlib_get_instance();
a983f05a 62
1ac87c73 63/// redirecting if adding a new entry
5fa0208e 64if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
65 redirect("edit.php?cmid=$cm->id&amp;mode=$mode");
66}
1ac87c73 67
68/// setting the defaut number of entries per page if not set
5fa0208e 69if ( !$entriesbypage = $glossary->entbypage ) {
70 $entriesbypage = $CFG->glossary_entbypage;
71}
1ac87c73 72
e2cf5316 73/// If we have received a page, recalculate offset
5fa0208e 74if ($page != 0 && $offset == 0) {
75 $offset = $page * $entriesbypage;
76}
e2cf5316 77
0dafa92b 78/// setting the default values for the display mode of the current glossary
79/// only if the glossary is viewed by the first time
5fa0208e 80if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
81/// Based on format->defaultmode, we build the defaulttab to be showed sometimes
82 switch ($dp->defaultmode) {
83 case 'cat':
84 $defaulttab = GLOSSARY_CATEGORY_VIEW;
85 break;
86 case 'date':
87 $defaulttab = GLOSSARY_DATE_VIEW;
88 break;
89 case 'author':
90 $defaulttab = GLOSSARY_AUTHOR_VIEW;
91 break;
92 default:
93 $defaulttab = GLOSSARY_STANDARD_VIEW;
0dafa92b 94 }
5fa0208e 95/// Fetch the rest of variables
96 $printpivot = $dp->showgroup;
97 if ( $mode == '' and $hook == '' and $show == '') {
98 $mode = $dp->defaultmode;
99 $hook = $dp->defaulthook;
100 $sortkey = $dp->sortkey;
101 $sortorder = $dp->sortorder;
fe32b4f6 102 }
5fa0208e 103} else {
104 $defaulttab = GLOSSARY_STANDARD_VIEW;
105 $printpivot = 1;
106 if ( $mode == '' and $hook == '' and $show == '') {
107 $mode = 'letter';
108 $hook = 'ALL';
0dafa92b 109 }
5fa0208e 110}
111
112if ( $displayformat == -1 ) {
113 $displayformat = $glossary->displayformat;
114}
115
116if ( $show ) {
117 $mode = 'term';
118 $hook = $show;
119 $show = '';
120}
1ac87c73 121/// Processing standard security processes
5fa0208e 122if ($course->id != SITEID) {
123 require_login($course->id);
124}
125if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
126 echo $OUTPUT->header();
127 notice(get_string("activityiscurrentlyhidden"));
128}
129add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&amp;tab=$tab", $glossary->id, $cm->id);
c197e607 130
6553cda7 131// Mark as viewed
132$completion = new completion_info($course);
133$completion->set_module_viewed($cm);
134
1ac87c73 135/// stablishing flag variables
5fa0208e 136if ( $sortorder = strtolower($sortorder) ) {
137 if ($sortorder != 'asc' and $sortorder != 'desc') {
138 $sortorder = '';
767a31c3 139 }
5fa0208e 140}
141if ( $sortkey = strtoupper($sortkey) ) {
142 if ($sortkey != 'CREATION' and
143 $sortkey != 'UPDATE' and
144 $sortkey != 'FIRSTNAME' and
145 $sortkey != 'LASTNAME'
146 ) {
147 $sortkey = '';
767a31c3 148 }
5fa0208e 149}
1d9ddaaf 150
5fa0208e 151switch ( $mode = strtolower($mode) ) {
152case 'search': /// looking for terms containing certain word(s)
153 $tab = GLOSSARY_STANDARD_VIEW;
c76e673a 154
5fa0208e 155 //Clean a bit the search string
156 $hook = trim(strip_tags($hook));
74a27855 157
5fa0208e 158break;
fe32b4f6 159
5fa0208e 160case 'entry': /// Looking for a certain entry id
161 $tab = GLOSSARY_STANDARD_VIEW;
162 if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
163 $displayformat = $dp->popupformatname;
164 }
165break;
fe32b4f6 166
5fa0208e 167case 'cat': /// Looking for a certain cat
168 $tab = GLOSSARY_CATEGORY_VIEW;
169 if ( $hook > 0 ) {
170 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
171 }
172break;
c76e673a 173
5fa0208e 174case 'approval': /// Looking for entries waiting for approval
175 $tab = GLOSSARY_APPROVAL_VIEW;
176 if ( !$hook and !$sortkey and !$sortorder) {
177 $hook = 'ALL';
178 }
179break;
1ac87c73 180
5fa0208e 181case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
182 $tab = GLOSSARY_STANDARD_VIEW;
183break;
1ac87c73 184
5fa0208e 185case 'date':
186 $tab = GLOSSARY_DATE_VIEW;
187 if ( !$sortkey ) {
188 $sortkey = 'UPDATE';
189 }
190 if ( !$sortorder ) {
191 $sortorder = 'desc';
192 }
193break;
fe32b4f6 194
5fa0208e 195case 'author': /// Looking for entries, browsed by author
196 $tab = GLOSSARY_AUTHOR_VIEW;
197 if ( !$hook ) {
198 $hook = 'ALL';
199 }
200 if ( !$sortkey ) {
201 $sortkey = 'FIRSTNAME';
202 }
203 if ( !$sortorder ) {
204 $sortorder = 'asc';
205 }
206break;
1ac87c73 207
5fa0208e 208case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
209default:
210 $tab = GLOSSARY_STANDARD_VIEW;
211 if ( !$hook ) {
212 $hook = 'ALL';
fe32b4f6 213 }
5fa0208e 214break;
215}
1ac87c73 216
5fa0208e 217switch ( $tab ) {
218case GLOSSARY_IMPORT_VIEW:
219case GLOSSARY_EXPORT_VIEW:
220case GLOSSARY_APPROVAL_VIEW:
221 $showcommonelements = 0;
222break;
fe32b4f6 223
5fa0208e 224default:
225 $showcommonelements = 1;
226break;
227}
1ac87c73 228
229/// Printing the heading
5fa0208e 230$strglossaries = get_string("modulenameplural", "glossary");
231$strglossary = get_string("modulename", "glossary");
232$strallcategories = get_string("allcategories", "glossary");
233$straddentry = get_string("addentry", "glossary");
234$strnoentries = get_string("noentries", "glossary");
5fa0208e 235$strsearchindefinition = get_string("searchindefinition", "glossary");
236$strsearch = get_string("search");
237$strwaitingapproval = get_string('waitingapproval', 'glossary');
ab397f26 238
239/// If we are in approval mode, prit special header
5fa0208e 240$PAGE->set_title(format_string($glossary->name));
2e4fd166 241$PAGE->set_heading($course->fullname);
a6855934 242$url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
5fa0208e 243if (isset($mode)) {
244 $url->param('mode', $mode);
245}
246$PAGE->set_url($url);
9e86f2e7
AD
247
248if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
249 && $glossary->rsstype && $glossary->rssarticles) {
250
251 $rsstitle = format_string($course->shortname) . ': %fullname%';
43b92251 252 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
9e86f2e7
AD
253}
254
5fa0208e 255if ($tab == GLOSSARY_APPROVAL_VIEW) {
256 require_capability('mod/glossary:approve', $context);
257 $PAGE->navbar->add($strwaitingapproval);
258 echo $OUTPUT->header();
259 echo $OUTPUT->heading($strwaitingapproval);
86342d63 260} else { /// Print standard header
5fa0208e 261 echo $OUTPUT->header();
262}
136ce8de 263
dd232d01 264/// All this depends if whe have $showcommonelements
5fa0208e 265if ($showcommonelements) {
266/// To calculate available options
267 $availableoptions = '';
268
269/// Decide about to print the import link
9e86f2e7 270 /*if (has_capability('mod/glossary:import', $context)) {
5fa0208e 271 $availableoptions = '<span class="helplink">' .
272 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
273 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
274 get_string('importentries', 'glossary') . '</a>' .
275 '</span>';
276 }
277/// Decide about to print the export link
278 if (has_capability('mod/glossary:export', $context)) {
279 if ($availableoptions) {
280 $availableoptions .= '&nbsp;/&nbsp;';
ab397f26 281 }
5fa0208e 282 $availableoptions .='<span class="helplink">' .
283 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
284 '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
285 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
286 get_string('exportentries', 'glossary') . '</a>' .
287 '</span>';
9e86f2e7 288 }*/
5fa0208e 289
290/// Decide about to print the approval link
291 if (has_capability('mod/glossary:approve', $context)) {
292 /// Check we have pending entries
293 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
136ce8de 294 if ($availableoptions) {
5fa0208e 295 $availableoptions .= '<br />';
f4646b44 296 }
136ce8de 297 $availableoptions .='<span class="helplink">' .
5fa0208e 298 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
299 '&amp;mode=approval' . '"' .
300 ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
301 get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
136ce8de 302 '</span>';
2dda0bbb 303 }
5fa0208e 304 }
5bd9aa93 305
5fa0208e 306/// Start to print glossary controls
52ea8aa0 307// print_box_start('glossarycontrol clearfix');
5fa0208e 308 echo '<div class="glossarycontrol" style="text-align: right">';
309 echo $availableoptions;
310
5fa0208e 311/// The print icon
312 if ( $showcommonelements and $mode != 'search') {
313 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
52ea8aa0 314// print_box_start('printicon');
5fa0208e 315 echo '<span class="wrap printicon">';
b5d0cafc 316 echo " <a 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\"><img class=\"icon\" src=\"".$OUTPUT->pix_url('print', 'glossary')."\" alt=\"". get_string("printerfriendly","glossary") . "\" /></a>";
5fa0208e 317 echo '</span>';
52ea8aa0 318// print_box_end();
ab397f26 319 }
5fa0208e 320 }
321/// End glossary controls
52ea8aa0 322// print_box_end(); /// glossarycontrol
5fa0208e 323 echo '</div>';
49bcd737 324
52ea8aa0 325// print_box('&nbsp;', 'clearer');
5fa0208e 326}
d3709d95 327
7dd88447 328/// Info box
5fa0208e 329if ($glossary->intro && $showcommonelements) {
330 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
331}
7dd88447 332
86183b07 333/// Search box
5fa0208e 334if ($showcommonelements ) {
335 echo '<form method="post" action="view.php">';
52159a0d 336
5fa0208e 337 echo '<table class="boxaligncenter" width="70%" border="0">';
338 echo '<tr><td align="center" class="glossarysearchbox">';
fe32b4f6 339
5fa0208e 340 echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
341 if ($mode == 'search') {
342 echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
343 } else {
344 echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
345 }
346 if ($fullsearch || $mode != 'search') {
347 $fullsearchchecked = 'checked="checked"';
348 } else {
349 $fullsearchchecked = '';
350 }
351 echo '<input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.' />';
352 echo '<input type="hidden" name="mode" value="search" />';
353 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
354 echo '<label for="fullsearch">'.$strsearchindefinition.'</label>';
355 echo '</td></tr></table>';
fe32b4f6 356
5fa0208e 357 echo '</form>';
66111853 358
5fa0208e 359 echo '<br />';
360}
66111853 361
362/// Show the add entry button if allowed
5fa0208e 363if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
364 echo '<div class="singlebutton glossaryaddentry">';
365 echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
366 echo '<div>';
367 echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
368 echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" />';
369 echo '</div>';
370 echo '</form>';
371 echo "</div>\n";
372}
66111853 373
5fa0208e 374echo '<br />';
fb443f1a 375
5fa0208e 376require("tabs.php");
07842023 377
5fa0208e 378require("sql.php");
fe32b4f6 379
1ac87c73 380/// printing the entries
5fa0208e 381$entriesshown = 0;
382$currentpivot = '';
5fa0208e 383$paging = NULL;
539f698d 384
5fa0208e 385if ($allentries) {
7fe60b0c 386
5fa0208e 387 //Decide if we must show the ALL link in the pagebar
388 $specialtext = '';
389 if ($glossary->showall) {
390 $specialtext = get_string("allentries","glossary");
391 }
db87439a 392
5fa0208e 393 //Build paging bar
394 $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);
1ac87c73 395
5fa0208e 396 echo '<div class="paging">';
397 echo $paging;
398 echo '</div>';
eee75231 399
63e87951 400 //load ratings
d251b259 401 require_once($CFG->dirroot.'/rating/lib.php');
2b04c41c
SH
402 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
403 $ratingoptions = new stdClass;
195a0848 404 $ratingoptions->context = $context;
2c2ff8d5 405 $ratingoptions->component = 'mod_glossary';
2b04c41c 406 $ratingoptions->ratingarea = 'entry';
198ff498
AD
407 $ratingoptions->items = $allentries;
408 $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
409 $ratingoptions->scaleid = $glossary->scale;
410 $ratingoptions->userid = $USER->id;
411 $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
412 $ratingoptions->assesstimestart = $glossary->assesstimestart;
413 $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
198ff498
AD
414
415 $rm = new rating_manager();
416 $allentries = $rm->get_ratings($ratingoptions);
417 }
dd232d01 418
5fa0208e 419 foreach ($allentries as $entry) {
539f698d 420
5fa0208e 421 // Setting the pivot for the current entry
422 $pivot = $entry->glossarypivot;
423 $upperpivot = $textlib->strtoupper($pivot);
bce3e3dc 424 $pivottoshow = $textlib->strtoupper(format_string($pivot, true, $fmtoptions));
5fa0208e 425 // Reduce pivot to 1cc if necessary
426 if ( !$fullpivot ) {
427 $upperpivot = $textlib->substr($upperpivot, 0, 1);
bce3e3dc 428 $pivottoshow = $textlib->substr($pivottoshow, 0, 1);
5fa0208e 429 }
539f698d 430
5fa0208e 431 // if there's a group break
432 if ( $currentpivot != $upperpivot ) {
539f698d 433
5fa0208e 434 // print the group break if apply
435 if ( $printpivot ) {
436 $currentpivot = $upperpivot;
fe32b4f6 437
5fa0208e 438 echo '<div>';
439 echo '<table cellspacing="0" class="glossarycategoryheader">';
c4a35419 440
5fa0208e 441 echo '<tr>';
5fa0208e 442 if ( isset($entry->userispivot) ) {
443 // printing the user icon if defined (only when browsing authors)
444 echo '<th align="left">';
c4a35419 445
5fa0208e 446 $user = $DB->get_record("user", array("id"=>$entry->userid));
812dbaf7 447 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
5fa0208e 448 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
449 } else {
450 echo '<th >';
539f698d 451 }
fe32b4f6 452
5fa0208e 453 echo $OUTPUT->heading($pivottoshow);
454 echo "</th></tr></table></div>\n";
455
fe32b4f6 456 }
5fa0208e 457 }
b1918034 458
5fa0208e 459 /// highlight the term if necessary
460 if ($mode == 'search') {
461 //We have to strip any word starting by + and take out words starting by -
462 //to make highlight works properly
463 $searchterms = explode(' ', $hook); // Search for words independently
464 foreach ($searchterms as $key => $searchterm) {
465 if (preg_match('/^\-/',$searchterm)) {
466 unset($searchterms[$key]);
467 } else {
468 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
469 }
470 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
471 if (strlen($searchterm) < 2) {
472 unset($searchterms[$key]);
473 }
1ac87c73 474 }
5fa0208e 475 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
476 $entry->highlight = $strippedsearch;
de1fe384 477 }
d23e2291 478
5fa0208e 479 /// and finally print the entry.
63e87951 480 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
5fa0208e 481 $entriesshown++;
63dd5fb2 482 }
5fa0208e 483}
484if ( !$entriesshown ) {
485 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
486}
487
5fa0208e 488if (!empty($formsent)) {
489 // close the form properly if used
490 echo "</div>";
491 echo "</form>";
492}
493
494if ( $paging ) {
495 echo '<hr />';
496 echo '<div class="paging">';
497 echo $paging;
498 echo '</div>';
499}
500echo '<br />';
501glossary_print_tabbed_table_end();
0dafa92b 502
db87439a 503/// Finish the page
5fa0208e 504echo $OUTPUT->footer();