MDL-49324 grades: Create helper function for regrading on report view
[moodle.git] / grade / report / user / 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/>.
4b98acca 16
a153c9f2
AD
17/**
18 * The gradebook user report
19 *
20 * @package gradereport_user
21 * @copyright 2007 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
65dd61bd 25require_once '../../../config.php';
26require_once $CFG->libdir.'/gradelib.php';
27require_once $CFG->dirroot.'/grade/lib.php';
28require_once $CFG->dirroot.'/grade/report/user/lib.php';
1fdab481 29
772229f3 30$courseid = required_param('id', PARAM_INT);
879c99bb 31$userid = optional_param('userid', $USER->id, PARAM_INT);
65dd61bd 32
4d5059d4 33$PAGE->set_url(new moodle_url('/grade/report/user/index.php', array('id'=>$courseid)));
beebcf26 34
65dd61bd 35/// basic access checks
5c75a0a3 36if (!$course = $DB->get_record('course', array('id' => $courseid))) {
65dd61bd 37 print_error('nocourseid');
38}
39require_login($course);
367a75fa 40$PAGE->set_pagelayout('report');
65dd61bd 41
d4060472 42$context = context_course::instance($course->id);
879c99bb 43require_capability('gradereport/user:view', $context);
d08bba83 44
879c99bb 45if (empty($userid)) {
46 require_capability('moodle/grade:viewall', $context);
47
48} else {
b0639b78 49 if (!$DB->get_record('user', array('id'=>$userid, 'deleted'=>0)) or isguestuser($userid)) {
772229f3 50 print_error('invaliduser');
51 }
879c99bb 52}
65dd61bd 53
772229f3 54$access = false;
65dd61bd 55if (has_capability('moodle/grade:viewall', $context)) {
56 //ok - can view all course grades
772229f3 57 $access = true;
65dd61bd 58
d08bba83 59} else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
65dd61bd 60 //ok - can view own grades
772229f3 61 $access = true;
65dd61bd 62
d4060472 63} else if (has_capability('moodle/grade:viewall', context_user::instance($userid)) and $course->showgrades) {
65dd61bd 64 // ok - can view grades of this user- parent most probably
772229f3 65 $access = true;
66}
7a6b7acf 67
772229f3 68if (!$access) {
69 // no access to grades!
cac0c3ef 70 print_error('nopermissiontoviewgrades', 'error', $CFG->wwwroot.'/course/view.php?id='.$courseid);
dfbec4d4 71}
72
65dd61bd 73/// return tracking object
74$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$courseid, 'userid'=>$userid));
75
76/// last selected report session tracking
77if (!isset($USER->grade_last_report)) {
78 $USER->grade_last_report = array();
79}
80$USER->grade_last_report[$course->id] = 'user';
81
30be6c84 82// First make sure we have proper final grades.
0a802c9c 83grade_regrade_final_grades_if_required($course);
65dd61bd 84
772229f3 85if (has_capability('moodle/grade:viewall', $context)) { //Teachers will see all student reports
86 $groupmode = groups_get_course_groupmode($course); // Groups are being used
87 $currentgroup = groups_get_course_group($course, true);
65dd61bd 88
772229f3 89 if (!$currentgroup) { // To make some other functions work better later
90 $currentgroup = NULL;
91 }
80bddb78 92
772229f3 93 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
dc482cfa 94
772229f3 95 if ($isseparategroups and (!$currentgroup)) {
48a4c849 96 // no separate group access, can view only self
97 $userid = $USER->id;
4d5059d4 98 $user_selector = false;
48a4c849 99 } else {
4d5059d4 100 $user_selector = true;
772229f3 101 }
102
38c1dd19
RT
103 $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
104 $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
105 $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
7ac88172 106 if (empty($userid)) {
772229f3 107 $gui = new graded_users_iterator($course, null, $currentgroup);
38c1dd19 108 $gui->require_active_enrolment($showonlyactiveenrol);
772229f3 109 $gui->init();
110 // Add tabs
111 print_grade_page_head($courseid, 'report', 'user');
112 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
113
4d5059d4
SH
114 if ($user_selector) {
115 $renderer = $PAGE->get_renderer('gradereport_user');
97451e02 116 echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, true);
4d5059d4
SH
117 }
118
772229f3 119 while ($userdata = $gui->next_user()) {
120 $user = $userdata->user;
121 $report = new grade_report_user($courseid, $gpr, $context, $user->id);
e59a1dd6 122
5625d33d
AD
123 $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
124 echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink);
dc482cfa 125
d08bba83 126 if ($report->fill_table()) {
dc482cfa 127 echo '<br />'.$report->print_table(true);
5c75a0a3 128 }
772229f3 129 echo "<p style = 'page-break-after: always;'></p>";
d08bba83 130 }
772229f3 131 $gui->close();
7ac88172 132 } else { // Only show one user's report
5c75a0a3 133 $report = new grade_report_user($courseid, $gpr, $context, $userid);
5625d33d
AD
134
135 $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
283ea788
AG
136 print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink,
137 false, false, true, null, null, $report->user);
138
772229f3 139 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
140
4d5059d4
SH
141 if ($user_selector) {
142 $renderer = $PAGE->get_renderer('gradereport_user');
f914e8d3
AD
143 $showallusersoptions = true;
144 echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, $showallusersoptions);
4d5059d4 145 }
7a6b7acf 146
772229f3 147 if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
c3b834b4 148 echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
7ac88172 149 } else {
150 if ($report->fill_table()) {
151 echo '<br />'.$report->print_table(true);
152 }
d08bba83 153 }
65dd61bd 154 }
772229f3 155} else { //Students will see just their own report
65dd61bd 156
772229f3 157 // Create a report instance
158 $report = new grade_report_user($courseid, $gpr, $context, $userid);
159
160 // print the page
b5e7b2bf 161 print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user));
772229f3 162
163 if ($report->fill_table()) {
164 echo '<br />'.$report->print_table(true);
165 }
1fdab481 166}
772229f3 167
f712aa28 168if (isset($report)) {
e2ec4f3a 169 // Trigger report viewed event.
f712aa28
JA
170 $report->viewed();
171} else {
e2ec4f3a 172 echo html_writer::tag('div', '', array('class' => 'clearfix'));
f712aa28
JA
173 echo $OUTPUT->notification(get_string('nostudentsyet'));
174}
06dc1e7f 175
a071dbb6 176echo $OUTPUT->footer();