Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / mod / glossary / view.php
blobfe16b5ec6caf856e63d891c8faab80ac9bcdbfed
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 = get_context_instance(CONTEXT_MODULE, $cm->id);
53 // Prepare format_string/text options
54 $fmtoptions = array(
55 'context' => $context);
57 require_once($CFG->dirroot . '/comment/lib.php');
58 comment::init();
60 /// Loading the textlib singleton instance. We are going to need it.
61 $textlib = textlib_get_instance();
63 /// redirecting if adding a new entry
64 if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
65 redirect("edit.php?cmid=$cm->id&amp;mode=$mode");
68 /// setting the defaut number of entries per page if not set
69 if ( !$entriesbypage = $glossary->entbypage ) {
70 $entriesbypage = $CFG->glossary_entbypage;
73 /// If we have received a page, recalculate offset
74 if ($page != 0 && $offset == 0) {
75 $offset = $page * $entriesbypage;
78 /// setting the default values for the display mode of the current glossary
79 /// only if the glossary is viewed by the first time
80 if ( $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;
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;
103 } else {
104 $defaulttab = GLOSSARY_STANDARD_VIEW;
105 $printpivot = 1;
106 if ( $mode == '' and $hook == '' and $show == '') {
107 $mode = 'letter';
108 $hook = 'ALL';
112 if ( $displayformat == -1 ) {
113 $displayformat = $glossary->displayformat;
116 if ( $show ) {
117 $mode = 'term';
118 $hook = $show;
119 $show = '';
121 /// Processing standard security processes
122 if ($course->id != SITEID) {
123 require_login($course->id);
125 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
126 echo $OUTPUT->header();
127 notice(get_string("activityiscurrentlyhidden"));
129 add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&amp;tab=$tab", $glossary->id, $cm->id);
131 // Mark as viewed
132 $completion = new completion_info($course);
133 $completion->set_module_viewed($cm);
135 /// stablishing flag variables
136 if ( $sortorder = strtolower($sortorder) ) {
137 if ($sortorder != 'asc' and $sortorder != 'desc') {
138 $sortorder = '';
141 if ( $sortkey = strtoupper($sortkey) ) {
142 if ($sortkey != 'CREATION' and
143 $sortkey != 'UPDATE' and
144 $sortkey != 'FIRSTNAME' and
145 $sortkey != 'LASTNAME'
147 $sortkey = '';
151 switch ( $mode = strtolower($mode) ) {
152 case 'search': /// looking for terms containing certain word(s)
153 $tab = GLOSSARY_STANDARD_VIEW;
155 //Clean a bit the search string
156 $hook = trim(strip_tags($hook));
158 break;
160 case '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;
165 break;
167 case '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));
172 break;
174 case 'approval': /// Looking for entries waiting for approval
175 $tab = GLOSSARY_APPROVAL_VIEW;
176 if ( !$hook and !$sortkey and !$sortorder) {
177 $hook = 'ALL';
179 break;
181 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
182 $tab = GLOSSARY_STANDARD_VIEW;
183 break;
185 case 'date':
186 $tab = GLOSSARY_DATE_VIEW;
187 if ( !$sortkey ) {
188 $sortkey = 'UPDATE';
190 if ( !$sortorder ) {
191 $sortorder = 'desc';
193 break;
195 case 'author': /// Looking for entries, browsed by author
196 $tab = GLOSSARY_AUTHOR_VIEW;
197 if ( !$hook ) {
198 $hook = 'ALL';
200 if ( !$sortkey ) {
201 $sortkey = 'FIRSTNAME';
203 if ( !$sortorder ) {
204 $sortorder = 'asc';
206 break;
208 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
209 default:
210 $tab = GLOSSARY_STANDARD_VIEW;
211 if ( !$hook ) {
212 $hook = 'ALL';
214 break;
217 switch ( $tab ) {
218 case GLOSSARY_IMPORT_VIEW:
219 case GLOSSARY_EXPORT_VIEW:
220 case GLOSSARY_APPROVAL_VIEW:
221 $showcommonelements = 0;
222 break;
224 default:
225 $showcommonelements = 1;
226 break;
229 /// Printing the heading
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");
235 $strsearchindefinition = get_string("searchindefinition", "glossary");
236 $strsearch = get_string("search");
237 $strwaitingapproval = get_string('waitingapproval', 'glossary');
239 /// If we are in approval mode, prit special header
240 $PAGE->set_title(format_string($glossary->name));
241 $PAGE->set_heading($course->fullname);
242 $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
243 if (isset($mode)) {
244 $url->param('mode', $mode);
246 $PAGE->set_url($url);
248 if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
249 && $glossary->rsstype && $glossary->rssarticles) {
251 $rsstitle = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))) . ': %fullname%';
252 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
255 if ($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);
260 } else { /// Print standard header
261 echo $OUTPUT->header();
264 /// All this depends if whe have $showcommonelements
265 if ($showcommonelements) {
266 /// To calculate available options
267 $availableoptions = '';
269 /// Decide about to print the import link
270 /*if (has_capability('mod/glossary:import', $context)) {
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>';
277 /// Decide about to print the export link
278 if (has_capability('mod/glossary:export', $context)) {
279 if ($availableoptions) {
280 $availableoptions .= '&nbsp;/&nbsp;';
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>';
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))) {
294 if ($availableoptions) {
295 $availableoptions .= '<br />';
297 $availableoptions .='<span class="helplink">' .
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>' .
302 '</span>';
306 /// Start to print glossary controls
307 // print_box_start('glossarycontrol clearfix');
308 echo '<div class="glossarycontrol" style="text-align: right">';
309 echo $availableoptions;
311 /// The print icon
312 if ( $showcommonelements and $mode != 'search') {
313 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
314 // print_box_start('printicon');
315 echo '<span class="wrap printicon">';
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>";
317 echo '</span>';
318 // print_box_end();
321 /// End glossary controls
322 // print_box_end(); /// glossarycontrol
323 echo '</div>';
325 // print_box('&nbsp;', 'clearer');
328 /// Info box
329 if ($glossary->intro && $showcommonelements) {
330 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
333 /// Search box
334 if ($showcommonelements ) {
335 echo '<form method="post" action="view.php">';
337 echo '<table class="boxaligncenter" width="70%" border="0">';
338 echo '<tr><td align="center" class="glossarysearchbox">';
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.'" /> ';
346 if ($fullsearch || $mode != 'search') {
347 $fullsearchchecked = 'checked="checked"';
348 } else {
349 $fullsearchchecked = '';
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>';
357 echo '</form>';
359 echo '<br />';
362 /// Show the add entry button if allowed
363 if (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";
374 echo '<br />';
376 require("tabs.php");
378 require("sql.php");
380 /// printing the entries
381 $entriesshown = 0;
382 $currentpivot = '';
383 $paging = NULL;
385 if ($allentries) {
387 //Decide if we must show the ALL link in the pagebar
388 $specialtext = '';
389 if ($glossary->showall) {
390 $specialtext = get_string("allentries","glossary");
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);
396 echo '<div class="paging">';
397 echo $paging;
398 echo '</div>';
400 //load ratings
401 require_once($CFG->dirroot.'/rating/lib.php');
402 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
403 $ratingoptions = new stdClass;
404 $ratingoptions->context = $context;
405 $ratingoptions->component = 'mod_glossary';
406 $ratingoptions->ratingarea = 'entry';
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;
415 $rm = new rating_manager();
416 $allentries = $rm->get_ratings($ratingoptions);
419 foreach ($allentries as $entry) {
421 // Setting the pivot for the current entry
422 $pivot = $entry->glossarypivot;
423 $upperpivot = $textlib->strtoupper($pivot);
424 $pivottoshow = $textlib->strtoupper(format_string($pivot, true, $fmtoptions));
425 // Reduce pivot to 1cc if necessary
426 if ( !$fullpivot ) {
427 $upperpivot = $textlib->substr($upperpivot, 0, 1);
428 $pivottoshow = $textlib->substr($pivottoshow, 0, 1);
431 // if there's a group break
432 if ( $currentpivot != $upperpivot ) {
434 // print the group break if apply
435 if ( $printpivot ) {
436 $currentpivot = $upperpivot;
438 echo '<div>';
439 echo '<table cellspacing="0" class="glossarycategoryheader">';
441 echo '<tr>';
442 if ( isset($entry->userispivot) ) {
443 // printing the user icon if defined (only when browsing authors)
444 echo '<th align="left">';
446 $user = $DB->get_record("user", array("id"=>$entry->userid));
447 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
448 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
449 } else {
450 echo '<th >';
453 echo $OUTPUT->heading($pivottoshow);
454 echo "</th></tr></table></div>\n";
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);
470 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
471 if (strlen($searchterm) < 2) {
472 unset($searchterms[$key]);
475 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
476 $entry->highlight = $strippedsearch;
479 /// and finally print the entry.
480 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
481 $entriesshown++;
484 if ( !$entriesshown ) {
485 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
488 if (!empty($formsent)) {
489 // close the form properly if used
490 echo "</div>";
491 echo "</form>";
494 if ( $paging ) {
495 echo '<hr />';
496 echo '<div class="paging">';
497 echo $paging;
498 echo '</div>';
500 echo '<br />';
501 glossary_print_tabbed_table_end();
503 /// Finish the page
504 echo $OUTPUT->footer();