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 400 and 401]

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