2008-07-31 02:51:28 +00:00
|
|
|
<?php
|
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
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2008-09-08 05:41:45 +00:00
|
|
|
* @author Moodle.com
|
2008-07-31 02:51:28 +00:00
|
|
|
* @version 1.0
|
|
|
|
* @since Moodle 2.0
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-09-11 23:11:24 +00:00
|
|
|
class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
|
2009-08-21 00:15:19 +00:00
|
|
|
public $_helpbutton = '';
|
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
|
|
|
|
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']);
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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-01-15 07:48:38 +00:00
|
|
|
$args = new stdclass;
|
|
|
|
// 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-01-15 07:48:38 +00:00
|
|
|
$args->context = $PAGE->context;
|
|
|
|
|
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);
|
|
|
|
$html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" />';
|
|
|
|
|
|
|
|
$module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker'));
|
|
|
|
$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,
|
|
|
|
));
|
|
|
|
|
|
|
|
// non js file picker
|
|
|
|
$html .= '<noscript>';
|
|
|
|
$html .= "<object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object>";
|
|
|
|
$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
|
|
|
|
2008-09-02 02:59:01 +00:00
|
|
|
function exportValue(&$submitValues, $assoc = false) {
|
2009-05-11 19:35:37 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
// make sure max one file is present and it is not too big
|
2009-08-11 09:40:59 +00:00
|
|
|
if ($draftitemid = $submitValues[$this->_attributes['name']]) {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-02 02:59:01 +00:00
|
|
|
return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
|
|
|
|
}
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|