-
Notifications
You must be signed in to change notification settings - Fork 2
/
block_my_feedback.php
245 lines (213 loc) · 8.35 KB
/
block_my_feedback.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
<?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/>.
use core\context\user;
use core_course\external\course_summary_exporter;
use mod_quiz\question\display_options;
/**
* Block definition class for the block_my_feedback plugin.
*
* @package block_my_feedback
* @copyright 2023 Stuart Lamour
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_my_feedback extends block_base {
/**
* Initialises the block.
*
* @return void
*/
public function init() {
global $USER;
// If $USER->firstname is not set yet do not try to use it.
if (!isset($USER->firstname)) {
$this->title = get_string('pluginname', 'block_my_feedback');
} else {
$this->title = get_string('feedbackfor', 'block_my_feedback').' '.$USER->firstname;
}
}
/**
* Gets the block contents.
*
* @return stdClass The block content.
*/
public function get_content(): stdClass {
global $OUTPUT, $USER;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$this->content->footer = '';
$template = new stdClass();
$template->feedback = $this->fetch_feedback($USER);
// Hide the block when no content.
if (!$template->feedback) {
return $this->content;
}
$this->content->text = $OUTPUT->render_from_template('block_my_feedback/content', $template);
return $this->content;
}
/**
* Get my feedback call for a user.
*
* Return users 5 most recent feedbacks.
* @param stdClass $user
* @return array feedback items.
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public function fetch_feedback($user): array {
global $DB;
$submissions = $this->get_submissions($user);
// No feedback.
if (!$submissions) {
return [];
}
// Template data for mustache.
$template = new stdClass();
$template->feedback = [];
$i = 0; // We only want to show up to 5 grades - so count the output.
foreach ($submissions as $f) {
// Check if a quiz feedback should be shown.
if ($f->modname == 'quiz' && !$this->show_quiz_submission($f)) {
continue;
}
// Check if we have enough grades to show.
if ($i++ >= 5) {
break;
}
$feedback = new stdClass();
$feedback->id = $f->gradeid;
$feedback->date = date('jS F', $f->lastmodified);
$feedback->activityname = $f->name;
$feedback->link = new moodle_url('/mod/'.$f->modname.'/view.php', ['id' => $f->cmid]);
// Course.
$course = $DB->get_record('course', ['id' => $f->course]);
$feedback->coursename = $course->fullname;
// UCL want to always hide grader for quiz and turnitintooltwo.
if ($f->modname == 'quiz' || $f->modname == 'turnitintooltwo') {
$f->hidegrader = true;
}
// Marker.
if ($f->hidegrader) {
// Hide grader, so use course image.
// Course image.
$feedback->icon = course_summary_exporter::get_course_image($course);
} else {
// Marker details.
$grader = core_user::get_user($f->grader);
$userpicture = new user_picture($grader);
$userpicture->size = 100;
$icon = $userpicture->get_url($this->page)->out(false);
$feedback->tutorname = fullname($grader);
$feedback->icon = $icon;
}
$template->feedback[] = $feedback;
}
return $template->feedback;
}
/**
* Get all assign, quiz and turnitintooltwo submissions for a user that are no older than 3 month.
*
* @param stdClass $user
* @return array
* @throws coding_exception
* @throws dml_exception
*/
public function get_submissions($user) {
global $DB;
// Limit to last 3 months.
$since = strtotime('-3 month');
// Construct the IN clause.
list($insql, $params) = $DB->get_in_or_equal(['assign', 'quiz', 'turnitintooltwo'], SQL_PARAMS_NAMED);
// Add other params.
$params['userid'] = $user->id;
$params['since'] = $since;
$params['wfreleased'] = 'released'; // Has the grade been released?
$unixtimestamp = time();
// Query the modified grades / feedbacks for assignments, quizzes and turnitin.
$sql = "SELECT
gg.id AS gradeid,
gi.courseid AS course,
a.hidegrader AS hidegrader,
gi.itemname AS name,
gg.userid AS userid,
gg.usermodified AS grader,
gg.timemodified AS lastmodified,
cm.id AS cmid,
gi.itemmodule AS modname,
cm.instance as instance
FROM
{grade_grades} gg
JOIN
{grade_items} gi ON gg.itemid = gi.id
JOIN
{modules} m ON gi.itemmodule = m.name
JOIN
{course_modules} cm ON gi.courseid = cm.course AND m.id = cm.module AND gi.iteminstance = cm.instance
JOIN
{user} u ON gg.usermodified = u.id
LEFT JOIN
{assign} a ON gi.iteminstance = a.id AND gi.itemmodule = 'assign'
LEFT JOIN
{assign_user_flags} uf ON gg.userid = uf.userid AND a.id = uf.assignment AND a.markingworkflow = 1
WHERE
(gg.finalgrade IS NOT NULL OR gg.feedback IS NOT NULL)
AND gi.itemmodule $insql
AND (COALESCE(a.markingworkflow, 0) = 0 OR (a.markingworkflow = 1 AND uf.workflowstate = :wfreleased))
AND gi.hidden < $unixtimestamp
AND gg.timemodified >= :since AND gg.timemodified <= $unixtimestamp
AND gg.userid = :userid
ORDER BY gg.timemodified DESC";
return $DB->get_records_sql($sql, $params);
}
/**
* Checking Review options for showing quiz submissions.
*
* @param stdClass $submission
* @return bool
* @throws dml_exception
*/
public function show_quiz_submission(stdClass $submission): bool {
global $DB;
$quizobj = $DB->get_record('quiz', ['id' => $submission->instance]);
if ($quizobj->timeclose > 0 && $quizobj->timeclose < time()) { // The quiz is closed.
$reviewoptions = display_options::make_from_quiz($quizobj, display_options::AFTER_CLOSE);
} else {
$reviewoptions = display_options::make_from_quiz($quizobj, display_options::LATER_WHILE_OPEN);
}
// Only when these options are all set the submission should be shown.
// NB: when maxmarks and marks are both set $reviewoptions->marks == 2.
if ($reviewoptions->attempt + $reviewoptions->correctness + $reviewoptions->marks == 4) {
return true;
}
return false;
}
/**
* Defines in which pages this block can be added.
*
* @return array of the pages where the block can be added.
*/
public function applicable_formats() {
return [
'admin' => false,
'site-index' => true,
'course-view' => false,
'mod' => false,
'my' => true,
];
}
}