mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
Merge branch 'MDL-62849-master' of git://github.com/abgreeve/moodle
This commit is contained in:
commit
4a91bd6f62
@ -757,6 +757,34 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all of the files in the draftarea.
|
||||
*
|
||||
* @param int $draftitemid The draft item ID
|
||||
* @param string $filepath path for the uploaded files.
|
||||
* @return array An array of files associated with this draft item id.
|
||||
*/
|
||||
function file_get_all_files_in_draftarea(int $draftitemid, string $filepath = '/') : array {
|
||||
$files = [];
|
||||
$draftfiles = file_get_drafarea_files($draftitemid, $filepath);
|
||||
file_get_drafarea_folders($draftitemid, $filepath, $draftfiles);
|
||||
|
||||
if (!empty($draftfiles)) {
|
||||
foreach ($draftfiles->list as $draftfile) {
|
||||
if ($draftfile->type == 'file') {
|
||||
$files[] = $draftfile;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($draftfiles->children)) {
|
||||
foreach ($draftfiles->children as $draftfile) {
|
||||
$files = array_merge($files, file_get_all_files_in_draftarea($draftitemid, $draftfile->filepath));
|
||||
}
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns draft area itemid for a given element.
|
||||
*
|
||||
|
@ -333,7 +333,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements temp
|
||||
return;
|
||||
}
|
||||
|
||||
$draftfiles = file_get_drafarea_files($value);
|
||||
$draftfiles = file_get_all_files_in_draftarea($value);
|
||||
$wrongfiles = array();
|
||||
|
||||
if (empty($draftfiles)) {
|
||||
@ -341,7 +341,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements temp
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($draftfiles->list as $file) {
|
||||
foreach ($draftfiles as $file) {
|
||||
if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
|
||||
$wrongfiles[] = $file->filename;
|
||||
}
|
||||
|
@ -1424,6 +1424,55 @@ EOF;
|
||||
$this->assertContains($file->get_filename(), $expected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all files in the draftarea are returned.
|
||||
*/
|
||||
public function test_file_get_all_files_in_draftarea() {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$filerecord = ['filename' => 'basepic.jpg'];
|
||||
$file = self::create_draft_file($filerecord);
|
||||
|
||||
$secondrecord = [
|
||||
'filename' => 'infolder.jpg',
|
||||
'filepath' => '/assignment/',
|
||||
'itemid' => $file->get_itemid()
|
||||
];
|
||||
$file = self::create_draft_file($secondrecord);
|
||||
|
||||
$thirdrecord = [
|
||||
'filename' => 'deeperfolder.jpg',
|
||||
'filepath' => '/assignment/pics/',
|
||||
'itemid' => $file->get_itemid()
|
||||
];
|
||||
$file = self::create_draft_file($thirdrecord);
|
||||
|
||||
$fourthrecord = [
|
||||
'filename' => 'differentimage.jpg',
|
||||
'filepath' => '/secondfolder/',
|
||||
'itemid' => $file->get_itemid()
|
||||
];
|
||||
$file = self::create_draft_file($fourthrecord);
|
||||
|
||||
// This record has the same name as the last record, but it's in a different folder.
|
||||
// Just checking this is also returned.
|
||||
$fifthrecord = [
|
||||
'filename' => 'differentimage.jpg',
|
||||
'filepath' => '/assignment/pics/',
|
||||
'itemid' => $file->get_itemid()
|
||||
];
|
||||
$file = self::create_draft_file($fifthrecord);
|
||||
|
||||
$allfiles = file_get_all_files_in_draftarea($file->get_itemid());
|
||||
$this->assertCount(5, $allfiles);
|
||||
$this->assertEquals($filerecord['filename'], $allfiles[0]->filename);
|
||||
$this->assertEquals($secondrecord['filename'], $allfiles[1]->filename);
|
||||
$this->assertEquals($thirdrecord['filename'], $allfiles[2]->filename);
|
||||
$this->assertEquals($fourthrecord['filename'], $allfiles[3]->filename);
|
||||
$this->assertEquals($fifthrecord['filename'], $allfiles[4]->filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user