MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / glossary / view.php
blob44f99b56814f6c498680da4729a7b4311d9f5079
1 <?php // $Id$
2 /// This page prints a particular instance of glossary
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->libdir/rsslib.php");
7 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
8 $g = optional_param('g', 0, PARAM_INT); // Glossary ID
10 $tab = optional_param('tab', GLOSSARY_NO_VIEW, PARAM_ALPHA); // browsing entries by categories?
11 $displayformat = optional_param('displayformat',-1, PARAM_INT); // override of the glossary display format
13 $mode = optional_param('mode', '', PARAM_ALPHA); // term entry cat date letter search author approval
14 $hook = optional_param('hook', '', PARAM_CLEAN); // the term, entry, cat, etc... to look for based on mode
15 $fullsearch = optional_param('fullsearch', 0,PARAM_INT); // full search (concept and definition) when searching?
16 $sortkey = optional_param('sortkey', '', PARAM_ALPHA);// Sorted view: CREATION | UPDATE | FIRSTNAME | LASTNAME...
17 $sortorder = optional_param('sortorder', 'ASC', PARAM_ALPHA); // it defines the order of the sorting (ASC or DESC)
18 $offset = optional_param('offset', 0,PARAM_INT); // entries to bypass (for paging purposes)
19 $page = optional_param('page', 0,PARAM_INT); // Page to show (for paging purposes)
20 $show = optional_param('show', '', PARAM_ALPHA); // [ concept | alias ] => mode=term hook=$show
22 if (!empty($id)) {
23 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
24 error("Course Module ID was incorrect");
26 if (! $course = get_record("course", "id", $cm->course)) {
27 error("Course is misconfigured");
29 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
30 error("Course module is incorrect");
32 } else if (!empty($g)) {
33 if (! $glossary = get_record("glossary", "id", $g)) {
34 error("Course module is incorrect");
36 if (! $course = get_record("course", "id", $glossary->course)) {
37 error("Could not determine which course this belonged to!");
39 if (!$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id)) {
40 error("Could not determine which course module this belonged to!");
42 $id = $cm->id;
43 } else {
44 error("Must specify glossary ID or course module ID");
47 require_course_login($course->id, true, $cm);
48 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
50 /// Loading the textlib singleton instance. We are going to need it.
51 $textlib = textlib_get_instance();
53 /// redirecting if adding a new entry
54 if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
55 redirect("edit.php?id=$cm->id&amp;mode=$mode");
58 /// setting the defaut number of entries per page if not set
60 if ( !$entriesbypage = $glossary->entbypage ) {
61 $entriesbypage = $CFG->glossary_entbypage;
64 /// If we have received a page, recalculate offset
65 if ($page != 0 && $offset == 0) {
66 $offset = $page * $entriesbypage;
69 /// setting the default values for the display mode of the current glossary
70 /// only if the glossary is viewed by the first time
71 if ( $dp = get_record('glossary_formats','name', addslashes($glossary->displayformat)) ) {
72 /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
73 switch ($dp->defaultmode) {
74 case 'cat':
75 $defaulttab = GLOSSARY_CATEGORY_VIEW;
76 break;
77 case 'date':
78 $defaulttab = GLOSSARY_DATE_VIEW;
79 break;
80 case 'author':
81 $defaulttab = GLOSSARY_AUTHOR_VIEW;
82 break;
83 default:
84 $defaulttab = GLOSSARY_STANDARD_VIEW;
86 /// Fetch the rest of variables
87 $printpivot = $dp->showgroup;
88 if ( $mode == '' and $hook == '' and $show == '') {
89 $mode = $dp->defaultmode;
90 $hook = $dp->defaulthook;
91 $sortkey = $dp->sortkey;
92 $sortorder = $dp->sortorder;
94 } else {
95 $defaulttab = GLOSSARY_STANDARD_VIEW;
96 $printpivot = 1;
97 if ( $mode == '' and $hook == '' and $show == '') {
98 $mode = 'letter';
99 $hook = 'ALL';
103 if ( $displayformat == -1 ) {
104 $displayformat = $glossary->displayformat;
107 if ( $show ) {
108 $mode = 'term';
109 $hook = $show;
110 $show = '';
112 /// Processing standard security processes
113 if ($course->id != SITEID) {
114 require_login($course->id);
116 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
117 print_header();
118 notice(get_string("activityiscurrentlyhidden"));
120 add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&amp;tab=$tab", $glossary->id, $cm->id);
122 /// stablishing flag variables
123 if ( $sortorder = strtolower($sortorder) ) {
124 if ($sortorder != 'asc' and $sortorder != 'desc') {
125 $sortorder = '';
128 if ( $sortkey = strtoupper($sortkey) ) {
129 if ($sortkey != 'CREATION' and
130 $sortkey != 'UPDATE' and
131 $sortkey != 'FIRSTNAME' and
132 $sortkey != 'LASTNAME'
134 $sortkey = '';
138 switch ( $mode = strtolower($mode) ) {
139 case 'search': /// looking for terms containing certain word(s)
140 $tab = GLOSSARY_STANDARD_VIEW;
142 //Clean a bit the search string
143 $hook = trim(strip_tags($hook));
145 break;
147 case 'entry': /// Looking for a certain entry id
148 $tab = GLOSSARY_STANDARD_VIEW;
149 if ( $dp = get_record("glossary_formats","name", $glossary->displayformat) ) {
150 $displayformat = $dp->popupformatname;
152 break;
154 case 'cat': /// Looking for a certain cat
155 $tab = GLOSSARY_CATEGORY_VIEW;
156 if ( $hook > 0 ) {
157 $category = get_record("glossary_categories","id",$hook);
159 break;
161 case 'approval': /// Looking for entries waiting for approval
162 $tab = GLOSSARY_APPROVAL_VIEW;
163 if ( !$hook and !$sortkey and !$sortorder) {
164 $hook = 'ALL';
166 break;
168 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
169 $tab = GLOSSARY_STANDARD_VIEW;
170 break;
172 case 'date':
173 $tab = GLOSSARY_DATE_VIEW;
174 if ( !$sortkey ) {
175 $sortkey = 'UPDATE';
177 if ( !$sortorder ) {
178 $sortorder = 'desc';
180 break;
182 case 'author': /// Looking for entries, browsed by author
183 $tab = GLOSSARY_AUTHOR_VIEW;
184 if ( !$hook ) {
185 $hook = 'ALL';
187 if ( !$sortkey ) {
188 $sortkey = 'FIRSTNAME';
190 if ( !$sortorder ) {
191 $sortorder = 'asc';
193 break;
195 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
196 default:
197 $tab = GLOSSARY_STANDARD_VIEW;
198 if ( !$hook ) {
199 $hook = 'ALL';
201 break;
204 switch ( $tab ) {
205 case GLOSSARY_IMPORT_VIEW:
206 case GLOSSARY_EXPORT_VIEW:
207 case GLOSSARY_APPROVAL_VIEW:
208 $showcommonelements = 0;
209 break;
211 default:
212 $showcommonelements = 1;
213 break;
216 /// Printing the heading
217 $strglossaries = get_string("modulenameplural", "glossary");
218 $strglossary = get_string("modulename", "glossary");
219 $strallcategories = get_string("allcategories", "glossary");
220 $straddentry = get_string("addentry", "glossary");
221 $strnoentries = get_string("noentries", "glossary");
222 $strsearchconcept = get_string("searchconcept", "glossary");
223 $strsearchindefinition = get_string("searchindefinition", "glossary");
224 $strsearch = get_string("search");
225 $strwaitingapproval = get_string('waitingapproval', 'glossary');
227 $navlinks = array();
228 $navlinks[] = array('name' => $strglossaries, 'link' => "index.php?id=$course->id", 'type' => 'activity');
229 $navlinks[] = array('name' => format_string($glossary->name), 'link' => "view.php?id=$id", 'type' => 'activityinstance');
231 /// If we are in approval mode, prit special header
232 if ($tab == GLOSSARY_APPROVAL_VIEW) {
233 require_capability('mod/glossary:approve', $context);
235 $navlinks[] = array('name' => $strwaitingapproval, 'link' => '', 'type' => 'title');
236 $navigation = build_navigation($navlinks);
238 print_header_simple(format_string($glossary->name), "", $navigation, "", "", true,
239 update_module_button($cm->id, $course->id, $strglossary), navmenu($course, $cm));
241 print_heading($strwaitingapproval);
242 } else { /// Print standard header
243 $navigation = build_navigation($navlinks);
244 print_header_simple(format_string($glossary->name), "", $navigation, "", "", true,
245 update_module_button($cm->id, $course->id, $strglossary), navmenu($course, $cm));
248 /// All this depends if whe have $showcommonelements
249 if ($showcommonelements) {
250 /// To calculate available options
251 $availableoptions = '';
253 /// Decide about to print the import link
254 if (has_capability('mod/glossary:import', $context)) {
255 $availableoptions = '<span class="helplink">' .
256 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
257 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
258 get_string('importentries', 'glossary') . '</a>' .
259 '</span>';
261 /// Decide about to print the export link
262 if (has_capability('mod/glossary:export', $context)) {
263 if ($availableoptions) {
264 $availableoptions .= '&nbsp;/&nbsp;';
266 $availableoptions .='<span class="helplink">' .
267 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
268 '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
269 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
270 get_string('exportentries', 'glossary') . '</a>' .
271 '</span>';
274 /// Decide about to print the approval link
275 if (has_capability('mod/glossary:approve', $context)) {
276 /// Check we have pending entries
277 if ($hiddenentries = count_records_select('glossary_entries',"glossaryid = $glossary->id and approved = 0")) {
278 if ($availableoptions) {
279 $availableoptions .= '<br />';
281 $availableoptions .='<span class="helplink">' .
282 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
283 '&amp;mode=approval' . '"' .
284 ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
285 get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
286 '</span>';
290 /// Start to print glossary controls
291 print_box_start('glossarycontrol');
292 echo $availableoptions;
294 /// If rss are activated at site and glossary level and this glossary has rss defined, show link
295 if (isset($CFG->enablerssfeeds) && isset($CFG->glossary_enablerssfeeds) &&
296 $CFG->enablerssfeeds && $CFG->glossary_enablerssfeeds && $glossary->rsstype && $glossary->rssarticles) {
298 $tooltiptext = get_string("rsssubscriberss","glossary",format_string($glossary->name,true));
299 if (empty($USER->id)) {
300 $userid = 0;
301 } else {
302 $userid = $USER->id;
304 print_box_start('rsslink');
305 rss_print_link($course->id, $userid, "glossary", $glossary->id, $tooltiptext);
306 print_box_end();
309 /// The print icon
310 if ( $showcommonelements and $mode != 'search') {
311 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
312 print_box_start('printicon');
313 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=\"print.gif\" alt=\"". get_string("printerfriendly","glossary") . "\" /></a>";
314 print_box_end();
317 /// End glossary controls
318 print_box_end(); /// glossarycontrol
320 print_box('&nbsp;', 'clearer');
323 /// Info box
324 if ( $glossary->intro && $showcommonelements ) {
325 print_box(format_text($glossary->intro), 'generalbox', 'intro');
328 /// Search box
329 if ($showcommonelements ) {
330 echo '<form method="post" action="view.php">';
332 echo '<table class="boxaligncenter" width="70%" border="0">';
333 echo '<tr><td align="center" class="glossarysearchbox">';
335 echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
336 if ($mode == 'search') {
337 echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
338 } else {
339 echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
341 if ($fullsearch || $mode != 'search') {
342 $fullsearchchecked = 'checked="checked"';
343 } else {
344 $fullsearchchecked = '';
346 echo '<input type="checkbox" name="fullsearch" value="1" '.$fullsearchchecked.' alt="'.$strsearchindefinition.'" />';
347 echo '<input type="hidden" name="mode" value="search" />';
348 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
349 echo $strsearchindefinition;
350 echo '</td></tr></table>';
352 echo '</form>';
354 echo '<br />';
357 /// Show the add entry button if allowed
358 if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
359 echo '<div class="singlebutton glossaryaddentry">';
360 echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
361 echo '<div>';
362 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
363 echo '<input type="submit" value="';
364 print_string('addentry', 'glossary');
365 echo '" />';
366 echo '</div>';
367 echo '</form>';
368 echo "</div>\n";
371 echo '<br />';
373 include("tabs.php");
375 include_once("sql.php");
377 /// printing the entries
378 $entriesshown = 0;
379 $currentpivot = '';
380 $ratingsmenuused = NULL;
381 $paging = NULL;
383 if ($allentries) {
385 //Decide if we must show the ALL link in the pagebar
386 $specialtext = '';
387 if ($glossary->showall) {
388 $specialtext = get_string("allentries","glossary");
391 //Build paging bar
392 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, "view.php?id=$id&amp;mode=$mode&amp;hook=$hook&amp;sortkey=$sortkey&amp;sortorder=$sortorder&amp;fullsearch=$fullsearch&amp;",9999,10,'&nbsp;&nbsp;', $specialtext, -1);
394 echo '<div class="paging">';
395 echo $paging;
396 echo '</div>';
398 $ratings = NULL;
399 $ratingsmenuused = false;
400 if ($glossary->assessed and isloggedin() and !isguestuser()) {
401 $ratings = new object();
402 if ($ratings->scale = make_grades_menu($glossary->scale)) {
403 $ratings->assesstimestart = $glossary->assesstimestart;
404 $ratings->assesstimefinish = $glossary->assesstimefinish;
406 if ($glossary->assessed == 2 and !has_capability('mod/glossary:rate', $context)) {
407 $ratings->allow = false;
408 } else {
409 $ratings->allow = true;
411 $formsent = 1;
413 echo "<form method=\"post\" action=\"rate.php\">";
414 echo "<div>";
415 echo "<input type=\"hidden\" name=\"glossaryid\" value=\"$glossary->id\" />";
418 foreach ($allentries as $entry) {
420 // Setting the pivot for the current entry
421 $pivot = $entry->glossarypivot;
422 $upperpivot = $textlib->strtoupper($pivot);
423 // Reduce pivot to 1cc if necessary
424 if ( !$fullpivot ) {
425 $upperpivot = $textlib->substr($upperpivot, 0, 1);
428 // if there's a group break
429 if ( $currentpivot != $upperpivot ) {
431 // print the group break if apply
432 if ( $printpivot ) {
433 $currentpivot = $upperpivot;
435 echo '<div>';
436 echo '<table cellspacing="0" class="glossarycategoryheader">';
438 echo '<tr>';
439 $pivottoshow = $currentpivot;
440 if ( isset($entry->userispivot) ) {
441 // printing the user icon if defined (only when browsing authors)
442 echo '<td align="left">';
444 $user = get_record("user","id",$entry->userid);
445 print_user_picture($user->id, $course->id, $user->picture);
446 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
447 } else {
448 echo '<td align="center">';
451 echo "<strong> $pivottoshow</strong>" ;
452 echo '</td></tr></table></div>';
457 $concept = $entry->concept;
458 $definition = $entry->definition;
460 /// highlight the term if necessary
461 if ($mode == 'search') {
462 //We have to strip any word starting by + and take out words starting by -
463 //to make highlight works properly
464 $searchterms = explode(' ', $hook); // Search for words independently
465 foreach ($searchterms as $key => $searchterm) {
466 if (preg_match('/^\-/',$searchterm)) {
467 unset($searchterms[$key]);
468 } else {
469 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
471 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
472 if (strlen($searchterm) < 2) {
473 unset($searchterms[$key]);
476 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
477 $entry->highlight = $strippedsearch;
480 /// and finally print the entry.
482 if ( glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat,$ratings) ) {
483 $ratingsmenuused = true;
486 $entriesshown++;
489 if ( !$entriesshown ) {
490 print_simple_box('<div style="text-align:center">' . get_string("noentries","glossary") . '</div>',"center","95%");
494 if ($ratingsmenuused) {
496 echo "<div class=\"boxaligncenter\"><input type=\"submit\" value=\"".get_string("sendinratings", "glossary")."\" />";
497 if ($glossary->scale < 0) {
498 if ($scale = get_record("scale", "id", abs($glossary->scale))) {
499 print_scale_menu_helpbutton($course->id, $scale );
502 echo "</div>";
505 if (!empty($formsent)) {
506 // close the form properly if used
507 echo "</div>";
508 echo "</form>";
511 if ( $paging ) {
512 echo '<hr />';
513 echo '<div class="paging">';
514 echo $paging;
515 echo '</div>';
517 echo '<br />';
518 glossary_print_tabbed_table_end();
520 /// Finish the page
522 print_footer($course);