Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

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