$newdirname = optional_param('newdirname', '', PARAM_FILE);
$delete = optional_param('delete', '', PARAM_PATH);
$subdirs = optional_param('subdirs', 0, PARAM_BOOL);
+ $maxbytes = optional_param('maxbytes', 0, PARAM_INT);
require_login();
if (isguestuser()) {
print_error('invalidcontext');
}
+ $notice = '';
+
$contextid = $context->id;
$filearea = 'user_draft';
$files = $fs->get_directory_files($context->id, 'user_draft', $itemid, $directory->get_filepath());
$parent = $directory->get_parent_directory();
+ $totalbytes = 0;
+ foreach ($files as $hash=>$file) {
+ if (!$subdirs and $file->get_filepath() !== '/') {
+ unset($files[$hash]);
+ continue;
+ }
+ $totalbytes += $file->get_filesize();
+ }
+
/// process actions
if ($newdirname !== '' and data_submitted() and confirm_sesskey()) {
$newdirname = $directory->get_filepath().$newdirname.'/';
$fs->create_directory($contextid, $filearea, $itemid, $newdirname, $USER->id);
- redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($newdirname).'&subdirs='.$subdirs);
+ redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($newdirname).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
}
if (isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
- $file = $_FILES['newfile'];
- $newfilename = clean_param($file['name'], PARAM_FILE);
- // TODO: some better error handling or use some upload manager
- if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
- if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) {
- $existingfile->delete();
+ if (!empty($_FILES['newfile']['error'])) {
+ $notice = file_get_upload_error($_FILES['newfile']['error']);
+ } else {
+ $file = $_FILES['newfile'];
+ $newfilename = clean_param($file['name'], PARAM_FILE);
+ // TODO: some better error handling or use some upload manager
+ if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
+ if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) {
+ $existingfile->delete();
+ }
+ $filerecord = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath,
+ 'filename'=>$newfilename, 'userid'=>$USER->id);
+ $newfile = $fs->create_file_from_pathname($filerecord, $_FILES['newfile']['tmp_name']);
+ redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
}
- $filerecord = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath,
- 'filename'=>$newfilename, 'userid'=>$USER->id);
- $newfile = $fs->create_file_from_pathname($filerecord, $_FILES['newfile']['tmp_name']);
- redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs);
}
}
$isdir = $file->is_directory();
$file->delete();
if ($isdir) {
- redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath()).'&subdirs='.$subdirs);
+ redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath()).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
} else {
- redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs);
+ redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
}
}
}
print_header();
+ if ($notice !== '') {
+ notify($notice);
+ }
+
echo '<div class="areafiles">';
$strfolder = get_string('folder');
if ($parent) {
echo '<div class="folder">';
- echo '<a href="draftfiles.php?itemid='.$itemid.'&filepath='.$parent->get_filepath().'&subdirs='.$subdirs.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
+ echo '<a href="draftfiles.php?itemid='.$itemid.'&filepath='.$parent->get_filepath().'&subdirs='.$subdirs.'&maxbytes='.$maxbytes.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
echo '</div>';
}
$dirname = explode('/', trim($filepath, '/'));
$dirname = array_pop($dirname);
echo '<div class="folder">';
- echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&subdirs=$subdirs\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($dirname)."</a> ";
- echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
+ echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($dirname)."</a> ";
+ echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
echo '</div>';
}
$viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false);
echo '<div class="file">';
echo "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize)</a> ";
- echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
+ echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
echo '</div>';
}
}
echo '</div>';
- echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><div>';
- echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
- echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
- echo '<input type="hidden" name="subdirs" value="'.$subdirs.'" />';
- echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
- echo '<input name="newfile" type="file" />';
- echo '<input type="submit" value="'.get_string('uploadafile').'" />';
- echo '</div></form>';
+ if ($maxbytes == 0 or $maxbytes > $totalbytes) {
+ echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><div>';
+ if ($maxbytes) {
+ echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxbytes-$totalbytes).'" />';
+ }
+ echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
+ echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
+ echo '<input type="hidden" name="subdirs" value="'.$subdirs.'" />';
+ echo '<input type="hidden" name="maxbytes" value="'.$maxbytes.'" />';
+ echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
+ echo '<input name="newfile" type="file" />';
+ echo '<input type="submit" value="'.get_string('uploadafile').'" />';
+ if ($maxbytes) {
+ echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes-$totalbytes))).')';
+ } else {
+ echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes))).')';
+ }
+ echo '</div></form>';
+ } else {
+ //TODO: notify upload limit reached here
+ echo get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes)));
+ }
if ($subdirs) {
echo '<form action="draftfiles.php" method="post"><div>';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
echo '<input type="hidden" name="subdirs" value="'.$subdirs.'" />';
+ echo '<input type="hidden" name="maxbytes" value="'.$maxbytes.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" name="newdirname" value="" />';
echo '<input type="submit" value="'.get_string('makeafolder').'" />';
protected $_options = array('subdirs'=>0, 'maxbytes'=>0);
function MoodleQuickForm_areafiles($elementName=null, $elementLabel=null, $options=null) {
+ global $CFG;
+
if (!empty($options['subdirs'])) {
$this->_options['subdirs'] = 1;
}
if (!empty($options['maxbytes'])) {
- $this->_options['maxbytes'] = $options['maxbytes'];
+ $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
}
parent::HTML_QuickForm_element($elementName, $elementLabel);
}
}
function setMaxbytes($maxbytes) {
- $this->_options['maxbytes'] = $maxbytes;
+ global $CFG;
+ $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
}
function getSubdirs() {
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
$subdirs = $this->_options['subdirs'];
+ $maxbytes = $this->_options['maxbytes'];
$draftitemid = $this->getValue();
if (empty($draftitemid)) {
$draftitemid = $this->getValue();
}
- $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&subdirs=$subdirs";
+ $editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&subdirs=$subdirs&maxbytes=$maxbytes";
$str = $this->_getTabs();
$str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
require_once 'HTML/QuickForm/Renderer/Tableless.php';
-require_once $CFG->libdir.'/uploadlib.php'; // TODO: remove
+require_once $CFG->libdir.'/filelib.php';
/**
* Callback called when PEAR throws an error
continue;
}
- if ($file['error'] > 0) {
- switch ($file['error']) {
- case 1: // UPLOAD_ERR_INI_SIZE
- $errmessage = get_string('uploadserverlimit');
- break;
-
- case 2: // UPLOAD_ERR_FORM_SIZE
- $errmessage = get_string('uploadformlimit');
- break;
-
- case 3: // UPLOAD_ERR_PARTIAL
- $errmessage = get_string('uploadpartialfile');
- break;
-
- case 4: // UPLOAD_ERR_NO_FILE
- $errmessage = get_string('uploadnofilefound');
- break;
-
- // Note: there is no error with a value of 5
-
- case 6: // UPLOAD_ERR_NO_TMP_DIR
- $errmessage = get_string('uploadnotempdir');
- break;
-
- case 7: // UPLOAD_ERR_CANT_WRITE
- $errmessage = get_string('uploadcantwrite');
- break;
-
- case 8: // UPLOAD_ERR_EXTENSION
- $errmessage = get_string('uploadextension');
- break;
-
- default:
- $errmessage = get_string('uploadproblem', $file['name']);
- }
- $errors[$elname] = $errmessage;
+ if (!empty($file['error'])) {
+ $errors[$elname] = file_get_upload_error($file['error']);
unset($_FILES[$elname]);
continue;
}