MDL-36104 mod_assign: added hidden labels to the feedback input fields
[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
5f845b3e 81 * @param int $userid The userid we are currently grading
bbd0e548
DW
82 * @return bool true if elements were added to the form
83 */
5f845b3e 84 public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
bbd0e548
DW
85
86 $fileoptions = $this->get_file_options();
87 $gradeid = $grade ? $grade->id : 0;
5f845b3e 88 $elementname = 'files_' . $userid;
bbd0e548 89
5f845b3e
DW
90 $data = file_prepare_standard_filemanager($data,
91 $elementname,
92 $fileoptions,
93 $this->assignment->get_context(),
94 'assignfeedback_file',
95 ASSIGNFEEDBACK_FILE_FILEAREA,
96 $gradeid);
55ab55ba
MN
97 $mform->addElement('filemanager', $elementname . '_filemanager', html_writer::tag('span', $this->get_name(),
98 array('class' => 'accesshide')), null, $fileoptions);
bbd0e548
DW
99
100 return true;
101 }
102
103 /**
104 * Count the number of files
105 *
106 * @param int $gradeid
107 * @param string $area
108 * @return int
109 */
110 private function count_files($gradeid, $area) {
111 global $USER;
112
113 $fs = get_file_storage();
114 $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignfeedback_file', $area, $gradeid, "id", false);
115
116 return count($files);
117 }
118
119 /**
120 * Save the feedback files
121 *
122 * @param stdClass $grade
123 * @param stdClass $data
124 * @return bool
125 */
126 public function save(stdClass $grade, stdClass $data) {
bbd0e548
DW
127 global $DB;
128
129 $fileoptions = $this->get_file_options();
130
5f845b3e
DW
131 $userid = $grade->userid;
132 $elementname = 'files_' . $userid;
bbd0e548 133
5f845b3e
DW
134 $data = file_postupdate_standard_filemanager($data,
135 $elementname,
136 $fileoptions,
137 $this->assignment->get_context(),
138 'assignfeedback_file',
139 ASSIGNFEEDBACK_FILE_FILEAREA,
140 $grade->id);
bbd0e548
DW
141
142 $filefeedback = $this->get_file_feedback($grade->id);
143 if ($filefeedback) {
8ab7bb9f 144 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
bbd0e548
DW
145 return $DB->update_record('assignfeedback_file', $filefeedback);
146 } else {
147 $filefeedback = new stdClass();
8ab7bb9f 148 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
bbd0e548
DW
149 $filefeedback->grade = $grade->id;
150 $filefeedback->assignment = $this->assignment->get_instance()->id;
151 return $DB->insert_record('assignfeedback_file', $filefeedback) > 0;
152 }
153 }
154
155 /**
156 * Display the list of files in the feedback status table
157 *
158 * @param stdClass $grade
159 * @param bool $showviewlink - Set to true to show a link to see the full list of files
160 * @return string
161 */
7ee1548a 162 public function view_summary(stdClass $grade, & $showviewlink) {
8ab7bb9f 163 $count = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
bbd0e548 164 // show a view all link if the number of files is over this limit
8ab7bb9f 165 $showviewlink = $count > ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES;
bbd0e548 166
8ab7bb9f
DW
167 if ($count <= ASSIGNFEEDBACK_FILE_MAXSUMMARYFILES) {
168 return $this->assignment->render_area_files('assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id);
bbd0e548
DW
169 } else {
170 return get_string('countfiles', 'assignfeedback_file', $count);
171 }
172 }
173
174 /**
175 * Display the list of files in the feedback status table
176 * @param stdClass $grade
177 * @return string
178 */
179 public function view(stdClass $grade) {
8ab7bb9f 180 return $this->assignment->render_area_files('assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id);
bbd0e548
DW
181 }
182
183 /**
184 * The assignment has been deleted - cleanup
185 *
186 * @return bool
187 */
188 public function delete_instance() {
189 global $DB;
190 // will throw exception on failure
191 $DB->delete_records('assignfeedback_file', array('assignment'=>$this->assignment->get_instance()->id));
192
193 return true;
194 }
195
196 /**
197 * Return true if there are no feedback files
198 * @param stdClass $grade
199 */
200 public function is_empty(stdClass $grade) {
8ab7bb9f 201 return $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA) == 0;
bbd0e548
DW
202 }
203
204 /**
205 * Get file areas returns a list of areas this plugin stores files
206 * @return array - An array of fileareas (keys) and descriptions (values)
207 */
208 public function get_file_areas() {
8ab7bb9f 209 return array(ASSIGNFEEDBACK_FILE_FILEAREA=>$this->get_name());
bbd0e548 210 }
9682626e 211
bbd0e548
DW
212 /**
213 * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
214 * and version.
215 *
216 * @param string $type old assignment subtype
217 * @param int $version old assignment version
218 * @return bool True if upgrade is possible
219 */
220 public function can_upgrade($type, $version) {
221
222 if (($type == 'upload' || $type == 'uploadsingle') && $version >= 2011112900) {
223 return true;
224 }
225 return false;
226 }
9682626e 227
bbd0e548
DW
228 /**
229 * Upgrade the settings from the old assignment to the new plugin based one
230 *
231 * @param context $oldcontext - the context for the old assignment
232 * @param stdClass $oldassignment - the data for the old assignment
233 * @param string $log - can be appended to by the upgrade
234 * @return bool was it a success? (false will trigger a rollback)
235 */
236 public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
237 // first upgrade settings (nothing to do)
238 return true;
239 }
240
241 /**
242 * Upgrade the feedback from the old assignment to the new one
243 *
244 * @param context $oldcontext - the database for the old assignment context
245 * @param stdClass $oldassignment The data record for the old assignment
246 * @param stdClass $oldsubmission The data record for the old submission
247 * @param stdClass $grade The data record for the new grade
248 * @param string $log Record upgrade messages in the log
249 * @return bool true or false - false will trigger a rollback
250 */
251 public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $grade, & $log) {
252 global $DB;
253
254 // now copy the area files
255 $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
256 'mod_assignment',
257 'response',
258 $oldsubmission->id,
259 // New file area
260 $this->assignment->get_context()->id,
261 'assignfeedback_file',
8ab7bb9f 262 ASSIGNFEEDBACK_FILE_FILEAREA,
bbd0e548
DW
263 $grade->id);
264
265 // now count them!
266 $filefeedback = new stdClass();
8ab7bb9f 267 $filefeedback->numfiles = $this->count_files($grade->id, ASSIGNFEEDBACK_FILE_FILEAREA);
bbd0e548
DW
268 $filefeedback->grade = $grade->id;
269 $filefeedback->assignment = $this->assignment->get_instance()->id;
270 if (!$DB->insert_record('assignfeedback_file', $filefeedback) > 0) {
271 $log .= get_string('couldnotconvertgrade', 'mod_assign', $grade->userid);
272 return false;
273 }
274 return true;
275 }
276}