MDL-30085 core_grades functions moved to correct location
[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
920a0fb2 82
772229f3 83//first make sure we have proper final grades - this must be done before constructing of the grade tree
84grade_regrade_final_grades($courseid);
65dd61bd 85
772229f3 86if (has_capability('moodle/grade:viewall', $context)) { //Teachers will see all student reports
87 $groupmode = groups_get_course_groupmode($course); // Groups are being used
88 $currentgroup = groups_get_course_group($course, true);
65dd61bd 89
772229f3 90 if (!$currentgroup) { // To make some other functions work better later
91 $currentgroup = NULL;
92 }
80bddb78 93
772229f3 94 $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
dc482cfa 95
772229f3 96 if ($isseparategroups and (!$currentgroup)) {
48a4c849 97 // no separate group access, can view only self
98 $userid = $USER->id;
4d5059d4 99 $user_selector = false;
48a4c849 100 } else {
4d5059d4 101 $user_selector = true;
772229f3 102 }
103
38c1dd19
RT
104 $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
105 $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
106 $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
7ac88172 107 if (empty($userid)) {
772229f3 108 $gui = new graded_users_iterator($course, null, $currentgroup);
38c1dd19 109 $gui->require_active_enrolment($showonlyactiveenrol);
772229f3 110 $gui->init();
111 // Add tabs
112 print_grade_page_head($courseid, 'report', 'user');
113 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
114
4d5059d4
SH
115 if ($user_selector) {
116 $renderer = $PAGE->get_renderer('gradereport_user');
97451e02 117 echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, true);
4d5059d4
SH
118 }
119
772229f3 120 while ($userdata = $gui->next_user()) {
121 $user = $userdata->user;
122 $report = new grade_report_user($courseid, $gpr, $context, $user->id);
e59a1dd6 123
5625d33d
AD
124 $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
125 echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink);
dc482cfa 126
d08bba83 127 if ($report->fill_table()) {
dc482cfa 128 echo '<br />'.$report->print_table(true);
5c75a0a3 129 }
772229f3 130 echo "<p style = 'page-break-after: always;'></p>";
d08bba83 131 }
772229f3 132 $gui->close();
7ac88172 133 } else { // Only show one user's report
5c75a0a3 134 $report = new grade_report_user($courseid, $gpr, $context, $userid);
5625d33d
AD
135
136 $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
137 print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink);
772229f3 138 groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
139
4d5059d4
SH
140 if ($user_selector) {
141 $renderer = $PAGE->get_renderer('gradereport_user');
f914e8d3
AD
142 $showallusersoptions = true;
143 echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, $showallusersoptions);
4d5059d4 144 }
7a6b7acf 145
772229f3 146 if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
c3b834b4 147 echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
7ac88172 148 } else {
149 if ($report->fill_table()) {
150 echo '<br />'.$report->print_table(true);
151 }
d08bba83 152 }
65dd61bd 153 }
772229f3 154} else { //Students will see just their own report
65dd61bd 155
772229f3 156 // Create a report instance
157 $report = new grade_report_user($courseid, $gpr, $context, $userid);
158
159 // print the page
b5e7b2bf 160 print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user));
772229f3 161
162 if ($report->fill_table()) {
163 echo '<br />'.$report->print_table(true);
164 }
1fdab481 165}
772229f3 166
a071dbb6 167echo $OUTPUT->footer();