MDL-36104 mod_assign: added hidden labels to the feedback input fields
[moodle.git] / mod / assign / feedback / comments / locallib.php
CommitLineData
bbd0e548
DW
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/>.
16
17/**
18 * This file contains the definition for the library class for comment feedback plugin
19 *
20 *
21 * @package assignfeedback_comments
22 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26 defined('MOODLE_INTERNAL') || die();
27
28/**
29 * library class for comment feedback plugin extending feedback plugin base class
30 *
31 * @package assignfeedback_comments
32 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class assign_feedback_comments extends assign_feedback_plugin {
36
37 /**
38 * Get the name of the online comment feedback plugin
39 * @return string
40 */
41 public function get_name() {
42 return get_string('pluginname', 'assignfeedback_comments');
43 }
44
45 /**
46 * Get the feedback comment from the database
47 *
48 * @param int $gradeid
49 * @return stdClass|false The feedback comments for the given grade if it exists. 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 }
55
2c6a3dbf
DW
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 }
71
72 return html_writer::tag('textarea', $commenttext, array('name'=>'quickgrade_comments_' . $userid,
73 'class'=>'quickgrade'));
74 }
75
76 /**
77 * Has the plugin quickgrading form element been modified in the current form submission?
78 *
79 * @param int $userid The user id in the table this quickgrading element relates to
80 * @param stdClass $grade The grade
81 * @return boolean - true if the quickgrading form element has been modified
82 */
83 public function is_quickgrading_modified($userid, $grade) {
84 $commenttext = '';
85 if ($grade) {
86 $feedbackcomments = $this->get_feedback_comments($grade->id);
87 if ($feedbackcomments) {
88 $commenttext = $feedbackcomments->commenttext;
89 }
90 }
91 return optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT) != $commenttext;
92 }
93
94
95 /**
96 * Override to indicate a plugin supports quickgrading
97 *
98 * @return boolean - True if the plugin supports quickgrading
99 */
100 public function supports_quickgrading() {
101 return true;
102 }
103
104 /**
105 * Save quickgrading changes
106 *
107 * @param int $userid The user id in the table this quickgrading element relates to
108 * @param stdClass $grade The grade
109 * @return boolean - true if the grade changes were saved correctly
110 */
111 public function save_quickgrading_changes($userid, $grade) {
112 global $DB;
113 $feedbackcomment = $this->get_feedback_comments($grade->id);
114 if ($feedbackcomment) {
115 $feedbackcomment->commenttext = optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT);
116 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
117 } else {
118 $feedbackcomment = new stdClass();
119 $feedbackcomment->commenttext = optional_param('quickgrade_comments_' . $userid, '', PARAM_TEXT);
120 $feedbackcomment->commentformat = FORMAT_HTML;
121 $feedbackcomment->grade = $grade->id;
122 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
123 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
124 }
125 }
126
bbd0e548
DW
127 /**
128 * Get form elements for the grading page
129 *
130 * @param stdClass|null $grade
131 * @param MoodleQuickForm $mform
132 * @param stdClass $data
133 * @return bool true if elements were added to the form
134 */
135 public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
136 if ($grade) {
137 $feedbackcomments = $this->get_feedback_comments($grade->id);
138 if ($feedbackcomments) {
9f913164
DW
139 $data->assignfeedbackcomments_editor['text'] = $feedbackcomments->commenttext;
140 $data->assignfeedbackcomments_editor['format'] = $feedbackcomments->commentformat;
bbd0e548
DW
141 }
142 }
143
55ab55ba
MN
144 $mform->addElement('editor', 'assignfeedbackcomments_editor', html_writer::tag('span', $this->get_name(),
145 array('class' => 'accesshide')), null, null);
146
bbd0e548
DW
147 return true;
148 }
149
150 /**
151 * Saving the comment content into dtabase
152 *
153 * @param stdClass $grade
154 * @param stdClass $data
155 * @return bool
156 */
157 public function save(stdClass $grade, stdClass $data) {
158 global $DB;
159 $feedbackcomment = $this->get_feedback_comments($grade->id);
160 if ($feedbackcomment) {
161 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
162 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
163 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
164 } else {
165 $feedbackcomment = new stdClass();
166 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
167 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
168 $feedbackcomment->grade = $grade->id;
169 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
170 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
171 }
172 }
173
174 /**
175 * display the comment in the feedback table
176 *
177 * @param stdClass $grade
178 * @param bool $showviewlink Set to true to show a link to view the full feedback
179 * @return string
180 */
7ee1548a 181 public function view_summary(stdClass $grade, & $showviewlink) {
bbd0e548
DW
182 $feedbackcomments = $this->get_feedback_comments($grade->id);
183 if ($feedbackcomments) {
184 $text = format_text($feedbackcomments->commenttext, $feedbackcomments->commentformat, array('context' => $this->assignment->get_context()));
185 $short = shorten_text($text, 140);
186
187 // show the view all link if the text has been shortened
188 $showviewlink = $short != $text;
189 return $short;
190 }
191 return '';
192 }
193
194 /**
195 * display the comment in the feedback table
196 *
197 * @param stdClass $grade
198 * @return string
199 */
200 public function view(stdClass $grade) {
201 $feedbackcomments = $this->get_feedback_comments($grade->id);
202 if ($feedbackcomments) {
203 return format_text($feedbackcomments->commenttext, $feedbackcomments->commentformat, array('context' => $this->assignment->get_context()));
204 }
205 return '';
206 }
9682626e 207
bbd0e548
DW
208 /**
209 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
210 * and version.
211 *
212 * @param string $type old assignment subtype
213 * @param int $version old assignment version
214 * @return bool True if upgrade is possible
215 */
216 public function can_upgrade($type, $version) {
217
9bdc95b7
DW
218 if (($type == 'upload' || $type == 'uploadsingle' ||
219 $type == 'online' || $type == 'offline') && $version >= 2011112900) {
bbd0e548
DW
220 return true;
221 }
222 return false;
223 }
1ca059ee 224
bbd0e548
DW
225 /**
226 * Upgrade the settings from the old assignment to the new plugin based one
227 *
228 * @param context $oldcontext - the context for the old assignment
229 * @param stdClass $oldassignment - the data for the old assignment
230 * @param string $log - can be appended to by the upgrade
231 * @return bool was it a success? (false will trigger a rollback)
232 */
233 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
234 // first upgrade settings (nothing to do)
235 return true;
236 }
237
238 /**
239 * Upgrade the feedback from the old assignment to the new one
240 *
241 * @param context $oldcontext - the database for the old assignment context
242 * @param stdClass $oldassignment The data record for the old assignment
243 * @param stdClass $oldsubmission The data record for the old submission
244 * @param stdClass $grade The data record for the new grade
245 * @param string $log Record upgrade messages in the log
246 * @return bool true or false - false will trigger a rollback
247 */
248 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, & $log) {
249 global $DB;
250
251 $feedbackcomments = new stdClass();
252 $feedbackcomments->commenttext = $oldsubmission->submissioncomment;
253 $feedbackcomments->commentformat = FORMAT_HTML;
254
255 $feedbackcomments->grade = $grade->id;
256 $feedbackcomments->assignment = $this->assignment->get_instance()->id;
257 if (!$DB->insert_record('assignfeedback_comments', $feedbackcomments) > 0) {
258 $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid);
259 return false;
260 }
261
262 return true;
263 }
264
265 /**
266 * If this plugin adds to the gradebook comments field, it must specify the format of the text
267 * of the comment
268 *
269 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
270 * settings page.
271 *
272 * @param stdClass $grade The grade
273 * @return int
274 */
275 public function format_for_gradebook(stdClass $grade) {
276 $feedbackcomments = $this->get_feedback_comments($grade->id);
277 if ($feedbackcomments) {
278 return $feedbackcomments->commentformat;
279 }
280 return FORMAT_MOODLE;
281 }
282
283 /**
284 * If this plugin adds to the gradebook comments field, it must format the text
285 * of the comment
286 *
287 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
288 * settings page.
289 *
290 * @param stdClass $grade The grade
291 * @return string
292 */
293 public function text_for_gradebook(stdClass $grade) {
294 $feedbackcomments = $this->get_feedback_comments($grade->id);
295 if ($feedbackcomments) {
296 return $feedbackcomments->commenttext;
297 }
298 return '';
299 }
300
301 /**
302 * The assignment has been deleted - cleanup
303 *
304 * @return bool
305 */
306 public function delete_instance() {
307 global $DB;
308 // will throw exception on failure
309 $DB->delete_records('assignfeedback_comments', array('assignment'=>$this->assignment->get_instance()->id));
310 return true;
311 }
312
313 /**
314 * Returns true if there are no feedback comments for the given grade
315 *
316 * @param stdClass $grade
317 * @return bool
318 */
319 public function is_empty(stdClass $grade) {
320 return $this->view($grade) == '';
321 }
322}