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   * The gradebook grader report
  19   *
  20   * @package   gradereport_grader
  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.'/user/renderer.php');
  28  require_once($CFG->dirroot.'/grade/lib.php');
  29  require_once($CFG->dirroot.'/grade/report/grader/lib.php');
  30  
  31  $courseid      = required_param('id', PARAM_INT);        // course id
  32  $page          = optional_param('page', 0, PARAM_INT);   // active page
  33  $edit          = optional_param('edit', -1, PARAM_BOOL); // sticky editting mode
  34  
  35  $sortitemid    = optional_param('sortitemid', 0, PARAM_ALPHANUMEXT);
  36  $sort          = optional_param('sort', '', PARAM_ALPHA);
  37  $action        = optional_param('action', 0, PARAM_ALPHAEXT);
  38  $move          = optional_param('move', 0, PARAM_INT);
  39  $type          = optional_param('type', 0, PARAM_ALPHA);
  40  $target        = optional_param('target', 0, PARAM_ALPHANUM);
  41  $toggle        = optional_param('toggle', null, PARAM_INT);
  42  $toggle_type   = optional_param('toggle_type', 0, PARAM_ALPHANUM);
  43  
  44  $graderreportsifirst  = optional_param('sifirst', null, PARAM_NOTAGS);
  45  $graderreportsilast   = optional_param('silast', null, PARAM_NOTAGS);
  46  
  47  $studentsperpage = optional_param('perpage', null, PARAM_INT);
  48  
  49  $PAGE->set_url(new moodle_url('/grade/report/grader/index.php', array('id'=>$courseid)));
  50  $PAGE->set_pagelayout('report');
  51  $PAGE->requires->js_call_amd('gradereport_grader/stickycolspan', 'init');
  52  $PAGE->requires->js_call_amd('gradereport_grader/search', 'init');
  53  $PAGE->requires->js_call_amd('gradereport_grader/feedback_modal', 'init');
  54  
  55  // basic access checks
  56  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  57      throw new \moodle_exception('invalidcourseid');
  58  }
  59  require_login($course);
  60  $context = context_course::instance($course->id);
  61  
  62  // The report object is recreated each time, save search information to SESSION object for future use.
  63  if (isset($graderreportsifirst)) {
  64      $SESSION->gradereport["filterfirstname-{$context->id}"] = $graderreportsifirst;
  65  }
  66  if (isset($graderreportsilast)) {
  67      $SESSION->gradereport["filtersurname-{$context->id}"] = $graderreportsilast;
  68  }
  69  
  70  if (isset($studentsperpage) && $studentsperpage >= 0) {
  71      set_user_preference('grade_report_studentsperpage', $studentsperpage);
  72  }
  73  
  74  require_capability('gradereport/grader:view', $context);
  75  require_capability('moodle/grade:viewall', $context);
  76  
  77  // return tracking object
  78  $gpr = new grade_plugin_return(
  79      array(
  80          'type' => 'report',
  81          'plugin' => 'grader',
  82          'course' => $course,
  83          'page' => $page
  84      )
  85  );
  86  
  87  // last selected report session tracking
  88  if (!isset($USER->grade_last_report)) {
  89      $USER->grade_last_report = array();
  90  }
  91  $USER->grade_last_report[$course->id] = 'grader';
  92  
  93  // Build editing on/off buttons.
  94  $buttons = '';
  95  
  96  $PAGE->set_other_editing_capability('moodle/grade:edit');
  97  if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
  98      if ($edit != - 1) {
  99          $USER->editing = $edit;
 100      }
 101  
 102      // Page params for the turn editing on button.
 103      $options = $gpr->get_options();
 104      $buttons = $OUTPUT->edit_button(new moodle_url($PAGE->url, $options), 'get');
 105  }
 106  
 107  $gradeserror = array();
 108  
 109  // Handle toggle change request
 110  if (!is_null($toggle) && !empty($toggle_type)) {
 111      set_user_preferences(array('grade_report_show'.$toggle_type => $toggle));
 112  }
 113  
 114  // Perform actions
 115  if (!empty($target) && !empty($action) && confirm_sesskey()) {
 116      grade_report_grader::do_process_action($target, $action, $courseid);
 117  }
 118  
 119  // Do this check just before printing the grade header (and only do it once).
 120  grade_regrade_final_grades_if_required($course);
 121  
 122  //Initialise the grader report object that produces the table
 123  //the class grade_report_grader_ajax was removed as part of MDL-21562
 124  if ($sort && strcasecmp($sort, 'desc') !== 0) {
 125      $sort = 'asc';
 126  }
 127  // We have lots of hardcoded 'ASC' and 'DESC' strings in grade/report/grader.lib :(. So we need to uppercase the sort.
 128  $sort = strtoupper($sort);
 129  
 130  $report = new grade_report_grader($courseid, $gpr, $context, $page, $sortitemid, $sort);
 131  
 132  // We call this a little later since we need some info from the grader report.
 133  $PAGE->requires->js_call_amd('gradereport_grader/collapse', 'init', [
 134      'userID' => $USER->id,
 135      'courseID' => $courseid,
 136      'defaultSort' => $report->get_default_sortable()
 137  ]);
 138  
 139  $numusers = $report->get_numusers(true, true);
 140  
 141  $actionbar = new \gradereport_grader\output\action_bar($context, $report, $numusers);
 142  print_grade_page_head($COURSE->id, 'report', 'grader', false, false, $buttons, true,
 143      null, null, null, $actionbar, false);
 144  
 145  // make sure separate group does not prevent view
 146  if ($report->currentgroup == -2) {
 147      echo $OUTPUT->heading(get_string("notingroup"));
 148      echo $OUTPUT->footer();
 149      exit;
 150  }
 151  
 152  $warnings = [];
 153  $isediting = has_capability('moodle/grade:edit', $context) && isset($USER->editing) && $USER->editing;
 154  if ($isediting && ($data = data_submitted()) && confirm_sesskey()) {
 155      // Processing posted grades here.
 156      $warnings = $report->process_data($data);
 157  }
 158  
 159  // Final grades MUST be loaded after the processing.
 160  $report->load_users();
 161  $report->load_final_grades();
 162  
 163  //show warnings if any
 164  foreach ($warnings as $warning) {
 165      echo $OUTPUT->notification($warning);
 166  }
 167  
 168  $displayaverages = true;
 169  if ($numusers == 0) {
 170      $displayaverages = false;
 171  }
 172  
 173  $reporthtml = $report->get_grade_table($displayaverages);
 174  
 175  $studentsperpage = $report->get_students_per_page();
 176  
 177  // Print per-page dropdown.
 178  $pagingoptions = grade_report_grader::PAGINATION_OPTIONS;
 179  if ($studentsperpage) {
 180      $pagingoptions[] = $studentsperpage; // To make sure the current preference is within the options.
 181  }
 182  $pagingoptions = array_unique($pagingoptions);
 183  sort($pagingoptions);
 184  $pagingoptions = array_combine($pagingoptions, $pagingoptions);
 185  if ($numusers > grade_report_grader::MAX_STUDENTS_PER_PAGE) {
 186      $pagingoptions['0'] = grade_report_grader::MAX_STUDENTS_PER_PAGE;
 187  } else {
 188      $pagingoptions['0'] = get_string('all');
 189  }
 190  
 191  $perpagedata = [
 192      'baseurl' => new moodle_url('/grade/report/grader/index.php', ['id' => s($courseid), 'report' => 'grader']),
 193      'options' => []
 194  ];
 195  foreach ($pagingoptions as $key => $name) {
 196      $perpagedata['options'][] = [
 197          'name' => $name,
 198          'value' => $key,
 199          'selected' => $key == $studentsperpage,
 200      ];
 201  }
 202  
 203  $footercontent = html_writer::div(
 204      $OUTPUT->render_from_template('gradereport_grader/perpage', $perpagedata)
 205      , 'col-auto'
 206  );
 207  
 208  // The number of students per page is always limited even if it is claimed to be unlimited.
 209  $studentsperpage = $studentsperpage ?: grade_report_grader::MAX_STUDENTS_PER_PAGE;
 210  $footercontent .= html_writer::div(
 211      $OUTPUT->paging_bar($numusers, $report->page, $studentsperpage, $report->pbarurl),
 212      'col'
 213  );
 214  
 215  // print submit button
 216  if (!empty($USER->editing) && $report->get_pref('quickgrading')) {
 217      echo '<form action="index.php" enctype="application/x-www-form-urlencoded" method="post" id="gradereport_grader">'; // Enforce compatibility with our max_input_vars hack.
 218      echo '<div>';
 219      echo '<input type="hidden" value="'.s($courseid).'" name="id" />';
 220      echo '<input type="hidden" value="'.sesskey().'" name="sesskey" />';
 221      echo '<input type="hidden" value="'.time().'" name="timepageload" />';
 222      echo '<input type="hidden" value="grader" name="report"/>';
 223      echo '<input type="hidden" value="'.$page.'" name="page"/>';
 224      echo $gpr->get_form_fields();
 225      echo $reporthtml;
 226  
 227      $footercontent .= html_writer::div(
 228          '<input type="submit" id="gradersubmit" class="btn btn-primary" value="'.s(get_string('savechanges')).'" />',
 229          'col-auto'
 230      );
 231  
 232      $stickyfooter = new core\output\sticky_footer($footercontent);
 233      echo $OUTPUT->render($stickyfooter);
 234  
 235      echo '</div></form>';
 236  } else {
 237      echo $reporthtml;
 238  
 239      $stickyfooter = new core\output\sticky_footer($footercontent);
 240      echo $OUTPUT->render($stickyfooter);
 241  }
 242  
 243  $event = \gradereport_grader\event\grade_report_viewed::create(
 244      array(
 245          'context' => $context,
 246          'courseid' => $courseid,
 247      )
 248  );
 249  $event->trigger();
 250  
 251  echo $OUTPUT->footer();