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   * The gradebook user report
  19   *
  20   * @package   gradereport_user
  21   * @copyright 2007 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once '../../../config.php';
  26  require_once $CFG->libdir.'/gradelib.php';
  27  require_once $CFG->dirroot.'/grade/lib.php';
  28  require_once $CFG->dirroot.'/grade/report/user/lib.php';
  29  
  30  $courseid = required_param('id', PARAM_INT);
  31  $userid   = optional_param('userid', null, PARAM_INT);
  32  $userview = optional_param('userview', 0, PARAM_INT);
  33  
  34  $PAGE->set_url(new moodle_url('/grade/report/user/index.php', ['id' => $courseid]));
  35  
  36  if ($userview == 0) {
  37      $userview = get_user_preferences('gradereport_user_view_user', GRADE_REPORT_USER_VIEW_USER);
  38  } else {
  39      set_user_preference('gradereport_user_view_user', $userview);
  40  }
  41  
  42  // Basic access checks.
  43  if (!$course = $DB->get_record('course', ['id' => $courseid])) {
  44      throw new \moodle_exception('invalidcourseid');
  45  }
  46  require_login($course);
  47  $PAGE->set_pagelayout('report');
  48  
  49  $context = context_course::instance($course->id);
  50  require_capability('gradereport/user:view', $context);
  51  
  52  if ($userid === 0) {
  53      require_capability('moodle/grade:viewall', $context);
  54  } else if ($userid) {
  55      if (!$DB->get_record('user', ['id' => $userid, 'deleted' => 0]) || isguestuser($userid)) {
  56          throw new \moodle_exception('invaliduser');
  57      }
  58  }
  59  
  60  $access = false;
  61  if (has_capability('moodle/grade:viewall', $context)) {
  62      // User can view all course grades.
  63      $access = true;
  64  } else if (($userid == $USER->id || is_null($userid)) && has_capability('moodle/grade:view', $context) && $course->showgrades) {
  65      // User can view own grades.
  66      $access = true;
  67  } else if (has_capability('moodle/grade:viewall', context_user::instance($userid)) && $course->showgrades) {
  68      // User can view grades of this user, The user is an parent most probably.
  69      $access = true;
  70  }
  71  
  72  if (!$access) {
  73      // The user has no access to grades.
  74      throw new \moodle_exception('nopermissiontoviewgrades', 'error',  $CFG->wwwroot.'/course/view.php?id='.$courseid);
  75  }
  76  
  77  // Initialise the grade tracking object.
  78  $gpr = new grade_plugin_return(['type' => 'report', 'plugin' => 'user', 'courseid' => $courseid, 'userid' => $userid]);
  79  
  80  // Infer the users previously selected report via session tracking.
  81  if (!isset($USER->grade_last_report)) {
  82      $USER->grade_last_report = [];
  83  }
  84  $USER->grade_last_report[$course->id] = 'user';
  85  
  86  // First make sure we have proper final grades.
  87  grade_regrade_final_grades_if_required($course);
  88  
  89  $gradesrenderer = $PAGE->get_renderer('core_grades');
  90  
  91  // Teachers will see all student reports.
  92  if (has_capability('moodle/grade:viewall', $context)) {
  93      // Verify if we are using groups or not.
  94      $groupmode = groups_get_course_groupmode($course);
  95      $currentgroup = $gpr->groupid;
  96      // Conditionally add the group JS if we have groups enabled.
  97      if ($groupmode) {
  98          $PAGE->requires->js_call_amd('gradereport_user/group', 'init');
  99      }
 100  
 101      // To make some other functions work better later.
 102      if (!$currentgroup) {
 103          $currentgroup = null;
 104      }
 105  
 106      $isseparategroups = ($course->groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context));
 107  
 108      if ($isseparategroups && (!$currentgroup)) {
 109          // No separate group access, The user can view only themselves.
 110          $userid = $USER->id;
 111      }
 112  
 113      // If there is a stored (last viewed) user in a session variable, bypass the user select zero state and display the
 114      // report for that user.
 115      $lastvieweduserid = $SESSION->gradereport_user["useritem-{$context->id}"] ?? null;
 116      if (is_null($userid) && !is_null($lastvieweduserid)) {
 117          $userid = $lastvieweduserid;
 118      }
 119  
 120      $gradableusers = grade_report::get_gradable_users($courseid, $currentgroup);
 121      // Validate whether the requested user is a valid gradable user in this course. If, not display the user select
 122      // zero state.
 123      if (empty($gradableusers) || ($userid && !array_key_exists($userid, $gradableusers))) {
 124          $userid = null;
 125      }
 126  
 127      $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 128      $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 129      $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
 130  
 131      if ($userview == GRADE_REPORT_USER_VIEW_USER) {
 132          $viewasuser = true;
 133      } else {
 134          $viewasuser = false;
 135      }
 136  
 137      $gui = new graded_users_iterator($course, null, $currentgroup);
 138      $gui->require_active_enrolment($showonlyactiveenrol);
 139      $gui->init();
 140  
 141      if (is_null($userid)) { // Zero state.
 142          $actionbar = new \gradereport_user\output\action_bar($context, $userview, null, $currentgroup);
 143          // Print header.
 144          print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
 145              null, null, null, $actionbar);
 146  
 147          if (empty($gradableusers)) { // There are no available gradable users, display a notification.
 148              $message = $currentgroup ? get_string('nostudentsingroup') : get_string('nostudentsyet');
 149              echo $OUTPUT->notification($message, 'warning', false);
 150          } else { // Otherwise, display the zero state template.
 151              $report = new gradereport_user\report\user($courseid, $gpr, $context, $USER->id, $viewasuser);
 152              echo $report->output_report_zerostate();
 153          }
 154      } else if ($userid == 0) { // Show all reports.
 155          // Store the id of the current user item in a session variable which represents the last viewed item.
 156          $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
 157  
 158          $actionbar = new \gradereport_user\output\action_bar($context, $userview, 0, $currentgroup);
 159          print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
 160              null, null, null, $actionbar);
 161  
 162          while ($userdata = $gui->next_user()) {
 163              $user = $userdata->user;
 164              $report = new gradereport_user\report\user($courseid, $gpr, $context, $user->id, $viewasuser);
 165              $userheading = $gradesrenderer->user_heading($report->user, $courseid, false);
 166  
 167              echo $userheading;
 168  
 169              if ($report->fill_table()) {
 170                  echo $report->print_table(true);
 171              }
 172          }
 173          $gui->close();
 174      } else { // Show one user's report.
 175          // Store the id of the current user item in a session variable which represents the last viewed item.
 176          $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
 177  
 178          $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid, $viewasuser);
 179          $actionbar = new \gradereport_user\output\action_bar($context, $userview, $report->user->id, $currentgroup);
 180  
 181          print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user, $actionbar);
 182  
 183          if ($currentgroup && !groups_is_member($currentgroup, $userid)) {
 184              echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
 185          } else {
 186              if ($report->fill_table()) {
 187                  echo $report->print_table(true);
 188              }
 189          }
 190          $userreportrenderer = $PAGE->get_renderer('gradereport_user');
 191          // Render the user report (previous/next) navigation in a sticky footer.
 192          $stickyfooter = new core\output\sticky_footer($userreportrenderer->user_navigation($gui, $userid, $courseid));
 193          echo $OUTPUT->render($stickyfooter);
 194      }
 195  } else {
 196      // Students will see just their own report.
 197      // Create a report instance.
 198      $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid ?? $USER->id);
 199  
 200      // Print the page.
 201      print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user);
 202  
 203      if ($report->fill_table()) {
 204          echo $report->print_table(true);
 205      }
 206  }
 207  
 208  if (isset($report)) {
 209      // Trigger report viewed event.
 210      $report->viewed();
 211  }
 212  
 213  echo $OUTPUT->footer();