Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace mod_glossary\output;
  18  
  19  use moodle_url;
  20  use context_module;
  21  use renderable;
  22  use renderer_base;
  23  use single_button;
  24  use templatable;
  25  use url_select;
  26  
  27  /**
  28   * Class standard_action_bar - Display the action bar
  29   *
  30   * @package   mod_glossary
  31   * @copyright 2021 Peter Dias
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class standard_action_bar implements renderable, templatable {
  35      /** @var object $cm The course module. */
  36      private $cm;
  37      /** @var string $mode The type of view. */
  38      private $mode;
  39      /** @var string $hook The term, entry, cat, etc... to look for based on mode. */
  40      private $hook;
  41      /** @var string $sortkey Sorted view: CREATION | UPDATE | FIRSTNAME | LASTNAME. */
  42      private $sortkey;
  43      /** @var string $sortorder The sort order (ASC or DESC). */
  44      private $sortorder;
  45      /** @var int $offset Entries to bypass (for paging purposes). */
  46      private $offset;
  47      /** @var int $pagelimit The page to resume with. */
  48      private $pagelimit;
  49      /** @var int $context The context of the glossary. */
  50      private $context;
  51      /** @var object $module The glossary record . */
  52      private $module;
  53      /** @var int $fullsearch Full search (concept and definition) when searching. */
  54      private $fullsearch;
  55      /** @var object $displayformat Override of the glossary display format. */
  56      private $displayformat;
  57      /** @var string $tab Browsing entries by categories. */
  58      private $tab;
  59  
  60      /**
  61       * standard_action_bar constructor.
  62       *
  63       * @param object $cm
  64       * @param object $module
  65       * @param object $displayformat
  66       * @param string $mode
  67       * @param string $hook
  68       * @param string $sortkey
  69       * @param string $sortorder
  70       * @param int $offset
  71       * @param int $pagelimit
  72       * @param int $fullsearch
  73       * @param string $tab
  74       * @param string $defaulttab
  75       * @throws \coding_exception
  76       */
  77      public function __construct(object $cm, object $module, object $displayformat, string $mode, string $hook,
  78              string $sortkey, string $sortorder, int $offset, int $pagelimit, int $fullsearch,
  79              string $tab, string $defaulttab) {
  80          $this->cm = $cm;
  81          $this->module = $module;
  82          $this->displayformat = $displayformat;
  83          $this->mode = $mode;
  84          $this->tab = $tab;
  85          $this->hook = $hook;
  86          $this->sortkey = $sortkey;
  87          $this->sortorder = $sortorder;
  88          $this->offset = $offset;
  89          $this->pagelimit = $pagelimit;
  90          $this->fullsearch = $fullsearch;
  91          $this->context = context_module::instance($this->cm->id);
  92  
  93          if (!has_capability('mod/glossary:approve', $this->context) && $this->tab == GLOSSARY_APPROVAL_VIEW) {
  94              // Non-teachers going to approval view go to defaulttab.
  95              $this->tab = $defaulttab;
  96          }
  97      }
  98  
  99      /**
 100       * Export the action bar
 101       *
 102       * @param renderer_base $output
 103       * @return array
 104       */
 105      public function export_for_template(renderer_base $output) {
 106          return [
 107              'addnewbutton' => $this->create_add_button($output),
 108              'searchbox' => $this->create_search_box(),
 109              'tools' => $this->get_additional_tools($output),
 110              'tabjumps' => $this->generate_tab_jumps($output)
 111          ];
 112      }
 113  
 114      /**
 115       * Render the search box with the checkbox
 116       *
 117       * @return array
 118       */
 119      private function create_search_box(): array {
 120          global $OUTPUT;
 121          $fullsearchchecked = false;
 122          if ($this->fullsearch || $this->mode != 'search') {
 123              $fullsearchchecked = true;
 124          }
 125  
 126          $check = [
 127              'name' => 'fullsearch',
 128              'id' => 'fullsearch',
 129              'value' => '1',
 130              'checked' => $fullsearchchecked,
 131              'label' => get_string("searchindefinition", "glossary"),
 132          ];
 133  
 134          $checkbox = $OUTPUT->render_from_template('core/checkbox', $check);
 135  
 136          $hiddenfields = [
 137              (object) ['name' => 'id', 'value' => $this->cm->id],
 138              (object) ['name' => 'mode', 'value' => 'search'],
 139          ];
 140          $data = [
 141              'action' => new moodle_url('/mod/glossary/view.php'),
 142              'hiddenfields' => $hiddenfields,
 143              'otherfields' => $checkbox,
 144              'inputname' => 'hook',
 145              'query' => ($this->mode == 'search') ? s($this->hook) : '',
 146              'searchstring' => get_string('search'),
 147          ];
 148  
 149          return $data;
 150      }
 151  
 152      /**
 153       * Render the add entry button
 154       *
 155       * @param renderer_base $output
 156       * @return \stdClass|null
 157       */
 158      private function create_add_button(renderer_base $output): ?\stdClass {
 159          if (!has_capability('mod/glossary:write', $this->context)) {
 160              return null;
 161          }
 162          $btn = new single_button(new moodle_url('/mod/glossary/edit.php', ['cmid' => $this->cm->id]),
 163              get_string('addsingleentry', 'glossary'), 'post', true);
 164  
 165          return $btn->export_for_template($output);
 166      }
 167  
 168      /**
 169       * Render the additional tools required by the glossary
 170       *
 171       * @param renderer_base $output
 172       * @return array
 173       */
 174      private function get_additional_tools(renderer_base $output): array {
 175          global $USER, $CFG;
 176          $items = [];
 177          $buttons = [];
 178          $openinnewwindow = [];
 179  
 180          if (has_capability('mod/glossary:import', $this->context)) {
 181              $items['button'] = new single_button(
 182                  new moodle_url('/mod/glossary/import.php', ['id' => $this->cm->id]),
 183                  get_string('importentries', 'glossary')
 184              );
 185          }
 186  
 187          if (has_capability('mod/glossary:export', $this->context)) {
 188              $url = new moodle_url('/mod/glossary/export.php', [
 189                  'id' => $this->cm->id,
 190                  'mode' => $this->mode,
 191                  'hook' => $this->hook
 192              ]);
 193              $buttons[get_string('export', 'glossary')] = $url->out(false);
 194          }
 195  
 196          if (has_capability('mod/glossary:manageentries', $this->context) or $this->module->allowprintview) {
 197              $params = array(
 198                  'id'        => $this->cm->id,
 199                  'mode'      => $this->mode,
 200                  'hook'      => $this->hook,
 201                  'sortkey'   => $this->sortkey,
 202                  'sortorder' => $this->sortorder,
 203                  'offset'    => $this->offset,
 204                  'pagelimit' => $this->pagelimit
 205              );
 206              $printurl = new moodle_url('/mod/glossary/print.php', $params);
 207              $buttons[get_string('printerfriendly', 'glossary')] = $printurl->out(false);
 208              $openinnewwindow[] = $printurl->out(false);
 209          }
 210  
 211          if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
 212                  && $this->module->rsstype && $this->module->rssarticles
 213                  && has_capability('mod/glossary:view', $this->context)) {
 214              require_once("$CFG->libdir/rsslib.php");
 215              $string = get_string('rssfeed', 'glossary');
 216              $url = new moodle_url(rss_get_url($this->context->id, $USER->id, 'mod_glossary', $this->cm->instance));
 217              $buttons[$string] = $url->out(false);
 218              $openinnewwindow[] = $url->out(false);
 219          }
 220  
 221          foreach ($items as $key => $value) {
 222              $items[$key] = $value->export_for_template($output);
 223          }
 224  
 225          if ($buttons) {
 226              foreach ($buttons as $index => $value) {
 227                  $items['select']['options'][] = [
 228                      'url' => $value,
 229                      'string' => $index,
 230                      'openinnewwindow' => ($openinnewwindow ? in_array($value, $openinnewwindow) : false)
 231                  ];
 232              }
 233          }
 234  
 235          return $items;
 236      }
 237  
 238      /**
 239       * Generate a url select to match any types of glossary views
 240       *
 241       * @param renderer_base $output
 242       * @return \stdClass|null
 243       */
 244      private function generate_tab_jumps(renderer_base $output) {
 245          $tabs = glossary_get_visible_tabs($this->displayformat);
 246          $validtabs = [
 247              GLOSSARY_STANDARD => [
 248                  'mode' => 'letter',
 249                  'descriptor' => 'standardview'
 250              ],
 251              GLOSSARY_CATEGORY => [
 252                  'mode' => 'cat',
 253                  'descriptor' => 'categoryview'
 254              ],
 255              GLOSSARY_DATE => [
 256                  'mode' => 'date',
 257                  'descriptor' => 'dateview'
 258              ],
 259              GLOSSARY_AUTHOR => [
 260                  'mode' => 'author',
 261                  'descriptor' => 'authorview'
 262              ],
 263          ];
 264  
 265          $baseurl = new moodle_url('/mod/glossary/view.php', ['id' => $this->cm->id]);
 266          $active = null;
 267          $options = [];
 268          foreach ($validtabs as $key => $tabinfo) {
 269              if (in_array($key, $tabs)) {
 270                  $baseurl->params(['mode' => $tabinfo['mode']]);
 271                  $active = $active ?? $baseurl->out(false);
 272                  $active = ($tabinfo['mode'] == $this->mode ? $baseurl->out(false) : $active);
 273                  $options[get_string($tabinfo['descriptor'], 'glossary')] = $baseurl->out(false);
 274              }
 275          }
 276  
 277          if ($this->tab < GLOSSARY_STANDARD_VIEW || $this->tab > GLOSSARY_AUTHOR_VIEW) {
 278              $options[get_string('edit')] = '#';
 279          }
 280  
 281          if (count($options) > 1) {
 282              $select = new url_select(array_flip($options), $active, null);
 283              $select->set_label(get_string('explainalphabet', 'glossary'), ['class' => 'sr-only']);
 284              return $select->export_for_template($output);
 285          }
 286  
 287          return null;
 288      }
 289  }