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   * 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', 100, 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      $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'user');
 169  } else if ($itemtype == 'grade' || $itemtype == 'grade_select') {
 170      $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'grade');
 171  } else {
 172      $actionbar = new \core_grades\output\general_action_bar($context, new moodle_url('/grade/report/singleview/index.php',
 173          ['id' => $courseid]), 'report', 'singleview');
 174  }
 175  
 176  if ($itemtype == 'user') {
 177      print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
 178          true, null, null, $report->screen->item, $actionbar);
 179  } else {
 180      print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button,
 181          true, null, null, null, $actionbar);
 182  }
 183  
 184  if ($data = data_submitted()) {
 185      // Must have a sesskey for all actions.
 186      require_sesskey();
 187      $result = $report->process_data($data);
 188  
 189      // If result is not null (because somedata was processed), warnings and success message should be displayed.
 190      if (!is_null($result)) {
 191          if (!empty($result->warnings)) {
 192              foreach ($result->warnings as $warning) {
 193                  \core\notification::add($warning);
 194              }
 195          }
 196  
 197          // And notify the user of the success result.
 198          \core\notification::add(
 199              get_string('savegradessuccess', 'gradereport_singleview', count((array) $result->changecount)),
 200              \core\notification::SUCCESS
 201          );
 202      }
 203  }
 204  
 205  // Make sure we have proper final grades.
 206  grade_regrade_final_grades_if_required($course);
 207  
 208  echo $report->output();
 209  // Save the screen state in a session variable as last viewed state.
 210  $SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype;
 211  if ($itemid) {
 212      $SESSION->gradereport_singleview["{$itemtype}item-{$context->id}"] = $itemid;
 213  }
 214  
 215  if (($itemtype !== 'select') && ($itemtype !== 'grade_select') &&($itemtype !== 'user_select')) {
 216      $item = (isset($userid)) ? $userid : $itemid;
 217  
 218      $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
 219      $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
 220      $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
 221  
 222      $gui = new graded_users_iterator($course, null, $currentgroup);
 223      $gui->require_active_enrolment($showonlyactiveenrol);
 224      $gui->init();
 225  
 226      $userreportrenderer = $PAGE->get_renderer('gradereport_singleview');
 227      // Add previous/next user navigation.
 228      echo $userreportrenderer->report_navigation($gpr, $courseid, $context, $report, $groupid, $itemtype, $itemid);
 229  }
 230  
 231  $event = \gradereport_singleview\event\grade_report_viewed::create(
 232      [
 233          'context' => $context,
 234          'courseid' => $courseid,
 235          'relateduserid' => $USER->id,
 236      ]
 237  );
 238  $event->trigger();
 239  
 240  echo $OUTPUT->footer();