-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathreport.php
More file actions
331 lines (289 loc) · 11.2 KB
/
report.php
File metadata and controls
331 lines (289 loc) · 11.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?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/>.
/**
* The report page for the mod_journal plugin
*
* @package mod_journal
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
require_once('../../config.php');
require_once('lib.php');
$id = required_param('id', PARAM_INT); // Course module.
$sortby = optional_param('sortby', 'dateasc', PARAM_ALPHA);
$selecteduser = optional_param('selecteduser', 0, PARAM_INT);
$validsortoptions = [
'dateasc',
'datedesc',
'firstnameasc',
'firstnamedesc',
'lastnameasc',
'lastnamedesc',
];
if (!in_array($sortby, $validsortoptions)) {
$sortby = 'dateasc';
}
$cm = get_coursemodule_from_id('journal', $id, 0, false, MUST_EXIST);
$course = get_course($cm->course);
$journal = $DB->get_record('journal', ['id' => $cm->instance], '*', MUST_EXIST);
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/journal:manageentries', $context);
$PAGE->set_url('/mod/journal/report.php', ['id' => $id]);
$PAGE->navbar->add(get_string('entries', 'journal'));
$PAGE->set_title(get_string('modulenameplural', 'journal'));
$PAGE->set_heading($course->fullname);
// Moodle 4.0+ Activity Header support.
if (method_exists($PAGE, 'set_activity_record')) {
$PAGE->set_activity_record($journal);
}
$PAGE->requires->js_call_amd('mod_journal/report', 'init');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('entries', 'journal'));
// Retrieve entries.
$entries = $DB->get_records('journal_entries', ['journal' => $journal->id]) ?: [];
$entrybyuser = [];
$entrybyentry = [];
foreach ($entries as $entry) {
$entrybyuser[$entry->userid] = $entry;
$entrybyentry[$entry->id] = $entry;
}
// Map users with entries for the filter.
$userswithentries = array_map(function ($entry) {
return $entry->userid;
}, $entries);
// Group mode.
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
// Fetch users who can ADD entries (Students, but also Teachers/Admins).
$groups = $currentgroup ? $currentgroup : '';
$users = get_users_by_capability($context, 'mod/journal:addentries', '', '', '', '', $groups);
// Fetch users who can MANAGE entries (Teachers, Managers, Admins).
$teachers = get_users_by_capability($context, 'mod/journal:manageentries');
// Filter out Teachers/Managers from the "Users" list.
// This ensures they don't appear in the "Users who have not completed the journal" list.
if ($users && $teachers) {
foreach ($teachers as $teacherid => $teacher) {
if (isset($users[$teacherid])) {
unset($users[$teacherid]);
}
}
}
// Build filter options.
$useroptions = [];
if ($users) {
foreach ($users as $user) {
if (in_array($user->id, $userswithentries)) {
$useroptions[$user->id] = fullname($user);
}
}
}
// Process incoming data if there is any.
if ($data = data_submitted()) {
confirm_sesskey();
$feedback = [];
$data = (array) $data;
// Extract ratings and comments.
foreach ($data as $key => $val) {
if (strpos($key, 'r') === 0 || strpos($key, 'c') === 0) {
$type = substr($key, 0, 1);
$num = substr($key, 1);
$feedback[$num][$type] = (strpos($key, 'r') === 0 && $val === '') ? -1 : $val;
}
}
$timenow = time();
$count = 0;
foreach ($feedback as $num => $vals) {
if (!isset($entrybyentry[$num])) {
continue;
}
$entry = $entrybyentry[$num];
$ratingchanged = false;
$studentrating = clean_param($vals['r'], PARAM_INT);
// Ensure text is a string to satisfy PHP 8.1 strict typing.
$rawtext = isset($vals['c']['text']) ? (string) $vals['c']['text'] : '';
$studentcomment = clean_text($rawtext, FORMAT_HTML);
$studentcomment = file_save_draft_area_files(
$vals['c']['itemid'],
$context->id,
'mod_journal',
'feedback',
$num,
[],
$studentcomment
);
if ($studentrating != $entry->rating || $studentcomment != $entry->entrycomment) {
$ratingchanged = $studentrating != $entry->rating;
$newentry = (object) [
'id' => $num,
'rating' => $studentrating,
'entrycomment' => $studentcomment,
'teacher' => $USER->id,
'timemarked' => $timenow,
'mailed' => 0,
];
if (!$DB->update_record('journal_entries', $newentry)) {
echo $OUTPUT->notification(
get_string('failedupdate', 'journal', $entry->userid),
\core\output\notification::NOTIFY_ERROR
);
} else {
$count++;
$entrybyuser[$entry->userid]->rating = $studentrating;
$entrybyuser[$entry->userid]->entrycomment = $studentcomment;
$entrybyuser[$entry->userid]->teacher = $USER->id;
$entrybyuser[$entry->userid]->timemarked = $timenow;
$journal = $DB->get_record('journal', ['id' => $entrybyuser[$entry->userid]->journal]);
$journal->cmidnumber = $cm->idnumber;
journal_update_grades($journal, $entry->userid);
}
}
}
// Trigger feedback updated event.
$event = \mod_journal\event\feedback_updated::create([
'objectid' => $journal->id,
'context' => $context,
]);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('journal', $journal);
$event->trigger();
echo $OUTPUT->notification(get_string('feedbackupdated', 'journal', $count), \core\output\notification::NOTIFY_SUCCESS);
} else {
// Trigger entries viewed event.
$event = \mod_journal\event\entries_viewed::create([
'objectid' => $journal->id,
'context' => $context,
]);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('journal', $journal);
$event->trigger();
}
if (!$users && !journal_get_users_done($journal, $currentgroup)) {
echo $OUTPUT->notification(get_string('nousersyet', 'journal'), \core\output\notification::NOTIFY_INFO);
} else {
// Toolbar Area: Filter, Group, Sort.
echo html_writer::start_div('d-flex flex-wrap justify-content-between align-items-center mb-3');
// 1. User Filter Form.
$filterform = html_writer::start_tag('form', [
'method' => 'get',
'action' => 'report.php',
'class' => 'd-flex align-items-center me-3', // Class me-3 for spacing (Bootstrap 5).
]);
$filterform .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'id', 'value' => $id]);
$filterform .= html_writer::select(
$useroptions,
'selecteduser',
$selecteduser,
['' => get_string('allusers', 'search')],
['class' => 'form-select me-2'] // Classes form-select and me-2 (Bootstrap 5).
);
$filterform .= html_writer::empty_tag('input', [
'type' => 'submit',
'value' => get_string('filter'),
'class' => 'btn btn-secondary',
]);
$filterform .= html_writer::end_tag('form');
echo $filterform;
// 2. Group Activity Menu.
echo html_writer::div('', 'me-3'); // Spacer.
groups_print_activity_menu($cm, $PAGE->url);
// 3. Sorting Dropdown.
$options = [
'dateasc' => get_string('dateasc', 'journal'),
'datedesc' => get_string('datedesc', 'journal'),
'firstnameasc' => get_string('firstnameasc', 'journal'),
'firstnamedesc' => get_string('firstnamedesc', 'journal'),
'lastnameasc' => get_string('lastnameasc', 'journal'),
'lastnamedesc' => get_string('lastnamedesc', 'journal'),
];
$select = new single_select(
new moodle_url($PAGE->url),
'sortby',
$options,
$sortby,
null,
);
$select->set_label(get_string('sortby'), ['class' => 'me-1']);
echo html_writer::div($OUTPUT->render($select), 'divwrapper sortbyselect');
echo html_writer::end_div(); // End Toolbar.
$grades = make_grades_menu($journal->grade);
// Start the form.
echo html_writer::start_tag('form', [
'action' => $PAGE->url,
'method' => 'post',
]);
if ($usersdone = journal_get_users_done($journal, $currentgroup)) {
mod_journal_sort_users($usersdone, $sortby, $entrybyuser);
echo html_writer::tag('h3', get_string('userswhocompletedthejournal', 'journal'), ['class' => 'journalheader']);
foreach ($usersdone as $user) {
// Apply User Filter.
if ($selecteduser == 0 || $selecteduser == $user->id) {
journal_print_user_entry($course, $user, $entrybyuser[$user->id], $teachers, $grades, $cm->id);
}
// Remove user from the "Not Completed" list if they are in the "Done" list.
if (isset($users[$user->id])) {
unset($users[$user->id]);
}
}
}
if ($users) {
mod_journal_sort_users($users, $sortby, $entrybyuser);
echo html_writer::tag('h3', get_string('userswhodidnotcompletedthejournal', 'journal'), ['class' => 'journalheader']);
foreach ($users as $user) {
// Apply User Filter.
if ($selecteduser == 0 || $selecteduser == $user->id) {
journal_print_user_entry($course, $user, null, $teachers, $grades, $cm->id);
}
}
}
// Add hidden input fields.
echo html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'id',
'value' => $cm->id,
]);
echo html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'sesskey',
'value' => sesskey(),
]);
echo html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'sortby',
'value' => $sortby,
]);
// Keep the user filter active when saving feedback.
echo html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'selecteduser',
'value' => $selecteduser,
]);
// Add the submit button inside a paragraph with class.
echo html_writer::tag(
'p',
html_writer::empty_tag('input', [
'type' => 'submit',
'value' => get_string('saveallfeedback', 'journal'),
'class' => 'btn btn-secondary mt-1',
]),
['class' => 'feedbacksave']
);
// Close the form.
echo html_writer::end_tag('form');
}
echo $OUTPUT->footer();