Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 401 and 403] [Versions 402 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 gradereport_user\output;
  18  
  19  use moodle_url;
  20  use core_grades\output\general_action_bar;
  21  
  22  /**
  23   * Renderable class for the action bar elements in the user report page.
  24   *
  25   * @package    gradereport_user
  26   * @copyright  2022 Mihail Geshoski <mihail@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class action_bar extends \core_grades\output\action_bar {
  30  
  31      /** @var int|null $userid The user ID. */
  32      protected $userid;
  33  
  34      /** @var int $userview The user report view mode. */
  35      protected $userview;
  36  
  37      /** @var int|null $currentgroupid The user report view mode. */
  38      protected $currentgroupid;
  39  
  40      /**
  41       * The class constructor.
  42       *
  43       * @param \context $context The context object.
  44       * @param int $userview The user report view mode.
  45       * @param int|null $userid The user ID or 0 if displaying all users.
  46       * @param int|null $currentgroupid The ID of the current group.
  47       */
  48      public function __construct(\context $context, int $userview, ?int $userid = null, ?int $currentgroupid = null) {
  49          parent::__construct($context);
  50          $this->userview = $userview;
  51          $this->userid = $userid;
  52          $this->currentgroupid = $currentgroupid;
  53      }
  54  
  55      /**
  56       * Returns the template for the action bar.
  57       *
  58       * @return string
  59       */
  60      public function get_template(): string {
  61          return 'gradereport_user/action_bar';
  62      }
  63  
  64      /**
  65       * Export the data for the mustache template.
  66       *
  67       * @param \renderer_base $output renderer to be used to render the action bar elements.
  68       * @return array
  69       */
  70      public function export_for_template(\renderer_base $output): array {
  71          global $PAGE, $USER;
  72  
  73          // If in the course context, we should display the general navigation selector in gradebook.
  74          $courseid = $this->context->instanceid;
  75          // Get the data used to output the general navigation selector.
  76          $generalnavselector = new general_action_bar($this->context,
  77              new moodle_url('/grade/report/user/index.php', ['id' => $courseid]), 'gradereport', 'user');
  78          $data = $generalnavselector->export_for_template($output);
  79  
  80          // If the user has the capability to view all grades, display the group selector (if applicable), the user selector
  81          // and the view mode selector (if applicable).
  82          if (has_capability('moodle/grade:viewall', $this->context)) {
  83              $userreportrenderer = $PAGE->get_renderer('gradereport_user');
  84              $data['groupselector'] = $PAGE->get_renderer('core_grades')->group_selector(get_course($courseid));
  85              $data['userselector'] = [
  86                  'courseid' => $courseid,
  87                  'content' => $userreportrenderer->users_selector(get_course($courseid), $this->userid, $this->currentgroupid)
  88              ];
  89  
  90              // Do not output the 'view mode' selector when in zero state or when the current user is viewing its own report.
  91              if (!is_null($this->userid) && $USER->id != $this->userid) {
  92                  $data['viewasselector'] = $userreportrenderer->view_mode_selector($this->userid, $this->userview, $courseid);
  93              }
  94          }
  95  
  96          return $data;
  97      }
  98  }