on-demand release 4.0dev+
[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))) {
d9ce6a4f 36 print_error('invalidcourseid');
aa330ebb 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
78310c30 74$access = grade_report_overview::check_access($systemcontext, $context, $personalcontext, $course, $userid);
aa330ebb 75
7ac88172 76if (!$access) {
77 // no access to grades!
cac0c3ef 78 print_error('nopermissiontoviewgrades', 'error', $CFG->wwwroot.'/course/view.php?id='.$courseid);
aa330ebb 79}
80
81/// return tracking object
bc430af2 82$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'overview', 'courseid'=>$course->id, 'userid'=>$userid));
aa330ebb 83
84/// last selected report session tracking
85if (!isset($USER->grade_last_report)) {
86 $USER->grade_last_report = array();
87}
bc430af2 88$USER->grade_last_report[$course->id] = 'overview';
aa330ebb 89
30be6c84 90// First make sure we have proper final grades.
0a802c9c 91grade_regrade_final_grades_if_required($course);
7ac88172 92
d1a0e4a9
MG
93$actionbar = new \core_grades\output\general_action_bar($context,
94 new moodle_url('/grade/report/overview/index.php', ['id' => $courseid]), 'report', 'overview');
95
c9451960 96if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
78527462 97 // Please note this would be extremely slow if we wanted to implement this properly for all teachers.
7ac88172 98 $groupmode = groups_get_course_groupmode($course); // Groups are being used
9a2da737 99 $currentgroup = $gpr->groupid;
aa330ebb 100
7ac88172 101 if (!$currentgroup) { // To make some other functions work better later
102 $currentgroup = NULL;
103 }
aa330ebb 104
7ac88172 105 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
106
107 if ($isseparategroups and (!$currentgroup)) {
48a4c849 108 // no separate group access, can view only self
109 $userid = $USER->id;
4d5059d4 110 $user_selector = false;
48a4c849 111 } else {
4d5059d4 112 $user_selector = true;
7ac88172 113 }
114
7ac88172 115 if (empty($userid)) {
d1a0e4a9
MG
116 print_grade_page_head($courseid, 'report', 'overview', false, false, false,
117 true, null, null, null, $actionbar);
4d5059d4 118
7ac88172 119 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
4d5059d4
SH
120
121 if ($user_selector) {
122 $renderer = $PAGE->get_renderer('gradereport_overview');
123 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
124 }
7ac88172 125 // do not list all users
126
127 } else { // Only show one user's report
128 $report = new grade_report_overview($userid, $gpr, $context);
283ea788 129 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview') .
d1a0e4a9
MG
130 ' - ' . fullname($report->user), false, false, true, null, null,
131 $report->user, $actionbar);
7ac88172 132 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
133
4d5059d4
SH
134 if ($user_selector) {
135 $renderer = $PAGE->get_renderer('gradereport_overview');
136 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
137 }
7ac88172 138
139 if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
c3b834b4 140 echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
7ac88172 141 } else {
142 if ($report->fill_table()) {
143 echo '<br />'.$report->print_table(true);
144 }
145 }
146 }
c9451960 147} else { // Non-admins and users viewing from the site context can just see their own report.
aa330ebb 148
149 // Create a report instance
150 $report = new grade_report_overview($userid, $gpr, $context);
7ac88172 151
c78dbe03
AG
152 if (!empty($report->studentcourseids)) {
153 // If the course id matches the site id then we don't have a course context to work with.
154 // Display a standard page.
155 if ($courseid == SITEID) {
156 $PAGE->set_pagelayout('standard');
4887d152 157 $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
c78dbe03
AG
158 $PAGE->set_title($header);
159 $PAGE->set_heading(fullname($report->user));
160
161 if ($USER->id != $report->user->id) {
162 $PAGE->navigation->extend_for_user($report->user);
163 if ($node = $PAGE->settingsnav->get('userviewingsettings'.$report->user->id)) {
164 $node->forceopen = true;
165 }
166 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
167 $node->forceopen = true;
168 }
169
170 echo $OUTPUT->header();
171 if ($report->fill_table(true, true)) {
172 echo html_writer::tag('h3', get_string('coursesiamtaking', 'grades'));
173 echo '<br />' . $report->print_table(true);
174 }
175 } else { // We have a course context. We must be navigating from the gradebook.
176 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview')
d1a0e4a9
MG
177 . ' - ' . fullname($report->user), false, false, true, null, null,
178 $report->user, $actionbar);
c78dbe03
AG
179 if ($report->fill_table()) {
180 echo '<br />' . $report->print_table(true);
181 }
182 }
183 } else {
c9451960 184 $PAGE->set_pagelayout('standard');
4887d152 185 $header = get_string('grades', 'grades') . ' - ' . fullname($report->user);
c9451960 186 $PAGE->set_title($header);
880c5073 187 $PAGE->set_heading(fullname($report->user));
c78dbe03
AG
188 echo $OUTPUT->header();
189 }
c9451960 190
c78dbe03
AG
191 if (count($report->teachercourses)) {
192 echo html_writer::tag('h3', get_string('coursesiamteaching', 'grades'));
193 $report->print_teacher_table();
194 }
aa330ebb 195
c78dbe03
AG
196 if (empty($report->studentcourseids) && empty($report->teachercourses)) {
197 // We have no report to show the user. Let them know something.
198 echo $OUTPUT->notification(get_string('noreports', 'grades'), 'notifymessage');
aa330ebb 199 }
aa330ebb 200}
7ac88172 201
78310c30 202grade_report_overview::viewed($context, $courseid, $userid);
06dc1e7f 203
5a931394 204echo $OUTPUT->footer();