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
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
26 defined('MOODLE_INTERNAL') || die();
29 * library class for comment feedback plugin extending feedback plugin base class
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
35 class assign_feedback_comments extends assign_feedback_plugin {
38 * Get the name of the online comment feedback plugin
41 public function get_name() {
42 return get_string('pluginname', 'assignfeedback_comments');
46 * Get the feedback comment from the database
49 * @return stdClass|false The feedback comments for the given grade if it exists. False if it doesn't.
51 public function get_feedback_comments($gradeid) {
53 return $DB->get_record('assignfeedback_comments', array('grade'=>$gradeid));
57 * Get form elements for the grading page
59 * @param stdClass|null $grade
60 * @param MoodleQuickForm $mform
61 * @param stdClass $data
62 * @return bool true if elements were added to the form
64 public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
66 $feedbackcomments = $this->get_feedback_comments($grade->id);
67 if ($feedbackcomments) {
68 $data->assignfeedbackcomments_editor['text'] = $feedbackcomments->commenttext;
69 $data->assignfeedbackcomments_editor['format'] = $feedbackcomments->commentformat;
73 $mform->addElement('editor', 'assignfeedbackcomments_editor', '', null, null);
78 * Saving the comment content into dtabase
80 * @param stdClass $grade
81 * @param stdClass $data
84 public function save(stdClass $grade, stdClass $data) {
86 $feedbackcomment = $this->get_feedback_comments($grade->id);
87 if ($feedbackcomment) {
88 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
89 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
90 return $DB->update_record('assignfeedback_comments', $feedbackcomment);
92 $feedbackcomment = new stdClass();
93 $feedbackcomment->commenttext = $data->assignfeedbackcomments_editor['text'];
94 $feedbackcomment->commentformat = $data->assignfeedbackcomments_editor['format'];
95 $feedbackcomment->grade = $grade->id;
96 $feedbackcomment->assignment = $this->assignment->get_instance()->id;
97 return $DB->insert_record('assignfeedback_comments', $feedbackcomment) > 0;
102 * display the comment in the feedback table
104 * @param stdClass $grade
105 * @param bool $showviewlink Set to true to show a link to view the full feedback
108 public function view_summary(stdClass $grade, $showviewlink) {
109 $feedbackcomments = $this->get_feedback_comments($grade->id);
110 if ($feedbackcomments) {
111 $text = format_text($feedbackcomments->commenttext, $feedbackcomments->commentformat, array('context' => $this->assignment->get_context()));
112 $short = shorten_text($text, 140);
114 // show the view all link if the text has been shortened
115 $showviewlink = $short != $text;
122 * display the comment in the feedback table
124 * @param stdClass $grade
127 public function view(stdClass $grade) {
128 $feedbackcomments = $this->get_feedback_comments($grade->id);
129 if ($feedbackcomments) {
130 return format_text($feedbackcomments->commenttext, $feedbackcomments->commentformat, array('context' => $this->assignment->get_context()));
136 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
139 * @param string $type old assignment subtype
140 * @param int $version old assignment version
141 * @return bool True if upgrade is possible
143 public function can_upgrade($type, $version) {
145 if (($type == 'upload' || $type == 'uploadsingle') && $version >= 2011112900) {
152 * Upgrade the settings from the old assignment to the new plugin based one
154 * @param context $oldcontext - the context for the old assignment
155 * @param stdClass $oldassignment - the data for the old assignment
156 * @param string $log - can be appended to by the upgrade
157 * @return bool was it a success? (false will trigger a rollback)
159 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
160 // first upgrade settings (nothing to do)
165 * Upgrade the feedback from the old assignment to the new one
167 * @param context $oldcontext - the database for the old assignment context
168 * @param stdClass $oldassignment The data record for the old assignment
169 * @param stdClass $oldsubmission The data record for the old submission
170 * @param stdClass $grade The data record for the new grade
171 * @param string $log Record upgrade messages in the log
172 * @return bool true or false - false will trigger a rollback
174 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, & $log) {
177 $feedbackcomments = new stdClass();
178 $feedbackcomments->commenttext = $oldsubmission->submissioncomment;
179 $feedbackcomments->commentformat = FORMAT_HTML;
181 $feedbackcomments->grade = $grade->id;
182 $feedbackcomments->assignment = $this->assignment->get_instance()->id;
183 if (!$DB->insert_record('assignfeedback_comments', $feedbackcomments) > 0) {
184 $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid);
192 * If this plugin adds to the gradebook comments field, it must specify the format of the text
195 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
198 * @param stdClass $grade The grade
201 public function format_for_gradebook(stdClass $grade) {
202 $feedbackcomments = $this->get_feedback_comments($grade->id);
203 if ($feedbackcomments) {
204 return $feedbackcomments->commentformat;
206 return FORMAT_MOODLE;
210 * If this plugin adds to the gradebook comments field, it must format the text
213 * Only one feedback plugin can push comments to the gradebook and that is chosen by the assignment
216 * @param stdClass $grade The grade
219 public function text_for_gradebook(stdClass $grade) {
220 $feedbackcomments = $this->get_feedback_comments($grade->id);
221 if ($feedbackcomments) {
222 return $feedbackcomments->commenttext;
228 * The assignment has been deleted - cleanup
232 public function delete_instance() {
234 // will throw exception on failure
235 $DB->delete_records('assignfeedback_comments', array('assignment'=>$this->assignment->get_instance()->id));
240 * Returns true if there are no feedback comments for the given grade
242 * @param stdClass $grade
245 public function is_empty(stdClass $grade) {
246 return $this->view($grade) == '';