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  /**
  18   * Renderer for the grade single view report.
  19   *
  20   * @package   gradereport_singleview
  21   * @copyright 2022 Mihail Geshoski <mihail@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use gradereport_singleview\report\singleview;
  26  
  27  /**
  28   * Custom renderer for the single view report.
  29   *
  30   * To get an instance of this use the following code:
  31   * $renderer = $PAGE->get_renderer('gradereport_singleview');
  32   *
  33   * @copyright 2022 Mihail Geshoski <mihail@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class gradereport_singleview_renderer extends plugin_renderer_base {
  37  
  38      /**
  39       * Renders the user selector trigger element.
  40       *
  41       * @param object $course The course object.
  42       * @param int|null $userid The user ID.
  43       * @param int|null $groupid The group ID.
  44       * @return string The raw HTML to render.
  45       */
  46      public function users_selector(object $course, ?int $userid = null, ?int $groupid = null): string {
  47  
  48          $data = [
  49              'courseid' => $course->id,
  50              'groupid' => $groupid ?? 0,
  51          ];
  52  
  53          // If a particular user option is selected (not in zero state).
  54          if ($userid) { // A single user selected.
  55              $user = core_user::get_user($userid);
  56              $data['selectedoption'] = [
  57                  'image' => $this->user_picture($user, ['size' => 40, 'link' => false]),
  58                  'text' => fullname($user),
  59                  'additionaltext' => $user->email,
  60              ];
  61          }
  62  
  63          $this->page->requires->js_call_amd('gradereport_singleview/user', 'init');
  64          return $this->render_from_template('core_grades/user_selector', $data);
  65      }
  66  
  67      /**
  68       * Renders the grade items selector trigger element.
  69       *
  70       * @param object $course The course object.
  71       * @param int|null $gradeitemid The grade item ID.
  72       * @return string The raw HTML to render.
  73       */
  74      public function grade_items_selector(object $course, ?int $gradeitemid = null): string {
  75  
  76          $data = [
  77              'courseid' => $course->id,
  78          ];
  79  
  80          // If a particular grade item option is selected (not in zero state).
  81          if ($gradeitemid) {
  82              $gradeitemname = grade_item::fetch(['id' => $gradeitemid])->get_name(true);
  83              $data['selectedoption'] = [
  84                  'text' => $gradeitemname,
  85              ];
  86          }
  87  
  88          $this->page->requires->js_call_amd('gradereport_singleview/grade', 'init');
  89          return $this->render_from_template('gradereport_singleview/grade_item_selector', $data);
  90      }
  91  
  92      /**
  93       * Creates and renders previous/next user/grade item navigation.
  94       *
  95       * @param object $gpr grade plugin return tracking object
  96       * @param int $courseid The course ID.
  97       * @param \context_course $context Context of the report.
  98       * @param singleview $report The single view report class.
  99       * @param int|null $groupid Group ID
 100       * @param string $itemtype User or Grade item type
 101       * @param int $itemid Either User ID or Grade item ID
 102       * @return string The raw HTML to render.
 103       * @throws moodle_exception
 104       */
 105      public function report_navigation(object $gpr, int $courseid, \context_course $context, singleview $report,
 106                                        ?int $groupid, string $itemtype, int $itemid): string {
 107  
 108          $navigation = '';
 109          $options = $report->screen->options();
 110  
 111          $optionkeys = array_keys($options);
 112          $optionitemid = array_shift($optionkeys);
 113  
 114          $relreport = new gradereport_singleview\report\singleview(
 115              $courseid, $gpr, $context,
 116              $report->screen->item_type(), $optionitemid
 117          );
 118          $reloptions = $relreport->screen->options();
 119          $reloptionssorting = array_keys($relreport->screen->options());
 120  
 121          $i = array_search($itemid, $reloptionssorting);
 122          $navparams = ['item' => $itemtype, 'id' => $courseid, 'group' => $groupid];
 123  
 124          // Determine directionality so that icons can be modified to suit language.
 125          $previousarrow = right_to_left() ? 'right' : 'left';
 126          $nextarrow = right_to_left() ? 'left' : 'right';
 127  
 128          if ($i > 0) {
 129              $navparams['itemid'] = $reloptionssorting[$i - 1];
 130              $link = (new moodle_url('/grade/report/singleview/index.php', $navparams))
 131                  ->out(false);
 132              $navigationdata['previoususer'] = [
 133                  'name' => $reloptions[$navparams['itemid']],
 134                  'url' => $link,
 135                  'previousarrow' => $previousarrow
 136              ];
 137          }
 138          if ($i < count($reloptionssorting) - 1) {
 139              $navparams['itemid'] = $reloptionssorting[$i + 1];
 140              $link = (new moodle_url('/grade/report/singleview/index.php', $navparams))
 141                  ->out(false);
 142              $navigationdata['nextuser'] = [
 143                  'name' => $reloptions[$navparams['itemid']],
 144                  'url' => $link,
 145                  'nextarrow' => $nextarrow
 146              ];
 147          }
 148  
 149          if ($report->screen->supports_paging()) {
 150              $navigationdata['perpageselect'] = $report->screen->perpage_select();
 151              $navigationdata['pager'] = $report->screen->pager();
 152          }
 153  
 154          if (isset($navigationdata)) {
 155              $navigation = $this->render_from_template('gradereport_singleview/report_navigation', $navigationdata);
 156          }
 157          return $navigation;
 158      }
 159  
 160  }