MDL-49324 grades: Create helper function for regrading on report view
[moodle.git] / grade / report / singleview / index.php
CommitLineData
7bd4a361 1<?php
65c134f9
ZD
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/**
8ec7b088 18 * Displays the Single view
65c134f9
ZD
19 *
20 * @package gradereport_singleview
8ec7b088 21 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
65c134f9
ZD
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once('../../../config.php');
26require_once($CFG->dirroot.'/lib/gradelib.php');
27require_once($CFG->dirroot.'/grade/lib.php');
28require_once($CFG->dirroot.'/grade/report/singleview/lib.php');
7bd4a361
ZD
29
30$courseid = required_param('id', PARAM_INT);
31$groupid = optional_param('group', null, PARAM_INT);
57fac09a
DW
32
33// Making this work with profile reports.
7bd4a361
ZD
34$userid = optional_param('userid', null, PARAM_INT);
35
65c134f9 36$defaulttype = $userid ? 'user' : 'select';
7bd4a361 37
a24abd55 38$itemid = optional_param('itemid', null, PARAM_INT);
65c134f9 39$itemtype = optional_param('item', $defaulttype, PARAM_TEXT);
57fac09a
DW
40$page = optional_param('page', 0, PARAM_INT);
41$perpage = optional_param('perpage', 100, PARAM_INT);
7bd4a361 42
a24abd55 43if (empty($itemid)) {
453f6926
C
44 $itemid = $userid;
45 $itemtype = $defaulttype;
46}
47
65c134f9 48$courseparams = array('id' => $courseid);
65c134f9 49$PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $courseparams));
2a6cdc51 50$PAGE->set_pagelayout('incourse');
7bd4a361 51
65c134f9 52if (!$course = $DB->get_record('course', $courseparams)) {
7bd4a361
ZD
53 print_error('nocourseid');
54}
55
57fac09a 56require_login($course);
57fac09a 57
8ec7b088 58if (!in_array($itemtype, gradereport_singleview::valid_screens())) {
d124a60b 59 print_error('notvalid', 'gradereport_singleview', '', $itemtype);
7bd4a361
ZD
60}
61
7bd4a361
ZD
62$context = context_course::instance($course->id);
63
65c134f9 64// This is the normal requirements.
d124a60b 65require_capability('gradereport/singleview:view', $context);
7bd4a361
ZD
66require_capability('moodle/grade:viewall', $context);
67require_capability('moodle/grade:edit', $context);
7bd4a361
ZD
68
69$gpr = new grade_plugin_return(array(
70 'type' => 'report',
d124a60b 71 'plugin' => 'singleview',
7bd4a361
ZD
72 'courseid' => $courseid
73));
74
65c134f9 75// Last selected report session tracking.
7bd4a361
ZD
76if (!isset($USER->grade_last_report)) {
77 $USER->grade_last_report = array();
78}
d124a60b 79$USER->grade_last_report[$course->id] = 'singleview';
7bd4a361 80
30be6c84 81// First make sure we have proper final grades.
0a802c9c 82grade_regrade_final_grades_if_required($course);
7bd4a361 83
aac66bef 84$report = new gradereport_singleview($courseid, $gpr, $context, $itemtype, $itemid);
7bd4a361
ZD
85
86$reportname = $report->screen->heading();
87
d124a60b 88$pluginname = get_string('pluginname', 'gradereport_singleview');
7bd4a361 89
57fac09a
DW
90$pageparams = array(
91 'id' => $courseid,
92 'itemid' => $itemid,
93 'item' => $itemtype,
94 'userid' => $userid,
95 'group' => $groupid,
96 'page' => $page,
97 'perpage' => $perpage
98);
7bd4a361 99
57fac09a 100$currentpage = new moodle_url('/grade/report/singleview/index.php', $pageparams);
7bd4a361
ZD
101
102if ($data = data_submitted()) {
50e30bd4
ZD
103 $PAGE->set_pagelayout('redirect');
104 $PAGE->set_title(get_string('savegrades', 'gradereport_singleview'));
105 echo $OUTPUT->header();
106
f548d49f 107 require_sesskey(); // Must have a sesskey for all actions.
50e30bd4 108 $result = $report->process_data($data);
7bd4a361 109
50e30bd4
ZD
110 if (!empty($result->warnings)) {
111 foreach ($result->warnings as $warning) {
112 echo $OUTPUT->notification($warning);
113 }
7bd4a361 114 }
50e30bd4
ZD
115 echo $OUTPUT->notification(get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount)));
116 echo $OUTPUT->continue_button($currentpage);
117 echo $OUTPUT->footer();
118 die();
7bd4a361
ZD
119}
120
50e30bd4 121$PAGE->set_pagelayout('report');
283ea788
AG
122if ($itemtype == 'user') {
123 print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false, true, null, null, $report->screen->item);
124} else {
125 print_grade_page_head($course->id, 'report', 'singleview', $reportname);
126}
57fac09a 127
7cdbf79d 128$graderrightnav = $graderleftnav = null;
7cdbf79d 129
57fac09a
DW
130$options = $report->screen->options();
131
132if (!empty($options)) {
133
134 $optionkeys = array_keys($options);
65c134f9 135 $optionitemid = array_shift($optionkeys);
7cdbf79d 136
8ec7b088 137 $relreport = new gradereport_singleview(
7cdbf79d 138 $courseid, $gpr, $context,
aac66bef 139 $report->screen->item_type(), $optionitemid
7cdbf79d
AB
140 );
141 $reloptions = $relreport->screen->options();
142 $reloptionssorting = array_keys($relreport->screen->options());
143
144 $i = array_search($itemid, $reloptionssorting);
145 $navparams = array('item' => $itemtype, 'id' => $courseid, 'group' => $groupid);
65c134f9
ZD
146 if ($i > 0) {
147 $navparams['itemid'] = $reloptionssorting[$i - 1];
d124a60b 148 $link = new moodle_url('/grade/report/singleview/index.php', $navparams);
57fac09a 149 $navprev = html_writer::link($link, $OUTPUT->larrow() . ' ' . $reloptions[$reloptionssorting[$i - 1]]);
2a6cdc51 150 $graderleftnav = html_writer::tag('div', $navprev, array('class' => 'itemnav previtem'));
7cdbf79d 151 }
65c134f9
ZD
152 if ($i < count($reloptionssorting) - 1) {
153 $navparams['itemid'] = $reloptionssorting[$i + 1];
d124a60b 154 $link = new moodle_url('/grade/report/singleview/index.php', $navparams);
57fac09a 155 $navnext = html_writer::link($link, $reloptions[$reloptionssorting[$i + 1]] . ' ' . $OUTPUT->rarrow());
2a6cdc51 156 $graderrightnav = html_writer::tag('div', $navnext, array('class' => 'itemnav nextitem'));
7cdbf79d
AB
157 }
158}
159
65c134f9 160if (!is_null($graderleftnav)) {
7cdbf79d
AB
161 echo $graderleftnav;
162}
65c134f9 163if (!is_null($graderrightnav)) {
7cdbf79d
AB
164 echo $graderrightnav;
165}
7bd4a361
ZD
166
167if ($report->screen->supports_paging()) {
168 echo $report->screen->pager();
169}
170
171if ($report->screen->display_group_selector()) {
172 echo $report->group_selector;
173}
174
7bd4a361
ZD
175echo $report->output();
176
7cdbf79d
AB
177if ($report->screen->supports_paging()) {
178 echo $report->screen->pager();
179}
180
57fac09a 181if (!is_null($graderleftnav)) {
7cdbf79d
AB
182 echo $graderleftnav;
183}
57fac09a 184if (!is_null($graderrightnav)) {
7cdbf79d
AB
185 echo $graderrightnav;
186}
187
57fac09a
DW
188$event = \gradereport_singleview\event\grade_report_viewed::create(
189 array(
190 'context' => $context,
191 'courseid' => $courseid,
192 'relateduserid' => $USER->id,
193 )
194);
195$event->trigger();
196
7bd4a361 197echo $OUTPUT->footer();