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.
   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_wiki\output;
  18  
  19  use moodle_url;
  20  use templatable;
  21  use renderable;
  22  
  23  /**
  24   * Renderable class for the action bar elements in the wiki activity pages.
  25   *
  26   * @package    mod_wiki
  27   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class action_bar implements templatable, renderable {
  31  
  32      /** @var int $pageid The database module id. */
  33      private $pageid;
  34  
  35      /** @var moodle_url $currenturl The URL of the current page. */
  36      private $currenturl;
  37  
  38      /** @var bool $displayprint Whether to display print wiki button. */
  39      private $displayprint;
  40  
  41      /**
  42       * The class constructor.
  43       *
  44       * @param int $pageid The wiki page id.
  45       * @param moodle_url $pageurl The URL of the current page.
  46       * @param bool $displayprint Whether to display print wiki button.
  47       */
  48      public function __construct(int $pageid, moodle_url $pageurl, bool $displayprint = false) {
  49          $this->pageid = $pageid;
  50          $this->currenturl = $pageurl;
  51          $this->displayprint = $displayprint;
  52      }
  53  
  54      /**
  55       * Export the data for the mustache template.
  56       *
  57       * @param \renderer_base $output renderer to be used to render the action bar elements.
  58       * @return array
  59       */
  60      public function export_for_template(\renderer_base $output): array {
  61  
  62          $urlselect = $this->get_action_selector();
  63  
  64          $data = [
  65              'urlselect' => $urlselect->export_for_template($output),
  66          ];
  67  
  68          if ($this->displayprint) {
  69              $printlink = new moodle_url('/mod/wiki/prettyview.php', ['pageid' => $this->pageid]);
  70  
  71              $data['printbutton'] = \html_writer::link($printlink, get_string('print', 'mod_wiki'),
  72                  ['class' => 'btn btn-secondary', 'target' => "_blank"]);
  73          }
  74  
  75          return $data;
  76      }
  77  
  78      /**
  79       * Returns the URL selector object.
  80       *
  81       * @return \url_select The URL select object.
  82       */
  83      private function get_action_selector(): \url_select {
  84          global $PAGE;
  85  
  86          $menu = [];
  87  
  88          if (has_capability('mod/wiki:viewpage', $PAGE->context)) {
  89              $viewlink = new moodle_url('/mod/wiki/view.php', ['pageid' => $this->pageid]);
  90              $menu[$viewlink->out(false)] = get_string('view', 'mod_wiki');
  91          }
  92  
  93          if (has_capability('mod/wiki:editpage', $PAGE->context)) {
  94              $editlink = new moodle_url('/mod/wiki/edit.php', ['pageid' => $this->pageid]);
  95              $menu[$editlink->out(false)] = get_string('edit', 'mod_wiki');
  96          }
  97  
  98          if (has_capability('mod/wiki:viewcomment', $PAGE->context)) {
  99              $commentslink = new moodle_url('/mod/wiki/comments.php', ['pageid' => $this->pageid]);
 100              $menu[$commentslink->out(false)] = get_string('comments', 'mod_wiki');
 101          }
 102  
 103          if (has_capability('mod/wiki:viewpage', $PAGE->context)) {
 104              $historylink = new moodle_url('/mod/wiki/history.php', ['pageid' => $this->pageid]);
 105              $menu[$historylink->out(false)] = get_string('history', 'mod_wiki');
 106          }
 107  
 108          if (has_capability('mod/wiki:viewpage', $PAGE->context)) {
 109              $maplink = new moodle_url('/mod/wiki/map.php', ['pageid' => $this->pageid]);
 110              $menu[$maplink->out(false)] = get_string('map', 'mod_wiki');
 111          }
 112  
 113          if (has_capability('mod/wiki:viewpage', $PAGE->context)) {
 114              $fileslink = new moodle_url('/mod/wiki/files.php', ['pageid' => $this->pageid]);
 115              $menu[$fileslink->out(false)] = get_string('files', 'mod_wiki');
 116          }
 117  
 118          if (has_capability('mod/wiki:managewiki', $PAGE->context)) {
 119              $adminlink = new moodle_url('/mod/wiki/admin.php', ['pageid' => $this->pageid]);
 120              $menu[$adminlink->out(false)] = get_string('admin', 'mod_wiki');
 121          }
 122  
 123          return new \url_select($menu, $this->currenturl->out(false), null, 'wikiactionselect');
 124      }
 125  }