MDL-39913 assignsubmission_file: add file types restriction

Testing for uploading a non-permitted file won't work as things stand
currently because the filemanager throws an exception that Behat
catches, which causes a fail.
This commit is contained in:
Jonathon Fowler
2017-04-07 16:02:00 +08:00
committed by John Okely
parent 4e5a22df7a
commit 4044cad5ea
5 changed files with 139 additions and 2 deletions
+1
View File
@@ -67,6 +67,7 @@ $string['group:archive'] = 'Archive files';
$string['group:audio'] = 'Audio files';
$string['group:document'] = 'Document files';
$string['group:html_audio'] = 'Audio files natively supported by browsers';
$string['group:html_track'] = 'HTML track files';
$string['group:html_video'] = 'Video files natively supported by browsers';
$string['group:image'] = 'Image files';
$string['group:presentation'] = 'Presentation files';
@@ -23,6 +23,7 @@
*/
$string['acceptedfiletypes'] = 'Accepted file types';
$string['configmaxbytes'] = 'Maximum file size';
$string['countfiles'] = '{$a} files';
$string['default'] = 'Enabled by default';
@@ -31,6 +32,8 @@ $string['enabled'] = 'File submissions';
$string['enabled_help'] = 'If enabled, students are able to upload one or more files as their submission.';
$string['eventassessableuploaded'] = 'A file has been uploaded.';
$string['file'] = 'File submissions';
$string['filesofthesetypes'] = 'Files of these types may be added to the submission:';
$string['filetypewithexts'] = '{$a->name} — {$a->extlist}';
$string['maxbytes'] = 'Maximum file size';
$string['maxfiles'] = 'Maximum files per submission';
$string['maxfiles_help'] = 'If file submissions are enabled, each assignment can be set to accept up to this number of files for their submission.';
+66 -1
View File
@@ -71,6 +71,7 @@ class assign_submission_file extends assign_submission_plugin {
$defaultmaxfilesubmissions = $this->get_config('maxfilesubmissions');
$defaultmaxsubmissionsizebytes = $this->get_config('maxsubmissionsizebytes');
$defaultfiletypes = (string)$this->get_config('filetypeslist');
$settings = array();
$options = array();
@@ -105,6 +106,12 @@ class assign_submission_file extends assign_submission_plugin {
$mform->disabledIf('assignsubmission_file_maxsizebytes',
'assignsubmission_file_enabled',
'notchecked');
$name = get_string('acceptedfiletypes', 'assignsubmission_file');
$mform->addElement('text', 'assignsubmission_file_filetypes', $name);
$mform->setType('assignsubmission_file_filetypes', PARAM_RAW);
$mform->setDefault('assignsubmission_file_filetypes', $defaultfiletypes);
$mform->disabledIf('assignsubmission_file_filetypes', 'assignsubmission_file_enabled', 'notchecked');
}
/**
@@ -116,6 +123,13 @@ class assign_submission_file extends assign_submission_plugin {
public function save_settings(stdClass $data) {
$this->set_config('maxfilesubmissions', $data->assignsubmission_file_maxfiles);
$this->set_config('maxsubmissionsizebytes', $data->assignsubmission_file_maxsizebytes);
if (!empty($data->assignsubmission_file_filetypes)) {
$this->set_config('filetypeslist', $data->assignsubmission_file_filetypes);
} else {
$this->set_config('filetypeslist', '');
}
return true;
}
@@ -128,7 +142,7 @@ class assign_submission_file extends assign_submission_plugin {
$fileoptions = array('subdirs' => 1,
'maxbytes' => $this->get_config('maxsubmissionsizebytes'),
'maxfiles' => $this->get_config('maxfilesubmissions'),
'accepted_types' => '*',
'accepted_types' => $this->get_accepted_types(),
'return_types' => (FILE_INTERNAL | FILE_CONTROLLED_LINK));
if ($fileoptions['maxbytes'] == 0) {
// Use module default.
@@ -163,6 +177,26 @@ class assign_submission_file extends assign_submission_plugin {
$submissionid);
$mform->addElement('filemanager', 'files_filemanager', $this->get_name(), null, $fileoptions);
if (!empty($this->get_config('filetypeslist'))) {
$text = html_writer::tag('p', get_string('filesofthesetypes', 'assignsubmission_file'));
$text .= html_writer::start_tag('ul');
$typesets = $this->get_configured_typesets();
foreach ($typesets as $type) {
$a = new stdClass();
if (strpos($type, '/') !== false) {
$a->name = get_mimetype_description($type);
} else {
$a->name = get_string("group:$type", 'mimetypes');
}
$a->extlist = implode(' ', file_get_typegroup('extension', $type));
$text .= html_writer::tag('li', get_string('filetypewithexts', 'assignsubmission_file', $a));
}
$text .= html_writer::end_tag('ul');
$mform->addElement('static', '', '', $text);
}
return true;
}
@@ -570,4 +604,35 @@ class assign_submission_file extends assign_submission_plugin {
}
return (array) $configs;
}
/**
* Get the type sets configured for this assignment.
*
* @return array('groupname', 'mime/type', ...)
*/
private function get_configured_typesets() {
$typeslist = (string)$this->get_config('filetypeslist');
$sets = array();
if (!empty($typeslist)) {
$sets = explode(';', $typeslist);
}
return $sets;
}
/**
* Return the accepted types list for the file manager component.
*
* @return array|string
*/
private function get_accepted_types() {
$acceptedtypes = $this->get_configured_typesets();
if (!empty($acceptedtypes)) {
return $acceptedtypes;
}
return '*';
}
}
@@ -0,0 +1,68 @@
@mod @mod_assign @assignsubmission_file
Feature: In an assignment, limit submittable file types
In order to constrain student submissions for marking
As a teacher
I need to limit the submittable file types
Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following config values are set as admin:
| filetypes | image/png;spreadsheet | assignsubmission_file |
@javascript
Scenario: Configuring permitted file types for an assignment
Given the following "activities" exist:
| activity | course | idnumber | name | intro | duedate | assignsubmission_onlinetext_enabled | assignsubmission_file_enabled | assignsubmission_file_maxfiles | assignsubmission_file_maxsizebytes |
| assign | C1 | assign1 | Test assignment name | Test assignment description | 1388534400 | 0 | 1 | 1 | 0 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Test assignment name"
And I navigate to "Edit settings" in current page administration
When I set the field "Accepted file types" to "image/png;spreadsheet"
And I press "Save and display"
And I navigate to "Edit settings" in current page administration
Then the field "Accepted file types" matches value "image/png;spreadsheet"
@javascript @_file_upload
Scenario: Uploading permitted file types for an assignment
Given the following "activities" exist:
| activity | course | idnumber | name | intro | duedate | assignsubmission_onlinetext_enabled | assignsubmission_file_enabled | assignsubmission_file_maxfiles | assignsubmission_file_maxsizebytes | assignsubmission_file_filetypes |
| assign | C1 | assign1 | Test assignment name | Test assignment description | 1388534400 | 0 | 1 | 2 | 0 | image/png;spreadsheet |
And I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Test assignment name"
When I press "Add submission"
And I should see "Files of these types may be added to the submission"
And I should see "Image (PNG) — .png"
And I should see "Spreadsheet files — .csv .ods .ots .xls .xlsx .xlsm"
And I upload "lib/tests/fixtures/gd-logo.png" file to "File submissions" filemanager
And I upload "lib/tests/fixtures/tabfile.csv" file to "File submissions" filemanager
And I press "Save changes"
Then "gd-logo.png" "link" should exist
And "tabfile.csv" "link" should exist
@javascript @_file_upload
Scenario: No filetypes allows all
Given the following "activities" exist:
| activity | course | idnumber | name | intro | duedate | assignsubmission_onlinetext_enabled | assignsubmission_file_enabled | assignsubmission_file_maxfiles | assignsubmission_file_maxsizebytes | assignsubmission_file_filetypes |
| assign | C1 | assign1 | Test assignment name | Test assignment description | 1388534400 | 0 | 1 | 2 | 0 | |
And I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Test assignment name"
When I press "Add submission"
And I should not see "Files of these types may be added to the submission"
And I upload "lib/tests/fixtures/gd-logo.png" file to "File submissions" filemanager
And I upload "lib/tests/fixtures/tabfile.csv" file to "File submissions" filemanager
And I press "Save changes"
Then "gd-logo.png" "link" should exist
And "tabfile.csv" "link" should exist
+1 -1
View File
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016120500;
$plugin->version = 2017032000;
$plugin->requires = 2016112900;
$plugin->component = 'assignsubmission_file';