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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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  /**
  18   * Renderer for the grade user report
  19   *
  20   * @package   gradereport_user
  21   * @copyright 2010 Sam Hemelryk
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use core\output\comboboxsearch;
  26  
  27  /**
  28   * Custom renderer for the user grade report
  29   *
  30   * To get an instance of this use the following code:
  31   * $renderer = $PAGE->get_renderer('gradereport_user');
  32   *
  33   * @copyright 2010 Sam Hemelryk
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class gradereport_user_renderer extends plugin_renderer_base {
  37  
  38      /**
  39       * Small rendering function that helps with outputting the relevant user selector.
  40       *
  41       * @param string $report
  42       * @param stdClass $course
  43       * @param int $userid
  44       * @param null|int $groupid
  45       * @param bool $includeall
  46       * @return string The raw HTML to render.
  47       * @throws coding_exception
  48       */
  49      public function graded_users_selector(string $report, stdClass $course, int $userid, ?int $groupid, bool $includeall): string {
  50  
  51          $select = grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall);
  52          $output = html_writer::tag('div', $this->output->render($select), ['id' => 'graded_users_selector']);
  53          $output .= html_writer::tag('p', '', ['style' => 'page-break-after: always;']);
  54  
  55          return $output;
  56      }
  57  
  58      /**
  59       * Creates and renders the single select box for the user view.
  60       *
  61       * @param int $userid The selected userid
  62       * @param int $userview The current view user setting constant
  63       * @return string
  64       */
  65      public function view_user_selector(int $userid, int $userview): string {
  66          global $USER;
  67          $url = $this->page->url;
  68          if ($userid != $USER->id) {
  69              $url->param('userid', $userid);
  70          }
  71  
  72          $options = [
  73              GRADE_REPORT_USER_VIEW_USER => get_string('otheruser', 'grades'),
  74              GRADE_REPORT_USER_VIEW_SELF => get_string('myself', 'grades')
  75          ];
  76          $select = new single_select($url, 'userview', $options, $userview, null);
  77  
  78          $select->label = get_string('viewas', 'grades');
  79  
  80          $output = html_writer::tag('div', $this->output->render($select), ['class' => 'view_users_selector']);
  81  
  82          return $output;
  83      }
  84  
  85      /**
  86       * Renders the user selector trigger element.
  87       *
  88       * @param object $course The course object.
  89       * @param int|null $userid The user ID.
  90       * @param int|null $groupid The group ID.
  91       * @return string The raw HTML to render.
  92       * @throws coding_exception
  93       */
  94      public function users_selector(object $course, ?int $userid = null, ?int $groupid = null): string {
  95          $resetlink = new moodle_url('/grade/report/user/index.php', ['id' => $course->id, 'group' => 0]);
  96          $data = [
  97              'currentvalue' => optional_param('searchvalue', '', PARAM_NOTAGS),
  98              'resetlink' => $resetlink->out(false),
  99              'name' => 'userid',
 100              'courseid' => $course->id,
 101              'groupid' => $groupid ?? 0,
 102          ];
 103  
 104          $searchdropdown = new comboboxsearch(
 105              true,
 106              $this->render_from_template('core_user/comboboxsearch/user_selector', $data),
 107              null,
 108              'user-search dropdown d-flex',
 109              null,
 110              'usersearchdropdown overflow-auto',
 111              null,
 112              false,
 113          );
 114          $this->page->requires->js_call_amd('gradereport_user/user', 'init');
 115          return $this->render_from_template($searchdropdown->get_template(), $searchdropdown->export_for_template($this));
 116      }
 117  
 118      /**
 119       * Creates and renders previous/next user navigation.
 120       *
 121       * @param graded_users_iterator $gui Objects that is used to iterate over a list of gradable users in the course.
 122       * @param int $userid The ID of the current user.
 123       * @param int $courseid The course ID.
 124       * @return string The raw HTML to render.
 125       */
 126      public function user_navigation(graded_users_iterator $gui, int $userid, int $courseid): string {
 127  
 128          $navigationdata = [];
 129  
 130          $users = [];
 131          while ($userdata = $gui->next_user()) {
 132              $users[$userdata->user->id] = $userdata->user;
 133          }
 134          $gui->close();
 135  
 136          $arraykeys = array_keys($users);
 137          $keynumber = array_search($userid, $arraykeys);
 138  
 139          // Without a valid user or users list, there's nothing to render.
 140          if ($keynumber === false) {
 141              return '';
 142          }
 143  
 144          // Determine directionality so that icons can be modified to suit language.
 145          $previousarrow = right_to_left() ? 'right' : 'left';
 146          $nextarrow = right_to_left() ? 'left' : 'right';
 147  
 148          // If the current user is not the first one in the list, find and render the previous user.
 149          if ($keynumber !== 0) {
 150              $previoususer = $users[$arraykeys[$keynumber - 1]];
 151              $navigationdata['previoususer'] = [
 152                  'name' => fullname($previoususer),
 153                  'url' => (new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $previoususer->id]))
 154                      ->out(false),
 155                  'previousarrow' => $previousarrow
 156              ];
 157          }
 158          // If the current user is not the last one in the list, find and render the last user.
 159          if ($keynumber < count($users) - 1) {
 160              $nextuser = $users[$arraykeys[$keynumber + 1]];
 161              $navigationdata['nextuser'] = [
 162                  'name' => fullname($nextuser),
 163                  'url' => (new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $nextuser->id]))
 164                      ->out(false),
 165                  'nextarrow' => $nextarrow
 166              ];
 167          }
 168  
 169          return $this->render_from_template('gradereport_user/user_navigation', $navigationdata);
 170      }
 171  
 172      /**
 173       * Creates and renders 'view report as' selector element.
 174       *
 175       * @param int $userid The selected userid
 176       * @param int $userview The current view user setting constant
 177       * @param int $courseid The course ID.
 178       * @return string The raw HTML to render.
 179       */
 180      public function view_mode_selector(int $userid, int $userview, int $courseid): string {
 181  
 182          $viewasotheruser = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
 183              'userview' => GRADE_REPORT_USER_VIEW_USER]);
 184          $viewasmyself = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
 185              'userview' => GRADE_REPORT_USER_VIEW_SELF]);
 186  
 187          $selectoroptions = [
 188              $viewasotheruser->out(false) => get_string('otheruser', 'core_grades'),
 189              $viewasmyself->out(false) => get_string('myself', 'core_grades')
 190          ];
 191  
 192          $selectoractiveurl = $userview === GRADE_REPORT_USER_VIEW_USER ? $viewasotheruser : $viewasmyself;
 193  
 194          $viewasselect = new \core\output\select_menu('viewas', $selectoroptions, $selectoractiveurl->out(false));
 195          $viewasselect->set_label(get_string('viewas', 'core_grades'));
 196  
 197          return $this->render_from_template('gradereport_user/view_mode_selector',
 198              $viewasselect->export_for_template($this));
 199      }
 200  }