2008-07-31 02:51:28 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
require_once("HTML/QuickForm/button.php");
|
|
|
|
require_once(dirname(dirname(dirname(__FILE__))) . '/repository/lib.php');
|
|
|
|
|
|
|
|
/**
|
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 {
|
2008-07-31 02:51:28 +00:00
|
|
|
var $_helpbutton='';
|
2008-09-07 13:30:46 +00:00
|
|
|
|
2008-11-26 03:26:33 +00:00
|
|
|
function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $filetypes = '*', $returnvalue = '*') {
|
|
|
|
$this->filetypes = $filetypes;
|
|
|
|
$this->returnvalue = $returnvalue;
|
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') {
|
|
|
|
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() {
|
2008-09-11 23:11:24 +00:00
|
|
|
global $CFG, $COURSE, $USER;
|
|
|
|
|
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
|
|
|
|
2008-09-11 23:11:24 +00:00
|
|
|
$currentfile = '';
|
|
|
|
$draftvalue = '';
|
|
|
|
if ($draftid = (int)$this->getValue()) {
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
|
|
|
|
if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftid, '', false)) {
|
|
|
|
$file = reset($files);
|
|
|
|
$currentfile = $file->get_filename();
|
|
|
|
$draftvalue = 'value="'.$draftid.'"';
|
2008-08-07 03:33:47 +00:00
|
|
|
}
|
2008-09-11 23:11:24 +00:00
|
|
|
}
|
|
|
|
$strsaved = get_string('filesaved', 'repository');
|
|
|
|
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();
|
|
|
|
$repository_info = repository_get_client($context, $client_id, $this->filetypes, $this->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.' />';
|
2008-09-08 05:41:45 +00:00
|
|
|
|
2008-09-11 23:11:24 +00:00
|
|
|
$str .= <<<EOD
|
2008-08-04 05:53:53 +00:00
|
|
|
<script type="text/javascript">
|
2009-04-20 08:53:21 +00:00
|
|
|
function updatefile(client_id, obj) {
|
|
|
|
document.getElementById('repo_info_'+client_id).innerHTML = obj['file'];
|
2008-08-05 05:12:30 +00:00
|
|
|
}
|
2009-04-20 08:53:21 +00:00
|
|
|
function callpicker(client_id, id) {
|
2008-08-20 02:52:29 +00:00
|
|
|
document.body.className += ' yui-skin-sam';
|
|
|
|
var picker = document.createElement('DIV');
|
2009-04-20 08:53:21 +00:00
|
|
|
picker.id = 'file-picker-'+client_id;
|
2008-09-11 23:11:24 +00:00
|
|
|
picker.className = 'file-picker';
|
2008-08-20 02:52:29 +00:00
|
|
|
document.body.appendChild(picker);
|
2009-04-20 08:53:21 +00:00
|
|
|
var el=document.getElementById(id);
|
|
|
|
open_filepicker({'env':'filepicker', 'target':el, 'callback':updatefile})
|
2008-08-04 05:53:53 +00:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
EOD;
|
2009-04-20 08:53:21 +00:00
|
|
|
$str .= '<input value="'.get_string('openpicker', 'repository').'" type="button" onclick="callpicker(\''+$client_id+'\', \''+$id+'\')" />'.'<span id="repo_info_'.$client_id.'" class="notifysuccess">'.$currentfile.'</span>'.$repository_info['css'].$repository_info['js'];
|
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) {
|
|
|
|
return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
|
|
|
|
}
|
2008-07-31 02:51:28 +00:00
|
|
|
}
|