MDL-49324 grades: Create helper function for regrading on report view
[moodle.git] / grade / report / overview / index.php
CommitLineData
e060e33d 1<?php
e060e33d 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/>.
aa330ebb 16
a153c9f2
AD
17/**
18 * The gradebook overview report
19 *
20 * @package gradereport_overview
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
aa330ebb 25require_once '../../../config.php';
26require_once $CFG->libdir.'/gradelib.php';
27require_once $CFG->dirroot.'/grade/lib.php';
28require_once $CFG->dirroot.'/grade/report/overview/lib.php';
29
c9451960 30$courseid = optional_param('id', SITEID, PARAM_INT);
aa330ebb 31$userid = optional_param('userid', $USER->id, PARAM_INT);
32
c9451960 33$PAGE->set_url(new moodle_url('/grade/report/overview/index.php', array('id' => $courseid, 'userid' => $userid)));
beebcf26 34
5c75a0a3 35if (!$course = $DB->get_record('course', array('id' => $courseid))) {
aa330ebb 36 print_error('nocourseid');
37}
c9451960 38require_login(null, false);
bb2c0b45 39$PAGE->set_course($course);
aa330ebb 40
d4060472
AG
41$context = context_course::instance($course->id);
42$systemcontext = context_system::instance();
c9451960 43$personalcontext = null;
c9451960
AG
44
45// If we are accessing the page from a site context then ignore this check.
46if ($courseid != SITEID) {
47 require_capability('gradereport/overview:view', $context);
48}
aa330ebb 49
7ac88172 50if (empty($userid)) {
78527462 51 require_capability('moodle/grade:viewall', $context);
7ac88172 52
53} else {
b0639b78 54 if (!$DB->get_record('user', array('id'=>$userid, 'deleted'=>0)) or isguestuser($userid)) {
cac0c3ef 55 print_error('invaliduserid');
7ac88172 56 }
c9451960 57 $personalcontext = context_user::instance($userid);
7ac88172 58}
59
c78dbe03 60if (isset($personalcontext) && $courseid == SITEID) {
880c5073
AG
61 $PAGE->set_context($personalcontext);
62} else {
63 $PAGE->set_context($context);
64}
c78dbe03
AG
65if ($userid == $USER->id) {
66 $settings = $PAGE->settingsnav->find('mygrades', null);
67 $settings->make_active();
e168e388 68} else if ($courseid != SITEID && $userid) {
c78dbe03 69 // Show some other navbar thing.
e168e388 70 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
c78dbe03
AG
71 $PAGE->navigation->extend_for_user($user);
72}
880c5073 73
7ac88172 74$access = false;
02b5087e 75if (has_capability('moodle/grade:viewall', $systemcontext)) {
78527462 76 // Ok - can view all course grades.
7ac88172 77 $access = true;
aa330ebb 78
78527462
PAM
79} else if (has_capability('moodle/grade:viewall', $context)) {
80 // Ok - can view any grades in context.
02b5087e 81 $access = true;
6c3ef410 82
c9451960
AG
83} else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades)
84 || $courseid == SITEID)) {
78527462 85 // Ok - can view own course grades.
7ac88172 86 $access = true;
aa330ebb 87
c9451960
AG
88} else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) {
89 // Ok - can view grades of this user - parent most probably.
90 $access = true;
91} else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) {
92 // Ok - can view grades of this user - parent most probably.
7ac88172 93 $access = true;
94}
aa330ebb 95
7ac88172 96if (!$access) {
97 // no access to grades!
cac0c3ef 98 print_error('nopermissiontoviewgrades', 'error', $CFG->wwwroot.'/course/view.php?id='.$courseid);
aa330ebb 99}
100
101/// return tracking object
bc430af2 102$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'overview', 'courseid'=>$course->id, 'userid'=>$userid));
aa330ebb 103
104/// last selected report session tracking
105if (!isset($USER->grade_last_report)) {
106 $USER->grade_last_report = array();
107}
bc430af2 108$USER->grade_last_report[$course->id] = 'overview';
aa330ebb 109
30be6c84 110// First make sure we have proper final grades.
0a802c9c 111grade_regrade_final_grades_if_required($course);
7ac88172 112
c9451960 113if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
78527462 114 // Please note this would be extremely slow if we wanted to implement this properly for all teachers.
7ac88172 115 $groupmode = groups_get_course_groupmode($course); // Groups are being used
116 $currentgroup = groups_get_course_group($course, true);
aa330ebb 117
7ac88172 118 if (!$currentgroup) { // To make some other functions work better later
119 $currentgroup = NULL;
120 }
aa330ebb 121
7ac88172 122 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
123
124 if ($isseparategroups and (!$currentgroup)) {
48a4c849 125 // no separate group access, can view only self
126 $userid = $USER->id;
4d5059d4 127 $user_selector = false;
48a4c849 128 } else {
4d5059d4 129 $user_selector = true;
7ac88172 130 }
131
7ac88172 132 if (empty($userid)) {
133 // Add tabs
134 print_grade_page_head($courseid, 'report', 'overview');
4d5059d4 135
7ac88172 136 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
4d5059d4
SH
137
138 if ($user_selector) {
139 $renderer = $PAGE->get_renderer('gradereport_overview');
140 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
141 }
7ac88172 142 // do not list all users
143
144 } else { // Only show one user's report
145 $report = new grade_report_overview($userid, $gpr, $context);
283ea788
AG
146 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview') .
147 ' - ' . fullname($report->user), false, false, true, null, null, $report->user);
7ac88172 148 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
149
4d5059d4
SH
150 if ($user_selector) {
151 $renderer = $PAGE->get_renderer('gradereport_overview');
152 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
153 }
7ac88172 154
155 if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
c3b834b4 156 echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
7ac88172 157 } else {
158 if ($report->fill_table()) {
159 echo '<br />'.$report->print_table(true);
160 }
161 }
162 }
c9451960 163} else { // Non-admins and users viewing from the site context can just see their own report.
aa330ebb 164
165 // Create a report instance
166 $report = new grade_report_overview($userid, $gpr, $context);
7ac88172 167
c78dbe03
AG
168 if (!empty($report->studentcourseids)) {
169 // If the course id matches the site id then we don't have a course context to work with.
170 // Display a standard page.
171 if ($courseid == SITEID) {
172 $PAGE->set_pagelayout('standard');
4887d152 173 $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
c78dbe03
AG
174 $PAGE->set_title($header);
175 $PAGE->set_heading(fullname($report->user));
176
177 if ($USER->id != $report->user->id) {
178 $PAGE->navigation->extend_for_user($report->user);
179 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$report->user->id)) {
180 $node->forceopen = true;
181 }
182 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
183 $node->forceopen = true;
184 }
185
186 echo $OUTPUT->header();
187 if ($report->fill_table(true, true)) {
188 echo html_writer::tag('h3', get_string('coursesiamtaking', 'grades'));
189 echo '<br />' . $report->print_table(true);
190 }
191 } else { // We have a course context. We must be navigating from the gradebook.
192 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview')
193 . ' - ' . fullname($report->user));
194 if ($report->fill_table()) {
195 echo '<br />' . $report->print_table(true);
196 }
197 }
198 } else {
c9451960 199 $PAGE->set_pagelayout('standard');
4887d152 200 $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
c9451960 201 $PAGE->set_title($header);
880c5073 202 $PAGE->set_heading(fullname($report->user));
c78dbe03
AG
203 echo $OUTPUT->header();
204 }
c9451960 205
c78dbe03
AG
206 if (count($report->teachercourses)) {
207 echo html_writer::tag('h3', get_string('coursesiamteaching', 'grades'));
208 $report->print_teacher_table();
209 }
aa330ebb 210
c78dbe03
AG
211 if (empty($report->studentcourseids) && empty($report->teachercourses)) {
212 // We have no report to show the user. Let them know something.
213 echo $OUTPUT->notification(get_string('noreports', 'grades'), 'notifymessage');
aa330ebb 214 }
aa330ebb 215}
7ac88172 216
87f860cd 217$event = \gradereport_overview\event\grade_report_viewed::create(
06dc1e7f
AG
218 array(
219 'context' => $context,
220 'courseid' => $courseid,
221 'relateduserid' => $userid,
222 )
223);
224$event->trigger();
225
5a931394 226echo $OUTPUT->footer();
aa330ebb 227
02b5087e 228