forked from moodle/moodle
/
locallib.php
406 lines (337 loc) · 15.1 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the definition for the library class for file submission plugin
*
* This class provides all the functionality for the new assign module.
*
* @package assignsubmission_file
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** Include eventslib.php */
require_once($CFG->libdir.'/eventslib.php');
defined('MOODLE_INTERNAL') || die();
/**
* File areas for file submission assignment
*/
define('ASSIGN_MAX_SUBMISSION_FILES', 20);
define('ASSIGN_SUBMISSION_FILE_MAX_SUMMARY_FILES', 5);
define('ASSIGN_FILEAREA_SUBMISSION_FILES', 'submission_files');
/**
* library class for file submission plugin extending submission plugin base class
*
* @package assignsubmission_file
* @copyright 2012 NetSpot {@link http://www.netspot.com.au}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class assign_submission_file extends assign_submission_plugin {
/**
* Get the name of the file submission plugin
* @return string
*/
public function get_name() {
return get_string('file', 'assignsubmission_file');
}
/**
* Get file submission information from the database
*
* @param int $submissionid
* @return mixed
*/
private function get_file_submission($submissionid) {
global $DB;
return $DB->get_record('assignsubmission_file', array('submission'=>$submissionid));
}
/**
* Get the default setting for file submission plugin
*
* @param MoodleQuickForm $mform The form to add elements to
* @return void
*/
public function get_settings(MoodleQuickForm $mform) {
global $CFG, $COURSE;
$defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
$defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
$settings = array();
$options = array();
for($i = 1; $i <= ASSIGN_MAX_SUBMISSION_FILES; $i++) {
$options[$i] = $i;
}
$mform->addElement('select', 'assignsubmission_file_maxfiles', get_string('maxfilessubmission', 'assignsubmission_file'), $options);
$mform->addHelpButton('assignsubmission_file_maxfiles', 'maxfilessubmission', 'assignsubmission_file');
$mform->setDefault('assignsubmission_file_maxfiles', $defaultmaxfilesubmissions);
$mform->disabledIf('assignsubmission_file_maxfiles', 'assignsubmission_file_enabled', 'eq', 0);
$choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, $CFG->assignsubmission_file_maxbytes);
if ($COURSE->maxbytes == 0) {
$choices[0] = get_string('siteuploadlimit', 'assignsubmission_file');
} else {
$choices[0] = get_string('courseuploadlimit') . ' (' . display_size($COURSE->maxbytes) . ')';
}
$settings[] = array('type' => 'select',
'name' => 'maxsubmissionsizebytes',
'description' => get_string('maximumsubmissionsize', 'assignsubmission_file'),
'options'=> $choices,
'default'=> $defaultmaxsubmissionsizebytes);
$mform->addElement('select', 'assignsubmission_file_maxsizebytes', get_string('maximumsubmissionsize', 'assignsubmission_file'), $choices);
$mform->addHelpButton('assignsubmission_file_maxsizebytes', 'maximumsubmissionsize', 'assignsubmission_file');
$mform->setDefault('assignsubmission_file_maxsizebytes', $defaultmaxsubmissionsizebytes);
$mform->disabledIf('assignsubmission_file_maxsizebytes', 'assignsubmission_file_enabled', 'eq', 0);
}
/**
* Save the settings for file submission plugin
*
* @param stdClass $data
* @return bool
*/
public function save_settings(stdClass $data) {
$this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
$this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
return true;
}
/**
* File format options
*
* @return array
*/
private function get_file_options() {
$fileoptions = array('subdirs'=>1,
'maxbytes'=>$this->get_config('maxsubmissionsizebytes'),
'maxfiles'=>$this->get_config('maxfilesubmissions'),
'accepted_types'=>'*',
'return_types'=>FILE_INTERNAL);
return $fileoptions;
}
/**
* Add elements to submission form
*
* @param mixed $submission stdClass|null
* @param MoodleQuickForm $mform
* @param stdClass $data
* @return bool
*/
public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
if ($this->get_config('maxfilesubmissions') <= 0) {
return false;
}
$fileoptions = $this->get_file_options();
$submissionid = $submission ? $submission->id : 0;
$data = file_prepare_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submissionid);
$mform->addElement('filemanager', 'files_filemanager', '', null, $fileoptions);
return true;
}
/**
* Count the number of files
*
* @param int $submissionid
* @param string $area
* @return int
*/
private function count_files($submissionid, $area) {
$fs = get_file_storage();
$files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', $area, $submissionid, "id", false);
return count($files);
}
/**
* Save the files and trigger plagiarism plugin, if enabled, to scan the uploaded files via events trigger
*
* @param stdClass $submission
* @param stdClass $data
* @return bool
*/
public function save(stdClass $submission, stdClass $data) {
global $USER, $DB;
$fileoptions = $this->get_file_options();
$data = file_postupdate_standard_filemanager($data, 'files', $fileoptions, $this->assignment->get_context(), 'assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id);
$filesubmission = $this->get_file_submission($submission->id);
//plagiarism code event trigger when files are uploaded
$fs = get_file_storage();
$files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id, "id", false);
$count = $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES);
// send files to event system
// Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
$eventdata = new stdClass();
$eventdata->modulename = 'assign';
$eventdata->cmid = $this->assignment->get_course_module()->id;
$eventdata->itemid = $submission->id;
$eventdata->courseid = $this->assignment->get_course()->id;
$eventdata->userid = $USER->id;
if ($count > 1) {
$eventdata->files = $files; // This is depreceated - please use pathnamehashes instead!
}
$eventdata->file = $files; // This is depreceated - please use pathnamehashes instead!
$eventdata->pathnamehashes = array_keys($files);
events_trigger('assessable_file_uploaded', $eventdata);
if ($filesubmission) {
$filesubmission->numfiles = $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES);
return $DB->update_record('assignsubmission_file', $filesubmission);
} else {
$filesubmission = new stdClass();
$filesubmission->numfiles = $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES);
$filesubmission->submission = $submission->id;
$filesubmission->assignment = $this->assignment->get_instance()->id;
return $DB->insert_record('assignsubmission_file', $filesubmission) > 0;
}
}
/**
* Produce a list of files suitable for export that represent this feedback or submission
*
* @param stdClass $submission The submission
* @return array - return an array of files indexed by filename
*/
public function get_files(stdClass $submission) {
$result = array();
$fs = get_file_storage();
$files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id, "timemodified", false);
foreach ($files as $file) {
$result[$file->get_filename()] = $file;
}
return $result;
}
/**
* Display the list of files in the submission status table
*
* @param stdClass $submission
* @param bool $showviewlink Set this to true if the list of files is long
* @return string
*/
public function view_summary(stdClass $submission, $showviewlink) {
$count = $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES);
// show we show a link to view all files for this plugin?
$showviewlink = $count > ASSIGN_SUBMISSION_FILE_MAX_SUMMARY_FILES;
if ($count <= ASSIGN_SUBMISSION_FILE_MAX_SUMMARY_FILES) {
return $this->assignment->render_area_files('assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id);
} else {
return get_string('countfiles', 'assignsubmission_file', $count);
}
}
/**
* No full submission view - the summary contains the list of files and that is the whole submission
*
* @param stdClass $submission
* @return string
*/
public function view(stdClass $submission) {
return $this->assignment->render_area_files('assignsubmission_file', ASSIGN_FILEAREA_SUBMISSION_FILES, $submission->id);
}
/**
* Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type
* and version.
*
* @param string $type
* @param int $version
* @return bool True if upgrade is possible
*/
public function can_upgrade($type, $version) {
$uploadsingletype ='uploadsingle';
$uploadtype ='upload';
if (($type == $uploadsingletype || $type == $uploadtype) && $version >= 2011112900) {
return true;
}
return false;
}
/**
* Upgrade the settings from the old assignment
* to the new plugin based one
*
* @param context $oldcontext - the old assignment context
* @param stdClass $oldassignment - the old assignment data record
* @param string $log record log events here
* @return bool Was it a success? (false will trigger rollback)
*/
public function upgrade_settings(context $oldcontext,stdClass $oldassignment, & $log) {
if ($oldassignment->assignmenttype == 'uploadsingle') {
$this->set_config('maxfilesubmissions', 1);
$this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
return true;
}else {
$this->set_config('maxfilesubmissions', $oldassignment->var1);
$this->set_config('maxsubmissionsizebytes', $oldassignment->maxbytes);
return true;
}
}
/**
* Upgrade the submission from the old assignment to the new one
*
* @param context $oldcontext The context of the old assignment
* @param stdClass $oldassignment The data record for the old oldassignment
* @param stdClass $oldsubmission The data record for the old submission
* @param stdClass $submission The data record for the new submission
* @param string $log Record upgrade messages in the log
* @return bool true or false - false will trigger a rollback
*/
public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
global $DB;
$filesubmission = new stdClass();
$filesubmission->numfiles = $oldsubmission->numfiles;
$filesubmission->submission = $submission->id;
$filesubmission->assignment = $this->assignment->get_instance()->id;
if (!$DB->insert_record('assignsubmission_file', $filesubmission) > 0) {
$log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
return false;
}
// now copy the area files
$this->assignment->copy_area_files_for_upgrade($oldcontext->id,
'mod_assignment',
'submission',
$oldsubmission->id,
// New file area
$this->assignment->get_context()->id,
'assignsubmission_file',
ASSIGN_FILEAREA_SUBMISSION_FILES,
$submission->id);
return true;
}
/**
* The assignment has been deleted - cleanup
*
* @return bool
*/
public function delete_instance() {
global $DB;
// will throw exception on failure
$DB->delete_records('assignsubmission_file', array('assignment'=>$this->assignment->get_instance()->id));
return true;
}
/**
* Formatting for log info
*
* @param stdClass $submission The submission
*
* @return string
*/
public function format_for_log(stdClass $submission) {
// format the info for each submission plugin add_to_log
$filecount = $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES);
$fileloginfo = '';
$fileloginfo .= ' the number of file(s) : ' . $filecount . " file(s).<br>";
return $fileloginfo;
}
/**
* Return true if there are no submission files
* @param stdClass $submission
*/
public function is_empty(stdClass $submission) {
return $this->count_files($submission->id, ASSIGN_FILEAREA_SUBMISSION_FILES) == 0;
}
/**
* Get file areas returns a list of areas this plugin stores files
* @return array - An array of fileareas (keys) and descriptions (values)
*/
public function get_file_areas() {
return array(ASSIGN_FILEAREA_SUBMISSION_FILES=>$this->get_name());
}
}