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 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  
  97      // To make some other functions work better later.
  98      if (!$currentgroup) {
  99          $currentgroup = null;
 100      }
 101  
 102      $isseparategroups = ($course->groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context));
 103  
 104      if ($isseparategroups && (!$currentgroup)) {
 105          // No separate group access, The user can view only themselves.
 106          $userid = $USER->id;
 107      }
 108  
 109      // If there is a stored (last viewed) user in a session variable, bypass the user select zero state and display the
 110      // report for that user.
 111      $lastvieweduserid = $SESSION->gradereport_user["useritem-{$context->id}"] ?? null;
 112      if (is_null($userid) && !is_null($lastvieweduserid)) {
 113          $userid = $lastvieweduserid;
 114      }
 115  
 116      $gradableusers = grade_report::get_gradable_users($courseid, $currentgroup);
 117      // Validate whether the requested user is a valid gradable user in this course. If, not display the user select
 118      // zero state.
 119      if (empty($gradableusers) || ($userid && !array_key_exists($userid, $gradableusers))) {
 120          $userid = null;
 121      }
 122  
 123      $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 124      $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 125      $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
 126  
 127      if ($userview == GRADE_REPORT_USER_VIEW_USER) {
 128          $viewasuser = true;
 129      } else {
 130          $viewasuser = false;
 131      }
 132  
 133      $gui = new graded_users_iterator($course, null, $currentgroup);
 134      $gui->require_active_enrolment($showonlyactiveenrol);
 135      $gui->init();
 136  
 137      if (is_null($userid)) { // Zero state.
 138          $actionbar = new \gradereport_user\output\action_bar($context, $userview, null, $currentgroup);
 139          // Print header.
 140          print_grade_page_head($courseid, 'report', 'user', ' ', false, null, true,
 141              null, null, null, $actionbar);
 142  
 143          if (empty($gradableusers)) { // There are no available gradable users, display a notification.
 144              $message = $currentgroup ? get_string('nostudentsingroup') : get_string('nostudentsyet');
 145              echo $OUTPUT->notification($message, 'warning', false);
 146          } else { // Otherwise, display the zero state template.
 147              $report = new gradereport_user\report\user($courseid, $gpr, $context, $USER->id, $viewasuser);
 148              echo $report->output_report_zerostate();
 149          }
 150      } else if ($userid == 0) { // Show all reports.
 151          // Store the id of the current user item in a session variable which represents the last viewed item.
 152          $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
 153  
 154          $actionbar = new \gradereport_user\output\action_bar($context, $userview, 0, $currentgroup);
 155          print_grade_page_head($courseid, 'report', 'user', ' ', false, null, true,
 156              null, null, null, $actionbar);
 157  
 158          while ($userdata = $gui->next_user()) {
 159              $user = $userdata->user;
 160              $report = new gradereport_user\report\user($courseid, $gpr, $context, $user->id, $viewasuser);
 161              $userheading = $gradesrenderer->user_heading($report->user, $courseid, false);
 162  
 163              echo $userheading;
 164  
 165              if ($report->fill_table()) {
 166                  echo $report->print_table(true);
 167              }
 168          }
 169          $gui->close();
 170      } else { // Show one user's report.
 171          // Store the id of the current user item in a session variable which represents the last viewed item.
 172          $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
 173  
 174          $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid, $viewasuser);
 175          $actionbar = new \gradereport_user\output\action_bar($context, $userview, $report->user->id, $currentgroup);
 176  
 177          print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user, $actionbar);
 178  
 179          if ($currentgroup && !groups_is_member($currentgroup, $userid)) {
 180              echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
 181          } else {
 182              if ($report->fill_table()) {
 183                  echo $report->print_table(true);
 184              }
 185          }
 186          $userreportrenderer = $PAGE->get_renderer('gradereport_user');
 187          // Add previous/next user navigation.
 188          echo $userreportrenderer->user_navigation($gui, $userid, $courseid);
 189      }
 190  } else {
 191      // Students will see just their own report.
 192      // Create a report instance.
 193      $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid ?? $USER->id);
 194  
 195      // Print the page.
 196      print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user);
 197  
 198      if ($report->fill_table()) {
 199          echo $report->print_table(true);
 200      }
 201  }
 202  
 203  if (isset($report)) {
 204      // Trigger report viewed event.
 205      $report->viewed();
 206  }
 207  
 208  echo $OUTPUT->footer();