-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathview.php
249 lines (221 loc) · 9.03 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Page module version information
*
* @package mod_listgrades
* @copyright 2023 Juan Pablo de Castro <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require_once($CFG->dirroot.'/mod/listgrades/lib.php');
require_once($CFG->dirroot.'/mod/listgrades/locallib.php');
require_once($CFG->libdir.'/completionlib.php');
$id = optional_param('id', 0, PARAM_INT); // Course Module ID.
if (!$cm = get_coursemodule_from_id('listgrades', $id)) {
throw new \moodle_exception('invalidcoursemodule');
}
$listgrades = $DB->get_record('listgrades', ['id' => $cm->instance], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/listgrades:view', $context);
// Completion and trigger events.
listgrades_view($listgrades, $course, $cm, $context);
$PAGE->set_url('/mod/listgrades/view.php', ['id' => $cm->id]);
$options = empty($listgrades->displayoptions) ? [] : (array) unserialize_array($listgrades->displayoptions);
$activityheader = ['hidecompletion' => false];
if (empty($options['printintro'])) {
$activityheader['description'] = '';
}
$PAGE->add_body_class('limitedwidth');
$PAGE->set_title($course->shortname.': '.$listgrades->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_activity_record($listgrades);
if (!$PAGE->activityheader->is_title_allowed()) {
$activityheader['title'] = "";
}
$PAGE->activityheader->set_attrs($activityheader);
echo $OUTPUT->header();
// Check if time is between opendate and closedate if set.
$isopen = ($listgrades->opendate == 0 || $listgrades->opendate < time())
&& ($listgrades->closedate == 0 || $listgrades->closedate > time());
// Check if user can manage the listgrades.
if (!$isopen) {
echo $OUTPUT->box_start('generalbox center clearfix');
echo $OUTPUT->notification(get_string('notopen', 'listgrades'), 'notifyproblem');
echo $OUTPUT->box_end();
}
if ($isopen || has_capability('moodle/course:manageactivities', $context)) {
// Grdoup mode.
groups_print_activity_menu($cm, $PAGE->url);
$groupid = groups_get_activity_group($cm, true) ?: null;
// Print intro text.
echo $OUTPUT->box_start('generalbox center clearfix');
echo format_module_intro('listgrades', $listgrades, $cm->id);
echo $OUTPUT->box_end();
// Use grader report to get the grades of the students.
$grader = new grade_report_listing($course->id, null, $context);
$grader->load_users();
$grader->load_final_grades();
$config = get_config('listgrades');
$mask = $config->userfieldmask;
$items = array_keys(unserialize($listgrades->items));
// Get the items that are to be published.
// Create the headers.
$headers = [];
if ($config->showusername) {
$headers[] = get_string('fullnameuser');
}
if ($config->showuserfield == 'always') {
$headers[] = 'ID';
}
$gradeitems = $grader->get_gradeitems();
$itemnames = $grader->get_item_names();
$listeditems = [];
// Extract the items preserving the order of gradetree.
foreach ($items as $itemid) {
$item = $gradeitems[abs($itemid)];
$listeditems[$itemid] = $item;
if ($itemid < 0) {
$headertext = get_string('feedbackforgradeitems', 'grades', $itemnames[-$itemid]);
} else {
$headertext = $itemnames[$itemid];
}
// Add the range of the grade item formatted to $gradeitem->get_decimals() decimals.
if ($itemid > 0 ) {
$headertext .= " (" . $item->get_formatted_range() . ")";
}
$headers[] = $headertext;
}
$grades = $grader->get_grades();
// Get users to be graded.
$users = $grader->get_users();
// Print a table with the grades.
// Create table object.
$table = new html_table();
$table->head = $headers;
$table->data = [];
// Get if the userfield is a custom field.
$userfield = $config->userfield;
$customfields = profile_get_custom_fields();
$iscustomfield = false;
foreach ($customfields as $field) {
if ($userfield == $field->shortname) {
$iscustomfield = true;
$fieldid = $field->id;
break;
}
}
// Load full user records.
$users = $DB->get_records_list('user', 'id', array_keys($users));
// Get users with userfield.
if ($iscustomfield) {
// Get custom field values.
$ids = array_keys($users);
[$sql_in, $params] = $DB->get_in_or_equal($ids);
// To Lower case.
$userfield = strtolower($userfield);
// Join the user_info_data table with the user table on userid and fieldid.
$sql = "SELECT d.userid, d.data as $userfield
FROM {user_info_data} d
WHERE d.fieldid = ?
AND d.userid $sql_in
ORDER BY d.data";
$paramssql = [$fieldid];
$paramssql = array_merge($paramssql, $params);
$customfields = $DB->get_records_sql($sql, $paramssql);
// Reset custom field.
foreach ($ids as $userid) {
$users[$userid]->$userfield = '';
}
// Add custom field to users.
foreach ($customfields as $userid => $customfield) {
$users[$userid]->$userfield = $customfield->$userfield;
}
}
$graderurl = new moodle_url('/grade/report/user/index.php', ['id' => $course->id]);
$group = groups_get_members($groupid, 'u.id', 'u.id');
$namecollisions = [];
// Compute full names.
foreach ($users as $user) {
$user->fullname = "{$user->lastname}, {$user->firstname}";
}
// if showuserfield is onlyifnamecollide, get the users that have the same username.
if ($config->showuserfield == 'onlyifnamecollide') {
// Get the list of users that have the same fullname.
$namecollisions = array_keys(array_count_values(array_column($users, 'fullname')), 2);
}
// Iterate over the users.
foreach ($users as $user) {
if ($groupid != null && !array_key_exists($user->id, $group)) {
continue;
}
$row = [];
// Get the grade of the student.
$grade = $grades[$user->id];
$id = $user->$userfield;
if ($id == null) {
debugging("User $user->id does not have a $userfield");
}
if ($config->aepdmethod) {
$maskeduserfield = listgrades_mask_identifier_aepd($id);
} else {
$maskeduserfield = listgrades_mask($id, $mask);
}
$graderurl->param('userid', $user->id);
// Print user fullname.
if ($config->showusername) {
$nametoshow = $user->fullname;
if ($config->showuserfield == 'onlyifnamecollide' && in_array($user->fullname, $namecollisions)) {
$nametoshow .= " ($maskeduserfield)";
}
$row[] = $nametoshow;
}
if ($config->showuserfield == 'always') {
$row[] = $maskeduserfield;
}
// Collect the gradeitems.
foreach ($listeditems as $itemid => $item) {
if ($itemid < 0) {
$gradevalue = $grade[$item->id];
$gradestr = $gradevalue->feedback ? $gradevalue->feedback : '--';
} else {
$gradevalue = $grade[$item->id];
$gradestr = $gradevalue->finalgrade ? format_float($gradevalue->finalgrade, $gradevalue->grade_item->get_decimals()) : '--';
}
if ($USER->id == $user->id) {
$row[] = html_writer::link($graderurl, $gradestr);
} else {
$row[] = $gradestr;
}
}
// Add the userfield and the grades to the table.
$table->data[] = $row;
}
// Sort the table by first column.
array_multisort($table->data);
// Print the table.
echo html_writer::table($table);
$signature = file_rewrite_pluginfile_urls($listgrades->footer, 'pluginfile.php', $context->id, 'mod_listgrades', 'footer', 0);
$formatoptions = new stdClass;
$formatoptions->noclean = true;
$formatoptions->overflowdiv = true;
$formatoptions->context = $context;
$signature = format_text($signature, $listgrades->footerformat, $formatoptions);
echo $OUTPUT->box($signature, "generalbox center clearfix");
}
echo $OUTPUT->footer();