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