Commit | Line | Data |
---|---|---|
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 file feedback plugin | |
19 | * | |
20 | * | |
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 | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | /** | |
28 | * File areas for file feedback assignment | |
29 | */ | |
30 | define('ASSIGN_MAX_FEEDBACK_FILES', 20); | |
31 | define('ASSIGN_FILEAREA_FEEDBACK_FILES', 'feedback_files'); | |
32 | define('ASSIGN_FEEDBACK_FILE_MAX_SUMMARY_FILES', 5); | |
33 | ||
34 | /** | |
35 | * library class for file feedback plugin extending feedback plugin base class | |
36 | * | |
37 | * @package asignfeedback_file | |
38 | * @copyright 2012 NetSpot {@link http://www.netspot.com.au} | |
39 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
40 | */ | |
41 | class assign_feedback_file extends assign_feedback_plugin { | |
42 | ||
43 | /** | |
44 | * Get the name of the file feedback plugin | |
45 | * @return string | |
46 | */ | |
47 | public function get_name() { | |
48 | return get_string('file', 'assignfeedback_file'); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Get file feedback information from the database | |
53 | * | |
54 | * @param int $gradeid | |
55 | * @return mixed | |
56 | */ | |
57 | public function get_file_feedback($gradeid) { | |
58 | global $DB; | |
59 | return $DB->get_record('assignfeedback_file', array('grade'=>$gradeid)); | |
60 | } | |
61 | ||
62 | /** | |
63 | * File format options | |
64 | * @return array | |
65 | */ | |
66 | private function get_file_options() { | |
67 | global $COURSE; | |
68 | ||
69 | $fileoptions = array('subdirs'=>1, | |
70 | 'maxbytes'=>$COURSE->maxbytes, | |
71 | 'accepted_types'=>'*', | |
72 | 'return_types'=>FILE_INTERNAL); | |
73 | return $fileoptions; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Get form elements for grading form | |
78 | * | |
9682626e | 79 | * @param stdClass $grade |
bbd0e548 DW |
80 | * @param MoodleQuickForm $mform |
81 | * @param stdClass $data | |
82 | * @return bool true if elements were added to the form | |
83 | */ | |
84 | public function get_form_elements($grade, MoodleQuickForm $mform, stdClass $data) { | |
85 | ||
86 | $fileoptions = $this->get_file_options(); | |
87 | $gradeid = $grade ? $grade->id : 0; | |
88 | ||
89 | ||
90 | $data = file_prepare_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignfeedback_file', ASSIGN_FILEAREA_FEEDBACK_FILES, $gradeid); | |
91 | ||
92 | $mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions); | |
93 | ||
94 | return true; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Count the number of files | |
99 | * | |
100 | * @param int $gradeid | |
101 | * @param string $area | |
102 | * @return int | |
103 | */ | |
104 | private function count_files($gradeid, $area) { | |
105 | global $USER; | |
106 | ||
107 | $fs = get_file_storage(); | |
108 | $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignfeedback_file', $area, $gradeid, "id", false); | |
109 | ||
110 | return count($files); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Save the feedback files | |
115 | * | |
116 | * @param stdClass $grade | |
117 | * @param stdClass $data | |
118 | * @return bool | |
119 | */ | |
120 | public function save(stdClass $grade, stdClass $data) { | |
121 | ||
122 | global $DB; | |
123 | ||
124 | $fileoptions = $this->get_file_options(); | |
125 | ||
126 | ||
127 | $data = file_postupdate_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignfeedback_file', ASSIGN_FILEAREA_FEEDBACK_FILES, $grade->id); | |
128 | ||
129 | ||
130 | $filefeedback = $this->get_file_feedback($grade->id); | |
131 | if ($filefeedback) { | |
132 | $filefeedback->numfiles = $this->count_files($grade->id, ASSIGN_FILEAREA_FEEDBACK_FILES); | |
133 | return $DB->update_record('assignfeedback_file', $filefeedback); | |
134 | } else { | |
135 | $filefeedback = new stdClass(); | |
136 | $filefeedback->numfiles = $this->count_files($grade->id, ASSIGN_FILEAREA_FEEDBACK_FILES); | |
137 | $filefeedback->grade = $grade->id; | |
138 | $filefeedback->assignment = $this->assignment->get_instance()->id; | |
139 | return $DB->insert_record('assignfeedback_file', $filefeedback) > 0; | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
144 | * Display the list of files in the feedback status table | |
145 | * | |
146 | * @param stdClass $grade | |
147 | * @param bool $showviewlink - Set to true to show a link to see the full list of files | |
148 | * @return string | |
149 | */ | |
150 | public function view_summary(stdClass $grade, $showviewlink) { | |
151 | $count = $this->count_files($grade->id, ASSIGN_FILEAREA_FEEDBACK_FILES); | |
152 | // show a view all link if the number of files is over this limit | |
153 | $showviewlink = $count > ASSIGN_FEEDBACK_FILE_MAX_SUMMARY_FILES; | |
154 | ||
155 | if ($count <= ASSIGN_FEEDBACK_FILE_MAX_SUMMARY_FILES) { | |
156 | return $this->assignment->render_area_files('assignfeedback_file', ASSIGN_FILEAREA_FEEDBACK_FILES, $grade->id); | |
157 | } else { | |
158 | return get_string('countfiles', 'assignfeedback_file', $count); | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * Display the list of files in the feedback status table | |
164 | * @param stdClass $grade | |
165 | * @return string | |
166 | */ | |
167 | public function view(stdClass $grade) { | |
168 | return $this->assignment->render_area_files('assignfeedback_file', ASSIGN_FILEAREA_FEEDBACK_FILES, $grade->id); | |
169 | } | |
170 | ||
171 | /** | |
172 | * The assignment has been deleted - cleanup | |
173 | * | |
174 | * @return bool | |
175 | */ | |
176 | public function delete_instance() { | |
177 | global $DB; | |
178 | // will throw exception on failure | |
179 | $DB->delete_records('assignfeedback_file', array('assignment'=>$this->assignment->get_instance()->id)); | |
180 | ||
181 | return true; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Return true if there are no feedback files | |
186 | * @param stdClass $grade | |
187 | */ | |
188 | public function is_empty(stdClass $grade) { | |
189 | return $this->count_files($grade->id, ASSIGN_FILEAREA_FEEDBACK_FILES) == 0; | |
190 | } | |
191 | ||
192 | /** | |
193 | * Get file areas returns a list of areas this plugin stores files | |
194 | * @return array - An array of fileareas (keys) and descriptions (values) | |
195 | */ | |
196 | public function get_file_areas() { | |
197 | return array(ASSIGN_FILEAREA_FEEDBACK_FILES=>$this->get_name()); | |
198 | } | |
9682626e | 199 | |
bbd0e548 DW |
200 | /** |
201 | * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type | |
202 | * and version. | |
203 | * | |
204 | * @param string $type old assignment subtype | |
205 | * @param int $version old assignment version | |
206 | * @return bool True if upgrade is possible | |
207 | */ | |
208 | public function can_upgrade($type, $version) { | |
209 | ||
210 | if (($type == 'upload' || $type == 'uploadsingle') && $version >= 2011112900) { | |
211 | return true; | |
212 | } | |
213 | return false; | |
214 | } | |
9682626e | 215 | |
bbd0e548 DW |
216 | /** |
217 | * Upgrade the settings from the old assignment to the new plugin based one | |
218 | * | |
219 | * @param context $oldcontext - the context for the old assignment | |
220 | * @param stdClass $oldassignment - the data for the old assignment | |
221 | * @param string $log - can be appended to by the upgrade | |
222 | * @return bool was it a success? (false will trigger a rollback) | |
223 | */ | |
224 | public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) { | |
225 | // first upgrade settings (nothing to do) | |
226 | return true; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Upgrade the feedback from the old assignment to the new one | |
231 | * | |
232 | * @param context $oldcontext - the database for the old assignment context | |
233 | * @param stdClass $oldassignment The data record for the old assignment | |
234 | * @param stdClass $oldsubmission The data record for the old submission | |
235 | * @param stdClass $grade The data record for the new grade | |
236 | * @param string $log Record upgrade messages in the log | |
237 | * @return bool true or false - false will trigger a rollback | |
238 | */ | |
239 | public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, & $log) { | |
240 | global $DB; | |
241 | ||
242 | // now copy the area files | |
243 | $this->assignment->copy_area_files_for_upgrade($oldcontext->id, | |
244 | 'mod_assignment', | |
245 | 'response', | |
246 | $oldsubmission->id, | |
247 | // New file area | |
248 | $this->assignment->get_context()->id, | |
249 | 'assignfeedback_file', | |
250 | ASSIGN_FILEAREA_FEEDBACK_FILES, | |
251 | $grade->id); | |
252 | ||
253 | // now count them! | |
254 | $filefeedback = new stdClass(); | |
255 | $filefeedback->numfiles = $this->count_files($grade->id, ASSIGN_FILEAREA_FEEDBACK_FILES); | |
256 | $filefeedback->grade = $grade->id; | |
257 | $filefeedback->assignment = $this->assignment->get_instance()->id; | |
258 | if (!$DB->insert_record('assignfeedback_file', $filefeedback) > 0) { | |
259 | $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid); | |
260 | return false; | |
261 | } | |
262 | return true; | |
263 | } | |
264 | } |