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   * Displays the Single view
  19   *
  20   * @package   gradereport_singleview
  21   * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('NO_OUTPUT_BUFFERING', true);
  26  
  27  require_once('../../../config.php');
  28  require_once($CFG->dirroot.'/lib/gradelib.php');
  29  require_once($CFG->dirroot.'/grade/lib.php');
  30  require_once($CFG->dirroot.'/grade/report/lib.php');
  31  
  32  $courseid = required_param('id', PARAM_INT);
  33  $groupid  = optional_param('group', null, PARAM_INT);
  34  
  35  // Making this work with profile reports.
  36  $userid   = optional_param('userid', null, PARAM_INT);
  37  $itemid = optional_param('itemid', null, PARAM_INT);
  38  $itemtype = optional_param('item', null, PARAM_TEXT);
  39  $page = optional_param('page', 0, PARAM_INT);
  40  $perpage = optional_param('perpage', null, PARAM_INT);
  41  
  42  $edit = optional_param('edit', -1, PARAM_BOOL); // Sticky editing mode.
  43  
  44  $courseparams = ['id' => $courseid];
  45  
  46  $PAGE->set_pagelayout('report');
  47  $PAGE->set_other_editing_capability('moodle/grade:edit');
  48  
  49  if (!$course = $DB->get_record('course', $courseparams)) {
  50      throw new \moodle_exception('invalidcourseid');
  51  }
  52  
  53  require_login($course);
  54  
  55  $context = context_course::instance($course->id);
  56  
  57  // This is the normal requirements.
  58  require_capability('gradereport/singleview:view', $context);
  59  require_capability('moodle/grade:viewall', $context);
  60  require_capability('moodle/grade:edit', $context);
  61  
  62  $gpr = new grade_plugin_return([
  63      'type' => 'report',
  64      'plugin' => 'singleview',
  65      'courseid' => $courseid
  66  ]);
  67  
  68  // Last selected report session tracking.
  69  if (!isset($USER->grade_last_report)) {
  70      $USER->grade_last_report = [];
  71  }
  72  $USER->grade_last_report[$course->id] = 'singleview';
  73  // If the item type is not explicitly defined or not valid, try to use the last viewed one (obtain in from the session)
  74  // or fallback to the user select (zero) state.
  75  if (!$itemtype || !in_array($itemtype, \gradereport_singleview\report\singleview::valid_screens())) {
  76      $itemtype = isset($SESSION->gradereport_singleview["itemtype-{$context->id}"]) ?
  77          $SESSION->gradereport_singleview["itemtype-{$context->id}"] : 'user_select';
  78  }
  79  
  80  $currentgroup = $gpr->groupid;
  81  // To make some other functions work better later.
  82  if (!$currentgroup) {
  83      $currentgroup = null;
  84  }
  85  
  86  $lastvieweduseritemid = $SESSION->gradereport_singleview["useritem-{$context->id}"] ?? null;
  87  $lastviewedgradeitemid = $SESSION->gradereport_singleview["gradeitem-{$context->id}"] ?? null;
  88  
  89  switch ($itemtype) {
  90      case 'user_select':
  91          // If there is a stored user item (last viewed) in a session variable, bypass the user select zero state
  92          // and display this user item. Also, make sure that the stored last viewed user is part of the current
  93          // list of gradable users in this course.
  94          if ($lastvieweduseritemid &&
  95                  array_key_exists($lastvieweduseritemid, grade_report::get_gradable_users($courseid, $currentgroup))) {
  96              $itemtype = 'user';
  97              $itemid = $lastvieweduseritemid;
  98          } else {
  99              $itemid = null;
 100          }
 101          break;
 102      case 'user':
 103          if (is_null($itemid)) {
 104              $itemid = $userid ?? $lastvieweduseritemid;
 105          }
 106          // If the item id (user id) cannot be defined or the user id is not part of the list of gradable users,
 107          // display the user select zero state.
 108          if (is_null($itemid) || !array_key_exists($itemid, grade_report::get_gradable_users($courseid, $currentgroup))) {
 109              $itemtype = 'user_select';
 110          }
 111          break;
 112      case 'grade_select':
 113          // If there is a stored grade item (last viewed) in a session variable, bypass the grade item select zero state
 114          // and display this grade item.
 115          if ($lastviewedgradeitemid) {
 116              $itemtype = 'grade';
 117              $itemid = $lastviewedgradeitemid;
 118          } else {
 119              $itemid = null;
 120          }
 121          break;
 122      case 'grade':
 123          // If there is a stored grade item (last viewed) in a session variable, use it.
 124          if (is_null($itemid) && $lastviewedgradeitemid) {
 125              $itemid = $lastviewedgradeitemid;
 126          }
 127          $gtree = new grade_tree($courseid, false, false, null, !$CFG->enableoutcomes);
 128          $gradeableitems = $gtree->get_items();
 129          // The item id (grade item id) cannot be defined, display the grade select zero state.
 130          if (is_null($itemid) || !array_key_exists($itemid, $gtree->get_items())) {
 131              $itemtype = 'grade_select';
 132          }
 133          break;
 134  }
 135  
 136  $report = new gradereport_singleview\report\singleview($courseid, $gpr, $context, $itemtype, $itemid);
 137  
 138  $pageparams = [
 139      'id'        => $courseid,
 140      'userid'    => $userid,
 141      'itemid'    => $itemid,
 142      'item'      => $itemtype,
 143      'page'      => $page,
 144      'perpage'   => $perpage,
 145  ];
 146  
 147  if (!is_null($groupid)) {
 148      $pageparams['group'] = $groupid;
 149  }
 150  
 151  $PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams));
 152  
 153  // Build editing on/off button for themes that need it.
 154  $button = '';
 155  if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
 156      if ($edit != - 1) {
 157          $USER->editing = $edit;
 158      }
 159  
 160      // Page params for the turn editing on button.
 161      $options = $gpr->get_options();
 162      $button = $OUTPUT->edit_button(new moodle_url($PAGE->url, $options), 'get');
 163  }
 164  
 165  $reportname = $report->screen->heading();
 166  
 167  if ($itemtype == 'user' || $itemtype == 'user_select') {
 168      $PAGE->requires->js_call_amd('gradereport_singleview/user', 'init');
 169      $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'user');
 170  } else if ($itemtype == 'grade' || $itemtype == 'grade_select') {
 171      $PAGE->requires->js_call_amd('gradereport_singleview/grade', 'init');
 172      $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'grade');
 173  } else {
 174      $actionbar = new \core_grades\output\general_action_bar($context, new moodle_url('/grade/report/singleview/index.php',
 175          ['id' => $courseid]), 'report', 'singleview');
 176  }
 177  if ($course->groupmode && $itemtype !== 'select') {
 178      $PAGE->requires->js_call_amd('gradereport_singleview/group', 'init', [$itemtype]);
 179  }
 180  
 181  if ($itemtype == 'user') {
 182      print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
 183          true, null, null, $report->screen->item, $actionbar);
 184  } else {
 185      print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
 186          true, null, null, null, $actionbar);
 187  }
 188  
 189  if ($data = data_submitted()) {
 190      // Must have a sesskey for all actions.
 191      require_sesskey();
 192      $result = $report->process_data($data);
 193  
 194      // If result is not null (because somedata was processed), warnings and success message should be displayed.
 195      if (!is_null($result)) {
 196          if (!empty($result->warnings)) {
 197              foreach ($result->warnings as $warning) {
 198                  \core\notification::add($warning);
 199              }
 200          }
 201  
 202          // And notify the user of the success result.
 203          \core\notification::add(
 204              get_string('savegradessuccess', 'gradereport_singleview', count((array) $result->changecount)),
 205              \core\notification::SUCCESS
 206          );
 207      }
 208  }
 209  
 210  // Make sure we have proper final grades.
 211  grade_regrade_final_grades_if_required($course);
 212  
 213  // Save the screen state in a session variable as last viewed state.
 214  $SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype;
 215  if ($itemid) {
 216      $SESSION->gradereport_singleview["{$itemtype}item-{$context->id}"] = $itemid;
 217  }
 218  
 219  $stickyfooter = '';
 220  if (($itemtype !== 'select') && ($itemtype !== 'grade_select') &&($itemtype !== 'user_select')) {
 221      $item = (isset($userid)) ? $userid : $itemid;
 222  
 223      $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 224      $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 225      $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
 226  
 227      $gui = new graded_users_iterator($course, null, $currentgroup);
 228      $gui->require_active_enrolment($showonlyactiveenrol);
 229      $gui->init();
 230  
 231      $userreportrenderer = $PAGE->get_renderer('gradereport_singleview');
 232      // Add previous/next user navigation.
 233      $footercontent = $userreportrenderer->report_navigation($gpr, $courseid, $context, $report, $groupid, $itemtype, $itemid);
 234  
 235      $buttonhtml = implode(' ', $report->screen->buttons($report->screen->is_readonly()));
 236      $footercontent .= $report->screen->bulk_insert() . $buttonhtml;
 237  
 238      $stickyfooter = new core\output\sticky_footer($footercontent);
 239      $stickyfooter = $OUTPUT->render($stickyfooter);
 240  
 241  }
 242  
 243  echo $OUTPUT->render_from_template('gradereport_singleview/report', [
 244      'table' => $report->output(),
 245      'stickyfooter' => $stickyfooter,
 246      'sesskey' => sesskey()
 247  ]);
 248  $event = \gradereport_singleview\event\grade_report_viewed::create(
 249      [
 250          'context' => $context,
 251          'courseid' => $courseid,
 252          'relateduserid' => $USER->id,
 253      ]
 254  );
 255  $event->trigger();
 256  
 257  echo $OUTPUT->footer();