From 8546def3b6c1d8229c1db83dcf693dc59bbe7f7b Mon Sep 17 00:00:00 2001 From: skodak Date: Sat, 20 Sep 2008 20:56:21 +0000 Subject: [PATCH] MDL-16596 basic areafiles formslib element --- draftfile.php | 7 +- files/areafiles.php | 209 ++++++++++++++++++++++++++++++ lib/file/file_browser.php | 20 +-- lib/file/file_info_coursefile.php | 2 +- lib/file/file_info_stored.php | 25 ++-- lib/filelib.php | 58 ++++++--- lib/form/areafiles.php | 101 +++++++++++++++ lib/formslib.php | 1 + mod/scorm/lib.php | 2 +- 9 files changed, 389 insertions(+), 36 deletions(-) create mode 100644 files/areafiles.php create mode 100644 lib/form/areafiles.php diff --git a/draftfile.php b/draftfile.php index 70df735ad66..7ff20c380e1 100644 --- a/draftfile.php +++ b/draftfile.php @@ -42,8 +42,11 @@ } switch ($filearea) { - case 'user_draft' : $itemid = (int)array_shift($args); break; - default: send_file_not_found(); + case 'user_draft': + $itemid = (int)array_shift($args); + break; + default: + send_file_not_found(); } $relativepath = '/'.implode('/', $args); diff --git a/files/areafiles.php b/files/areafiles.php new file mode 100644 index 00000000000..90fced9666a --- /dev/null +++ b/files/areafiles.php @@ -0,0 +1,209 @@ +libdir.'/filelib.php'); + + $contextid = required_param('contextid', PARAM_INT); + $filearea = required_param('filearea', PARAM_ALPHAEXT); + $itemid = required_param('itemid', PARAM_INT); + + $filepath = optional_param('filepath', '/', PARAM_PATH); + $filename = optional_param('filename', '', PARAM_FILE); + + $newdirname = optional_param('newdirname', '', PARAM_FILE); + $delete = optional_param('delete', 0, PARAM_BOOL); + + if (!$context = get_context_instance_by_id($contextid)) { + print_error('invalidcontext'); + } + + require_login(); + if (isguestuser()) { + print_error('noguest'); + } + + $browser = get_file_browser(); + + if (!$area_info = $browser->get_file_info($context, $filearea, $itemid, '/', null)) { + error('Can not browse this area!'); // TODO: localise + } + + if ($filename === '') { + $filename = null; + } + + $error = ''; + + if ($filepath === '/' and is_null($filename)) { + $file_info = $area_info; + } else { + if (!$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) { + error('Can not browse this directory!'); // TODO: localise + } + } + +/// process actions + if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) { + if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) { + $params = $newdir_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("areafiles.php?$params"); + } else { + $error = "Could not create new dir"; // TODO: localise + } + } + + if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { + $file = $_FILES['newfile']; + $newfilename = clean_param($file['name'], PARAM_FILE); + if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { + try { + if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) { + $params = $file_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("areafiles.php?$params"); + + } else { + $error = "Could not create upload file"; // TODO: localise + } + } catch (file_exception $e) { + $error = "Exception: Could not create upload file"; // TODO: localise + } + } + } + + if ($file_info and $delete) { + if (!data_submitted() or !confirm_sesskey()) { + print_header(); + notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name()); + $parent_info = $file_info->get_parent(); + + $optionsno = $parent_info->get_params(); + $optionsyes = $file_info->get_params(); + $optionsyes['delete'] = 1; + $optionsyes['sesskey'] = sesskey(); + + notice_yesno (get_string('deletecheckfiles'), 'areafiles.php', 'areafiles.php', $optionsyes, $optionsno, 'post', 'get'); + print_footer('empty'); + die; + } + + if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) { + if (!$file_info->delete()) { + $error = "Could not delete file!"; // TODO: localise + } + $params = $parent_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("areafiles.php?$params", $error); + } + } + + print_header(); + + if ($error !== '') { + notify($error); + } + + echo '
'; + displaydir($file_info); + echo '
'; + + if ($file_info and $file_info->is_directory() and $file_info->is_writable()) { + + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + } + + + print_footer('empty'); + +/// FILE FUNCTIONS /////////////////////////////////////////////////////////// + +function displaydir($file_info) { + global $CFG; + + $children = $file_info->get_children(); + $parent_info = $file_info->get_parent(); + + $strname = get_string('name'); + $strsize = get_string('size'); + $strmodified = get_string('modified'); + $strfolder = get_string('folder'); + $strfile = get_string('file'); + $strdownload = get_string('download'); + $strdelete = get_string('delete'); + $straction = get_string('action'); + + $parentwritable = $file_info->is_writable(); + + $directory = $file_info->get_params(); + $directory = $directory['filepath']; + + if ($parent_info and $directory !== '/') { + $params = $parent_info->get_params_rawencoded(); + $params = implode('&', $params); + + echo '
'; + echo ' '.get_string('parentfolder').''; + echo '
'; + } + + if ($children) { + foreach ($children as $child_info) { + $filename = $child_info->get_visible_name(); + $filesize = $child_info->get_filesize(); + $filesize = $filesize ? display_size($filesize) : ''; + + $mimetype = $child_info->get_mimetype(); + + $params = $child_info->get_params_rawencoded(); + $params = implode('&', $params); + + if ($child_info->is_directory()) { + + echo '
'; + echo "pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($filename).""; + if ($parentwritable) { + echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />"; + } + echo '
'; + + } else { + + $icon = mimeinfo_from_type('icon', $mimetype); + echo '
'; + echo "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize)"; + if ($viewurl = $child_info->get_url()) { + echo " ".link_to_popup_window ($viewurl, "display", + "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strfile\" /> ", + 480, 640, get_string('viewfileinpopup'), null, true); + } + if ($parentwritable) { + echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />";; + } + echo '
'; + } + } + } +} + +?> diff --git a/lib/file/file_browser.php b/lib/file/file_browser.php index ce0d87a0e75..6cbd6ef387e 100644 --- a/lib/file/file_browser.php +++ b/lib/file/file_browser.php @@ -82,7 +82,7 @@ class file_browser { } } $urlbase = $CFG->wwwroot.'/userfile.php'; - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false); } else if ($filearea == 'user_profile') { if (is_null($itemid)) { @@ -112,7 +112,7 @@ class file_browser { } } $urlbase = $CFG->wwwroot.'/userfile.php'; - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false); } else if ($filearea == 'user_draft') { @@ -126,6 +126,10 @@ class file_browser { return null; } $urlbase = $CFG->wwwroot.'/draftfile.php'; + + $filepath = is_null($filepath) ? '/' : $filepath; + $filename = is_null($filename) ? '.' : $filename; + if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { if ($filepath === '/' and $filename === '.') { $storedfile = new virtual_root_file($context->id, $filearea, $itemid); @@ -134,7 +138,7 @@ class file_browser { return null; } } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true); } } @@ -173,7 +177,7 @@ class file_browser { return null; } } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false); } } @@ -214,7 +218,7 @@ class file_browser { return null; } } - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false); } else if ($filearea == 'course_backup') { if (!has_capability('moodle/site:backup', $context) and !has_capability('moodle/site:restore', $context)) { @@ -233,7 +237,7 @@ class file_browser { $downloadable = has_capability('moodle/site:backupdownload', $context); $uploadable = has_capability('moodle/site:backupupload', $context); - return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable); + return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false); } else if ($filearea == 'course_content') { if (!has_capability('moodle/course:managefiles', $context)) { @@ -307,7 +311,7 @@ class file_browser { return null; } } - return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true); + return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false); } else { $fileinfofunction = $modname.'_get_file_info'; @@ -329,7 +333,7 @@ class file_browser { $storedfiles = $fs->get_directory_files($context->id, $filearea, $itemid, $filepath, false, true, "filepath, filename"); foreach ($storedfiles as $file) { - $result[] = new file_info_stored($this, $context, $file, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess); + $result[] = new file_info_stored($this, $context, $file, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, false); } return $result; diff --git a/lib/file/file_info_coursefile.php b/lib/file/file_info_coursefile.php index 9b3e7928f8c..1f201e84120 100644 --- a/lib/file/file_info_coursefile.php +++ b/lib/file/file_info_coursefile.php @@ -4,7 +4,7 @@ class file_info_coursefile extends file_info_stored { public function __construct($browser, $context, $storedfile) { global $CFG; $urlbase = $CFG->wwwroot.'/file.php'; - parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true); + parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false); } public function get_url($forcedownload=false, $https=false) { diff --git a/lib/file/file_info_stored.php b/lib/file/file_info_stored.php index d226d5c27ae..f5df2f108be 100644 --- a/lib/file/file_info_stored.php +++ b/lib/file/file_info_stored.php @@ -7,8 +7,9 @@ class file_info_stored extends file_info { protected $itemidused; protected $readaccess; protected $writeaccess; + protected $areaonly; - public function __construct($browser, $context, $storedfile, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess) { + public function __construct($browser, $context, $storedfile, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) { parent::__construct($browser, $context); $this->lf = $storedfile; @@ -17,6 +18,7 @@ class file_info_stored extends file_info { $this->itemidused = $itemidused; $this->readaccess = $readaccess; $this->writeaccess = $writeaccess; + $this->areaonly = $areaonly; } public function get_params() { @@ -39,7 +41,9 @@ class file_info_stored extends file_info { $dir = explode('/', $dir); $dir = array_pop($dir); if ($dir === '') { - if ($this->itemidused) { + if ($this->areaonly) { + return $this->areavisiblename; + } else if ($this->itemidused) { return $this->lf->get_itemid(); } else { return $this->areavisiblename; @@ -110,22 +114,25 @@ class file_info_stored extends file_info { return array(); } return $this->browser->build_stored_file_children($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), - $this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess); + $this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess, + $this->areaonly); } public function get_parent() { - if (!$this->lf->is_directory()) { - return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.'); - } - - if ($this->lf->get_filepath() === '/') { - if ($this->itemidused) { + if ($this->lf->get_filepath() === '/' and $this->lf->is_directory()) { + if ($this->areaonly) { + return null; + } else if ($this->itemidused) { return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid()); } else { return $this->browser->get_file_info($this->context, $this->lf->get_filearea()); } } + if (!$this->lf->is_directory()) { + return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.'); + } + $filepath = $this->lf->get_filepath(); $filepath = trim($filepath, '/'); $dirs = explode('/', $filepath); diff --git a/lib/filelib.php b/lib/filelib.php index b0b96df8ed4..52522f52b50 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -58,19 +58,60 @@ function get_file_url($path, $options=null, $type='coursefile') { return $ffurl; } +/** + * Returns empty user upload draft area information + * @return array with area info + */ +function get_new_draftarea() { + global $DB, $USER; + + if (isguestuser() or !isloggedin()) { + print_error('noguest'); + } + + $contextid = get_context_instance(CONTEXT_USER, $USER->id)->id; + $filearea = 'user_draft'; + + $fs = get_file_storage(); + $draftitemid = rand(1, 999999999); + while ($files = $fs->get_area_files($contextid, $filearea, $draftitemid)) { + $draftitemid = rand(1, 999999999); + } + + return array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$draftitemid); +} + /** * Converts absolute links in text and moves draft files. * @param int $draftitemid - * @param string $text usually html text with embedded links to draft area * @param int $contextid * @param string $filearea * @param int $itemid + * @param string $text usually html text with embedded links to draft area * @param boolean $https force https * @return string text with relative links starting with @@PLUGINFILE@@ */ -function file_convert_draftarea($text, $draftitemid, $contextid, $filearea, $itemid, $https=false) { +function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $text=null, $https=false) { global $CFG, $USER; + /// move draft files first + $usercontext = get_context_instance(CONTEXT_USER, $USER->id); + + $fs = get_file_storage(); + if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', 'false')) { + $file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid); + foreach ($files as $file) { + $fs->create_file_from_stored($file_record, $file); + $file->delete(); + } + } + + if (is_null($text)) { + return null; + } + + /// relink embedded files if text submitted - no absolute links allowed! + if ($CFG->slasharguments) { $draftbase = "$CFG->wwwroot/draftfile.php/user_draft/$draftitemid/"; } else { @@ -81,21 +122,8 @@ function file_convert_draftarea($text, $draftitemid, $contextid, $filearea, $ite $draftbase = str_replace('http://', 'https://', $draftbase); } - // replace absolute links $text = str_ireplace($draftbase, '@@PLUGINFILE@@/'); - $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - - // move draft files - $fs = get_file_storage(); - if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', 'false')) { - $file_record = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid); - foreach ($files as $file) { - $fs->create_file_from_stored($file_record, $file); - $file->delete(); - } - } - return $text; } diff --git a/lib/form/areafiles.php b/lib/form/areafiles.php new file mode 100644 index 00000000000..28454ee98fc --- /dev/null +++ b/lib/form/areafiles.php @@ -0,0 +1,101 @@ +updateAttributes(array('name'=>$name)); + } + + function getName() { + return $this->getAttribute('name'); + } + + function setValue($value) { + if (!is_array($value)) { + $this->_areainfo = array(); + } else { + $this->_areainfo = $value; + } + } + + function getValue() { + return $this->_areainfo; + } + + function setHelpButton($helpbuttonargs, $function='helpbutton') { + if (!is_array($helpbuttonargs)) { + $helpbuttonargs = array($helpbuttonargs); + } else { + $helpbuttonargs = $helpbuttonargs; + } + //we do this to to return html instead of printing it + //without having to specify it in every call to make a button. + if ('helpbutton' == $function){ + $defaultargs = array('', '', 'moodle', true, false, '', true); + $helpbuttonargs = $helpbuttonargs + $defaultargs ; + } + $this->_helpbutton=call_user_func_array($function, $helpbuttonargs); + } + + function getHelpButton() { + return $this->_helpbutton; + } + + function getElementTemplateType() { + if ($this->_flagFrozen){ + return 'nodisplay'; + } else { + return 'default'; + } + } + + function toHtml() { + global $CFG; + + if ($this->_flagFrozen) { + return $this->getFrozenHtml(); + } + + $id = $this->_attributes['id']; + $elname = $this->_attributes['name']; + + $value = $this->getValue(); + + if (empty($value['contextid'])) { + // no existing area info provided - let's use fresh new draft area + require_once("$CFG->libdir/filelib.php"); + $this->setValue(get_new_draftarea()); + $value = $this->getValue(); + } + + $contextid = $value['contextid']; + $filearea = $value['filearea']; + $itemid = $value['itemid']; + + $str = ''; + $str .= ''; + $str .= ''; + + $url = "$CFG->wwwroot/files/areafiles.php?contextid=$contextid&filearea=$filearea&itemid=$itemid"; + + $str .= 'Error'; // TODO: localise, fix styles, etc. + + return $str; + } + + function exportValue(&$submitValues, $assoc = false) { + return array( + $this->_attributes['name']['contexid'] => $submitValues[$this->_attributes['name']]['contextid'], + $this->_attributes['name']['filearea'] => $submitValues[$this->_attributes['name']]['filearea'], + $this->_attributes['name']['itemid'] => $submitValues[$this->_attributes['name']]['itemid'], + ); + } +} diff --git a/lib/formslib.php b/lib/formslib.php index 5d19486e92d..a7be9935168 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -2063,6 +2063,7 @@ $GLOBALS['_HTML_QuickForm_default_renderer'] =& new MoodleQuickForm_Renderer(); MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox'); MoodleQuickForm::registerElementType('file', "$CFG->libdir/form/file.php", 'MoodleQuickForm_file'); +MoodleQuickForm::registerElementType('areafiles', "$CFG->libdir/form/areafiles.php", 'MoodleQuickForm_areafiles'); MoodleQuickForm::registerElementType('filepicker', "$CFG->libdir/form/filepicker.php", 'MoodleQuickForm_filepicker'); MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group'); MoodleQuickForm::registerElementType('password', "$CFG->libdir/form/password.php", 'MoodleQuickForm_password'); diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 5042c38930b..2ca0b9855d8 100755 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -738,7 +738,7 @@ function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea return parent::get_visible_name(); } } - return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false); + return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false); } else if ($filearea === 'scorm_package') { $filepath = is_null($filepath) ? '/' : $filepath;