From a2fb838e82ae739e5aeeec32302a28503cf84366 Mon Sep 17 00:00:00 2001 From: Ben Tindell Date: Mon, 4 May 2015 12:29:32 -0500 Subject: [PATCH] MDL-53577 repository: Added maxbytes error message Changing the error message that is displayed to users when they upload a file that is greater than the maximum upload size. Does not include all upload cases; focuses on those most used by students. --- lang/en/deprecated.txt | 2 ++ lang/en/error.php | 5 ++++- lang/en/moodle.php | 2 +- lib/form/dndupload.js | 31 ++++++++++++++++++++++++++++++- lib/outputrequirementslib.php | 3 ++- repository/lib.php | 12 +++++++++--- repository/repository_ajax.php | 4 +++- repository/upload/lib.php | 4 +++- 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index ada31e611e0..7da297ae24b 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -28,3 +28,5 @@ tagtype,core_tag manageofficialtags,core_tag settypeofficial,core_tag filetoolarge,core +maxbytesforfile,core +maxbytes,core_error diff --git a/lang/en/error.php b/lang/en/error.php index ae0d20756a9..092ec783cf4 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -368,7 +368,7 @@ $string['listupdatefail'] = 'DB operation failed when editing list hierarchy'; $string['logfilenotavailable'] = 'Logs not available'; $string['loginasnoenrol'] = 'You cannot use enrol or unenrol when in course "Login as" session'; $string['loginasonecourse'] = 'You cannot enter this course.
You have to terminate the "Login as" session before entering any other course.'; -$string['maxbytes'] = 'The file is larger than the maximum size allowed.'; +$string['maxbytesfile'] = 'The file {$a->file} is too large. The maximum size you can upload is {$a->size}.'; $string['maxareabytes'] = 'The file is larger than the space remaining in this area.'; $string['messagingdisable'] = 'Messaging is disabled on this site'; $string['mimetexisnotexist'] = 'Your system is not configured to run mimeTeX. You need to download the appropriate executable for you PHP_OS platform from http://moodle.org/download/mimetex/, or obtain the C source from http://www.forkosh.com/mimetex.zip, compile it and put the executable into your moodle/filter/tex/ directory.'; @@ -578,3 +578,6 @@ $string['xmldberror'] = 'XMLDB error!'; $string['alreadyloggedin'] = 'You are already logged in as {$a}, you need to log out before logging in as different user.'; $string['youcannotdeletecategory'] = 'You cannot delete category \'{$a}\' because you can neither delete the contents, nor move them elsewhere.'; $string['protected_cc_not_supported'] = 'Protected cartridges not supported.'; + +// Deprecated since Moodle 3.1. +$string['maxbytes'] = 'The file is larger than the maximum size allowed.'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 53c98f4a5ab..5fa03b8f98e 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1096,7 +1096,6 @@ $string['markedthistopic'] = 'This topic is highlighted as the current topic'; $string['markthistopic'] = 'Highlight this topic as the current topic'; $string['matchingsearchandrole'] = 'Matching \'{$a->search}\' and {$a->role}'; $string['maxareabytesreached'] = 'The file (or the total size of several files) is larger than the space remaining in this area.'; -$string['maxbytesforfile'] = 'The file {$a} is larger than the maximum size allowed.'; $string['maxfilesize'] = 'Maximum size for new files: {$a}'; $string['maxfilesreached'] = 'You are allowed to attach a maximum of {$a} file(s) to this item'; $string['maximumchars'] = 'Maximum of {$a} characters'; @@ -2030,3 +2029,4 @@ $string['zippingbackup'] = 'Zipping backup'; // Deprecated since Moodle 3.1. $string['filetoolarge'] = 'is too large to upload'; +$string['maxbytesforfile'] = 'The file {$a} is larger than the maximum size allowed.'; diff --git a/lib/form/dndupload.js b/lib/form/dndupload.js index de0ee972c93..22e8cf3784f 100644 --- a/lib/form/dndupload.js +++ b/lib/form/dndupload.js @@ -589,7 +589,11 @@ M.form_dndupload.init = function(Y, options) { for (i=0; i 0 && files[i].size > this.options.maxbytes) { // Check filesize before attempting to upload. - this.print_msg(M.util.get_string('maxbytesforfile', 'moodle', files[i].name), 'error'); + var maxbytesdisplay = this.display_size(this.options.maxbytes); + this.print_msg(M.util.get_string('maxbytesfile', 'error', { + file: files[i].name, + size: maxbytesdisplay + }), 'error'); this.uploadqueue = []; // No uploads if one file is too big. return; } @@ -606,6 +610,31 @@ M.form_dndupload.init = function(Y, options) { return true; }, + /** + * Generate the display for file size + * @param int size The size to convert to human readable form + * @return string + */ + display_size: function(size) { + // This is snippet of code (with some changes) is from the display_size function in moodlelib. + var gb = M.util.get_string('sizegb'), + mb = M.util.get_string('sizemb'), + kb = M.util.get_string('sizekb'), + b = M.util.get_string('sizeb'); + + if (size >= 1073741824) { + size = Math.round(size / 1073741824 * 10) / 10 + gb; + } else if (size >= 1048576) { + size = Math.round(size / 1048576 * 10) / 10 + mb; + } else if (size >= 1024) { + size = Math.round(size / 1024 * 10) / 10 + kb; + } else { + size = parseInt(size, 10) + ' ' + b; + } + + return size; + }, + /** * Add a single file to the uploadqueue, whilst checking the maxfiles limit * @param File file - the file to add diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index e79308d7168..b9a25384ce8 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -785,7 +785,8 @@ class page_requirements_manager { 'fullpath' => '/lib/form/dndupload.js', 'requires' => array('node', 'event', 'json', 'core_filepicker'), 'strings' => array(array('uploadformlimit', 'moodle'), array('droptoupload', 'moodle'), array('maxfilesreached', 'moodle'), - array('dndenabled_inbox', 'moodle'), array('fileexists', 'moodle'), array('maxbytesforfile', 'moodle'), + array('dndenabled_inbox', 'moodle'), array('fileexists', 'moodle'), array('maxbytesfile', 'error'), + array('sizegb', 'moodle'), array('sizemb', 'moodle'), array('sizekb', 'moodle'), array('sizeb', 'moodle'), array('maxareabytesreached', 'moodle'), array('serverconnection', 'error'), )); break; diff --git a/repository/lib.php b/repository/lib.php index a8258a2cd72..201a05fd7fd 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -888,7 +888,9 @@ abstract class repository implements cacheable_object { // the file needs to copied to draft area $stored_file = self::get_moodle_file($source); if ($maxbytes != -1 && $stored_file->get_filesize() > $maxbytes) { - throw new file_exception('maxbytes'); + $maxbytesdisplay = display_size($maxbytes); + throw new file_exception('maxbytesfile', (object) array('file' => $filerecord['filename'], + 'size' => $maxbytesdisplay)); } // Validate the size of the draft area. if (file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $stored_file->get_filesize())) { @@ -1699,13 +1701,17 @@ abstract class repository implements cacheable_object { // files that are references to local files are already in moodle filepool // just validate the size if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) { - throw new file_exception('maxbytes'); + $maxbytesdisplay = display_size($maxbytes); + throw new file_exception('maxbytesfile', (object) array('file' => $file->get_filename(), + 'size' => $maxbytesdisplay)); } return; } else { if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) { // note that stored_file::get_filesize() also calls synchronisation - throw new file_exception('maxbytes'); + $maxbytesdisplay = display_size($maxbytes); + throw new file_exception('maxbytesfile', (object) array('file' => $file->get_filename(), + 'size' => $maxbytesdisplay)); } $fs = get_file_storage(); $contentexists = $fs->content_exists($file->get_contenthash()); diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index 0873a4a1987..6547db148fb 100644 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -283,7 +283,9 @@ switch ($action) { // Check if exceed maxbytes. if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) { - throw new file_exception('maxbytes'); + $maxbytesdisplay = display_size($maxbytes); + throw new file_exception('maxbytesfile', (object) array('file' => $record->filename, + 'size' => $maxbytesdisplay)); } // Check if we exceed the max bytes of the area. diff --git a/repository/upload/lib.php b/repository/upload/lib.php index 2f14477415c..713ffbda9d1 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -191,7 +191,9 @@ class repository_upload extends repository { } if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) { - throw new file_exception('maxbytesforfile', $_FILES[$elname]['name']); + $maxbytesdisplay = display_size($maxbytes); + throw new file_exception('maxbytesfile', (object) array('file' => $record->filename, + 'size' => $maxbytesdisplay)); } if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {