Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [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      $PAGE->navbar->add($strwaitingapproval);
 299  }
 300  
 301  $hassecondary = $PAGE->has_secondary_navigation();
 302  if ($tab == GLOSSARY_APPROVAL_VIEW && !$hassecondary &&  $PAGE->activityheader->is_title_allowed()) {
 303      $PAGE->activityheader->set_title(
 304          $OUTPUT->heading($strwaitingapproval) .
 305          $OUTPUT->heading(format_string($glossary->name))
 306      );
 307  }
 308  
 309  if ($tab == GLOSSARY_APPROVAL_VIEW || !($glossary->intro && $showcommonelements)) {
 310      $PAGE->activityheader->set_description('');
 311  }
 312  
 313  echo $OUTPUT->header();
 314  if ($showcommonelements) {
 315      echo $renderer->main_action_bar($actionbar);
 316  }
 317  
 318  /// All this depends if whe have $showcommonelements
 319  if ($showcommonelements) {
 320  /// To calculate available options
 321      $availableoptions = '';
 322  
 323  /// Decide about to print the import link
 324      /*if (has_capability('mod/glossary:import', $context)) {
 325          $availableoptions = '<span class="helplink">' .
 326                              '<a href="' . $CFG->wwwroot . '/mod/glossary/import.php?id=' . $cm->id . '"' .
 327                              '  title="' . s(get_string('importentries', 'glossary')) . '">' .
 328                              get_string('importentries', 'glossary') . '</a>' .
 329                              '</span>';
 330      }
 331  /// Decide about to print the export link
 332      if (has_capability('mod/glossary:export', $context)) {
 333          if ($availableoptions) {
 334              $availableoptions .= '&nbsp;/&nbsp;';
 335          }
 336          $availableoptions .='<span class="helplink">' .
 337                              '<a href="' . $CFG->wwwroot . '/mod/glossary/export.php?id=' . $cm->id .
 338                              '&amp;mode='.$mode . '&amp;hook=' . urlencode($hook) . '"' .
 339                              '  title="' . s(get_string('exportentries', 'glossary')) . '">' .
 340                              get_string('exportentries', 'glossary') . '</a>' .
 341                              '</span>';
 342      }*/
 343  
 344  /// Decide about to print the approval link
 345      if (has_capability('mod/glossary:approve', $context) && !$hassecondary) {
 346      /// Check we have pending entries
 347          if ($hiddenentries = $DB->count_records('glossary_entries', array('glossaryid'=>$glossary->id, 'approved'=>0))) {
 348              if ($availableoptions) {
 349                  $availableoptions .= '<br />';
 350              }
 351              $availableoptions .='<span class="helplink">' .
 352                                  '<a href="' . $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id .
 353                                  '&amp;mode=approval' . '"' .
 354                                  '  title="' . s($strwaitingapproval) . '">' .
 355                                  $strwaitingapproval . ' ('.$hiddenentries.')</a>' .
 356                                  '</span>';
 357          }
 358      }
 359  
 360  /// Start to print glossary controls
 361  //        print_box_start('glossarycontrol clearfix');
 362      echo '<div class="glossarycontrol" style="text-align: right">';
 363      echo $availableoptions;
 364  
 365  /// End glossary controls
 366  //        print_box_end(); /// glossarycontrol
 367      echo '</div><br />';
 368  
 369  //        print_box('&nbsp;', 'clearer');
 370  }
 371  
 372  require ("tabs.php");
 373  
 374  require ("sql.php");
 375  
 376  /// printing the entries
 377  $entriesshown = 0;
 378  $currentpivot = '';
 379  $paging = NULL;
 380  
 381  if ($allentries) {
 382  
 383      //Decide if we must show the ALL link in the pagebar
 384      $specialtext = '';
 385      if ($glossary->showall) {
 386          $specialtext = get_string("allentries","glossary");
 387      }
 388  
 389      //Build paging bar
 390      $baseurl = new moodle_url('/mod/glossary/view.php', ['id' => $id, 'mode' => $mode, 'hook' => $hook,
 391          'sortkey' => $sortkey, 'sortorder' => $sortorder, 'fullsearch' => $fullsearch]);
 392      $paging = glossary_get_paging_bar($count, $page, $entriesbypage, $baseurl->out() . '&amp;',
 393          9999, 10, '&nbsp;&nbsp;', $specialtext, -1);
 394  
 395      echo '<div class="paging">';
 396      echo $paging;
 397      echo '</div>';
 398  
 399      //load ratings
 400      require_once($CFG->dirroot.'/rating/lib.php');
 401      if ($glossary->assessed != RATING_AGGREGATE_NONE) {
 402          $ratingoptions = new stdClass;
 403          $ratingoptions->context = $context;
 404          $ratingoptions->component = 'mod_glossary';
 405          $ratingoptions->ratingarea = 'entry';
 406          $ratingoptions->items = $allentries;
 407          $ratingoptions->aggregate = $glossary->assessed;//the aggregation method
 408          $ratingoptions->scaleid = $glossary->scale;
 409          $ratingoptions->userid = $USER->id;
 410          $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
 411          $ratingoptions->assesstimestart = $glossary->assesstimestart;
 412          $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
 413  
 414          $rm = new rating_manager();
 415          $allentries = $rm->get_ratings($ratingoptions);
 416      }
 417  
 418      foreach ($allentries as $entry) {
 419  
 420          // Setting the pivot for the current entry
 421          if ($printpivot) {
 422              $pivot = $entry->{$pivotkey};
 423              $upperpivot = core_text::strtoupper($pivot);
 424              $pivottoshow = core_text::strtoupper(format_string($pivot, true, $fmtoptions));
 425  
 426              // Reduce pivot to 1cc if necessary.
 427              if (!$fullpivot) {
 428                  $upperpivot = core_text::substr($upperpivot, 0, 1);
 429                  $pivottoshow = core_text::substr($pivottoshow, 0, 1);
 430              }
 431  
 432              // If there's a group break.
 433              if ($currentpivot != $upperpivot) {
 434                  $currentpivot = $upperpivot;
 435  
 436                  // print the group break if apply
 437  
 438                  echo '<div>';
 439                  echo '<table cellspacing="0" class="glossarycategoryheader">';
 440  
 441                  echo '<tr>';
 442                  if ($userispivot) {
 443                  // printing the user icon if defined (only when browsing authors)
 444                      echo '<th align="left">';
 445                      $user = mod_glossary_entry_query_builder::get_user_from_record($entry);
 446                      echo $OUTPUT->user_picture($user, array('courseid'=>$course->id));
 447                      $pivottoshow = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
 448                  } else {
 449                      echo '<th >';
 450                  }
 451  
 452                  echo $OUTPUT->heading($pivottoshow, 3);
 453                  echo "</th></tr></table></div>\n";
 454              }
 455          }
 456  
 457          /// highlight the term if necessary
 458          if ($mode == 'search') {
 459              //We have to strip any word starting by + and take out words starting by -
 460              //to make highlight works properly
 461              $searchterms = explode(' ', $hook);    // Search for words independently
 462              foreach ($searchterms as $key => $searchterm) {
 463                  if (preg_match('/^\-/',$searchterm)) {
 464                      unset($searchterms[$key]);
 465                  } else {
 466                      $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
 467                  }
 468                  //Avoid highlight of <2 len strings. It's a well known hilight limitation.
 469                  if (strlen($searchterm) < 2) {
 470                      unset($searchterms[$key]);
 471                  }
 472              }
 473              $strippedsearch = implode(' ', $searchterms);    // Rebuild the string
 474              $entry->highlight = $strippedsearch;
 475          }
 476  
 477          /// and finally print the entry.
 478          glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat);
 479          $entriesshown++;
 480      }
 481      // The all entries value may be a recordset or an array.
 482      if ($allentries instanceof moodle_recordset) {
 483          $allentries->close();
 484      }
 485  }
 486  if ( !$entriesshown ) {
 487      echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide");
 488  }
 489  
 490  if (!empty($formsent)) {
 491      // close the form properly if used
 492      echo "</div>";
 493      echo "</form>";
 494  }
 495  
 496  if ( $paging ) {
 497      echo '<hr />';
 498      echo '<div class="paging">';
 499      echo $paging;
 500      echo '</div>';
 501  }
 502  echo '<br />';
 503  
 504  /// Finish the page
 505  echo $OUTPUT->footer();