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