Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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  /**
  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              'name' => 'userid',
  96              'courseid' => $course->id,
  97              'groupid' => $groupid ?? 0,
  98          ];
  99  
 100          // If a particular option is selected (not in zero state).
 101          if (!is_null($userid)) {
 102              if ($userid) { // A single user selected.
 103                  $user = core_user::get_user($userid);
 104                  $data['selectedoption'] = [
 105                      'image' => $this->user_picture($user, ['size' => 40, 'link' => false]),
 106                      'text' => fullname($user),
 107                      'additionaltext' => $user->email,
 108                  ];
 109              } else { // All users selected.
 110                  // Get the total number of users.
 111                  $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 112                  $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 113                  $showonlyactiveenrol = $showonlyactiveenrol ||
 114                      !has_capability('moodle/course:viewsuspendedusers', context_course::instance($course->id));
 115                  $gui = new graded_users_iterator($course, null, $groupid);
 116                  $gui->require_active_enrolment($showonlyactiveenrol);
 117                  $gui->init();
 118                  $totalusersnum = 0;
 119                  while ($userdata = $gui->next_user()) {
 120                      $totalusersnum++;
 121                  }
 122                  $gui->close();
 123  
 124                  $data['selectedoption'] = [
 125                      'text' => get_string('allusersnum', 'gradereport_user', $totalusersnum),
 126                  ];
 127              }
 128  
 129              $data['userid'] = $userid;
 130          }
 131  
 132          $this->page->requires->js_call_amd('gradereport_user/user', 'init');
 133          return $this->render_from_template('core_grades/user_selector', $data);
 134      }
 135  
 136      /**
 137       * Creates and renders previous/next user navigation.
 138       *
 139       * @param graded_users_iterator $gui Objects that is used to iterate over a list of gradable users in the course.
 140       * @param int $userid The ID of the current user.
 141       * @param int $courseid The course ID.
 142       * @return string The raw HTML to render.
 143       */
 144      public function user_navigation(graded_users_iterator $gui, int $userid, int $courseid): string {
 145  
 146          $navigationdata = [];
 147  
 148          $users = [];
 149          while ($userdata = $gui->next_user()) {
 150              $users[$userdata->user->id] = $userdata->user;
 151          }
 152          $gui->close();
 153  
 154          $arraykeys = array_keys($users);
 155          $keynumber = array_search($userid, $arraykeys);
 156  
 157          // Without a valid user or users list, there's nothing to render.
 158          if ($keynumber === false) {
 159              return '';
 160          }
 161  
 162          // Determine directionality so that icons can be modified to suit language.
 163          $previousarrow = right_to_left() ? 'right' : 'left';
 164          $nextarrow = right_to_left() ? 'left' : 'right';
 165  
 166          // If the current user is not the first one in the list, find and render the previous user.
 167          if ($keynumber !== 0) {
 168              $previoususer = $users[$arraykeys[$keynumber - 1]];
 169              $navigationdata['previoususer'] = [
 170                  'name' => fullname($previoususer),
 171                  'url' => (new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $previoususer->id]))
 172                      ->out(false),
 173                  'previousarrow' => $previousarrow
 174              ];
 175          }
 176          // If the current user is not the last one in the list, find and render the last user.
 177          if ($keynumber < count($users) - 1) {
 178              $nextuser = $users[$arraykeys[$keynumber + 1]];
 179              $navigationdata['nextuser'] = [
 180                  'name' => fullname($nextuser),
 181                  'url' => (new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $nextuser->id]))
 182                      ->out(false),
 183                  'nextarrow' => $nextarrow
 184              ];
 185          }
 186  
 187          return $this->render_from_template('gradereport_user/user_navigation', $navigationdata);
 188      }
 189  
 190      /**
 191       * Creates and renders 'view report as' selector element.
 192       *
 193       * @param int $userid The selected userid
 194       * @param int $userview The current view user setting constant
 195       * @param int $courseid The course ID.
 196       * @return string The raw HTML to render.
 197       */
 198      public function view_mode_selector(int $userid, int $userview, int $courseid): string {
 199  
 200          $viewasotheruser = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
 201              'userview' => GRADE_REPORT_USER_VIEW_USER]);
 202          $viewasmyself = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
 203              'userview' => GRADE_REPORT_USER_VIEW_SELF]);
 204  
 205          $selectoroptions = [
 206              $viewasotheruser->out(false) => get_string('otheruser', 'core_grades'),
 207              $viewasmyself->out(false) => get_string('myself', 'core_grades')
 208          ];
 209  
 210          $selectoractiveurl = $userview === GRADE_REPORT_USER_VIEW_USER ? $viewasotheruser : $viewasmyself;
 211  
 212          $viewasselect = new \core\output\select_menu('viewas', $selectoroptions, $selectoractiveurl->out(false));
 213          $viewasselect->set_label(get_string('viewas', 'core_grades'));
 214  
 215          return $this->render_from_template('gradereport_user/view_mode_selector',
 216              $viewasselect->export_for_template($this));
 217      }
 218  }