2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Filepicker form element
21 * Contains HTML class for a single filepicker form element
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once("HTML/QuickForm/button.php");
31 require_once($CFG->dirroot.'/repository/lib.php');
34 * Filepicker form element
36 * HTML class for a single filepicker element (based on button)
40 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
44 /** @var string html for help button, if empty then no help will icon will be dispalyed. */
45 public $_helpbutton = '';
47 /** @var array options provided to initalize filemanager */
48 // PHP doesn't support 'key' => $value1 | $value2 in class definition
49 // We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
50 // So I have to set null here, and do it in constructor
51 protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>null);
56 * @param string $elementName (optional) name of the filepicker
57 * @param string $elementLabel (optional) filepicker label
58 * @param array $attributes (optional) Either a typical HTML attribute string
59 * or an associative array
60 * @param array $options set of options to initalize filepicker
62 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
65 $options = (array)$options;
66 foreach ($options as $name=>$value) {
67 if (array_key_exists($name, $this->_options)) {
68 $this->_options[$name] = $value;
71 if (empty($options['return_types'])) {
72 $this->_options['return_types'] = FILE_INTERNAL;
75 if (!empty($options['maxbytes'])) {
76 $fpmaxbytes = $options['maxbytes'];
79 if (!empty($PAGE->course->maxbytes)) {
80 $coursemaxbytes = $PAGE->course->maxbytes;
82 $this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $coursemaxbytes, $fpmaxbytes);
83 $this->_type = 'filepicker';
84 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
88 * Sets help button for filepicker
90 * @param mixed $helpbuttonargs arguments to create help button
91 * @param string $function name of the callback function
92 * @deprecated since Moodle 2.0. Please do not call this function any more.
93 * @todo MDL-31047 this api will be removed.
94 * @see MoodleQuickForm::setHelpButton()
96 function setHelpButton($helpbuttonargs, $function='helpbutton') {
97 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
101 * Returns html for help button.
103 * @return string html for help button
105 function getHelpButton() {
106 return $this->_helpbutton;
110 * Returns type of filepicker element
114 function getElementTemplateType() {
115 if ($this->_flagFrozen){
123 * Returns HTML for filepicker form element.
128 global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
129 $id = $this->_attributes['id'];
130 $elname = $this->_attributes['name'];
132 if ($this->_flagFrozen) {
133 return $this->getFrozenHtml();
135 if (!$draftitemid = (int)$this->getValue()) {
136 // no existing area info provided - let's use fresh new draft area
137 $draftitemid = file_get_unused_draft_itemid();
138 $this->setValue($draftitemid);
141 if ($COURSE->id == SITEID) {
142 $context = get_context_instance(CONTEXT_SYSTEM);
144 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
147 $client_id = uniqid();
149 $args = new stdClass();
150 // need these three to filter repositories list
151 $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
152 $args->return_types = $this->_options['return_types'];
153 $args->itemid = $draftitemid;
154 $args->maxbytes = $this->_options['maxbytes'];
155 $args->context = $PAGE->context;
156 $args->buttonname = $elname.'choose';
157 $args->elementname = $elname;
159 $html = $this->_getTabs();
160 $fp = new file_picker($args);
161 $options = $fp->options;
162 $options->context = $PAGE->context;
163 $html .= $OUTPUT->render($fp);
164 $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
166 $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload'));
167 $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
169 $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
172 'itemid'=>$draftitemid,
174 'maxbytes'=>$options->maxbytes,
176 'ctx_id'=>$PAGE->context->id,
177 'course'=>$PAGE->course->id,
178 'sesskey'=>sesskey(),
181 // non js file picker
182 $html .= '<noscript>';
183 $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
184 $html .= '</noscript>';
190 * export uploaded file
192 * @param array $submitValues values submitted.
193 * @param bool $assoc specifies if returned array is associative
196 function exportValue(&$submitValues, $assoc = false) {
199 $draftitemid = $this->_findValue($submitValues);
200 if (null === $draftitemid) {
201 $draftitemid = $this->getValue();
204 // make sure max one file is present and it is not too big
205 if (!is_null($draftitemid)) {
206 $fs = get_file_storage();
207 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
208 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
209 $file = array_shift($files);
210 if ($this->_options['maxbytes']
211 and $this->_options['maxbytes'] !== USER_CAN_IGNORE_FILE_SIZE_LIMITS
212 and $file->get_filesize() > $this->_options['maxbytes']) {
214 // bad luck, somebody tries to sneak in oversized file
217 foreach ($files as $file) {
218 // only one file expected
224 return $this->_prepareValue($draftitemid, true);