2008-07-31 02:51:28 +00:00
|
|
|
<?php
|
2012-01-06 16:38:06 +08:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filepicker form element
|
|
|
|
*
|
|
|
|
* Contains HTML class for a single filepicker form element
|
|
|
|
*
|
|
|
|
* @package core_form
|
|
|
|
* @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2009-08-11 09:40:59 +00:00
|
|
|
|
|
|
|
global $CFG;
|
2008-07-31 02:51:28 +00:00
|
|
|
|
|
|
|
require_once("HTML/QuickForm/button.php");
|
2009-08-11 09:40:59 +00:00
|
|
|
require_once($CFG->dirroot.'/repository/lib.php');
|
2008-07-31 02:51:28 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-06 16:38:06 +08:00
|
|
|
* Filepicker form element
|
|
|
|
*
|
2008-09-08 05:41:45 +00:00
|
|
|
* HTML class for a single filepicker element (based on button)
|
2008-07-31 02:51:28 +00:00
|
|
|
*
|
2012-01-06 16:38:06 +08:00
|
|
|
* @package core_form
|
|
|
|
* @category form
|
|
|
|
* @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
|
2012-02-21 12:39:35 +13:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-07-31 02:51:28 +00:00
|
|
|
*/
|
2008-09-11 23:11:24 +00:00
|
|
|
class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
|
2012-01-06 16:38:06 +08:00
|
|
|
/** @var string html for help button, if empty then no help will icon will be dispalyed. */
|
2009-08-21 00:15:19 +00:00
|
|
|
public $_helpbutton = '';
|
2012-01-06 16:38:06 +08:00
|
|
|
|
|
|
|
/** @var array options provided to initalize filemanager */
|
2010-01-15 07:48:38 +00:00
|
|
|
protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
|
2009-05-11 19:35:37 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $elementName (optional) name of the filepicker
|
|
|
|
* @param string $elementLabel (optional) filepicker label
|
|
|
|
* @param array $attributes (optional) Either a typical HTML attribute string
|
|
|
|
* or an associative array
|
|
|
|
* @param array $options set of options to initalize filepicker
|
|
|
|
*/
|
2009-05-11 18:49:04 +00:00
|
|
|
function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
|
|
|
|
global $CFG;
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2009-05-11 18:49:04 +00:00
|
|
|
$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']);
|
|
|
|
}
|
2011-09-26 11:02:44 +08:00
|
|
|
$this->_type = 'filepicker';
|
2008-09-11 23:11:24 +00:00
|
|
|
parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
|
2008-08-27 07:57:54 +00:00
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* Sets help button for filepicker
|
2012-02-21 12:39:35 +13:00
|
|
|
*
|
2012-01-06 16:38:06 +08:00
|
|
|
* @param mixed $helpbuttonargs arguments to create help button
|
|
|
|
* @param string $function name of the callback function
|
|
|
|
* @deprecated since Moodle 2.0. Please do not call this function any more.
|
|
|
|
* @todo MDL-31047 this api will be removed.
|
|
|
|
* @see MoodleQuickForm::setHelpButton()
|
|
|
|
*/
|
2008-09-11 23:11:24 +00:00
|
|
|
function setHelpButton($helpbuttonargs, $function='helpbutton') {
|
2009-12-30 15:19:55 +00:00
|
|
|
debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* Returns html for help button.
|
|
|
|
*
|
|
|
|
* @return string html for help button
|
|
|
|
*/
|
2008-09-11 23:11:24 +00:00
|
|
|
function getHelpButton() {
|
2008-07-31 02:51:28 +00:00
|
|
|
return $this->_helpbutton;
|
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* Returns type of filepicker element
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-09-11 23:11:24 +00:00
|
|
|
function getElementTemplateType() {
|
2008-07-31 02:51:28 +00:00
|
|
|
if ($this->_flagFrozen){
|
|
|
|
return 'nodisplay';
|
|
|
|
} else {
|
|
|
|
return 'default';
|
|
|
|
}
|
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* Returns HTML for filepicker form element.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-07-31 02:51:28 +00:00
|
|
|
function toHtml() {
|
2010-05-24 07:55:57 +00:00
|
|
|
global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
|
|
|
|
$id = $this->_attributes['id'];
|
|
|
|
$elname = $this->_attributes['name'];
|
2009-08-14 08:22:06 +00:00
|
|
|
|
2008-07-31 02:51:28 +00:00
|
|
|
if ($this->_flagFrozen) {
|
|
|
|
return $this->getFrozenHtml();
|
2008-09-11 23:11:24 +00:00
|
|
|
}
|
2010-05-24 07:55:57 +00:00
|
|
|
if (!$draftitemid = (int)$this->getValue()) {
|
2009-09-11 03:24:51 +00:00
|
|
|
// no existing area info provided - let's use fresh new draft area
|
2010-02-06 19:43:35 +00:00
|
|
|
$draftitemid = file_get_unused_draft_itemid();
|
|
|
|
$this->setValue($draftitemid);
|
2008-09-11 23:11:24 +00:00
|
|
|
}
|
2010-05-24 07:55:57 +00:00
|
|
|
|
2008-09-11 23:11:24 +00:00
|
|
|
if ($COURSE->id == SITEID) {
|
|
|
|
$context = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
} else {
|
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
|
|
|
|
}
|
2010-01-15 07:48:38 +00:00
|
|
|
|
2009-04-20 08:53:21 +00:00
|
|
|
$client_id = uniqid();
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2010-09-21 08:54:01 +00:00
|
|
|
$args = new stdClass();
|
2010-01-15 07:48:38 +00:00
|
|
|
// need these three to filter repositories list
|
|
|
|
$args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
|
|
|
|
$args->return_types = FILE_INTERNAL;
|
2010-05-24 07:55:57 +00:00
|
|
|
$args->itemid = $draftitemid;
|
2010-09-30 03:36:38 +00:00
|
|
|
$args->maxbytes = $this->_options['maxbytes'];
|
2010-01-15 07:48:38 +00:00
|
|
|
$args->context = $PAGE->context;
|
2011-04-12 14:30:13 +12:00
|
|
|
$args->buttonname = $elname.'choose';
|
2011-09-26 15:35:27 +08:00
|
|
|
$args->elementname = $elname;
|
2010-01-15 07:48:38 +00:00
|
|
|
|
2010-05-31 09:41:46 +00:00
|
|
|
$html = $this->_getTabs();
|
2010-05-24 07:55:57 +00:00
|
|
|
$fp = new file_picker($args);
|
|
|
|
$options = $fp->options;
|
2010-07-19 17:44:23 +00:00
|
|
|
$options->context = $PAGE->context;
|
2010-05-31 09:41:46 +00:00
|
|
|
$html .= $OUTPUT->render($fp);
|
2011-04-12 14:30:13 +12:00
|
|
|
$html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
|
2010-05-31 09:41:46 +00:00
|
|
|
|
2011-11-08 20:05:19 +00:00
|
|
|
$module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload'));
|
2010-05-31 09:41:46 +00:00
|
|
|
$PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
|
|
|
|
|
2010-08-04 03:57:10 +00:00
|
|
|
$nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
|
|
|
|
'env'=>'filepicker',
|
|
|
|
'action'=>'browse',
|
|
|
|
'itemid'=>$draftitemid,
|
|
|
|
'subdirs'=>0,
|
|
|
|
'maxbytes'=>$options->maxbytes,
|
|
|
|
'maxfiles'=>1,
|
|
|
|
'ctx_id'=>$PAGE->context->id,
|
|
|
|
'course'=>$PAGE->course->id,
|
2010-08-13 06:53:28 +00:00
|
|
|
'sesskey'=>sesskey(),
|
2010-08-04 03:57:10 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// non js file picker
|
|
|
|
$html .= '<noscript>';
|
2010-10-13 17:35:21 +00:00
|
|
|
$html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
|
2010-08-04 03:57:10 +00:00
|
|
|
$html .= '</noscript>';
|
|
|
|
|
2010-05-31 09:41:46 +00:00
|
|
|
return $html;
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2012-01-06 16:38:06 +08:00
|
|
|
/**
|
|
|
|
* export uploaded file
|
|
|
|
*
|
|
|
|
* @param array $submitValues values submitted.
|
|
|
|
* @param bool $assoc specifies if returned array is associative
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-02 02:59:01 +00:00
|
|
|
function exportValue(&$submitValues, $assoc = false) {
|
2009-05-11 19:35:37 +00:00
|
|
|
global $USER;
|
|
|
|
|
2011-06-29 18:39:15 +07:00
|
|
|
$draftitemid = $this->_findValue($submitValues);
|
|
|
|
if (null === $draftitemid) {
|
|
|
|
$draftitemid = $this->getValue();
|
|
|
|
}
|
|
|
|
|
2009-05-11 19:35:37 +00:00
|
|
|
// make sure max one file is present and it is not too big
|
2011-06-29 18:39:15 +07:00
|
|
|
if (!is_null($draftitemid)) {
|
2009-05-11 19:35:37 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
|
2010-07-03 13:37:13 +00:00
|
|
|
if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
|
2009-05-11 19:35:37 +00:00
|
|
|
$file = array_shift($files);
|
|
|
|
if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
|
|
|
|
// bad luck, somebody tries to sneak in oversized file
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
foreach ($files as $file) {
|
|
|
|
// only one file expected
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 14:34:10 +07:00
|
|
|
|
|
|
|
return $this->_prepareValue($draftitemid, true);
|
2008-09-02 02:59:01 +00:00
|
|
|
}
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|