MDL-48273 singleview: add load_user method and other group validations
[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
ZD
37
38$itemid = optional_param('itemid', $userid, 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
65c134f9 43$courseparams = array('id' => $courseid);
65c134f9 44$PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $courseparams));
7bd4a361 45
65c134f9 46if (!$course = $DB->get_record('course', $courseparams)) {
7bd4a361
ZD
47 print_error('nocourseid');
48}
49
57fac09a 50require_login($course);
57fac09a 51
8ec7b088 52if (!in_array($itemtype, gradereport_singleview::valid_screens())) {
d124a60b 53 print_error('notvalid', 'gradereport_singleview', '', $itemtype);
7bd4a361
ZD
54}
55
7bd4a361
ZD
56$context = context_course::instance($course->id);
57
65c134f9 58// This is the normal requirements.
d124a60b 59require_capability('gradereport/singleview:view', $context);
7bd4a361
ZD
60require_capability('moodle/grade:viewall', $context);
61require_capability('moodle/grade:edit', $context);
7bd4a361
ZD
62
63$gpr = new grade_plugin_return(array(
64 'type' => 'report',
d124a60b 65 'plugin' => 'singleview',
7bd4a361
ZD
66 'courseid' => $courseid
67));
68
65c134f9 69// Last selected report session tracking.
7bd4a361
ZD
70if (!isset($USER->grade_last_report)) {
71 $USER->grade_last_report = array();
72}
d124a60b 73$USER->grade_last_report[$course->id] = 'singleview';
7bd4a361 74
57fac09a
DW
75// First make sure we have proper final grades -
76// this must be done before constructing of the grade tree.
7bd4a361
ZD
77grade_regrade_final_grades($courseid);
78
aac66bef 79$report = new gradereport_singleview($courseid, $gpr, $context, $itemtype, $itemid);
7bd4a361
ZD
80
81$reportname = $report->screen->heading();
82
d124a60b 83$pluginname = get_string('pluginname', 'gradereport_singleview');
7bd4a361 84
57fac09a
DW
85$pageparams = array(
86 'id' => $courseid,
87 'itemid' => $itemid,
88 'item' => $itemtype,
89 'userid' => $userid,
90 'group' => $groupid,
91 'page' => $page,
92 'perpage' => $perpage
93);
7bd4a361 94
57fac09a 95$currentpage = new moodle_url('/grade/report/singleview/index.php', $pageparams);
7bd4a361
ZD
96
97if ($data = data_submitted()) {
50e30bd4
ZD
98 $PAGE->set_pagelayout('redirect');
99 $PAGE->set_title(get_string('savegrades', 'gradereport_singleview'));
100 echo $OUTPUT->header();
101
f548d49f 102 require_sesskey(); // Must have a sesskey for all actions.
50e30bd4 103 $result = $report->process_data($data);
7bd4a361 104
50e30bd4
ZD
105 if (!empty($result->warnings)) {
106 foreach ($result->warnings as $warning) {
107 echo $OUTPUT->notification($warning);
108 }
7bd4a361 109 }
50e30bd4
ZD
110 echo $OUTPUT->notification(get_string('savegradessuccess', 'gradereport_singleview', count ((array)$result->changecount)));
111 echo $OUTPUT->continue_button($currentpage);
112 echo $OUTPUT->footer();
113 die();
7bd4a361
ZD
114}
115
50e30bd4 116$PAGE->set_pagelayout('report');
57fac09a
DW
117print_grade_page_head($course->id, 'report', 'singleview', $reportname);
118
7cdbf79d 119$graderrightnav = $graderleftnav = null;
7cdbf79d 120
57fac09a
DW
121$options = $report->screen->options();
122
123if (!empty($options)) {
124
125 $optionkeys = array_keys($options);
65c134f9 126 $optionitemid = array_shift($optionkeys);
7cdbf79d 127
8ec7b088 128 $relreport = new gradereport_singleview(
7cdbf79d 129 $courseid, $gpr, $context,
aac66bef 130 $report->screen->item_type(), $optionitemid
7cdbf79d
AB
131 );
132 $reloptions = $relreport->screen->options();
133 $reloptionssorting = array_keys($relreport->screen->options());
134
135 $i = array_search($itemid, $reloptionssorting);
136 $navparams = array('item' => $itemtype, 'id' => $courseid, 'group' => $groupid);
65c134f9
ZD
137 if ($i > 0) {
138 $navparams['itemid'] = $reloptionssorting[$i - 1];
d124a60b 139 $link = new moodle_url('/grade/report/singleview/index.php', $navparams);
57fac09a 140 $navprev = html_writer::link($link, $OUTPUT->larrow() . ' ' . $reloptions[$reloptionssorting[$i - 1]]);
7cdbf79d
AB
141 $graderleftnav = html_writer::tag('small', $navprev, array('class' => 'itemnav previtem'));
142 }
65c134f9
ZD
143 if ($i < count($reloptionssorting) - 1) {
144 $navparams['itemid'] = $reloptionssorting[$i + 1];
d124a60b 145 $link = new moodle_url('/grade/report/singleview/index.php', $navparams);
57fac09a 146 $navnext = html_writer::link($link, $reloptions[$reloptionssorting[$i + 1]] . ' ' . $OUTPUT->rarrow());
7cdbf79d
AB
147 $graderrightnav = html_writer::tag('small', $navnext, array('class' => 'itemnav nextitem'));
148 }
149}
150
65c134f9 151if (!is_null($graderleftnav)) {
7cdbf79d
AB
152 echo $graderleftnav;
153}
65c134f9 154if (!is_null($graderrightnav)) {
7cdbf79d
AB
155 echo $graderrightnav;
156}
7bd4a361
ZD
157
158if ($report->screen->supports_paging()) {
159 echo $report->screen->pager();
160}
161
162if ($report->screen->display_group_selector()) {
163 echo $report->group_selector;
164}
165
7bd4a361
ZD
166echo $report->output();
167
7cdbf79d
AB
168if ($report->screen->supports_paging()) {
169 echo $report->screen->pager();
170}
171
57fac09a 172if (!is_null($graderleftnav)) {
7cdbf79d
AB
173 echo $graderleftnav;
174}
57fac09a 175if (!is_null($graderrightnav)) {
7cdbf79d
AB
176 echo $graderrightnav;
177}
178
57fac09a
DW
179$event = \gradereport_singleview\event\grade_report_viewed::create(
180 array(
181 'context' => $context,
182 'courseid' => $courseid,
183 'relateduserid' => $USER->id,
184 )
185);
186$event->trigger();
187
7bd4a361 188echo $OUTPUT->footer();