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
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');
47 print_error('invalidid', 'glossary');
50 require_course_login($course->id, true, $cm);
51 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
53 require_once($CFG->dirroot . '/comment/lib.php');
56 /// Loading the textlib singleton instance. We are going to need it.
57 $textlib = textlib_get_instance();
59 /// redirecting if adding a new entry
60 if ($tab == GLOSSARY_ADDENTRY_VIEW ) {
61 redirect("edit.php?cmid=$cm->id&mode=$mode");
64 /// setting the defaut number of entries per page if not set
65 if ( !$entriesbypage = $glossary->entbypage ) {
66 $entriesbypage = $CFG->glossary_entbypage;
69 /// If we have received a page, recalculate offset
70 if ($page != 0 && $offset == 0) {
71 $offset = $page * $entriesbypage;
74 /// setting the default values for the display mode of the current glossary
75 /// only if the glossary is viewed by the first time
76 if ( $dp = $DB->get_record('glossary_formats', array('name'=>$glossary->displayformat)) ) {
77 /// Based on format->defaultmode, we build the defaulttab to be showed sometimes
78 switch ($dp->defaultmode) {
80 $defaulttab = GLOSSARY_CATEGORY_VIEW;
83 $defaulttab = GLOSSARY_DATE_VIEW;
86 $defaulttab = GLOSSARY_AUTHOR_VIEW;
89 $defaulttab = GLOSSARY_STANDARD_VIEW;
91 /// Fetch the rest of variables
92 $printpivot = $dp->showgroup;
93 if ( $mode == '' and $hook == '' and $show == '') {
94 $mode = $dp->defaultmode;
95 $hook = $dp->defaulthook;
96 $sortkey = $dp->sortkey;
97 $sortorder = $dp->sortorder;
100 $defaulttab = GLOSSARY_STANDARD_VIEW;
102 if ( $mode == '' and $hook == '' and $show == '') {
108 if ( $displayformat == -1 ) {
109 $displayformat = $glossary->displayformat;
117 /// Processing standard security processes
118 if ($course->id != SITEID) {
119 require_login($course->id);
121 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
122 echo $OUTPUT->header();
123 notice(get_string("activityiscurrentlyhidden"));
125 add_to_log($course->id, "glossary", "view", "view.php?id=$cm->id&tab=$tab", $glossary->id, $cm->id);
128 $completion = new completion_info($course);
129 $completion->set_module_viewed($cm);
131 /// stablishing flag variables
132 if ( $sortorder = strtolower($sortorder) ) {
133 if ($sortorder != 'asc' and $sortorder != 'desc') {
137 if ( $sortkey = strtoupper($sortkey) ) {
138 if ($sortkey != 'CREATION' and
139 $sortkey != 'UPDATE' and
140 $sortkey != 'FIRSTNAME' and
141 $sortkey != 'LASTNAME'
147 switch ( $mode = strtolower($mode) ) {
148 case 'search': /// looking for terms containing certain word(s)
149 $tab = GLOSSARY_STANDARD_VIEW;
151 //Clean a bit the search string
152 $hook = trim(strip_tags($hook));
156 case 'entry': /// Looking for a certain entry id
157 $tab = GLOSSARY_STANDARD_VIEW;
158 if ( $dp = $DB->get_record("glossary_formats", array("name"=>$glossary->displayformat)) ) {
159 $displayformat = $dp->popupformatname;
163 case 'cat': /// Looking for a certain cat
164 $tab = GLOSSARY_CATEGORY_VIEW;
166 $category = $DB->get_record("glossary_categories", array("id"=>$hook));
170 case 'approval': /// Looking for entries waiting for approval
171 $tab = GLOSSARY_APPROVAL_VIEW;
172 if ( !$hook and !$sortkey and !$sortorder) {
177 case 'term': /// Looking for entries that include certain term in its concept, definition or aliases
178 $tab = GLOSSARY_STANDARD_VIEW;
182 $tab = GLOSSARY_DATE_VIEW;
191 case 'author': /// Looking for entries, browsed by author
192 $tab = GLOSSARY_AUTHOR_VIEW;
197 $sortkey = 'FIRSTNAME';
204 case 'letter': /// Looking for entries that begin with a certain letter, ALL or SPECIAL characters
206 $tab = GLOSSARY_STANDARD_VIEW;
214 case GLOSSARY_IMPORT_VIEW:
215 case GLOSSARY_EXPORT_VIEW:
216 case GLOSSARY_APPROVAL_VIEW:
217 $showcommonelements = 0;
221 $showcommonelements = 1;
225 /// Printing the heading
226 $strglossaries = get_string("modulenameplural", "glossary");
227 $strglossary = get_string("modulename", "glossary");
228 $strallcategories = get_string("allcategories", "glossary");
229 $straddentry = get_string("addentry", "glossary");
230 $strnoentries = get_string("noentries", "glossary");
231 $strsearchindefinition = get_string("searchindefinition", "glossary");
232 $strsearch = get_string("search");
233 $strwaitingapproval = get_string('waitingapproval', 'glossary');
235 /// If we are in approval mode, prit special header
236 $PAGE->set_title(format_string($glossary->name));
237 $PAGE->set_heading($course->fullname);
238 $url = new moodle_url('/mod/glossary/view.php', array('id'=>$cm->id));
240 $url->param('mode', $mode);
242 $PAGE->set_url($url);
244 if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
245 && $glossary->rsstype && $glossary->rssarticles) {
247 $rsstitle = format_string($course->shortname) . ': %fullname%';
248 rss_add_http_header($context, 'mod_glossary', $glossary, $rsstitle);
251 if ($tab == GLOSSARY_APPROVAL_VIEW) {
252 require_capability('mod/glossary:approve', $context);
253 $PAGE->navbar->add($strwaitingapproval);
254 echo $OUTPUT->header();
255 echo $OUTPUT->heading($strwaitingapproval);
256 } else { /// Print standard header
257 echo $OUTPUT->header();
260 /// All this depends if whe have $showcommonelements
261 if ($showcommonelements) {
262 /// To calculate available options
263 $availableoptions = '';
265 /// Decide about to print the import link
266 /*if (has_capability('mod/glossary:import', $context)) {
267 $availableoptions = '<span class="helplink">' .
268 '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
269 ' title="' . s(get_string('importentries', 'glossary')) . '">' .
270 get_string('importentries', 'glossary') . '</a>' .
273 /// Decide about to print the export link
274 if (has_capability('mod/glossary:export', $context)) {
275 if ($availableoptions) {
276 $availableoptions .= ' / ';
278 $availableoptions .='<span class="helplink">' .
279 '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
280 '&mode='.$mode . '&hook=' . urlencode($hook) . '"' .
281 ' title="' . s(get_string('exportentries', 'glossary')) . '">' .
282 get_string('exportentries', 'glossary') . '</a>' .
286 /// Decide about to print the approval link
287 if (has_capability('mod/glossary:approve', $context)) {
288 /// Check we have pending entries
289 if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
290 if ($availableoptions) {
291 $availableoptions .= '<br />';
293 $availableoptions .='<span class="helplink">' .
294 '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
295 '&mode=approval' . '"' .
296 ' title="' . s(get_string('waitingapproval', 'glossary')) . '">' .
297 get_string('waitingapproval', 'glossary') . ' ('.$hiddenentries.')</a>' .
302 /// Start to print glossary controls
303 // print_box_start('glossarycontrol clearfix');
304 echo '<div class="glossarycontrol" style="text-align: right">';
305 echo $availableoptions;
308 if ( $showcommonelements and $mode != 'search') {
309 if (has_capability('mod/glossary:manageentries', $context) or $glossary->allowprintview) {
310 // print_box_start('printicon');
311 echo '<span class="wrap printicon">';
312 echo " <a title =\"". get_string("printerfriendly","glossary") ."\" href=\"print.php?id=$cm->id&mode=$mode&hook=".urlencode($hook)."&sortkey=$sortkey&sortorder=$sortorder&offset=$offset\"><img class=\"icon\" src=\"".$OUTPUT->pix_url('print', 'glossary')."\" alt=\"". get_string("printerfriendly","glossary") . "\" /></a>";
317 /// End glossary controls
318 // print_box_end(); /// glossarycontrol
321 // print_box(' ', 'clearer');
325 if ($glossary->intro && $showcommonelements) {
326 echo $OUTPUT->box(format_module_intro('glossary', $glossary, $cm->id), 'generalbox', 'intro');
330 if ($showcommonelements ) {
331 echo '<form method="post" action="view.php">';
333 echo '<table class="boxaligncenter" width="70%" border="0">';
334 echo '<tr><td align="center" class="glossarysearchbox">';
336 echo '<input type="submit" value="'.$strsearch.'" name="searchbutton" /> ';
337 if ($mode == 'search') {
338 echo '<input type="text" name="hook" size="20" value="'.s($hook).'" alt="'.$strsearch.'" /> ';
340 echo '<input type="text" name="hook" size="20" value="" alt="'.$strsearch.'" /> ';
342 if ($fullsearch || $mode != 'search') {
343 $fullsearchchecked = 'checked="checked"';
345 $fullsearchchecked = '';
347 echo '<input type="checkbox" name="fullsearch" id="fullsearch" value="1" '.$fullsearchchecked.' />';
348 echo '<input type="hidden" name="mode" value="search" />';
349 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
350 echo '<label for="fullsearch">'.$strsearchindefinition.'</label>';
351 echo '</td></tr></table>';
358 /// Show the add entry button if allowed
359 if (has_capability('mod/glossary:write', $context) && $showcommonelements ) {
360 echo '<div class="singlebutton glossaryaddentry">';
361 echo "<form id=\"newentryform\" method=\"get\" action=\"$CFG->wwwroot/mod/glossary/edit.php\">";
363 echo "<input type=\"hidden\" name=\"cmid\" value=\"$cm->id\" />";
364 echo '<input type="submit" value="'.get_string('addentry', 'glossary').'" />';
376 /// printing the entries
383 //Decide if we must show the ALL link in the pagebar
385 if ($glossary->showall) {
386 $specialtext = get_string("allentries","glossary");
390 $paging = glossary_get_paging_bar($count, $page, $entriesbypage, "view.php?id=$id&mode=$mode&hook=".urlencode($hook)."&sortkey=$sortkey&sortorder=$sortorder&fullsearch=$fullsearch&",9999,10,' ', $specialtext, -1);
392 echo '<div class="paging">';
397 require_once($CFG->dirroot.'/rating/lib.php');
398 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
399 $ratingoptions = new stdClass;
400 $ratingoptions->context = $context;
401 $ratingoptions->component = 'mod_glossary';
402 $ratingoptions->ratingarea = 'entry';
403 $ratingoptions->items = $allentries;
404 $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
405 $ratingoptions->scaleid = $glossary->scale;
406 $ratingoptions->userid = $USER->id;
407 $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
408 $ratingoptions->assesstimestart = $glossary->assesstimestart;
409 $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
411 $rm = new rating_manager();
412 $allentries = $rm->get_ratings($ratingoptions);
415 foreach ($allentries as $entry) {
417 // Setting the pivot for the current entry
418 $pivot = $entry->glossarypivot;
419 $upperpivot = $textlib->strtoupper($pivot);
420 // Reduce pivot to 1cc if necessary
422 $upperpivot = $textlib->substr($upperpivot, 0, 1);
425 // if there's a group break
426 if ( $currentpivot != $upperpivot ) {
428 // print the group break if apply
430 $currentpivot = $upperpivot;
433 echo '<table cellspacing="0" class="glossarycategoryheader">';
436 $pivottoshow = $currentpivot;
437 if ( isset($entry->userispivot) ) {
438 // printing the user icon if defined (only when browsing authors)
439 echo '<th align="left">';
441 $user = $DB->get_record("user", array("id"=>$entry->userid));
442 echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
443 $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
448 echo $OUTPUT->heading($pivottoshow);
449 echo "</th></tr></table></div>\n";
454 /// highlight the term if necessary
455 if ($mode == 'search') {
456 //We have to strip any word starting by + and take out words starting by -
457 //to make highlight works properly
458 $searchterms = explode(' ', $hook); // Search for words independently
459 foreach ($searchterms as $key => $searchterm) {
460 if (preg_match('/^\-/',$searchterm)) {
461 unset($searchterms[$key]);
463 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
465 //Avoid highlight of <2 len strings. It's a well known hilight limitation.
466 if (strlen($searchterm) < 2) {
467 unset($searchterms[$key]);
470 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
471 $entry->highlight = $strippedsearch;
474 /// and finally print the entry.
475 glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
479 if ( !$entriesshown ) {
480 echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
483 if (!empty($formsent)) {
484 // close the form properly if used
491 echo '<div class="paging">';
496 glossary_print_tabbed_table_end();
499 echo $OUTPUT->footer();