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