mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-33766 files: Support for userquota/size limit in draft areas
This commit is contained in:
parent
6109f2112c
commit
21e3ea77a3
@ -454,14 +454,21 @@ class core_files_renderer extends plugin_renderer_base {
|
||||
*/
|
||||
private function fm_print_restrictions($fm) {
|
||||
$maxbytes = display_size($fm->options->maxbytes);
|
||||
if (empty($fm->options->maxfiles) || $fm->options->maxfiles == -1) {
|
||||
$maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
|
||||
} else {
|
||||
$strparam = (object)array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles);
|
||||
$strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
|
||||
'areasize' => display_size($fm->options->areamaxbytes));
|
||||
$hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
|
||||
$hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
|
||||
if ($hasmaxfiles && $hasarealimit) {
|
||||
$maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
|
||||
} else if ($hasmaxfiles) {
|
||||
$maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
|
||||
} else if ($hasarealimit) {
|
||||
$maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
|
||||
} else {
|
||||
$maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
|
||||
}
|
||||
// TODO MDL-32020 also should say about 'File types accepted'
|
||||
return '<span>'. $maxsize. '</span>';
|
||||
return '<span>'. $maxsize . '</span>';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -346,6 +346,7 @@ $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.<br /> You have to terminate the "Login as" session before entering any other course.';
|
||||
$string['maxbytes'] = 'This file is bigger than the maximum size';
|
||||
$string['maxareabytes'] = 'Not enough available space to store this file';
|
||||
$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 <a href="http://moodle.org/download/mimetex/">http://moodle.org/download/mimetex/</a>, or obtain the C source from <a href="http://www.forkosh.com/mimetex.zip"> http://www.forkosh.com/mimetex.zip</a>, compile it and put the executable into your moodle/filter/tex/ directory.';
|
||||
$string['mimetexnotexecutable'] = 'Custom mimetex is not executable!';
|
||||
|
@ -985,6 +985,8 @@ $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['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}';
|
||||
$string['maxsizeandattachmentsandareasize'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}, overall limit: {$a->areasize}';
|
||||
$string['maxsizeandareasize'] = 'Maximum size for new files: {$a->size}, overall limit: {$a->areasize}';
|
||||
$string['maxfilesreached'] = 'You are allowed to attach a maximum of {$a} file(s) to this item';
|
||||
$string['maximumgrade'] = 'Maximum grade';
|
||||
$string['maximumgradex'] = 'Maximum grade: {$a}';
|
||||
|
@ -481,6 +481,25 @@ function file_get_draft_area_info($draftitemid) {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a draft area has exceeded/will exceed its size limit.
|
||||
*
|
||||
* @param int $draftitemid the draft area item id.
|
||||
* @param int $areamaxbytes the maximum size allowed in this draft area.
|
||||
* @param int $newfilesize the size that would be added to the current area.
|
||||
* @return bool true if the area will/has exceeded its limit.
|
||||
* @since 2.4
|
||||
*/
|
||||
function file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $newfilesize = 0) {
|
||||
if ($areamaxbytes != -1) {
|
||||
$draftinfo = file_get_draft_area_info($draftitemid);
|
||||
if ($draftinfo['filesize'] + $newfilesize > $areamaxbytes) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get used space of files
|
||||
* @global moodle_database $DB
|
||||
|
@ -38,6 +38,8 @@ M.form_dndupload.init = function(Y, options) {
|
||||
acceptedtypes: [],
|
||||
// maximum size of files allowed in this form
|
||||
maxbytes: 0,
|
||||
// maximum combined size of files allowed in this form
|
||||
areamaxbytes: 0,
|
||||
// unqiue id of this form field used for html elements
|
||||
clientid: '',
|
||||
// upload repository id, used for upload
|
||||
@ -63,6 +65,7 @@ M.form_dndupload.init = function(Y, options) {
|
||||
* acceptdtypes: accepted filetypes by this form
|
||||
* maxfiles: maximum number of files this form allows
|
||||
* maxbytes: maximum size of files allowed in this form
|
||||
* areamaxbytes: maximum combined size of files allowed in this form
|
||||
* clientid: unqiue id of this form field used for html elements
|
||||
* containerid: htmlid of container
|
||||
* repositories: array of repository objects passed from filepicker
|
||||
@ -90,6 +93,7 @@ M.form_dndupload.init = function(Y, options) {
|
||||
this.acceptedtypes = options.acceptedtypes;
|
||||
this.clientid = options.clientid;
|
||||
this.maxbytes = options.maxbytes;
|
||||
this.areamaxbytes = options.areamaxbytes;
|
||||
this.itemid = options.itemid;
|
||||
this.author = options.author;
|
||||
this.container = this.Y.one('#'+options.containerid);
|
||||
@ -406,10 +410,14 @@ M.form_dndupload.init = function(Y, options) {
|
||||
currentfiles: null,
|
||||
// Total number of files already uploaded (to check for exceeding limits).
|
||||
currentfilecount: 0,
|
||||
// Total size of the files present in the area.
|
||||
currentareasize: 0,
|
||||
// The list of files to upload.
|
||||
uploadqueue: [],
|
||||
// This list of files with name clashes.
|
||||
renamequeue: [],
|
||||
// Size of the current queue.
|
||||
queuesize: 0,
|
||||
// Set to true if the user has clicked on 'overwrite all'.
|
||||
overwriteall: false,
|
||||
// Set to true if the user has clicked on 'rename all'.
|
||||
@ -432,6 +440,12 @@ M.form_dndupload.init = function(Y, options) {
|
||||
this.callback = params.callback;
|
||||
this.currentfiles = params.currentfiles;
|
||||
this.currentfilecount = params.currentfilecount;
|
||||
this.currentareasize = 0;
|
||||
|
||||
// Retrieve the current size of the area.
|
||||
for (var i = 0; i < this.currentfiles.length; i++) {
|
||||
this.currentareasize += this.currentfiles[i].size;
|
||||
};
|
||||
|
||||
this.initialise_queue(params.files);
|
||||
},
|
||||
@ -489,8 +503,9 @@ M.form_dndupload.init = function(Y, options) {
|
||||
initialise_queue: function(files) {
|
||||
this.uploadqueue = [];
|
||||
this.renamequeue = [];
|
||||
this.queuesize = 0;
|
||||
|
||||
// Loop through the files and find any name clashes with existing files
|
||||
// Loop through the files and find any name clashes with existing files.
|
||||
var i;
|
||||
for (i=0; i<files.length; i++) {
|
||||
if (this.options.maxbytes > 0 && files[i].size > this.options.maxbytes) {
|
||||
@ -507,6 +522,7 @@ M.form_dndupload.init = function(Y, options) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.queuesize += files[i].size;
|
||||
}
|
||||
},
|
||||
|
||||
@ -528,6 +544,16 @@ M.form_dndupload.init = function(Y, options) {
|
||||
this.print_msg(M.util.get_string('maxfilesreached', 'moodle', this.options.maxfiles), 'error');
|
||||
return false;
|
||||
}
|
||||
// The new file will cause the area to reach its limit, we cancel the upload of all files.
|
||||
if (this.options.areamaxbytes > -1) {
|
||||
var sizereached = this.currentareasize + this.queuesize + file.size;
|
||||
if (sizereached > this.options.areamaxbytes) {
|
||||
this.uploadqueue = [];
|
||||
this.renamequeue = [];
|
||||
this.print_msg(M.util.get_string('uploadformlimit', 'moodle', file.name), 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.uploadqueue.push({file:file, filename:filename, overwrite:overwrite});
|
||||
return true;
|
||||
},
|
||||
|
@ -26,6 +26,7 @@
|
||||
* this.filecount, how many files in this filemanager
|
||||
* this.maxfiles
|
||||
* this.maxbytes
|
||||
* this.areamaxbytes, the maximum size of the area
|
||||
* this.filemanager, contains reference to filemanager Node
|
||||
* this.selectnode, contains referenct to select-file Node
|
||||
* this.selectui, YUI Panel to select the file
|
||||
@ -68,6 +69,7 @@ M.form_filemanager.init = function(Y, options) {
|
||||
this.currentpath = '/';
|
||||
this.maxfiles = options.maxfiles;
|
||||
this.maxbytes = options.maxbytes;
|
||||
this.areamaxbytes = options.areamaxbytes;
|
||||
this.emptycallback = null; // Used by drag and drop upload
|
||||
|
||||
this.filepicker_options = options.filepicker?options.filepicker:{};
|
||||
@ -75,6 +77,7 @@ M.form_filemanager.init = function(Y, options) {
|
||||
this.filepicker_options.context = options.context;
|
||||
this.filepicker_options.maxfiles = this.maxfiles;
|
||||
this.filepicker_options.maxbytes = this.maxbytes;
|
||||
this.filepicker_options.areamaxbytes = this.areamaxbytes;
|
||||
this.filepicker_options.env = 'filemanager';
|
||||
this.filepicker_options.itemid = options.itemid;
|
||||
|
||||
@ -1003,6 +1006,7 @@ M.form_filemanager.init = function(Y, options) {
|
||||
author: options.author,
|
||||
maxfiles: options.maxfiles,
|
||||
maxbytes: options.maxbytes,
|
||||
areamaxbytes: options.areamaxbytes,
|
||||
itemid: options.itemid,
|
||||
repositories: manager.filepicker_options.repositories,
|
||||
containerid: manager.dndcontainer.get('id')
|
||||
|
@ -48,7 +48,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
// PHP doesn't support 'key' => $value1 | $value2 in class definition
|
||||
// We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
|
||||
// So I have to set null here, and do it in constructor
|
||||
protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=> null);
|
||||
protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=> null, 'areamaxbytes' => -1);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -133,6 +133,24 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
$this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $maxbytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum size of the area.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function getAreamaxbytes() {
|
||||
return $this->_options['areamaxbytes'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum size of the area.
|
||||
*
|
||||
* @param int $areamaxbytes size limit
|
||||
*/
|
||||
function setAreamaxbytes($areamaxbytes) {
|
||||
$this->_options['areamaxbytes'] = $areamaxbytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if subdirectoy can be created, else false
|
||||
*
|
||||
@ -237,6 +255,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
$options->accepted_types = $accepted_types;
|
||||
$options->return_types = $this->_options['return_types'];
|
||||
$options->context = $PAGE->context;
|
||||
$options->areamaxbytes = $this->_options['areamaxbytes'];
|
||||
|
||||
$html = $this->_getTabs();
|
||||
$fm = new form_filemanager($options);
|
||||
|
@ -544,6 +544,7 @@ M.core_filepicker.init = function(Y, options) {
|
||||
params['client_id'] = args.client_id;
|
||||
params['itemid'] = this.options.itemid?this.options.itemid:0;
|
||||
params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1;
|
||||
params['areamaxbytes'] = this.options.areamaxbytes ? this.options.areamaxbytes : -1;
|
||||
if (this.options.context && this.options.context.id) {
|
||||
params['ctx_id'] = this.options.context.id;
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes
|
||||
$req_path = optional_param('p', '', PARAM_RAW); // Path
|
||||
$accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW);
|
||||
$saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name
|
||||
$areamaxbytes = optional_param('areamaxbytes', -1, PARAM_INT); // Area max bytes
|
||||
$saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path
|
||||
$search_text = optional_param('s', '', PARAM_CLEANHTML);
|
||||
$linkexternal = optional_param('linkexternal', '', PARAM_ALPHA);
|
||||
@ -301,6 +302,11 @@ switch ($action) {
|
||||
die(json_encode($err));
|
||||
}
|
||||
|
||||
// Check if we exceed the max bytes of the area.
|
||||
if (file_is_draft_area_limit_reached($itemid, $areamaxbytes, filesize($downloadedfile['path']))) {
|
||||
throw new file_exception('maxareabytes');
|
||||
}
|
||||
|
||||
// Check if exceed maxbytes.
|
||||
if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) {
|
||||
throw new file_exception('maxbytes');
|
||||
|
@ -57,9 +57,10 @@ class repository_upload extends repository {
|
||||
$itemid = optional_param('itemid', 0, PARAM_INT);
|
||||
$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
|
||||
$author = optional_param('author', '', PARAM_TEXT);
|
||||
$areamaxbytes = optional_param('areamaxbytes', -1, PARAM_INT);
|
||||
$overwriteexisting = optional_param('overwrite', false, PARAM_BOOL);
|
||||
|
||||
return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting);
|
||||
return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting, $areamaxbytes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,7 +75,8 @@ class repository_upload extends repository {
|
||||
* @param bool $overwriteexisting optional user has asked to overwrite the existing file
|
||||
* @return object containing details of the file uploaded
|
||||
*/
|
||||
public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, $license = null, $author = '', $overwriteexisting = false) {
|
||||
public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0,
|
||||
$license = null, $author = '', $overwriteexisting = false, $areamaxbytes = -1) {
|
||||
global $USER, $CFG;
|
||||
|
||||
if ((is_array($types) and in_array('*', $types)) or $types == '*') {
|
||||
@ -192,6 +194,10 @@ class repository_upload extends repository {
|
||||
$record->itemid = 0;
|
||||
}
|
||||
|
||||
if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) {
|
||||
throw new file_exception('maxareabytes');
|
||||
}
|
||||
|
||||
if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) {
|
||||
throw new file_exception('maxbytes');
|
||||
}
|
||||
|
@ -53,7 +53,8 @@ $PAGE->set_pagetype('user-files');
|
||||
|
||||
$data = new stdClass();
|
||||
$data->returnurl = $returnurl;
|
||||
$options = array('subdirs'=>1, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>-1, 'accepted_types'=>'*');
|
||||
$options = array('subdirs' => 1, 'maxbytes' => $CFG->userquota, 'maxfiles' => -1, 'accepted_types' => '*',
|
||||
'areamaxbytes' => $CFG->userquota);
|
||||
file_prepare_standard_filemanager($data, 'files', $options, $context, 'user', 'private', 0);
|
||||
|
||||
$mform = new user_files_form(null, array('data'=>$data, 'options'=>$options));
|
||||
|
@ -46,8 +46,7 @@ class user_files_form extends moodleform {
|
||||
|
||||
$errors = array();
|
||||
$draftitemid = $data['files_filemanager'];
|
||||
$fileinfo = file_get_draft_area_info($draftitemid);
|
||||
if ($fileinfo['filesize'] > $CFG->userquota) {
|
||||
if (file_is_draft_area_limit_reached($draftitemid, $this->_customdata['options']['areamaxbytes'])) {
|
||||
$errors['files_filemanager'] = get_string('userquotalimit', 'error');
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user