MDL-42919 Assign: Remove no-label form hacks
[moodle.git] / mod / assign / feedback / comments / locallib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * This file contains the definition for the library class for comment feedback plugin
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Library class for comment feedback plugin extending feedback plugin base class.
29  *
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
33  */
34 class assign_feedback_comments extends assign_feedback_plugin {
36     /**
37      * Get the name of the online comment feedback plugin.
38      * @return string
39      */
40     public function get_name() {
41         return get_string('pluginname', 'assignfeedback_comments');
42     }
44     /**
45      * Get the feedback comment from the database.
46      *
47      * @param int $gradeid
48      * @return stdClass|false The feedback comments for the given grade if it exists.
49      *                        False if it doesn't.
50      */
51     public function get_feedback_comments($gradeid) {
52         global $DB;
53         return $DB->get_record('assignfeedback_comments', array('grade'=>$gradeid));
54     }
56     /**
57      * Get quickgrading form elements as html.
58      *
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
62      */
63     public function get_quickgrading_html($userid, $grade) {
64         $commenttext = '';
65         if ($grade) {
66             $feedbackcomments = $this->get_feedback_comments($grade->id);
67             if ($feedbackcomments) {
68                 $commenttext = $feedbackcomments->commenttext;
69             }
70         }
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);
80     }
82     /**
83      * Has the plugin quickgrading form element been modified in the current form submission?
84      *
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
88      */
89     public function is_quickgrading_modified($userid, $grade) {
90         $commenttext = '';
91         if ($grade) {
92             $feedbackcomments = $this->get_feedback_comments($grade->id);
93             if ($feedbackcomments) {
94                 $commenttext = $feedbackcomments->commenttext;
95             }
96         }
97         return optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT) != $commenttext;
98     }
101     /**
102      * Override to indicate a plugin supports quickgrading.
103      *
104      * @return boolean - True if the plugin supports quickgrading
105      */
106     public function supports_quickgrading() {
107         return true;
108     }
110     /**
111      * Return a list of the text fields that can be imported/exported by this plugin.
112      *
113      * @return array An array of field names and descriptions. (name=>description, ...)
114      */
115     public function get_editor_fields() {
116         return array('comments' => get_string('pluginname', 'assignfeedback_comments'));
117     }
119     /**
120      * Get the saved text content from the editor.
121      *
122      * @param string $name
123      * @param int $gradeid
124      * @return string
125      */
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;
131             }
132         }
134         return '';
135     }
137     /**
138      * Get the saved text content from the editor.
139      *
140      * @param string $name
141      * @param string $value
142      * @param int $gradeid
143      * @return string
144      */
145     public function set_editor_text($name, $value, $gradeid) {
146         global $DB;
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);
153             } else {
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;
160             }
161         }
163         return false;
164     }
166     /**
167      * Save quickgrading changes.
168      *
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
172      */
173     public function save_quickgrading_changes($userid, $grade) {
174         global $DB;
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);
179         } else {
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;
186         }
187     }
189     /**
190      * Get form elements for the grading page
191      *
192      * @param stdClass|null $grade
193      * @param MoodleQuickForm $mform
194      * @param stdClass $data
195      * @return bool true if elements were added to the form
196      */
197     public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
198         if ($grade) {
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;
203             }
204         }
206         $mform->addElement('editor', 'assignfeedbackcomments_editor', $this->get_name(), null, null);
208         return true;
209     }
211     /**
212      * Saving the comment content into database.
213      *
214      * @param stdClass $grade
215      * @param stdClass $data
216      * @return bool
217      */
218     public function save(stdClass $grade, stdClass $data) {
219         global $DB;
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);
225         } else {
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;
232         }
233     }
235     /**
236      * Display the comment in the feedback table.
237      *
238      * @param stdClass $grade
239      * @param bool $showviewlink Set to true to show a link to view the full feedback
240      * @return string
241      */
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;
252             return $short;
253         }
254         return '';
255     }
257     /**
258      * Display the comment in the feedback table.
259      *
260      * @param stdClass $grade
261      * @return string
262      */
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()));
269         }
270         return '';
271     }
273     /**
274      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
275      * and version.
276      *
277      * @param string $type old assignment subtype
278      * @param int $version old assignment version
279      * @return bool True if upgrade is possible
280      */
281     public function can_upgrade($type, $version) {
283         if (($type == 'upload' || $type == 'uploadsingle' ||
284              $type == 'online' || $type == 'offline') && $version >= 2011112900) {
285             return true;
286         }
287         return false;
288     }
290     /**
291      * Upgrade the settings from the old assignment to the new plugin based one
292      *
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)
297      */
298     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
299         return true;
300     }
302     /**
303      * Upgrade the feedback from the old assignment to the new one
304      *
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
311      */
312     public function upgrade(context $oldcontext,
313                             stdClass $oldassignment,
314                             stdClass $oldsubmission,
315                             stdClass $grade,
316                             & $log) {
317         global $DB;
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);
327             return false;
328         }
330         return true;
331     }
333     /**
334      * If this plugin adds to the gradebook comments field, it must specify the format of the text
335      * of the comment
336      *
337      * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
338      * settings page.
339      *
340      * @param stdClass $grade The grade
341      * @return int
342      */
343     public function format_for_gradebook(stdClass $grade) {
344         $feedbackcomments = $this->get_feedback_comments($grade->id);
345         if ($feedbackcomments) {
346             return $feedbackcomments->commentformat;
347         }
348         return FORMAT_MOODLE;
349     }
351     /**
352      * If this plugin adds to the gradebook comments field, it must format the text
353      * of the comment
354      *
355      * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
356      * settings page.
357      *
358      * @param stdClass $grade The grade
359      * @return string
360      */
361     public function text_for_gradebook(stdClass $grade) {
362         $feedbackcomments = $this->get_feedback_comments($grade->id);
363         if ($feedbackcomments) {
364             return $feedbackcomments->commenttext;
365         }
366         return '';
367     }
369     /**
370      * The assignment has been deleted - cleanup
371      *
372      * @return bool
373      */
374     public function delete_instance() {
375         global $DB;
376         // Will throw exception on failure.
377         $DB->delete_records('assignfeedback_comments',
378                             array('assignment'=>$this->assignment->get_instance()->id));
379         return true;
380     }
382     /**
383      * Returns true if there are no feedback comments for the given grade.
384      *
385      * @param stdClass $grade
386      * @return bool
387      */
388     public function is_empty(stdClass $grade) {
389         return $this->view($grade) == '';
390     }
392     /**
393      * Return a description of external params suitable for uploading an feedback comment from a webservice.
394      *
395      * @return external_description|null
396      */
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);
402     }