2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains the definition for the library class for comment feedback plugin
20 * @package assignfeedback_comments
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Library class for comment feedback plugin extending feedback plugin base class.
30 * @package assignfeedback_comments
31 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class assign_feedback_comments extends assign_feedback_plugin {
37 * Get the name of the online comment feedback plugin.
40 public function get_name() {
41 return get_string('pluginname', 'assignfeedback_comments');
45 * Get the feedback comment from the database.
48 * @return stdClass|false The feedback comments for the given grade if it exists.
49 * False if it doesn't.
51 public function get_feedback_comments($gradeid) {
53 return $DB->get_record('assignfeedback_comments', array('grade'=>$gradeid));
57 * Get quickgrading form elements as html.
59 * @param int $userid The user id in the table this quickgrading element relates to
60 * @param mixed $grade - The grade data - may be null if there are no grades for this user (yet)
61 * @return mixed - A html string containing the html form elements required for quickgrading
63 public function get_quickgrading_html($userid, $grade) {
66 $feedbackcomments = $this->get_feedback_comments($grade->id);
67 if ($feedbackcomments) {
68 $commenttext = $feedbackcomments->commenttext;
72 $pluginname = get_string('pluginname', 'assignfeedback_comments');
73 $labeloptions = array('for'=>'quickgrade_comments_' . $userid,
74 'class'=>'accesshide');
75 $textareaoptions = array('name'=>'quickgrade_comments_' . $userid,
76 'id'=>'quickgrade_comments_' . $userid,
77 'class'=>'quickgrade');
78 return html_writer::tag('label', $pluginname, $labeloptions) .
79 html_writer::tag('textarea', $commenttext, $textareaoptions);
83 * Has the plugin quickgrading form element been modified in the current form submission?
85 * @param int $userid The user id in the table this quickgrading element relates to
86 * @param stdClass $grade The grade
87 * @return boolean - true if the quickgrading form element has been modified
89 public function is_quickgrading_modified($userid, $grade) {
92 $feedbackcomments = $this->get_feedback_comments($grade->id);
93 if ($feedbackcomments) {
94 $commenttext = $feedbackcomments->commenttext;
97 return optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT) != $commenttext;
102 * Override to indicate a plugin supports quickgrading.
104 * @return boolean - True if the plugin supports quickgrading
106 public function supports_quickgrading() {
111 * Return a list of the text fields that can be imported/exported by this plugin.
113 * @return array An array of field names and descriptions. (name=>description, ...)
115 public function get_editor_fields() {
116 return array('comments' => get_string('pluginname', 'assignfeedback_comments'));
120 * Get the saved text content from the editor.
122 * @param string $name
123 * @param int $gradeid
126 public function get_editor_text($name, $gradeid) {
127 if ($name == 'comments') {
128 $feedbackcomments = $this->get_feedback_comments($gradeid);
129 if ($feedbackcomments) {
130 return $feedbackcomments->commenttext;
138 * Get the saved text content from the editor.
140 * @param string $name
141 * @param string $value
142 * @param int $gradeid
145 public function set_editor_text($name, $value, $gradeid) {
148 if ($name == 'comments') {
149 $feedbackcomment = $this->get_feedback_comments($gradeid);
150 if ($feedbackcomment) {
151 $feedbackcomment->commenttext = $value;
152 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
154 $feedbackcomment = new stdClass();
155 $feedbackcomment->commenttext = $value;
156 $feedbackcomment->commentformat = FORMAT_HTML;
157 $feedbackcomment->grade = $gradeid;
158 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
159 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
167 * Save quickgrading changes.
169 * @param int $userid The user id in the table this quickgrading element relates to
170 * @param stdClass $grade The grade
171 * @return boolean - true if the grade changes were saved correctly
173 public function save_quickgrading_changes($userid, $grade) {
175 $feedbackcomment = $this->get_feedback_comments($grade->id);
176 if ($feedbackcomment) {
177 $feedbackcomment->commenttext = optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT);
178 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
180 $feedbackcomment = new stdClass();
181 $feedbackcomment->commenttext = optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT);
182 $feedbackcomment->commentformat = FORMAT_HTML;
183 $feedbackcomment->grade = $grade->id;
184 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
185 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
190 * Get form elements for the grading page
192 * @param stdClass|null $grade
193 * @param MoodleQuickForm $mform
194 * @param stdClass $data
195 * @return bool true if elements were added to the form
197 public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
199 $feedbackcomments = $this->get_feedback_comments($grade->id);
200 if ($feedbackcomments) {
201 $data->assignfeedbackcomments_editor['text'] = $feedbackcomments->commenttext;
202 $data->assignfeedbackcomments_editor['format'] = $feedbackcomments->commentformat;
206 $mform->addElement('editor', 'assignfeedbackcomments_editor', $this->get_name(), null, null);
212 * Saving the comment content into database.
214 * @param stdClass $grade
215 * @param stdClass $data
218 public function save(stdClass $grade, stdClass $data) {
220 $feedbackcomment = $this->get_feedback_comments($grade->id);
221 if ($feedbackcomment) {
222 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
223 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
224 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
226 $feedbackcomment = new stdClass();
227 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
228 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
229 $feedbackcomment->grade = $grade->id;
230 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
231 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
236 * Display the comment in the feedback table.
238 * @param stdClass $grade
239 * @param bool $showviewlink Set to true to show a link to view the full feedback
242 public function view_summary(stdClass $grade, & $showviewlink) {
243 $feedbackcomments = $this->get_feedback_comments($grade->id);
244 if ($feedbackcomments) {
245 $text = format_text($feedbackcomments->commenttext,
246 $feedbackcomments->commentformat,
247 array('context' => $this->assignment->get_context()));
248 $short = shorten_text($text, 140);
250 // Show the view all link if the text has been shortened.
251 $showviewlink = $short != $text;
258 * Display the comment in the feedback table.
260 * @param stdClass $grade
263 public function view(stdClass $grade) {
264 $feedbackcomments = $this->get_feedback_comments($grade->id);
265 if ($feedbackcomments) {
266 return format_text($feedbackcomments->commenttext,
267 $feedbackcomments->commentformat,
268 array('context' => $this->assignment->get_context()));
274 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
277 * @param string $type old assignment subtype
278 * @param int $version old assignment version
279 * @return bool True if upgrade is possible
281 public function can_upgrade($type, $version) {
283 if (($type == 'upload' || $type == 'uploadsingle' ||
284 $type == 'online' || $type == 'offline') && $version >= 2011112900) {
291 * Upgrade the settings from the old assignment to the new plugin based one
293 * @param context $oldcontext - the context for the old assignment
294 * @param stdClass $oldassignment - the data for the old assignment
295 * @param string $log - can be appended to by the upgrade
296 * @return bool was it a success? (false will trigger a rollback)
298 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
303 * Upgrade the feedback from the old assignment to the new one
305 * @param context $oldcontext - the database for the old assignment context
306 * @param stdClass $oldassignment The data record for the old assignment
307 * @param stdClass $oldsubmission The data record for the old submission
308 * @param stdClass $grade The data record for the new grade
309 * @param string $log Record upgrade messages in the log
310 * @return bool true or false - false will trigger a rollback
312 public function upgrade(context $oldcontext,
313 stdClass $oldassignment,
314 stdClass $oldsubmission,
319 $feedbackcomments = new stdClass();
320 $feedbackcomments->commenttext = $oldsubmission->submissioncomment;
321 $feedbackcomments->commentformat = FORMAT_HTML;
323 $feedbackcomments->grade = $grade->id;
324 $feedbackcomments->assignment = $this->assignment->get_instance()->id;
325 if (!$DB->insert_record('assignfeedback_comments', $feedbackcomments) > 0) {
326 $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid);
334 * If this plugin adds to the gradebook comments field, it must specify the format of the text
337 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
340 * @param stdClass $grade The grade
343 public function format_for_gradebook(stdClass $grade) {
344 $feedbackcomments = $this->get_feedback_comments($grade->id);
345 if ($feedbackcomments) {
346 return $feedbackcomments->commentformat;
348 return FORMAT_MOODLE;
352 * If this plugin adds to the gradebook comments field, it must format the text
355 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
358 * @param stdClass $grade The grade
361 public function text_for_gradebook(stdClass $grade) {
362 $feedbackcomments = $this->get_feedback_comments($grade->id);
363 if ($feedbackcomments) {
364 return $feedbackcomments->commenttext;
370 * The assignment has been deleted - cleanup
374 public function delete_instance() {
376 // Will throw exception on failure.
377 $DB->delete_records('assignfeedback_comments',
378 array('assignment'=>$this->assignment->get_instance()->id));
383 * Returns true if there are no feedback comments for the given grade.
385 * @param stdClass $grade
388 public function is_empty(stdClass $grade) {
389 return $this->view($grade) == '';
393 * Return a description of external params suitable for uploading an feedback comment from a webservice.
395 * @return external_description|null
397 public function get_external_parameters() {
398 $editorparams = array('text' => new external_value(PARAM_TEXT, 'The text for this feedback.'),
399 'format' => new external_value(PARAM_INT, 'The format for this feedback'));
400 $editorstructure = new external_single_structure($editorparams);
401 return array('assignfeedbackcomments_editor' => $editorstructure);