Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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);
> $perpage = optional_param('perpage', null, 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(get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount))); > $itemtype = 'user_select'; echo $OUTPUT->continue_button($currentpage); > } echo $OUTPUT->footer(); > break; die(); > case 'grade_select': } > // If there is a stored grade item (last viewed) in a session variable, bypass the grade item select zero state > // and display this grade item. $PAGE->set_pagelayout('report'); > if ($lastviewedgradeitemid) { if ($itemtype == 'user') { > $itemtype = 'grade'; print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false, true, null, null, $report->screen->item); > $itemid = $lastviewedgradeitemid; } else { > } else { print_grade_page_head($course->id, 'report', 'singleview', $reportname); > $itemid = null; } > } > break; $graderrightnav = $graderleftnav = null; > case 'grade': > // If there is a stored grade item (last viewed) in a session variable, use it. $options = $report->screen->options(); > if (is_null($itemid) && $lastviewedgradeitemid) { > $itemid = $lastviewedgradeitemid; if (!empty($options)) { > } > $gtree = new grade_tree($courseid, false, false, null, !$CFG->enableoutcomes); $optionkeys = array_keys($options); > $gradeableitems = $gtree->get_items(); $optionitemid = array_shift($optionkeys); > // 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())) { $relreport = new gradereport_singleview( > $itemtype = 'grade_select'; $courseid, $gpr, $context, > } $report->screen->item_type(), $optionitemid > break; ); > }
< // 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 = [
$graderleftnav = html_writer::tag('div', $navprev, array('class' => 'itemnav previtem'));
> 'userid' => $userid,
< 'userid' => $userid, < 'group' => $groupid,
< 'perpage' => $perpage < );
> 'perpage' => $perpage, > ];
< $currentpage = new moodle_url('/grade/report/singleview/index.php', $pageparams);
> if (!is_null($groupid)) { > $pageparams['group'] = $groupid; > }
< if ($data = data_submitted()) { < $PAGE->set_pagelayout('redirect'); < $PAGE->set_title(get_string('savegrades', 'gradereport_singleview')); < echo $OUTPUT->header();
> $PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams));
< require_sesskey(); // Must have a sesskey for all actions. < $result = $report->process_data($data);
> // 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 (!empty($result->warnings)) { < foreach ($result->warnings as $warning) { < echo $OUTPUT->notification($warning);
> // Page params for the turn editing on button. > $options = $gpr->get_options(); > $button = $OUTPUT->edit_button(new moodle_url($PAGE->url, $options), 'get');
}
> > $reportname = $report->screen->heading(); if ($report->screen->supports_paging()) { > echo $report->screen->pager(); > if ($itemtype == 'user' || $itemtype == 'user_select') { } > $PAGE->requires->js_call_amd('gradereport_singleview/user', 'init'); > $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'user'); if ($report->screen->display_group_selector()) { > } else if ($itemtype == 'grade' || $itemtype == 'grade_select') { echo $report->group_selector; > $PAGE->requires->js_call_amd('gradereport_singleview/grade', 'init'); } > $actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'grade'); > } else { echo $report->output(); > $actionbar = new \core_grades\output\general_action_bar($context, new moodle_url('/grade/report/singleview/index.php', > ['id' => $courseid]), 'report', 'singleview');
< echo $OUTPUT->notification(get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount))); < echo $OUTPUT->continue_button($currentpage); < echo $OUTPUT->footer(); < die();
> if ($course->groupmode && $itemtype !== 'select') { > $PAGE->requires->js_call_amd('gradereport_singleview/group', 'init', [$itemtype]);
< $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;
> // And notify the user of the success result. > \core\notification::add( > get_string('savegradessuccess', 'gradereport_singleview', count((array) $result->changecount)), > \core\notification::SUCCESS > );
< if (!is_null($graderrightnav)) { < echo $graderrightnav;
< if ($report->screen->supports_paging()) { < echo $report->screen->pager(); < }
> // Make sure we have proper final grades. > grade_regrade_final_grades_if_required($course);
< if ($report->screen->display_group_selector()) { < echo $report->group_selector;
> // 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;
< echo $report->output();
> $stickyfooter = ''; > if (($itemtype !== 'select') && ($itemtype !== 'grade_select') &&($itemtype !== 'user_select')) { > $item = (isset($userid)) ? $userid : $itemid;
< if ($report->screen->supports_paging()) { < echo $report->screen->perpage_select(); < echo $report->screen->pager(); < }
> $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. > $footercontent = $userreportrenderer->report_navigation($gpr, $courseid, $context, $report, $groupid, $itemtype, $itemid); > > $buttonhtml = implode(' ', $report->screen->buttons($report->screen->is_readonly())); > $footercontent .= $report->screen->bulk_insert() . $buttonhtml; > > $stickyfooter = new core\output\sticky_footer($footercontent); > $stickyfooter = $OUTPUT->render($stickyfooter);
< if (!is_null($graderleftnav)) { < echo $graderleftnav; < } < if (!is_null($graderrightnav)) { < echo $graderrightnav;
> echo $OUTPUT->render_from_template('gradereport_singleview/report', [ > 'table' => $report->output(), > 'stickyfooter' => $stickyfooter, > 'sesskey' => sesskey() > ]);
< array(
> [
< )
> ]