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   * The gradebook overview report
  19   *
  20   * @package   gradereport_overview
  21   * @copyright 2007 Nicolas Connault
  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/overview/lib.php';
  29  
  30  $courseid = optional_param('id', SITEID, PARAM_INT);
  31  $userid   = optional_param('userid', $USER->id, PARAM_INT);
  32  
  33  $PAGE->set_url(new moodle_url('/grade/report/overview/index.php', array('id' => $courseid, 'userid' => $userid)));
  34  
  35  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  36      throw new \moodle_exception('invalidcourseid');
  37  }
  38  require_login(null, false);
  39  $PAGE->set_course($course);
  40  
  41  $context = context_course::instance($course->id);
  42  $systemcontext = context_system::instance();
  43  $personalcontext = null;
  44  
  45  // If we are accessing the page from a site context then ignore this check.
  46  if ($courseid != SITEID) {
  47      require_capability('gradereport/overview:view', $context);
  48  }
  49  
  50  if (empty($userid)) {
  51      require_capability('moodle/grade:viewall', $context);
  52  
  53  } else {
  54      if (!$DB->get_record('user', array('id'=>$userid, 'deleted'=>0)) or isguestuser($userid)) {
  55          throw new \moodle_exception('invaliduserid');
  56      }
  57      $personalcontext = context_user::instance($userid);
  58  }
  59  
  60  if (isset($personalcontext) && $courseid == SITEID) {
  61      $PAGE->set_context($personalcontext);
  62  } else {
  63      $PAGE->set_context($context);
  64  }
  65  if ($userid == $USER->id) {
  66      $settings = $PAGE->settingsnav->find('mygrades', null);
  67      $settings->make_active();
  68  } else if ($courseid != SITEID && $userid) {
  69      // Show some other navbar thing.
  70      $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
  71      $PAGE->navigation->extend_for_user($user);
  72  }
  73  
  74  $access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid);
  75  
  76  if (!$access) {
  77      // no access to grades!
  78      throw new \moodle_exception('nopermissiontoviewgrades', 'error',  $CFG->wwwroot.'/course/view.php?id='.$courseid);
  79  }
  80  
  81  /// return tracking object
  82  $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'overview', 'courseid'=>$course->id, 'userid'=>$userid));
  83  
  84  /// last selected report session tracking
  85  if (!isset($USER->grade_last_report)) {
  86      $USER->grade_last_report = array();
  87  }
  88  $USER->grade_last_report[$course->id] = 'overview';
  89  
  90  // First make sure we have proper final grades.
  91  grade_regrade_final_grades_if_required($course);
  92  
  93  $actionbar = new \core_grades\output\general_action_bar($context,
  94      new moodle_url('/grade/report/overview/index.php', ['id' => $courseid]), 'report', 'overview');
  95  
  96  if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
  97      // Please note this would be extremely slow if we wanted to implement this properly for all teachers.
  98      $groupmode    = groups_get_course_groupmode($course);   // Groups are being used
  99      $currentgroup = $gpr->groupid;
 100  
 101      if (!$currentgroup) {      // To make some other functions work better later
 102          $currentgroup = NULL;
 103      }
 104  
 105      $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
 106  
 107      if ($isseparategroups and (!$currentgroup)) {
 108          // no separate group access, can view only self
 109          $userid = $USER->id;
 110          $user_selector = false;
 111      } else {
 112          $user_selector = true;
 113      }
 114  
 115      if (empty($userid)) {
 116          print_grade_page_head($courseid, 'report', 'overview', false, false, false,
 117              true, null, null, null, $actionbar);
 118  
 119          groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
 120  
 121          if ($user_selector) {
 122              $renderer = $PAGE->get_renderer('gradereport_overview');
 123              echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
 124          }
 125          // do not list all users
 126  
 127      } else { // Only show one user's report
 128          $report = new grade_report_overview($userid, $gpr, $context);
 129          print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview') .
 130              ' - ' . fullname($report->user), false, false, true, null, null,
 131              $report->user, $actionbar);
 132          groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
 133  
 134          if ($user_selector) {
 135              $renderer = $PAGE->get_renderer('gradereport_overview');
 136              echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
 137          }
 138  
 139          if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
 140              echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
 141          } else {
 142              if ($report->fill_table()) {
 143                  echo '<br />'.$report->print_table(true);
 144              }
 145          }
 146      }
 147  } else { // Non-admins and users viewing from the site context can just see their own report.
 148  
 149      // Create a report instance
 150      $report = new grade_report_overview($userid, $gpr, $context);
 151  
 152      if (!empty($report->studentcourseids)) {
 153          // If the course id matches the site id then we don't have a course context to work with.
 154          // Display a standard page.
 155          if ($courseid == SITEID) {
 156              $PAGE->set_pagelayout('standard');
 157              $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
 158              $PAGE->set_title($header);
 159              $PAGE->set_heading(fullname($report->user));
 160  
 161              if ($USER->id != $report->user->id) {
 162                  $PAGE->navigation->extend_for_user($report->user);
 163                  if ($node = $PAGE->settingsnav->get('userviewingsettings'.$report->user->id)) {
 164                      $node->forceopen = true;
 165                  }
 166              } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
 167                  $node->forceopen = true;
 168              }
 169  
 170              echo $OUTPUT->header();
 171              if ($report->fill_table(true, true)) {
 172                  echo html_writer::tag('h3', get_string('coursesiamtaking', 'grades'));
 173                  echo '<br />' . $report->print_table(true);
 174              }
 175          } else { // We have a course context. We must be navigating from the gradebook.
 176              print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview')
 177                  . ' - ' . fullname($report->user), false, false, true, null, null,
 178                  $report->user, $actionbar);
 179              if ($report->fill_table()) {
 180                  echo '<br />' . $report->print_table(true);
 181              }
 182          }
 183      } else {
 184          $PAGE->set_pagelayout('standard');
 185          $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
 186          $PAGE->set_title($header);
 187          $PAGE->set_heading(fullname($report->user));
 188          echo $OUTPUT->header();
 189      }
 190  
 191      if (count($report->teachercourses)) {
 192          echo html_writer::tag('h3', get_string('coursesiamteaching', 'grades'));
 193          $report->print_teacher_table();
 194      }
 195  
 196      if (empty($report->studentcourseids) && empty($report->teachercourses)) {
 197          // We have no report to show the user. Let them know something.
 198          echo $OUTPUT->notification(get_string('noreports', 'grades'), 'notifymessage');
 199      }
 200  }
 201  
 202  grade_report_overview::viewed($context, $courseid, $userid);
 203  
 204  echo $OUTPUT->footer();