-
Notifications
You must be signed in to change notification settings - Fork 7
/
reportsingle.php
359 lines (308 loc) · 14.5 KB
/
reportsingle.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?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/>.
/**
* This page opens the current report instance of diary.
*
* @package mod_diary
* @copyright 2020 AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_diary\local\results;
require_once("../../config.php");
require_once("lib.php");
require_once($CFG->dirroot.'/rating/lib.php');
$id = required_param('id', PARAM_INT); // Course module.
$action = optional_param('action', 'currententry', PARAM_ALPHANUMEXT); // Action(default to current entry).
$user = required_param('user', PARAM_INT); // User ID.
if (!$cm = get_coursemodule_from_id('diary', $id)) {
throw new moodle_exception(get_string('incorrectmodule', 'diary'));
}
if (!$course = $DB->get_record('course', ['id' => $cm->course])) {
throw new moodle_exception(get_string('incorrectcourseid', 'diary'));
}
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/diary:manageentries', $context);
if (!$diary = $DB->get_record('diary', ['id' => $cm->instance])) {
throw new moodle_exception(get_string('invalidid', 'diary'));
}
// 20201016 Get the name for this diary activity.
$diaryname = format_string($diary->name, true, ['context' => $context]);
// 20201014 Set a default sorting order for entry retrieval.
if ($sortoption = get_user_preferences('sortoption')) {
$sortoption = get_user_preferences('sortoption');
} else {
set_user_preference('sortoption', 'u.lastname ASC, u.firstname ASC');
$sortoption = get_user_preferences('sortoption');
}
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'reportsingleallentries';
// Get ALL diary entries from this diary, for this user, from newest to oldest.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user], $sort = 'timecreated DESC');
}
// Handle toolbar capabilities.
if (! empty($action)) {
switch ($action) {
case 'download':
if (has_capability('mod/diary:manageentries', $context)) {
// Call download entries function in lib.php.
// 20231007 Added set_url to fix error.
$PAGE->set_url('/mod/diary/view.php', ['id' => $cm->id]);
results::download_entries($context, $course, $diary);
}
break;
case 'currententry':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'currententry';
// Get ALL diary entries in an order that will result in showing the users most current entry.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user]);
}
break;
case 'firstentry':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'firstentry';
// Get ALL diary entries in an order that will result in showing the users very first entry.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user], $sort = 'timecreated ASC');
}
break;
case 'lowestgradeentry':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'lowestgradeentry';
// Get ALL diary entries in an order that will result in showing the users
// oldest, ungraded entry. Once all ungraded entries have a grade, the entry
// with the lowest grade is shown. For duplicate low grades, the entry that
// is oldest, is shown.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user],
$sort = 'rating ASC, timemodified DESC');
}
break;
case 'highestgradeentry':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'highestgradeentry';
// Get ALL diary entries in an order that will result in showing the users highest
// graded entry. Duplicates high grades result in showing the most recent entry.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user], $sort = 'rating DESC');
}
break;
case 'latestmodifiedentry':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'latestmodifiedentry';
// Get ALL diary entries in an order that will result in showing the users
// most recently modified entry. At the moment, this is no different from current entry.
// May be needed for future version if editing old entries is allowed.
$eee = $DB->get_records('diary_entries', ['diary' => $diary->id, 'userid' => $user], $sort = 'timemodified DESC');
}
break;
default:
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'currententry';
}
}
}
// 20211214 Header with additional info in the url.
$PAGE->set_url('/mod/diary/reportsingle.php',
[
'id' => $id,
'user' => $user,
'action' => $action,
]
);
$PAGE->navbar->add((get_string("rate", "diary")).' '.(get_string("entries", "diary")));
$PAGE->set_title($diaryname);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading($diaryname);
// 20201016 Added missing header label. 20210511 Changed to remove hard coded <h5>'s.
echo '<div>'.(get_string('sortorder', "diary"));
echo (get_string($stringlable, "diary"));
// 20200827 Added link to index.php page.
echo '<span style="float: right;"><a href="index.php?id='.$course->id.'">'
.get_string('viewalldiaries', 'diary').'</a></span></div>';
// Save our current user id and also get his details. CHECK - might not need this.
$users = $user;
$user = $DB->get_record('user', ['id' => $user]);
$debug = [];
$countnum = 0;
if ($eee) {
// Now, filter down to get entry by any user who has made at least one entry.
foreach ($eee as $ee) {
$countnum++;
$debug[$countnum.' printing $user'] = $user;
$debug[$countnum.' printing $eee'] = $eee;
$debug[$countnum.' printing $ee'] = $ee;
print_object($debug);
//die;
$entrybyuser[$ee->userid] = $ee;
$entrybyentry[$ee->id] = $ee;
$entrybyuserentry[$ee->userid][$ee->id] = $ee;
// ...print_object($entrybyuserentry);...
}
} else {
$entrybyuser = [];
$entrybyentry = [];
}
// Process incoming data if there is any.
if ($data = data_submitted()) {
results::diary_entries_feedback_update($cm, $context, $diary, $data, $entrybyuser, $entrybyentry);
// Trigger module feedback updated event.
$event = \mod_diary\event\feedback_updated::create(
[
'objectid' => $diary->id,
'context' => $context,
]
);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('diary', $diary);
$event->trigger();
} else {
// Trigger module viewed event.
$event = \mod_diary\event\entries_viewed::create(
[
'objectid' => $diary->id,
'context' => $context,
]
);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('diary', $diary);
$event->trigger();
}
if (! $users) {
echo $OUTPUT->heading(get_string("nousersyet"));
} else {
$output = '';
// Create download, reload, current, oldest, lowest, highest, and most recent tool buttons for all entries.
if (has_capability('mod/diary:manageentries', $context)) {
// 20201003 Changed toolbar code to $output instead of html_writer::alist.
$options = [];
$options['id'] = $id;
$options['diary'] = $diary->id;
$options['user'] = $users;
// Add download button.
$options['action'] = 'download';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('i/export', get_string('csvexport', 'diary')),
[
'class' => 'toolbutton',
]
);
// Add reload toolbutton.
$options['action'] = $stringlable;
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/reload', get_string('reload', 'diary')),
[
'class' => 'toolbutton',
]
);
$options['action'] = 'currententry';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('i/edit', get_string('currententry', 'diary')),
[
'class' => 'toolbutton',
]
);
$options['action'] = 'firstentry';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/left', get_string('firstentry', 'diary')),
[
'class' => 'toolbutton',
]
);
$options['action'] = 'lowestgradeentry';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/down', get_string('lowestgradeentry', 'diary')),
[
'class' => 'toolbutton',
]
);
$options['action'] = 'highestgradeentry';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/up', get_string('highestgradeentry', 'diary')),
[
'class' => 'toolbutton',
]
);
$options['action'] = 'latestmodifiedentry';
$url = new moodle_url('/mod/diary/reportsingle.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/right', get_string('latestmodifiedentry', 'diary')),
[
'class' => 'toolbutton',
]
);
// 20210511 Reorganized group and toolbar output. 20220102 Added action.
echo '<span>'.groups_print_activity_menu($cm, $CFG->wwwroot."/mod/diary/reportsingle.php?id=$cm->id&action=currententry")
.'</span><span style="float: right;">'.get_string('toolbar', 'diary').$output.'</span>';
}
// Next line is different from Journal line 171.
$grades = make_grades_menu($diary->scale);
if (! $teachers = get_users_by_capability($context, 'mod/diary:manageentries')) {
throw new moodle_exception(get_string('noentriesmanagers', 'diary'));
}
// 20211213 Start the page area where feedback and grades are added and will need to be saved.
// 20230810 Changed due to pull request #29.
$url = new moodle_url('reportsingle.php', ['id' => $id, 'user' => $user->id, 'action' => 'allentries']);
echo '<form action="'.$url->out(false).'" method="post">';
// Create a variable with all the info to save all my feedback, so it can be used multiple places.
// 20211210 Cleaned up unnecessary escaped double quotes.
$saveallbutton = '';
$saveallbutton = '<p class="feedbacksavereturn">';
$saveallbutton .= '<input type="hidden" name="id" value="'.$cm->id.'" />';
$saveallbutton .= '<input type="hidden" name="sesskey" value="sesskey()" />';
$saveallbutton .= '<input type="submit" class="btn btn-primary" style="border-radius: 8px" value="'
.get_string('saveallfeedback', 'diary').'" />';
// phpcs:ignore
/*
$url = $CFG->wwwroot.'/mod/diary/reportsingle.php?id='.$id.'&user='.$user->id.'&action=allentries';
// 20211210 Cleaned up unnecessary escaped double quotes.
$saveallbutton .= ' <a href="'.$url.' class="feedbacksavestay">';
$saveallbutton .= '<input type="hidden" name="id" value="'.$cm->id.'" />';
$saveallbutton .= '<input type="hidden" name="sesskey" value="sesskey()" />';
$saveallbutton .= '<input type="submit" class="btn btn-primary" style="border-radius: 8px" value="'
.get_string('addtofeedback', 'diary').'"</a>';
*/
// 20211230 Tacked on an action for the return URL.
// 20201222 Added a return to report.php button if you do not want to save feedback.
// 20230810 Made changes based on pull request#29.
$url2 = new moodle_url($CFG->wwwroot.'/mod/diary/report.php', ['id' => $id, 'action' => 'currententry']);
$saveallbutton .= ' <a href="'.$url2->out(true)
.'" class="btn btn-secondary" role="button" style="border-radius: 8px">'
.get_string('returntoreport', 'diary', $diary->name)
.'</a>';
$saveallbutton .= "</p>";
// Add save button at the top of the list of users with entries.
echo $saveallbutton;
// 20210705 Added new activity color setting. Only need to set the overall background color here.
$dcolor3 = $diary->entrybgc;
foreach ($eee as $ee) {
// 20210511 Changed to using class.
echo '<div class="entry" style="background: '.$dcolor3.'">';
// Based on the single selected user, print all their entries on screen.
echo results::diary_print_user_entry($context,
$course,
$diary,
$user,
$ee,
$teachers,
$grades);
echo '</div>';
// Since the list can be quite long, add a save button after each entry that will save ALL visible changes.
echo $saveallbutton;
}
// End the page area where feedback and grades are added and will need to be saved.
echo "</form>";
}
echo $OUTPUT->footer();