Permalink
Browse files

Merge branch 'MDL-59299-master' of https://github.com/xow/moodle

  • Loading branch information...
dmonllao committed Aug 1, 2017
2 parents 5283fa3 + 1318aec commit 85a9c0b1e51927707bc79c2cafb8fc2e626139ba
@@ -42,6 +42,7 @@
$string['err_rangelength'] = 'You must enter between {$a->format[0]} and {$a->format[1]} characters here.';
$string['err_required'] = 'You must supply a value here.';
$string['err_wrongfileextension'] = 'Some files ({$a->wrongfiles}) cannot be uploaded. Only file types {$a->whitelist} are allowed.';
$string['filesofthesetypes'] = 'Accepted files types:';
$string['filetypesany'] = 'All file types';
$string['filetypesnotall'] = 'It is not allowed to select \'All file types\' here';
$string['filetypesnotwhitelisted'] = 'These file types are not allowed here: {$a}';
@@ -300,6 +300,14 @@ function toHtml() {
// label element needs 'for' attribute work
$html .= html_writer::empty_tag('input', array('value' => '', 'id' => 'id_'.$elname, 'type' => 'hidden'));
if (!empty($options->accepted_types) && $options->accepted_types != '*') {
$html .= html_writer::tag('p', get_string('filesofthesetypes', 'form'));
$util = new \core_form\filetypes_util();
$filetypes = $options->accepted_types;
$filetypedescriptions = $util->describe_file_types($filetypes);
$html .= $OUTPUT->render_from_template('core_form/filetypes-descriptions', $filetypedescriptions);
}
return $html;
}
@@ -184,6 +184,14 @@ function toHtml() {
$html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
$html .= '</noscript>';
if (!empty($options->accepted_types) && $options->accepted_types != '*') {
$html .= html_writer::tag('p', get_string('filesofthesetypes', 'form'));
$util = new \core_form\filetypes_util();
$filetypes = $options->accepted_types;
$filetypedescriptions = $util->describe_file_types($filetypes);
$html .= $OUTPUT->render_from_template('core_form/filetypes-descriptions', $filetypedescriptions);
}
return $html;
}
@@ -24,7 +24,7 @@
$string['acceptedfiletypes'] = 'Accepted file types';
$string['acceptedfiletypes_help'] = 'Accepted file types can be restricted by entering a comma-separated list of mimetypes, e.g. video/mp4, audio/mp3, image/png, image/jpeg, or file extensions including a dot, e.g. .png, .jpg. If the field is left empty, then all file types are allowed.';
$string['acceptedfiletypes_help'] = 'Accepted file types can be restricted by entering a list of file extensions. If the field is left empty, then all file types are allowed.';
$string['configmaxbytes'] = 'Maximum file size';
$string['countfiles'] = '{$a} files';
$string['default'] = 'Enabled by default';
@@ -34,17 +34,18 @@
$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} &mdash; {$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.';
$string['maxfilessubmission'] = 'Maximum number of uploaded files';
$string['maxfilessubmission_help'] = 'If file submissions are enabled, each student will be able to upload up to this number of files for their submission.';
$string['maximumsubmissionsize'] = 'Maximum submission size';
$string['maximumsubmissionsize_help'] = 'Files uploaded by students may be up to this size.';
$string['nonexistentfiletypes'] = 'The following file types were not recognised: {$a}';
$string['numfilesforlog'] = 'The number of file(s) : {$a} file(s).';
$string['pluginname'] = 'File submissions';
$string['siteuploadlimit'] = 'Site upload limit';
$string['submissionfilearea'] = 'Uploaded submission files';
// Deprecated since Moodle 3.4.
$string['filesofthesetypes'] = 'Files of these types may be added to the submission:';
$string['filetypewithexts'] = '{$a->name} &mdash; {$a->extlist}';
$string['nonexistentfiletypes'] = 'The following file types were not recognised: {$a}';
@@ -0,0 +1,3 @@
filesofthesetypes,assignsubmission_file
filetypewithexts,assignsubmission_file
nonexistentfiletypes,assignsubmission_file
@@ -113,23 +113,10 @@ public function get_settings(MoodleQuickForm $mform) {
'notchecked');
$name = get_string('acceptedfiletypes', 'assignsubmission_file');
$mform->addElement('text', 'assignsubmission_file_filetypes', $name, array('size' => '60'));
$mform->addElement('filetypes', 'assignsubmission_file_filetypes', $name);
$mform->addHelpButton('assignsubmission_file_filetypes', 'acceptedfiletypes', 'assignsubmission_file');
$mform->setType('assignsubmission_file_filetypes', PARAM_RAW);
$mform->setDefault('assignsubmission_file_filetypes', $defaultfiletypes);
$mform->disabledIf('assignsubmission_file_filetypes', 'assignsubmission_file_enabled', 'notchecked');
$mform->addFormRule(function ($values, $files) {
if (empty($values['assignsubmission_file_filetypes'])) {
return true;
}
$nonexistent = $this->get_nonexistent_file_types($values['assignsubmission_file_filetypes']);
if (empty($nonexistent)) {
return true;
} else {
$a = join(' ', $nonexistent);
return ["assignsubmission_file_filetypes" => get_string('nonexistentfiletypes', 'assignsubmission_file', $a)];
}
});
}
/**
@@ -160,7 +147,7 @@ private function get_file_options() {
$fileoptions = array('subdirs' => 1,
'maxbytes' => $this->get_config('maxsubmissionsizebytes'),
'maxfiles' => $this->get_config('maxfilesubmissions'),
'accepted_types' => $this->get_accepted_types(),
'accepted_types' => $this->get_configured_typesets(),
'return_types' => (FILE_INTERNAL | FILE_CONTROLLED_LINK));
if ($fileoptions['maxbytes'] == 0) {
// Use module default.
@@ -178,6 +165,7 @@ private function get_file_options() {
* @return bool
*/
public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
global $OUTPUT;
if ($this->get_config('maxfilesubmissions') <= 0) {
return false;
@@ -195,34 +183,6 @@ public function get_form_elements($submission, MoodleQuickForm $mform, stdClass
$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();
$extensions = file_get_typegroup('extension', $type);
$typetext = html_writer::tag('li', $type);
// Only bother checking if it's a mimetype or group if it has extensions in the group.
if (!empty($extensions)) {
if (strpos($type, '/') !== false) {
$a->name = get_mimetype_description($type);
$a->extlist = implode(' ', $extensions);
$typetext = html_writer::tag('li', get_string('filetypewithexts', 'assignsubmission_file', $a));
} else if (get_string_manager()->string_exists("group:$type", 'mimetypes')) {
$a->name = get_string("group:$type", 'mimetypes');
$a->extlist = implode(' ', $extensions);
$typetext = html_writer::tag('li', get_string('filetypewithexts', 'assignsubmission_file', $a));
}
}
$text .= $typetext;
}
$text .= html_writer::end_tag('ul');
$mform->addElement('static', '', '', $text);
}
return true;
}
@@ -639,56 +599,9 @@ public function get_config_for_external() {
private function get_configured_typesets() {
$typeslist = (string)$this->get_config('filetypeslist');
$sets = $this->get_typesets($typeslist);
return $sets;
}
$util = new \core_form\filetypes_util();
$sets = $util->normalize_file_types($typeslist);
/**
* Get the type sets passed.
*
* @param string $types The space , ; separated list of types
* @return array('groupname', 'mime/type', ...)
*/
private function get_typesets($types) {
$sets = array();
if (!empty($types)) {
$sets = preg_split('/[\s,;:"\']+/', $types, null, PREG_SPLIT_NO_EMPTY);
}
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 '*';
}
/**
* List the nonexistent file types that need to be removed.
*
* @param string $types space , or ; separated types
* @return array A list of the nonexistent file types.
*/
private function get_nonexistent_file_types($types) {
$nonexistent = [];
foreach ($this->get_typesets($types) as $type) {
// If there's no extensions under that group, it doesn't exist.
$extensions = file_get_typegroup('extension', [$type]);
if (empty($extensions)) {
$nonexistent[$type] = true;
}
}
return array_keys($nonexistent);
}
}
@@ -32,9 +32,9 @@
new lang_string('maxfiles', 'assignsubmission_file'),
new lang_string('maxfiles_help', 'assignsubmission_file'), 20, PARAM_INT));
$settings->add(new admin_setting_configtext('assignsubmission_file/filetypes',
$settings->add(new admin_setting_filetypes('assignsubmission_file/filetypes',
new lang_string('defaultacceptedfiletypes', 'assignsubmission_file'),
new lang_string('acceptedfiletypes_help', 'assignsubmission_file'), '', PARAM_RAW, 60));
new lang_string('acceptedfiletypes_help', 'assignsubmission_file'), ''));
if (isset($CFG->maxbytes)) {
@@ -30,25 +30,32 @@ Feature: In an assignment, limit submittable file types
And I navigate to "Edit settings" in current page administration
When I set the field "Accepted file types" to "image/png;doesntexist;.anything;unreal/mimetype;nodot"
And I press "Save and display"
And I should see "The following file types were not recognised: doesntexist .anything unreal/mimetype nodot"
And I should see "Unknown file types: .doesntexist, .anything, unreal/mimetype, .nodot"
And 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"
And the field "Accepted file types" matches value "image/png,spreadsheet"
And I set the field "Accepted file types" to ""
And I press "Choose"
And I set the field "Image files" to "1"
And I press "Save changes"
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"
@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 | 3 | 0 | image/png;spreadsheet;.xml;.txt |
| assign | C1 | assign1 | Test assignment name | Test assignment description | 1388534400 | 0 | 1 | 3 | 0 | image/png,spreadsheet,.xml,.txt |
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 .gsheet .ods .ots .xls .xlsx .xlsm"
And I should see ".txt"
And I should see "Image (PNG)"
And I should see "Spreadsheet files"
And I should see "Text file"
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 upload "lib/tests/fixtures/empty.txt" file to "File submissions" filemanager
@@ -136,70 +136,4 @@ public function submission_is_empty_testcases() {
'Without file' => [null, true]
];
}
/**
* Data provider for testing test_get_nonexistent_file_types.
*
* @return array
*/
public function get_nonexistent_file_types_provider() {
return [
'Nonexistent extensions are not allowed' => [
'filetypes' => '.rat',
'expected' => ['.rat']
],
'Multiple nonexistent extensions are not allowed' => [
'filetypes' => '.ricefield .rat',
'expected' => ['.ricefield', '.rat']
],
'Existent extension is allowed' => [
'filetypes' => '.xml',
'expected' => []
],
'Existent group is allowed' => [
'filetypes' => 'web_file',
'expected' => []
],
'Nonexistent group is not allowed' => [
'filetypes' => '©ç√√ß∂å√©åß©√',
'expected' => ['©ç√√ß∂å√©åß©√']
],
'Existent mimetype is allowed' => [
'filetypes' => 'application/xml',
'expected' => []
],
'Nonexistent mimetype is not allowed' => [
'filetypes' => 'ricefield/rat',
'expected' => ['ricefield/rat']
],
'Multiple nonexistent mimetypes are not allowed' => [
'filetypes' => 'ricefield/rat cam/ball',
'expected' => ['ricefield/rat', 'cam/ball']
],
'Missing dot in extension is not allowed' => [
'filetypes' => 'png',
'expected' => ['png']
],
'Some existent some not' => [
'filetypes' => '.txt application/xml web_file ©ç√√ß∂å√©åß©√ .png ricefield/rat document png',
'expected' => ['©ç√√ß∂å√©åß©√', 'ricefield/rat', 'png']
]
];
}
/**
* Test get_nonexistent_file_types().
* @dataProvider get_nonexistent_file_types_provider
* @param string $filetypes The filetypes to check
* @param array $expected The expected result. The list of non existent file types.
*/
public function test_get_nonexistent_file_types($filetypes, $expected) {
$this->resetAfterTest();
$method = new ReflectionMethod(assign_submission_file::class, 'get_nonexistent_file_types');
$method->setAccessible(true);
$plugin = $this->assign->get_submission_plugin_by_type('file');
$nonexistentfiletypes = $method->invokeArgs($plugin, [$filetypes]);
$this->assertSame($expected, $nonexistentfiletypes);
}
}

0 comments on commit 85a9c0b

Please sign in to comment.