. /** * File manager * * @package moodlecore * @subpackage file * @copyright 1999 onwards Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ global $CFG; require_once('HTML/QuickForm/element.php'); require_once($CFG->dirroot.'/lib/filelib.php'); class MoodleQuickForm_filemanager extends HTML_QuickForm_element { public $_helpbutton = ''; protected $_options = array('mainfile'=>'', 'subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>-1, 'filetypes'=>'*', 'returnvalue'=>'*'); function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) { global $CFG, $PAGE; require_once("$CFG->dirroot/repository/lib.php"); // has to require these js files before head $PAGE->requires->yui_lib('menu'); $PAGE->requires->yui_lib('connection'); $PAGE->requires->yui_lib('json'); $options = (array)$options; foreach ($options as $name=>$value) { if (array_key_exists($name, $this->_options)) { $this->_options[$name] = $value; } } if (!empty($options['maxbytes'])) { $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); } // XXX: hide element lable $elementLabel = ''; parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); repository_head_setup(); } function setName($name) { $this->updateAttributes(array('name'=>$name)); } function getName() { return $this->getAttribute('name'); } function setValue($value) { $this->updateAttributes(array('value'=>$value)); } function getValue() { return $this->getAttribute('value'); } function getMaxbytes() { return $this->_options['maxbytes']; } function setMaxbytes($maxbytes) { global $CFG; $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes); } function getSubdirs() { return $this->_options['subdirs']; } function setSubdirs($allow) { $this->_options['subdirs'] = $allow; } function getMaxfiles() { return $this->_options['maxfiles']; } function setMaxfiles($num) { $this->_options['maxfiles'] = $num; } 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, $USER, $COURSE, $PAGE, $OUTPUT; require_once("$CFG->dirroot/repository/lib.php"); // security - never ever allow guest/not logged in user to upload anything or use this element! if (isguestuser() or !isloggedin()) { print_error('noguest'); } if ($this->_flagFrozen) { return $this->getFrozenHtml(); } $id = $this->_attributes['id']; $elname = $this->_attributes['name']; $subdirs = $this->_options['subdirs']; $maxbytes = $this->_options['maxbytes']; $draftitemid = $this->getValue(); // language strings $straddfile = get_string('add', 'repository') . '...'; $strmakedir = get_string('makeafolder', 'moodle'); $strdownload = get_string('downloadfolder', 'repository'); $PAGE->requires->string_for_js('loading', 'repository'); $PAGE->requires->string_for_js('nomorefiles', 'repository'); $PAGE->requires->string_for_js('confirmdeletefile', 'repository'); $PAGE->requires->string_for_js('add', 'repository'); $PAGE->requires->string_for_js('accessiblefilepicker', 'repository'); $PAGE->requires->string_for_js('move', 'moodle'); $PAGE->requires->string_for_js('cancel', 'moodle'); $PAGE->requires->string_for_js('download', 'moodle'); $PAGE->requires->string_for_js('ok', 'moodle'); $PAGE->requires->string_for_js('emptylist', 'repository'); $PAGE->requires->string_for_js('entername', 'repository'); $PAGE->requires->string_for_js('enternewname', 'repository'); $PAGE->requires->string_for_js('zip', 'editor'); $PAGE->requires->string_for_js('unzip', 'moodle'); $PAGE->requires->string_for_js('rename', 'moodle'); $PAGE->requires->string_for_js('delete', 'moodle'); $PAGE->requires->string_for_js('setmainfile', 'resource'); $PAGE->requires->string_for_js('cannotdeletefile', 'error'); $PAGE->requires->string_for_js('confirmdeletefile', 'repository'); $PAGE->requires->string_for_js('nopathselected', 'repository'); $PAGE->requires->string_for_js('popupblockeddownload', 'repository'); if (empty($draftitemid)) { // no existing area info provided - let's use fresh new draft area require_once("$CFG->libdir/filelib.php"); $this->setValue(file_get_unused_draft_itemid()); $draftitemid = $this->getValue(); } if ($COURSE->id == SITEID) { $context = get_context_instance(CONTEXT_SYSTEM); } else { $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); } $client_id = uniqid(); // Generate file picker $repojs = repository_get_client($context, $client_id, $this->_options['filetypes'], $this->_options['returnvalue']); $result = new stdclass; $options = file_get_draft_area_files($draftitemid); $options->mainfile = $this->_options['mainfile']; $options->maxbytes = $this->getMaxbytes(); $options->maxfiles = $this->getMaxfiles(); $options->client_id = $client_id; $options->itemid = $draftitemid; $options->target = $id; $html = $this->_getTabs(); $html .= $repojs; if (empty($CFG->filemanagerjsloaded)) { $PAGE->requires->js('lib/form/filemanager.js'); $CFG->filemanagerjsloaded = true; // print html template $html .= <<