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-05-11 18:49:04 +00:00
|
|
|
protected $_helpbutton = '';
|
|
|
|
protected $_options = array('maxbytes'=>0, 'filetypes'=>'*', 'returnvalue'=>'*');
|
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;
|
2009-06-24 22:34:29 +00:00
|
|
|
require_once("$CFG->dirroot/repository/lib.php");
|
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);
|
2009-06-24 22:34:29 +00:00
|
|
|
|
|
|
|
repository_head_setup();
|
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') {
|
|
|
|
if (!is_array($helpbuttonargs)) {
|
|
|
|
$helpbuttonargs = array($helpbuttonargs);
|
|
|
|
} else {
|
|
|
|
$helpbuttonargs = $helpbuttonargs;
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|
|
|
|
//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){
|
2008-09-11 23:11:24 +00:00
|
|
|
$defaultargs = array('', '', 'moodle', true, false, '', true);
|
|
|
|
$helpbuttonargs = $helpbuttonargs + $defaultargs ;
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|
|
|
|
$this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
|
|
|
|
}
|
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() {
|
2009-07-07 08:49:00 +00:00
|
|
|
global $CFG, $COURSE, $USER, $PAGE;
|
2008-09-11 23:11:24 +00:00
|
|
|
|
2008-07-31 02:51:28 +00:00
|
|
|
if ($this->_flagFrozen) {
|
|
|
|
return $this->getFrozenHtml();
|
2008-09-11 23:11:24 +00:00
|
|
|
}
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2009-06-18 06:31:51 +00:00
|
|
|
$strsaved = get_string('filesaved', 'repository');
|
|
|
|
$straddfile = get_string('openpicker', 'repository');
|
2008-09-11 23:11:24 +00:00
|
|
|
$currentfile = '';
|
|
|
|
$draftvalue = '';
|
2009-08-11 09:40:59 +00:00
|
|
|
if ($draftitemid = (int)$this->getValue()) {
|
2008-09-11 23:11:24 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
|
2009-08-11 09:40:59 +00:00
|
|
|
if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id DESC', false)) {
|
2008-09-11 23:11:24 +00:00
|
|
|
$file = reset($files);
|
|
|
|
$currentfile = $file->get_filename();
|
2009-08-11 09:40:59 +00:00
|
|
|
$draftvalue = 'value="'.$draftitemid.'"';
|
2008-08-07 03:33:47 +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);
|
|
|
|
}
|
2009-04-20 08:53:21 +00:00
|
|
|
$client_id = uniqid();
|
2009-06-24 22:34:29 +00:00
|
|
|
$repojs = repository_get_client($context, $client_id, $this->_options['filetypes'], $this->_options['returnvalue']);
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2008-09-11 23:11:24 +00:00
|
|
|
$id = $this->_attributes['id'];
|
|
|
|
$elname = $this->_attributes['name'];
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2008-09-11 23:11:24 +00:00
|
|
|
$str = $this->_getTabs();
|
|
|
|
$str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" '.$draftvalue.' />';
|
2009-06-24 22:34:29 +00:00
|
|
|
$str .= $repojs;
|
2008-09-08 05:41:45 +00:00
|
|
|
|
2009-08-11 09:40:59 +00:00
|
|
|
$str .= $PAGE->requires->data_for_js('filepicker', Array('maxbytes'=>$this->_options['maxbytes'],'maxfiles'=>1))->asap();
|
2009-07-07 08:49:00 +00:00
|
|
|
$str .= $PAGE->requires->js('lib/form/filepicker.js')->asap();
|
2008-09-11 23:11:24 +00:00
|
|
|
$str .= <<<EOD
|
2009-08-14 07:37:56 +00:00
|
|
|
<button onclick="return callpicker('$id', '$client_id', '$draftvalue')">$straddfile</button>
|
|
|
|
<span id="file_info_{$client_id}" class="notifysuccess">$currentfile</span>
|
|
|
|
|
2009-06-18 06:31:51 +00:00
|
|
|
<noscript>
|
|
|
|
<a name="nonjsfp"></a>
|
|
|
|
<object type="text/html" data="{$CFG->httpswwwroot}/repository/filepicker.php?action=embedded&itemid={$draftitemid}&ctx_id=$context->id" height="300" width="800" style="border:1px solid #000">Error</object>
|
|
|
|
</noscript>
|
2008-08-04 05:53:53 +00:00
|
|
|
EOD;
|
2008-09-11 23:11:24 +00:00
|
|
|
return $str;
|
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);
|
2009-08-11 09:40:59 +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
|
|
|
}
|