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 gradereport_singleview\report;
  18  
  19  use context_course;
  20  use grade_report;
  21  use moodle_url;
  22  use renderer_base;
  23  use stdClass;
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  require_once($CFG->dirroot . '/grade/report/lib.php');
  28  
  29  /**
  30   * This class is the main class that must be implemented by a grade report plugin.
  31   *
  32   * @package   gradereport_singleview
  33   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class singleview extends grade_report {
  37  
  38      /** @var string|null $itemselector The raw HTML of the item selector based on the selected single view item type. */
  39      public ?string $itemselector = null;
  40  
  41      /**
  42       * Return the list of valid screens, used to validate the input.
  43       *
  44       * @return array List of screens.
  45       */
  46      public static function valid_screens(): array {
  47          // This is a list of all the known classes representing a screen in this plugin.
  48          return ['user', 'select', 'grade', 'user_select', 'grade_select'];
  49      }
  50  
  51      /**
  52       * Process data from a form submission. Delegated to the current screen.
  53       *
  54       * @param array $data The data from the form
  55       * @return array|object List of warnings
  56       */
  57      public function process_data($data) {
  58          if (has_capability('moodle/grade:edit', $this->context)) {
  59              return $this->screen->process($data);
  60          }
  61      }
  62  
  63      /**
  64       * Unused - abstract function declared in the parent class.
  65       *
  66       * @param string $target
  67       * @param string $action
  68       */
  69      public function process_action($target, $action) {
  70      }
  71  
  72      /**
  73       * Constructor for this report. Creates the appropriate screen class based on itemtype.
  74       *
  75       * @param int $courseid The course id.
  76       * @param object $gpr grade plugin return tracking object
  77       * @param context_course $context
  78       * @param string $itemtype Should be user, select or grade
  79       * @param int|null $itemid The id of the user or grade item
  80       * @param string|null $unused Used to be group id but that was removed and this is now unused.
  81       */
  82      public function __construct(
  83          int $courseid,
  84          object $gpr,
  85          context_course $context,
  86          string $itemtype,
  87          ?int $itemid,
  88          ?string $unused = null
  89      ) {
  90          parent::__construct($courseid, $gpr, $context);
  91  
  92          $base = '/grade/report/singleview/index.php';
  93  
  94          $idparams = ['id' => $courseid];
  95  
  96          $this->baseurl = new moodle_url($base, $idparams);
  97  
  98          $this->pbarurl = new moodle_url($base, $idparams + [
  99                  'item' => $itemtype,
 100                  'itemid' => $itemid
 101              ]);
 102  
 103          //  The setup_group method is used to validate group mode and permissions and define the currentgroup value.
 104          $this->setup_groups();
 105  
 106          if (($itemtype !== 'grade') && ($itemtype !== 'user')) {
 107              $itemid = null;
 108          }
 109  
 110          $this->setup_item_selector($itemtype, $itemid);
 111  
 112          $screenclass = "\\gradereport_singleview\\local\\screen\\$itemtype}";
 113  
 114          $this->screen = new $screenclass($courseid, $itemid, $this->currentgroup);
 115  
 116          // Load custom or predifined js.
 117          $this->screen->js();
 118      }
 119  
 120      /**
 121       * Build the html for the screen.
 122       * @return string HTML to display
 123       */
 124      public function output(): string {
 125          global $OUTPUT;
 126          return $OUTPUT->container($this->screen->html());
 127      }
 128  
 129      protected function setup_groups() {
 130          parent::setup_groups();
 131          $this->group_selector = static::groups_course_menu($this->course, $this->pbarurl);
 132      }
 133  
 134      /**
 135       * Ideally we should move this function to the base class and call it from the setup_groups in the base class,
 136       * so all reports would automatically use it.
 137       *
 138       * @param stdClass $course
 139       * @param moodle_url $urlroot
 140       * @return string
 141       */
 142      protected static function groups_course_menu(stdClass $course, moodle_url $urlroot) {
 143          global $PAGE;
 144  
 145          $renderer = $PAGE->get_renderer('core_grades');
 146          $params = $urlroot->params();
 147          if ($params['item'] == 'user') {
 148              $params['item'] = 'user_select';
 149              $urlroot->params($params);
 150          }
 151          return $renderer->group_selector($course, $urlroot->out());
 152      }
 153  
 154      /**
 155       * Function used to set the appropriate item selector (raw HTML) based on the selected single view item type.
 156       *
 157       * @param string $itemtype The single view item type.
 158       * @param int|null $itemid The item ID.
 159       */
 160      protected function setup_item_selector(string $itemtype, ?int $itemid) {
 161          global $PAGE;
 162  
 163          $renderer = $PAGE->get_renderer('gradereport_singleview');
 164  
 165          if ($itemtype === 'user' || $itemtype === 'user_select' ) {
 166              $this->itemselector = $renderer->users_selector($this->course, $itemid, $this->currentgroup);
 167          } else if ($itemtype === 'grade' || $itemtype === 'grade_select' ) {
 168              $this->itemselector = $renderer->grade_items_selector($this->course, $itemid);
 169          }
 170      }
 171  
 172      /**
 173       * Adds bulk actions menu.
 174       *
 175       * @param renderer_base $output
 176       * @return string HTML to display
 177       */
 178      public function bulk_actions_menu(renderer_base $output) : string {
 179          $options = [
 180              'overrideallgrades' => get_string('overrideallgrades', 'gradereport_singleview'),
 181              'overridenonegrades' => get_string('overridenonegrades', 'gradereport_singleview'),
 182              'excludeallgrades' => get_string('excludeallgrades', 'gradereport_singleview'),
 183              'excludenonegrades' => get_string('excludenonegrades', 'gradereport_singleview'),
 184              'bulklegend' => get_string('bulklegend', 'gradereport_singleview')
 185          ];
 186  
 187          $menu = new \action_menu();
 188          $menu->set_menu_trigger(get_string('actions'), 'text-dark');
 189  
 190          foreach ($options as $type => $option) {
 191              $action = new \action_menu_link_secondary(new \moodle_url('#'), null, $option,
 192                  ['data-action' => $type, 'data-role' => 'bulkaction']);
 193              $menu->add($action);
 194          }
 195          $menu->attributes['class'] .= ' float-left my-auto';
 196  
 197          return $output->render($menu);
 198      }
 199  
 200  }