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 file feedback plugin
21 * @package assignfeedback_file
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();
28 * File areas for file feedback assignment
30 define('ASSIGNFEEDBACK_FILE_FILEAREA', 'feedback_files');
31 define('ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES', 5);
34 * library class for file feedback plugin extending feedback plugin base class
36 * @package asignfeedback_file
37 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class assign_feedback_file extends assign_feedback_plugin {
43 * Get the name of the file feedback plugin
46 public function get_name() {
47 return get_string('file', 'assignfeedback_file');
51 * Get file feedback information from the database
56 public function get_file_feedback($gradeid) {
58 return $DB->get_record('assignfeedback_file', array('grade'=>$gradeid));
65 private function get_file_options() {
68 $fileoptions = array('subdirs'=>1,
69 'maxbytes'=>$COURSE->maxbytes,
70 'accepted_types'=>'*',
71 'return_types'=>FILE_INTERNAL);
76 * Get form elements for grading form
78 * @param stdClass $grade
79 * @param MoodleQuickForm $mform
80 * @param stdClass $data
81 * @return bool true if elements were added to the form
83 public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) {
85 $fileoptions = $this->get_file_options();
86 $gradeid = $grade ? $grade->id : 0;
89 $data = file_prepare_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $gradeid);
91 $mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions);
97 * Count the number of files
100 * @param string $area
103 private function count_files($gradeid, $area) {
106 $fs = get_file_storage();
107 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignfeedback_file', $area, $gradeid, "id", false);
109 return count($files);
113 * Save the feedback files
115 * @param stdClass $grade
116 * @param stdClass $data
119 public function save(stdClass $grade, stdClass $data) {
123 $fileoptions = $this->get_file_options();
126 $data = file_postupdate_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id);
129 $filefeedback = $this->get_file_feedback($grade->id);
131 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
132 return $DB->update_record('assignfeedback_file', $filefeedback);
134 $filefeedback = new stdClass();
135 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
136 $filefeedback->grade = $grade->id;
137 $filefeedback->assignment = $this->assignment->get_instance()->id;
138 return $DB->insert_record('assignfeedback_file', $filefeedback) > 0;
143 * Display the list of files in the feedback status table
145 * @param stdClass $grade
146 * @param bool $showviewlink - Set to true to show a link to see the full list of files
149 public function view_summary(stdClass $grade, $showviewlink) {
150 $count = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
151 // show a view all link if the number of files is over this limit
152 $showviewlink = $count > ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;
154 if ($count <= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {
155 return $this->assignment->render_area_files('assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id);
157 return get_string('countfiles', 'assignfeedback_file', $count);
162 * Display the list of files in the feedback status table
163 * @param stdClass $grade
166 public function view(stdClass $grade) {
167 return $this->assignment->render_area_files('assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id);
171 * The assignment has been deleted - cleanup
175 public function delete_instance() {
177 // will throw exception on failure
178 $DB->delete_records('assignfeedback_file', array('assignment'=>$this->assignment->get_instance()->id));
184 * Return true if there are no feedback files
185 * @param stdClass $grade
187 public function is_empty(stdClass $grade) {
188 return $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA) == 0;
192 * Get file areas returns a list of areas this plugin stores files
193 * @return array - An array of fileareas (keys) and descriptions (values)
195 public function get_file_areas() {
196 return array(ASSIGNFEEDBACK_FILE_FILEAREA=>$this->get_name());
200 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
203 * @param string $type old assignment subtype
204 * @param int $version old assignment version
205 * @return bool True if upgrade is possible
207 public function can_upgrade($type, $version) {
209 if (($type == 'upload' || $type == 'uploadsingle') && $version >= 2011112900) {
216 * Upgrade the settings from the old assignment to the new plugin based one
218 * @param context $oldcontext - the context for the old assignment
219 * @param stdClass $oldassignment - the data for the old assignment
220 * @param string $log - can be appended to by the upgrade
221 * @return bool was it a success? (false will trigger a rollback)
223 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
224 // first upgrade settings (nothing to do)
229 * Upgrade the feedback from the old assignment to the new one
231 * @param context $oldcontext - the database for the old assignment context
232 * @param stdClass $oldassignment The data record for the old assignment
233 * @param stdClass $oldsubmission The data record for the old submission
234 * @param stdClass $grade The data record for the new grade
235 * @param string $log Record upgrade messages in the log
236 * @return bool true or false - false will trigger a rollback
238 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, & $log) {
241 // now copy the area files
242 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
247 $this->assignment->get_context()->id,
248 'assignfeedback_file',
249 ASSIGNFEEDBACK_FILE_FILEAREA,
253 $filefeedback = new stdClass();
254 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
255 $filefeedback->grade = $grade->id;
256 $filefeedback->assignment = $this->assignment->get_instance()->id;
257 if (!$DB->insert_record('assignfeedback_file', $filefeedback) > 0) {
258 $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid);