From a32dabc26038861aa6f065a65b0e73a1064cbe86 Mon Sep 17 00:00:00 2001 From: Davo Smith Date: Mon, 9 Jul 2012 20:50:35 +0100 Subject: [PATCH] MDL-33927 Filemanager - enforce the maxfiles limit on form submission --- lang/en/form.php | 1 + lib/formslib.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lang/en/form.php b/lang/en/form.php index fbd8c9d46b5..d55667d4b3e 100644 --- a/lang/en/form.php +++ b/lang/en/form.php @@ -30,6 +30,7 @@ $string['display'] = 'Display'; $string['err_alphanumeric'] = 'You must enter only letters or numbers here.'; $string['err_email'] = 'You must enter a valid email address here.'; $string['err_lettersonly'] = 'You must enter only letters here.'; +$string['err_maxfiles'] = 'You must not attach more than {$a} files here.'; $string['err_maxlength'] = 'You must enter not more than {$a->format} characters here.'; $string['err_minlength'] = 'You must enter at least {$a->format} characters here.'; $string['err_nonzero'] = 'You must enter a number not starting with a 0 here.'; diff --git a/lib/formslib.php b/lib/formslib.php index 306c4e5d2b8..e970c6db1cb 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -385,6 +385,22 @@ abstract class moodleform { } } } + // Check all the filemanager elements to make sure they do not have too many + // files in them. + foreach ($mform->_elements as $element) { + if ($element->_type == 'filemanager') { + $maxfiles = $element->getMaxfiles(); + if ($maxfiles > 0) { + $draftid = (int)$element->getValue(); + $fs = get_file_storage(); + $context = context_user::instance($USER->id); + $files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, '', false); + if (count($files) > $maxfiles) { + $errors[$element->getName()] = get_string('err_maxfiles', 'form', $maxfiles); + } + } + } + } if (empty($errors)) { return true; } else { -- 2.11.4.GIT