Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 core_grades\output;
  18  
  19  use moodle_url;
  20  
  21  /**
  22   * Renderable class for the general action bar in the gradebook pages.
  23   *
  24   * This class is responsible for rendering the general navigation select menu in the gradebook pages.
  25   *
  26   * @package    core_grades
  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 general_action_bar extends action_bar {
  31  
  32      /** @var moodle_url $activeurl The URL that should be set as active in the URL selector element. */
  33      protected $activeurl;
  34  
  35      /**
  36       * The type of the current gradebook page (report, settings, import, export, scales, outcomes, letters).
  37       *
  38       * @var string $activetype
  39       */
  40      protected $activetype;
  41  
  42      /** @var string $activeplugin The plugin of the current gradebook page (grader, fullview, ...). */
  43      protected $activeplugin;
  44  
  45      /**
  46       * The class constructor.
  47       *
  48       * @param \context $context The context object.
  49       * @param moodle_url $activeurl The URL that should be set as active in the URL selector element.
  50       * @param string $activetype The type of the current gradebook page (report, settings, import, export, scales,
  51       *                           outcomes, letters).
  52       * @param string $activeplugin The plugin of the current gradebook page (grader, fullview, ...).
  53       */
  54      public function __construct(\context $context, moodle_url $activeurl, string $activetype, string $activeplugin) {
  55          parent::__construct($context);
  56          $this->activeurl = $activeurl;
  57          $this->activetype = $activetype;
  58          $this->activeplugin = $activeplugin;
  59      }
  60  
  61      /**
  62       * Export the data for the mustache template.
  63       *
  64       * @param \renderer_base $output renderer to be used to render the action bar elements.
  65       * @return array
  66       */
  67      public function export_for_template(\renderer_base $output): array {
  68          $urlselect = $this->get_action_selector();
  69  
  70          if (is_null($urlselect)) {
  71              return [];
  72          }
  73  
  74          return [
  75              'generalnavselector' => $urlselect->export_for_template($output),
  76          ];
  77      }
  78  
  79      /**
  80       * Returns the template for the action bar.
  81       *
  82       * @return string
  83       */
  84      public function get_template(): string {
  85          return 'core_grades/general_action_bar';
  86      }
  87  
  88      /**
  89       * Returns the URL selector object.
  90       *
  91       * @return \url_select|null The URL select object.
  92       */
  93      private function get_action_selector(): ?\url_select {
  94          if ($this->context->contextlevel !== CONTEXT_COURSE) {
  95              return null;
  96          }
  97          $courseid = $this->context->instanceid;
  98          $plugininfo = grade_get_plugin_info($courseid, $this->activetype, $this->activeplugin);
  99          $menu = [];
 100          $viewgroup = [];
 101          $setupgroup = [];
 102          $moregroup = [];
 103  
 104          foreach ($plugininfo as $plugintype => $plugins) {
 105              // Skip if the plugintype value is 'strings'. This particular item only returns an array of strings
 106              // which we do not need.
 107              if ($plugintype == 'strings') {
 108                  continue;
 109              }
 110  
 111              // If $plugins is actually the definition of a child-less parent link.
 112              if (!empty($plugins->id)) {
 113                  $string = $plugins->string;
 114                  if (!empty($plugininfo[$this->activetype]->parent)) {
 115                      $string = $plugininfo[$this->activetype]->parent->string;
 116                  }
 117                  $menu[$plugins->link->out(false)] = $string;
 118                  continue;
 119              }
 120  
 121              foreach ($plugins as $key => $plugin) {
 122                  // Depending on the plugin type, include the plugin to the appropriate item group for the URL selector
 123                  // element.
 124                  switch ($plugintype) {
 125                      case 'report':
 126                          $viewgroup[$plugin->link->out(false)] = $plugin->string;
 127                          break;
 128                      case 'settings':
 129                          $setupgroup[$plugin->link->out(false)] = $plugin->string;
 130                          break;
 131                      case 'scale':
 132                          // We only need the link to the 'view scales' page, otherwise skip and continue to the next
 133                          // plugin.
 134                          if ($key !== 'view') {
 135                              continue 2;
 136                          }
 137                          $moregroup[$plugin->link->out(false)] = get_string('scales');
 138                          break;
 139                      case 'outcome':
 140                          // We only need the link to the 'outcomes used in course' page, otherwise skip and continue to
 141                          // the next plugin.
 142                          if ($key !== 'course') {
 143                              continue 2;
 144                          }
 145                          $moregroup[$plugin->link->out(false)] = get_string('outcomes', 'grades');
 146                          break;
 147                      case 'letter':
 148                          // We only need the link to the 'view grade letters' page, otherwise skip and continue to the
 149                          // next plugin.
 150                          if ($key !== 'view') {
 151                              continue 2;
 152                          }
 153                          $moregroup[$plugin->link->out(false)] = get_string('gradeletters', 'grades');
 154                          break;
 155                      case 'import':
 156                          $link = new moodle_url('/grade/import/index.php', ['id' => $courseid]);
 157                          // If the link to the grade import options is already added to the group, skip and continue to
 158                          // the next plugin.
 159                          if (array_key_exists($link->out(false), $moregroup)) {
 160                              continue 2;
 161                          }
 162                          $moregroup[$link->out(false)] = get_string('import', 'grades');
 163                          break;
 164                      case 'export':
 165                          $link = new moodle_url('/grade/export/index.php', ['id' => $courseid]);
 166                          // If the link to the grade export options is already added to the group, skip and continue to
 167                          // the next plugin.
 168                          if (array_key_exists($link->out(false), $moregroup)) {
 169                              continue 2;
 170                          }
 171                          $moregroup[$link->out(false)] = get_string('export', 'grades');
 172                          break;
 173                  }
 174              }
 175          }
 176  
 177          if (!empty($viewgroup)) {
 178              $menu[][get_string('view')] = $viewgroup;
 179          }
 180  
 181          if (!empty($setupgroup)) {
 182              $menu[][get_string('setup', 'grades')] = $setupgroup;
 183          }
 184  
 185          if (!empty($moregroup)) {
 186              $menu[][get_string('moremenu')] = $moregroup;
 187          }
 188  
 189          return new \url_select($menu, $this->activeurl->out(false), null, 'gradesactionselect');
 190      }
 191  }