forked from coactum/moodle-mod_margic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grade_entry.php
239 lines (191 loc) · 10.1 KB
/
grade_entry.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
<?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 page for the grading of entries in mod_margic.
*
* @package mod_margic
* @copyright 2022 coactum GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\notification;
use mod_margic\local\helper;
require(__DIR__.'/../../config.php');
require_once($CFG->dirroot . '/mod/margic/grading_form.php');
require_once($CFG->dirroot . '/mod/margic/locallib.php');
global $DB, $USER;
// Course Module ID.
$id = required_param('id', PARAM_INT);
// Module instance ID as alternative.
$m = optional_param('m', null, PARAM_INT);
// ID of the entry to be graded.
$entryid = required_param('entryid', PARAM_INT);
$margic = margic::get_margic_instance($id, $m, false, 'currententry', 0, 1);
$moduleinstance = $margic->get_module_instance();
$course = $margic->get_course();
$context = $margic->get_context();
$cm = $margic->get_course_module();
if (! $cm) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
if (! $course) {
throw new moodle_exception(get_string('incorrectcourseid', 'margic'));
}
if (! $coursesections = $DB->get_record("course_sections", ["id" => $cm->section])) {
throw new moodle_exception(get_string('incorrectmodule', 'margic'));
}
require_login($course, true, $cm);
require_capability('mod/margic:addentries', $context);
$PAGE->set_url('/mod/margic/grade_entry.php', ['id' => $id]);
$entry = $DB->get_record('margic_entries', ['id' => $entryid, 'margic' => $cm->instance]);
$grades = make_grades_menu($moduleinstance->scale);
// Prepare editor for files.
$data = new stdClass();
$data->id = $id;
$data->entry = $entry->id;
$data->timecreated = $entry->timecreated;
$data->{'feedback_' . $entry->id} = $entry->feedback;
$data->{'feedback_' . $entry->id . 'format'} = $entry->formatfeedback;
$data->sendgradingmessage = $moduleinstance->sendgradingmessage;
list ($editoroptions, $attachmentoptions) = helper::margic_get_editor_and_attachment_options($course, $context, $moduleinstance);
$data = file_prepare_standard_editor($data, 'feedback_' . $entry->id, $editoroptions, $context,
'mod_margic', 'feedback', $data->entry);
$data = file_prepare_standard_filemanager($data, 'attachment', $attachmentoptions, $context,
'mod_margic', 'attachment', $data->entry);
$data->{'rating_' . $entry->id} = $entry->rating;
// Instantiate gradingform and save submitted data if it exists.
$mform = new \mod_margic_grading_form(null, ['courseid' => $course->id, 'margic' => $moduleinstance, 'entry' => $entry,
'grades' => $grades, 'teacherimg' => '', 'editoroptions' => $editoroptions, ]);
$mform->set_data($data);
if ($fromform = $mform->get_data()) { // If grading form is submitted.
// In this case you process validated data.
if ($fromform->entry !== $entryid) {
redirect(new moodle_url('/mod/margic/view.php', ['id' => $id]), get_string('errfeedbacknotupdated', 'mod_margic'),
null, notification::NOTIFY_ERROR);
}
if (!$fromform->{'feedback_' . $entry->id . '_editor'}) {
redirect(new moodle_url('/mod/margic/view.php', ['id' => $id]),
get_string('errnofeedbackorratingdisabled', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
$fromform = file_postupdate_standard_editor($fromform, 'feedback_' . $entry->id, $editoroptions, $editoroptions['context'],
'mod_margic', 'feedback', $entry->id);
$newfeedback = file_rewrite_pluginfile_urls($fromform->{'feedback_' . $entry->id}, 'pluginfile.php', $context->id,
'mod_margic', 'feedback', $entry->id);
$newfeedback = format_text($newfeedback, $fromform->{'feedback_' . $entry->id . '_editor'}['format'], ['para' => false]);
if (isset($fromform->{'rating_' . $entry->id})) {
$newrating = $fromform->{'rating_' . $entry->id};
}
if (isset($newrating) && $newrating != $entry->rating) {
$ratingchanged = true;
} else {
$ratingchanged = false;
}
if ($newfeedback != $entry->feedback && !($newfeedback == '' && $entry->feedback == null)) {
$feedbackchanged = true;
} else {
$feedbackchanged = false;
}
if ($ratingchanged || $feedbackchanged) { // Only update entry if feedback has actually changed.
$timenow = time();
if (isset($newrating)) {
$entry->rating = $newrating;
}
$entry->feedback = $newfeedback;
$entry->formatfeedback = $fromform->{'feedback_' . $entry->id . '_editor'}['format'];
$entry->teacher = $USER->id;
$entry->timemarked = $timenow;
if (!$DB->update_record("margic_entries", $entry)) {
redirect(new moodle_url('/mod/margic/view.php', ['id' => $id]), get_string('errfeedbacknotupdated', 'mod_margic'),
null, notification::NOTIFY_ERROR);
}
if ($moduleinstance->assessed != 0) {
$ratingoptions = new stdClass();
$ratingoptions->contextid = $context->id;
$ratingoptions->component = 'mod_margic';
$ratingoptions->ratingarea = 'entry';
$ratingoptions->itemid = $entry->id;
$ratingoptions->aggregate = $moduleinstance->assessed; // The aggregation method.
$ratingoptions->scaleid = $moduleinstance->scale;
$ratingoptions->rating = $newrating;
$ratingoptions->userid = $entry->userid;
$ratingoptions->timecreated = $entry->timecreated;
$ratingoptions->timemodified = $timenow;
$ratingoptions->returnurl = $CFG->wwwroot . '/mod/margic/view.php?id' . $id;
$ratingoptions->assesstimestart = $moduleinstance->assesstimestart;
$ratingoptions->assesstimefinish = $moduleinstance->assesstimefinish;
// Check if there is already a rating, and if so, just update it.
if ($rec = helper::check_rating_entry($ratingoptions)) {
$ratingoptions->id = $rec->id;
$DB->update_record('rating', $ratingoptions, false);
} else {
$DB->insert_record('rating', $ratingoptions, false);
}
}
$record = $moduleinstance;
$record->cmidnumber = $cm->idnumber;
margic_update_grades($record, $entry->userid);
// Trigger module feedback updated event.
$event = \mod_margic\event\feedback_updated::create([
'objectid' => $entry->id,
'context' => $context,
]);
$event->trigger();
if ($fromform->sendgradingmessage) {
global $CFG;
$url = (new \moodle_url("/mod/margic/view.php?id=$id"))->out(false);
$obj = new stdClass();
$obj->user = fullname(core_user::get_user($entry->userid));
$obj->teacher = fullname(core_user::get_user($entry->teacher));
$obj->margic = format_string($moduleinstance->name);
$obj->url = $url;
// Send grading message.
$message = new \core\message\message();
$message->component = 'mod_margic';
$message->name = 'gradingmessages';
$message->userfrom = core_user::get_noreply_user(); // If the message is 'from' a specific user you can set them here.
$message->userto = $entry->userid;
$message->subject = get_string('gradingmailsubject', 'mod_margic');
$message->fullmessage = get_string('gradingmailfullmessage', 'mod_margic', $obj);
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>' . get_string('gradingmailfullmessagehtml', 'mod_margic', $obj) . '</p>';
$message->smallmessage = get_string('gradingmailfullmessage', 'mod_margic', $obj);
$message->notification = 1; // Because this is a notification generated from Moodle, not a user-to-user message.
$message->contexturl = $url; // A relevant URL for the notification.
$message->contexturlname = get_string('modulename', 'mod_margic')
. ' - ' . get_string('overview', 'mod_margic'); // Link title explaining where users get to for the contexturl.
$header = '';
$urllink = '<a href="' . $url . '" target="_blank">' . $url . '</a>';
$footer = '<br><br> --------------------------------------------------------------------- <br> '
. get_string('mailfooter', 'mod_margic', ['systemname' => get_config('shortname'),
'coursename' => $course->fullname, 'name' => $moduleinstance->name, 'url' => $url, ]);
$content = ['*' => ['header' => $header, 'footer' => $footer]]; // Extra content for specific processor.
$message->set_additional_content('email', $content);
// Actually send the message.
$messageid = message_send($message);
}
// Redirect after updated from feedback and grades.
redirect(new moodle_url('/mod/margic/view.php',
['id' => $id, 'focusgradingform' => $entry->id, 'annotationmode' => 1]),
get_string('feedbackupdated', 'mod_margic'), null, notification::NOTIFY_SUCCESS);
} else {
redirect(new moodle_url('/mod/margic/view.php',
['id' => $id, 'focusgradingform' => $entry->id, 'annotationmode' => 1]),
get_string('errfeedbacknotupdated', 'mod_margic'), null, notification::NOTIFY_ERROR);
}
} else {
redirect(new moodle_url('/mod/margic/view.php',
['id' => $id, 'focusgradingform' => $entry->id, 'annotationmode' => 1]),
get_string('errfeedbacknotupdated', 'mod_margic'), null, notification::NOTIFY_ERROR);
}