2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * The gradebook overview report
20 * @package gradereport_overview
21 * @copyright 2007 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once '../../../config.php';
26 require_once $CFG->libdir.'/gradelib.php';
27 require_once $CFG->dirroot.'/grade/lib.php';
28 require_once $CFG->dirroot.'/grade/report/overview/lib.php';
30 $courseid = optional_param('id', SITEID, PARAM_INT);
31 $userid = optional_param('userid', $USER->id, PARAM_INT);
33 $PAGE->set_url(new moodle_url('/grade/report/overview/index.php', array('id' => $courseid, 'userid' => $userid)));
35 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
36 print_error('nocourseid');
38 require_login(null, false);
40 $context = context_course::instance($course->id);
41 $systemcontext = context_system::instance();
42 $personalcontext = null;
43 $PAGE->set_context($context);
45 // If we are accessing the page from a site context then ignore this check.
46 if ($courseid != SITEID) {
47 require_capability('gradereport/overview:view', $context);
51 require_capability('moodle/grade:viewall', $context);
54 if (!$DB->get_record('user', array('id'=>$userid, 'deleted'=>0)) or isguestuser($userid)) {
55 print_error('invaliduserid');
57 $personalcontext = context_user::instance($userid);
61 if (has_capability('moodle/grade:viewall', $systemcontext)) {
62 // Ok - can view all course grades.
65 } else if (has_capability('moodle/grade:viewall', $context)) {
66 // Ok - can view any grades in context.
69 } else if ($userid == $USER->id and ((has_capability('moodle/grade:view', $context) and $course->showgrades)
70 || $courseid == SITEID)) {
71 // Ok - can view own course grades.
74 } else if (has_capability('moodle/grade:viewall', $personalcontext) and $course->showgrades) {
75 // Ok - can view grades of this user - parent most probably.
77 } else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and $course->showgrades) {
78 // Ok - can view grades of this user - parent most probably.
83 // no access to grades!
84 print_error('nopermissiontoviewgrades', 'error', $CFG->wwwroot.'/course/view.php?id='.$courseid);
87 /// return tracking object
88 $gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'overview', 'courseid'=>$course->id, 'userid'=>$userid));
90 /// last selected report session tracking
91 if (!isset($USER->grade_last_report)) {
92 $USER->grade_last_report = array();
94 $USER->grade_last_report[$course->id] = 'overview';
96 //first make sure we have proper final grades - this must be done before constructing of the grade tree
97 grade_regrade_final_grades($courseid);
99 if (has_capability('moodle/grade:viewall', $context) && $courseid != SITEID) {
100 // Please note this would be extremely slow if we wanted to implement this properly for all teachers.
101 $groupmode = groups_get_course_groupmode($course); // Groups are being used
102 $currentgroup = groups_get_course_group($course, true);
104 if (!$currentgroup) { // To make some other functions work better later
105 $currentgroup = NULL;
108 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
110 if ($isseparategroups and (!$currentgroup)) {
111 // no separate group access, can view only self
113 $user_selector = false;
115 $user_selector = true;
118 if (empty($userid)) {
120 print_grade_page_head($courseid, 'report', 'overview');
122 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
124 if ($user_selector) {
125 $renderer = $PAGE->get_renderer('gradereport_overview');
126 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
128 // do not list all users
130 } else { // Only show one user's report
131 $report = new grade_report_overview($userid, $gpr, $context);
132 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview'). ' - '.fullname($report->user));
133 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
135 if ($user_selector) {
136 $renderer = $PAGE->get_renderer('gradereport_overview');
137 echo $renderer->graded_users_selector('overview', $course, $userid, $currentgroup, false);
140 if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
141 echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
143 if ($report->fill_table()) {
144 echo '<br />'.$report->print_table(true);
148 } else { // Non-admins and users viewing from the site context can just see their own report.
150 // Create a report instance
151 $report = new grade_report_overview($userid, $gpr, $context);
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');
157 $header = get_string('mygrades', 'grades'). ' - '.fullname($report->user);
158 $PAGE->set_title($header);
159 $PAGE->set_heading($header);
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;
166 } else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
167 $node->forceopen = true;
170 echo $OUTPUT->header();
171 if ($report->fill_table(true)) {
172 echo '<br />'.$report->print_table(true);
174 } else { // We have a course context. We must be navigating from the gradebook.
175 print_grade_page_head($courseid, 'report', 'overview', get_string('pluginname', 'gradereport_overview')
176 . ' - ' . fullname($report->user));
177 if ($report->fill_table()) {
178 echo '<br />'.$report->print_table(true);
183 $event = \gradereport_overview\event\grade_report_viewed::create(
185 'context' => $context,
186 'courseid' => $courseid,
187 'relateduserid' => $userid,
192 echo $OUTPUT->footer();