forked from drachels/moodle-mod_diary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.php
382 lines (336 loc) · 15.9 KB
/
report.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?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 2019 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.
if (! $cm = get_coursemodule_from_id('diary', $id)) {
throw new moodle_exception(get_string('incorrectmodule', 'diary'));
}
if (! $course = $DB->get_record("course", array(
"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", array(
"id" => $cm->instance
))) {
throw new moodle_exception(get_string('invalidid', 'diary'));
}
$diaryid = optional_param('diary', $diary->id, PARAM_INT);
$action = optional_param('action', 'currententry', PARAM_ACTION); // Action(default to current entry).
// 20201016 Get the name for this diary activity.
$diaryname = format_string($diary->name, true, array(
'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');
}
// Handle toolbar capabilities.
if (! empty($action)) {
switch ($action) {
case 'download':
if (has_capability('mod/diary:manageentries', $context)) {
// Call download entries function in lib.php.
results::download_entries($context, $course, $diary);
}
break;
case 'lastnameasc':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'lastnameasc';
// 20201014 Set order and get ALL diary entries in lastname ascending order.
set_user_preference('sortoption', 'u.lastname ASC, u.firstname ASC');
$sortoption = get_user_preferences('sortoption');
$eee = $DB->get_records("diary_entries", array(
"diary" => $diary->id
));
}
break;
case 'lastnamedesc':
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'lastnamedesc';
// 20201014 Set order and get ALL diary entries in lastname descending order.
set_user_preference('sortoption', 'u.lastname DESC, u.firstname DESC');
$sortoption = get_user_preferences('sortoption');
$eee = $DB->get_records("diary_entries", array(
"diary" => $diary->id
));
}
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", array(
"diary" => $diary->id
));
}
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", array(
"diary" => $diary->id
), $sort = 'timecreated DESC');
}
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", array(
"diary" => $diary->id
), $sort = 'rating DESC, 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", array(
"diary" => $diary->id
), $sort = 'rating ASC');
}
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", array(
"diary" => $diary->id
), $sort = 'timemodified ASC');
}
break;
default:
if (has_capability('mod/diary:manageentries', $context)) {
$stringlable = 'currententry';
}
}
}
// Header.
$PAGE->set_url('/mod/diary/report.php', array(
'id' => $id,
'diary' => $diaryid,
'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);
// 20210511 Changed to using div and span.
echo '<div class="sortandaggregate">';
echo ('<span>'.get_string('sortorder', "diary"));
echo (get_string($stringlable, "diary").'</span>');
// 20200827 Added link to index.php page. 20210501 Moved to here.
echo '<span><a style="float: right;" href="index.php?id='.$course->id.'">'
.get_string('viewalldiaries', 'diary').'</a></span></div>';
// Get a list of groups for this course.
$currentgroup = groups_get_activity_group($cm, true);
if ($currentgroup) {
$groups = $currentgroup;
} else {
$groups = '';
}
// Get a sorted list of users in the current group to use for processing the report.
$users = get_users_by_capability($context, 'mod/diary:addentries', '', $sort = 'lastname ASC, firstname ASC', '', '', $groups);
if ($eee) {
// Now, filter down to get entry by any user who has made at least one entry.
foreach ($eee as $ee) {
$entrybyuser[$ee->userid] = $ee;
$entrybyentry[$ee->id] = $ee;
$entrybyuserentry[$ee->userid][$ee->id] = $ee;
}
} else {
$entrybyuser = array();
$entrybyentry = array();
}
// 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(array(
'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(array(
'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 = array();
$options['id'] = $id;
$options['diary'] = $diary->id;
// Add download button.
$options['action'] = 'download';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('i/export', get_string('csvexport', 'diary')), array(
'class' => 'toolbutton'
));
// Add sort by lastname ascending button.
$options['action'] = 'lastnameasc';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/sort_asc', get_string('lastnameasc', 'diary')), array(
'class' => 'toolbutton'
));
// Add sort by lastname descending button.
$options['action'] = 'lastnamedesc';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/sort_desc', get_string('lastnamedesc', 'diary')), array(
'class' => 'toolbutton'
));
// Add reload toolbutton.
$options['action'] = $stringlable;
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/reload', get_string('reload', 'diary')), array(
'class' => 'toolbutton'
));
$options['action'] = 'currententry';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('i/edit', get_string('currententry', 'diary')), array(
'class' => 'toolbutton'
));
$options['action'] = 'firstentry';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/left', get_string('firstentry', 'diary')), array(
'class' => 'toolbutton'
));
$options['action'] = 'lowestgradeentry';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/down', get_string('lowestgradeentry', 'diary')), array(
'class' => 'toolbutton'
));
$options['action'] = 'highestgradeentry';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/up', get_string('highestgradeentry', 'diary')), array(
'class' => 'toolbutton'
));
$options['action'] = 'latestmodifiedentry';
$url = new moodle_url('/mod/diary/report.php', $options);
$output .= html_writer::link($url, $OUTPUT->pix_icon('t/right', get_string('latestmodifiedentry', 'diary')), array(
'class' => 'toolbutton'
));
// 20210511 Reorganized group and toolbar output. 20220102 Added action.
echo '<span>'.groups_print_activity_menu($cm, $CFG->wwwroot."/mod/diary/report.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'));
}
// 20211230 Changed action so that the sort order (action) is maintained.
// Start the page area where feedback and grades are added and will need to be saved.
echo '<form action="report.php?id='.$id.'&diaryid='.$diaryid.'&action='.$action.'" method="post">';
// Create a variable with all the info to save all my feedback, so it can be used multiple places.
// 20211027 changed to rounded buttons. 20211229 Removed escaped double quotes.
$saveallbutton = '';
$saveallbutton = '<p class="feedbacksave">';
$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").'" />';
// 20200421 Added a return button.
$url = $CFG->wwwroot . '/mod/diary/view.php?id=' . $id;
$saveallbutton .= ' <a href="'.$url
.'" class="btn btn-secondary" role="button" style="border-radius: 8px">'
.get_string('returnto', '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 the overall background here. Entry text bgc is in results.
$dcolor3 = $diary->entrybgc;
// Print a list of users who have completed at least one entry.
if ($usersdone = diary_get_users_done($diary, $currentgroup, $sortoption)) {
foreach ($usersdone as $user) {
echo '<div class="entry" style="background: '.$dcolor3.'">';
// Based on toolbutton and on list of users with at least one entry, print the entries on screen.
echo results::diary_print_user_entry($context,
$course,
$diary,
$user,
$entrybyuser[$user->id],
$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;
// Remove users who are done from our list of everyone so we finish with a list of users with no entries.
unset($users[$user->id]);
}
}
// List remaining users with no entries.
foreach ($users as $user) {
// 20210511 Changed to class.
echo '<div class="entry" style="background: '.$dcolor3.'">';
echo results::diary_print_user_entry($context,
$course,
$diary,
$user,
null,
$teachers,
$grades);
echo '</div><br>';
}
// 20210609 Check for empty list to prevent two sets of buttons at bottom of the report page.
if ($users) {
// Add a, Save all my feedback, button at the bottom of the page/list of users with no entries.
echo $saveallbutton;
}
// End the page area where feedback and grades are added and will need to be saved.
echo "</form>";
}
echo $OUTPUT->footer();