Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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/>.
16
 
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
 
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/user/lib.php';
29
 
30
$courseid = required_param('id', PARAM_INT);
1441 ariadna 31
// 0 - view all reports. null - view own report. non-zero and non-null - view other user report.
1 efrain 32
$userid   = optional_param('userid', null, PARAM_INT);
33
$userview = optional_param('userview', 0, PARAM_INT);
34
 
35
$PAGE->set_url(new moodle_url('/grade/report/user/index.php', ['id' => $courseid]));
36
 
37
if ($userview == 0) {
38
    $userview = get_user_preferences('gradereport_user_view_user', GRADE_REPORT_USER_VIEW_USER);
39
} else {
40
    set_user_preference('gradereport_user_view_user', $userview);
41
}
42
 
43
// Basic access checks.
44
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
45
    throw new \moodle_exception('invalidcourseid');
46
}
47
require_login($course);
48
$PAGE->set_pagelayout('report');
49
 
50
$context = context_course::instance($course->id);
51
require_capability('gradereport/user:view', $context);
52
 
53
if ($userid === 0) {
54
    require_capability('moodle/grade:viewall', $context);
55
} else if ($userid) {
56
    if (!$DB->get_record('user', ['id' => $userid, 'deleted' => 0]) || isguestuser($userid)) {
57
        throw new \moodle_exception('invaliduser');
58
    }
59
}
60
 
61
$access = false;
62
if (has_capability('moodle/grade:viewall', $context)) {
63
    // User can view all course grades.
64
    $access = true;
65
} else if (($userid == $USER->id || is_null($userid)) && has_capability('moodle/grade:view', $context) && $course->showgrades) {
66
    // User can view own grades.
67
    $access = true;
68
} else if (has_capability('moodle/grade:viewall', context_user::instance($userid)) && $course->showgrades) {
69
    // User can view grades of this user, The user is an parent most probably.
70
    $access = true;
71
}
72
 
73
if (!$access) {
74
    // The user has no access to grades.
75
    throw new \moodle_exception('nopermissiontoviewgrades', 'error',  $CFG->wwwroot.'/course/view.php?id='.$courseid);
76
}
77
 
78
// Initialise the grade tracking object.
79
$gpr = new grade_plugin_return(['type' => 'report', 'plugin' => 'user', 'courseid' => $courseid, 'userid' => $userid]);
80
 
81
// Infer the users previously selected report via session tracking.
82
if (!isset($USER->grade_last_report)) {
83
    $USER->grade_last_report = [];
84
}
85
$USER->grade_last_report[$course->id] = 'user';
86
 
87
// First make sure we have proper final grades.
1441 ariadna 88
$taskindicator = new \core\output\task_indicator(
89
    \core_course\task\regrade_final_grades::create($courseid),
90
    get_string('recalculatinggrades', 'grades'),
91
    get_string('recalculatinggradesadhoc', 'grades'),
92
    $PAGE->url,
93
);
94
if (!$taskindicator->has_task_record()) {
95
    grade_regrade_final_grades_if_required($course);
96
}
1 efrain 97
 
98
$gradesrenderer = $PAGE->get_renderer('core_grades');
99
 
100
// Teachers will see all student reports.
101
if (has_capability('moodle/grade:viewall', $context)) {
1441 ariadna 102
    if ($taskindicator->has_task_record()) {
103
        // We need to bail out early as getting the gradeable users requires calculations to be complete,
104
        // so just display a basic header with navigation, and the indicator.
105
        $actionbar = new \core_grades\output\general_action_bar($context, $PAGE->url, 'report', 'user');
106
        print_grade_page_head($course->id, 'report', 'user', actionbar: $actionbar);
107
        echo $OUTPUT->render($taskindicator);
108
        echo $OUTPUT->footer();
109
        exit;
110
    }
111
 
1 efrain 112
    // Verify if we are using groups or not.
113
    $groupmode = groups_get_course_groupmode($course);
114
    $currentgroup = $gpr->groupid;
115
    // Conditionally add the group JS if we have groups enabled.
116
    if ($groupmode) {
1441 ariadna 117
        $baseurl = new moodle_url('/grade/report/user/index.php', ['id' => $courseid]);
118
        $PAGE->requires->js_call_amd('core_course/actionbar/group', 'init', [$baseurl->out(false)]);
1 efrain 119
    }
120
 
121
    // To make some other functions work better later.
122
    if (!$currentgroup) {
123
        $currentgroup = null;
124
    }
125
 
126
    $isseparategroups = ($course->groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context));
127
 
128
    if ($isseparategroups && (!$currentgroup)) {
129
        // No separate group access, The user can view only themselves.
130
        $userid = $USER->id;
131
    }
132
 
133
    // If there is a stored (last viewed) user in a session variable, bypass the user select zero state and display the
134
    // report for that user.
135
    $lastvieweduserid = $SESSION->gradereport_user["useritem-{$context->id}"] ?? null;
136
    if (is_null($userid) && !is_null($lastvieweduserid)) {
137
        $userid = $lastvieweduserid;
138
    }
139
 
140
    $gradableusers = grade_report::get_gradable_users($courseid, $currentgroup);
141
    // Validate whether the requested user is a valid gradable user in this course. If, not display the user select
142
    // zero state.
143
    if (empty($gradableusers) || ($userid && !array_key_exists($userid, $gradableusers))) {
144
        $userid = null;
145
    }
146
 
147
    $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
148
    $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
149
    $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
150
 
151
    if ($userview == GRADE_REPORT_USER_VIEW_USER) {
152
        $viewasuser = true;
153
    } else {
154
        $viewasuser = false;
155
    }
156
 
157
    $gui = new graded_users_iterator($course, null, $currentgroup);
158
    $gui->require_active_enrolment($showonlyactiveenrol);
159
    $gui->init();
160
 
161
    if (is_null($userid)) { // Zero state.
162
        $actionbar = new \gradereport_user\output\action_bar($context, $userview, null, $currentgroup);
163
        // Print header.
164
        print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
165
            null, null, null, $actionbar);
166
 
167
        if (empty($gradableusers)) { // There are no available gradable users, display a notification.
168
            $message = $currentgroup ? get_string('nostudentsingroup') : get_string('nostudentsyet');
169
            echo $OUTPUT->notification($message, 'warning', false);
170
        } else { // Otherwise, display the zero state template.
171
            $report = new gradereport_user\report\user($courseid, $gpr, $context, $USER->id, $viewasuser);
172
            echo $report->output_report_zerostate();
173
        }
174
    } else if ($userid == 0) { // Show all reports.
175
        // Store the id of the current user item in a session variable which represents the last viewed item.
176
        $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
177
 
178
        $actionbar = new \gradereport_user\output\action_bar($context, $userview, 0, $currentgroup);
179
        print_grade_page_head($courseid, 'report', 'user', false, false, null, true,
180
            null, null, null, $actionbar);
181
 
182
        while ($userdata = $gui->next_user()) {
183
            $user = $userdata->user;
184
            $report = new gradereport_user\report\user($courseid, $gpr, $context, $user->id, $viewasuser);
185
            $userheading = $gradesrenderer->user_heading($report->user, $courseid, false);
186
 
187
            echo $userheading;
188
 
189
            if ($report->fill_table()) {
190
                echo $report->print_table(true);
191
            }
192
        }
193
    } else { // Show one user's report.
194
        // Store the id of the current user item in a session variable which represents the last viewed item.
195
        $SESSION->gradereport_user["useritem-{$context->id}"] = $userid;
196
 
197
        $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid, $viewasuser);
198
        $actionbar = new \gradereport_user\output\action_bar($context, $userview, $report->user->id, $currentgroup);
199
 
200
        print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user, $actionbar);
201
 
202
        if ($currentgroup && !groups_is_member($currentgroup, $userid)) {
203
            echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
204
        } else {
205
            if ($report->fill_table()) {
206
                echo $report->print_table(true);
207
            }
208
        }
209
        $userreportrenderer = $PAGE->get_renderer('gradereport_user');
210
        // Render the user report (previous/next) navigation in a sticky footer.
211
        $stickyfooter = new core\output\sticky_footer($userreportrenderer->user_navigation($gui, $userid, $courseid));
212
        echo $OUTPUT->render($stickyfooter);
213
    }
214
 
215
    $gui->close();
216
} else {
217
    // Students will see just their own report.
218
    // Create a report instance.
219
    $report = new gradereport_user\report\user($courseid, $gpr, $context, $userid ?? $USER->id);
220
 
221
    // Print the page.
222
    print_grade_page_head($courseid, 'report', 'user', false, false, false, true, null, null, $report->user);
223
 
1441 ariadna 224
    if ($taskindicator->has_task_record()) {
225
        echo $OUTPUT->render($taskindicator);
226
    } else if ($report->fill_table()) {
1 efrain 227
        echo $report->print_table(true);
228
    }
229
}
230
 
231
if (isset($report)) {
232
    // Trigger report viewed event.
233
    $report->viewed();
234
}
235
 
236
echo $OUTPUT->footer();