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.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Displays the Single view
 *
 * @package   gradereport_singleview
 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

define('NO_OUTPUT_BUFFERING', true);

require_once('../../../config.php');
require_once($CFG->dirroot.'/lib/gradelib.php');
require_once($CFG->dirroot.'/grade/lib.php');
< require_once($CFG->dirroot.'/grade/report/singleview/lib.php');
> require_once($CFG->dirroot.'/grade/report/lib.php');
$courseid = required_param('id', PARAM_INT); $groupid = optional_param('group', null, PARAM_INT); // Making this work with profile reports. $userid = optional_param('userid', null, PARAM_INT);
< < $defaulttype = $userid ? 'user' : 'select'; <
$itemid = optional_param('itemid', null, PARAM_INT);
< $itemtype = optional_param('item', $defaulttype, PARAM_TEXT);
> $itemtype = optional_param('item', null, PARAM_TEXT);
$page = optional_param('page', 0, PARAM_INT); $perpage = optional_param('perpage', 100, PARAM_INT);
< if (empty($itemid)) { < $itemid = $userid; < $itemtype = $defaulttype; < }
> $edit = optional_param('edit', -1, PARAM_BOOL); // Sticky editing mode.
< $courseparams = array('id' => $courseid); < $pageparams = array( < 'id' => $courseid, < 'group' => $groupid, < 'userid' => $userid, < 'itemid' => $itemid, < 'item' => $itemtype, < 'page' => $page, < 'perpage' => $perpage, < ); < $PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams)); < $PAGE->set_pagelayout('incourse');
> $courseparams = ['id' => $courseid]; > > $PAGE->set_pagelayout('report'); > $PAGE->set_other_editing_capability('moodle/grade:edit');
if (!$course = $DB->get_record('course', $courseparams)) {
< print_error('invalidcourseid');
> throw new \moodle_exception('invalidcourseid');
} require_login($course);
< if (!in_array($itemtype, gradereport_singleview::valid_screens())) { < print_error('notvalid', 'gradereport_singleview', '', $itemtype); < } <
$context = context_course::instance($course->id); // This is the normal requirements. require_capability('gradereport/singleview:view', $context); require_capability('moodle/grade:viewall', $context); require_capability('moodle/grade:edit', $context);
< $gpr = new grade_plugin_return(array(
> $gpr = new grade_plugin_return([
'type' => 'report', 'plugin' => 'singleview', 'courseid' => $courseid
< ));
> ]);
// Last selected report session tracking. if (!isset($USER->grade_last_report)) {
< $USER->grade_last_report = array();
> $USER->grade_last_report = [];
} $USER->grade_last_report[$course->id] = 'singleview';
> // If the item type is not explicitly defined or not valid, try to use the last viewed one (obtain in from the session) > // or fallback to the user select (zero) state. // First make sure we have proper final grades. > if (!$itemtype || !in_array($itemtype, \gradereport_singleview\report\singleview::valid_screens())) { grade_regrade_final_grades_if_required($course); > $itemtype = isset($SESSION->gradereport_singleview["itemtype-{$context->id}"]) ? > $SESSION->gradereport_singleview["itemtype-{$context->id}"] : 'user_select'; $report = new gradereport_singleview($courseid, $gpr, $context, $itemtype, $itemid); > } > $reportname = $report->screen->heading(); > $currentgroup = $gpr->groupid; > // To make some other functions work better later. $pluginname = get_string('pluginname', 'gradereport_singleview'); > if (!$currentgroup) { > $currentgroup = null; $pageparams = array( > } 'id' => $courseid, > 'itemid' => $itemid, > $lastvieweduseritemid = $SESSION->gradereport_singleview["useritem-{$context->id}"] ?? null; 'item' => $itemtype, > $lastviewedgradeitemid = $SESSION->gradereport_singleview["gradeitem-{$context->id}"] ?? null; 'userid' => $userid, > 'group' => $groupid, > switch ($itemtype) { 'page' => $page, > case 'user_select': 'perpage' => $perpage > // If there is a stored user item (last viewed) in a session variable, bypass the user select zero state ); > // and display this user item. Also, make sure that the stored last viewed user is part of the current > // list of gradable users in this course. $currentpage = new moodle_url('/grade/report/singleview/index.php', $pageparams); > if ($lastvieweduseritemid && > array_key_exists($lastvieweduseritemid, grade_report::get_gradable_users($courseid, $currentgroup))) { if ($data = data_submitted()) { > $itemtype = 'user'; $PAGE->set_pagelayout('redirect'); > $itemid = $lastvieweduseritemid; $PAGE->set_title(get_string('savegrades', 'gradereport_singleview')); > } else { echo $OUTPUT->header(); > $itemid = null; > } require_sesskey(); // Must have a sesskey for all actions. > break; $result = $report->process_data($data); > case 'user': > if (is_null($itemid)) { if (!empty($result->warnings)) { > $itemid = $userid ?? $lastvieweduseritemid; foreach ($result->warnings as $warning) { > } echo $OUTPUT->notification($warning); > // If the item id (user id) cannot be defined or the user id is not part of the list of gradable users, } > // display the user select zero state. } > if (is_null($itemid) || !array_key_exists($itemid, grade_report::get_gradable_users($courseid, $currentgroup))) { echo $OUTPUT->notification( > $itemtype = 'user_select'; get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount)), > } 'success' > break; ); > case 'grade_select': echo $OUTPUT->continue_button($currentpage); > // If there is a stored grade item (last viewed) in a session variable, bypass the grade item select zero state echo $OUTPUT->footer(); > // and display this grade item. die(); > if ($lastviewedgradeitemid) { } > $itemtype = 'grade'; > $itemid = $lastviewedgradeitemid; $PAGE->set_pagelayout('report'); > } else { if ($itemtype == 'user') { > $itemid = null; print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false, true, null, null, $report->screen->item); > } } else { > break; print_grade_page_head($course->id, 'report', 'singleview', $reportname); > case 'grade': } > // If there is a stored grade item (last viewed) in a session variable, use it. > if (is_null($itemid) && $lastviewedgradeitemid) { $graderrightnav = $graderleftnav = null; > $itemid = $lastviewedgradeitemid; > } $options = $report->screen->options(); > $gtree = new grade_tree($courseid, false, false, null, !$CFG->enableoutcomes); > $gradeableitems = $gtree->get_items(); if (!empty($options)) { > // The item id (grade item id) cannot be defined, display the grade select zero state. > if (is_null($itemid) || !array_key_exists($itemid, $gtree->get_items())) { $optionkeys = array_keys($options); > $itemtype = 'grade_select'; $optionitemid = array_shift($optionkeys); > } > break; $relreport = new gradereport_singleview( > }
< // First make sure we have proper final grades. < grade_regrade_final_grades_if_required($course); < < $report = new gradereport_singleview($courseid, $gpr, $context, $itemtype, $itemid); < < $reportname = $report->screen->heading(); < < $pluginname = get_string('pluginname', 'gradereport_singleview');
> $report = new gradereport_singleview\report\singleview($courseid, $gpr, $context, $itemtype, $itemid);
< $pageparams = array(
> $pageparams = [
$navparams['itemid'] = $reloptionssorting[$i - 1];
> 'userid' => $userid,
< 'userid' => $userid, < 'group' => $groupid,
< 'perpage' => $perpage < ); < < $currentpage = new moodle_url('/grade/report/singleview/index.php', $pageparams);
> 'perpage' => $perpage, > ];
< if ($data = data_submitted()) { < $PAGE->set_pagelayout('redirect'); < $PAGE->set_title(get_string('savegrades', 'gradereport_singleview')); < echo $OUTPUT->header();
> if (!is_null($groupid)) { > $pageparams['group'] = $groupid; > }
< require_sesskey(); // Must have a sesskey for all actions. < $result = $report->process_data($data);
> $PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams));
< if (!empty($result->warnings)) { < foreach ($result->warnings as $warning) { < echo $OUTPUT->notification($warning);
> // Build editing on/off button for themes that need it. > $button = ''; > if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) { > if ($edit != - 1) { > $USER->editing = $edit;
if (!is_null($graderrightnav)) {
> echo $graderrightnav; > // Page params for the turn editing on button. } > $options = $gpr->get_options(); > $button = $OUTPUT->edit_button(new moodle_url($PAGE->url, $options), 'get');
< echo $OUTPUT->notification( < get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount)), < 'success' < ); < echo $OUTPUT->continue_button($currentpage); < echo $OUTPUT->footer(); < die();
> > $reportname = $report->screen->heading(); > > if ($itemtype == 'user' || $itemtype == 'user_select') { > $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'user'); > } else if ($itemtype == 'grade' || $itemtype == 'grade_select') { > $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'grade'); > } else { > $actionbar = new \core_grades\output\general_action_bar($context, new moodle_url('/grade/report/singleview/index.php', > ['id' => $courseid]), 'report', 'singleview');
< $PAGE->set_pagelayout('report');
< print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false, true, null, null, $report->screen->item);
> print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button, > true, null, null, $report->screen->item, $actionbar);
< print_grade_page_head($course->id, 'report', 'singleview', $reportname);
> print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, $button, > true, null, null, null, $actionbar);
< $graderrightnav = $graderleftnav = null; < < $options = $report->screen->options(); < < if (!empty($options)) { < < $optionkeys = array_keys($options); < $optionitemid = array_shift($optionkeys); < < $relreport = new gradereport_singleview( < $courseid, $gpr, $context, < $report->screen->item_type(), $optionitemid < ); < $reloptions = $relreport->screen->options(); < $reloptionssorting = array_keys($relreport->screen->options());
> if ($data = data_submitted()) { > // Must have a sesskey for all actions. > require_sesskey(); > $result = $report->process_data($data);
< $i = array_search($itemid, $reloptionssorting); < $navparams = array('item' => $itemtype, 'id' => $courseid, 'group' => $groupid); < if ($i > 0) { < $navparams['itemid'] = $reloptionssorting[$i - 1]; < $link = new moodle_url('/grade/report/singleview/index.php', $navparams); < $navprev = html_writer::link($link, $OUTPUT->larrow() . ' ' . $reloptions[$reloptionssorting[$i - 1]]); < $graderleftnav = html_writer::tag('div', $navprev, array('class' => 'itemnav previtem')); < } < if ($i < count($reloptionssorting) - 1) { < $navparams['itemid'] = $reloptionssorting[$i + 1]; < $link = new moodle_url('/grade/report/singleview/index.php', $navparams); < $navnext = html_writer::link($link, $reloptions[$reloptionssorting[$i + 1]] . ' ' . $OUTPUT->rarrow()); < $graderrightnav = html_writer::tag('div', $navnext, array('class' => 'itemnav nextitem'));
> // If result is not null (because somedata was processed), warnings and success message should be displayed. > if (!is_null($result)) { > if (!empty($result->warnings)) { > foreach ($result->warnings as $warning) { > \core\notification::add($warning);
< if (!is_null($graderleftnav)) { < echo $graderleftnav; < } < if (!is_null($graderrightnav)) { < echo $graderrightnav;
> // And notify the user of the success result. > \core\notification::add( > get_string('savegradessuccess', 'gradereport_singleview', count((array) $result->changecount)), > \core\notification::SUCCESS > );
< < if ($report->screen->supports_paging()) { < echo $report->screen->pager();
< if ($report->screen->display_group_selector()) { < echo $report->group_selector; < }
> // Make sure we have proper final grades. > grade_regrade_final_grades_if_required($course);
< < if ($report->screen->supports_paging()) { < echo $report->screen->perpage_select(); < echo $report->screen->pager(); < } < < if (!is_null($graderleftnav)) { < echo $graderleftnav; < } < if (!is_null($graderrightnav)) { < echo $graderrightnav;
> // Save the screen state in a session variable as last viewed state. > $SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype; > if ($itemid) { > $SESSION->gradereport_singleview["{$itemtype}item-{$context->id}"] = $itemid; > } > > if (($itemtype !== 'select') && ($itemtype !== 'grade_select') &&($itemtype !== 'user_select')) { > $item = (isset($userid)) ? $userid : $itemid; > > $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); > $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol); > $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context); > > $gui = new graded_users_iterator($course, null, $currentgroup); > $gui->require_active_enrolment($showonlyactiveenrol); > $gui->init(); > > $userreportrenderer = $PAGE->get_renderer('gradereport_singleview'); > // Add previous/next user navigation. > echo $userreportrenderer->report_navigation($gpr, $courseid, $context, $report, $groupid, $itemtype, $itemid);
< array(
> [
< )
> ]