Commit | Line | Data |
---|---|---|
c5704ec6 | 1 | <?php |
6c1fd304 RT |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
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. | |
8 | // | |
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. | |
13 | // | |
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/>. | |
16 | ||
17 | ||
18 | /** | |
19 | * Filepicker form element | |
20 | * | |
21 | * Contains HTML class for a single filepicker form element | |
22 | * | |
23 | * @package core_form | |
24 | * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com> | |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
26 | */ | |
ae4a98a1 | 27 | |
28 | global $CFG; | |
c5704ec6 | 29 | |
30 | require_once("HTML/QuickForm/button.php"); | |
ae4a98a1 | 31 | require_once($CFG->dirroot.'/repository/lib.php'); |
c5704ec6 | 32 | |
33 | /** | |
6c1fd304 RT |
34 | * Filepicker form element |
35 | * | |
757f30a2 | 36 | * HTML class for a single filepicker element (based on button) |
c5704ec6 | 37 | * |
6c1fd304 RT |
38 | * @package core_form |
39 | * @category form | |
40 | * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com> | |
58b7d48f | 41 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
c5704ec6 | 42 | */ |
b8d9c719 | 43 | class MoodleQuickForm_filepicker extends HTML_QuickForm_input { |
6c1fd304 | 44 | /** @var string html for help button, if empty then no help will icon will be dispalyed. */ |
12f11f23 | 45 | public $_helpbutton = ''; |
6c1fd304 RT |
46 | |
47 | /** @var array options provided to initalize filemanager */ | |
67233725 DC |
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); | |
b7335412 | 52 | |
6c1fd304 RT |
53 | /** |
54 | * Constructor | |
55 | * | |
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 | |
61 | */ | |
9d54b8cd | 62 | function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) { |
2d7c1117 | 63 | global $CFG, $PAGE; |
4287fc0d | 64 | |
9d54b8cd | 65 | $options = (array)$options; |
66 | foreach ($options as $name=>$value) { | |
67 | if (array_key_exists($name, $this->_options)) { | |
68 | $this->_options[$name] = $value; | |
69 | } | |
70 | } | |
2d7c1117 MG |
71 | if (empty($options['return_types'])) { |
72 | $this->_options['return_types'] = FILE_INTERNAL; | |
67233725 | 73 | } |
2d7c1117 | 74 | $fpmaxbytes = 0; |
9d54b8cd | 75 | if (!empty($options['maxbytes'])) { |
2d7c1117 | 76 | $fpmaxbytes = $options['maxbytes']; |
9d54b8cd | 77 | } |
2d7c1117 MG |
78 | $coursemaxbytes = 0; |
79 | if (!empty($PAGE->course)) { | |
80 | $coursemaxbytes = $PAGE->course->maxbytes; | |
81 | } | |
82 | $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $coursemaxbytes, $fpmaxbytes); | |
c6aa3670 | 83 | $this->_type = 'filepicker'; |
b8d9c719 | 84 | parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes); |
43023002 | 85 | } |
4287fc0d | 86 | |
6c1fd304 RT |
87 | /** |
88 | * Sets help button for filepicker | |
58b7d48f | 89 | * |
6c1fd304 RT |
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() | |
95 | */ | |
b8d9c719 | 96 | function setHelpButton($helpbuttonargs, $function='helpbutton') { |
4bcc5118 | 97 | debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead'); |
c5704ec6 | 98 | } |
4287fc0d | 99 | |
6c1fd304 RT |
100 | /** |
101 | * Returns html for help button. | |
102 | * | |
103 | * @return string html for help button | |
104 | */ | |
b8d9c719 | 105 | function getHelpButton() { |
c5704ec6 | 106 | return $this->_helpbutton; |
107 | } | |
4287fc0d | 108 | |
6c1fd304 RT |
109 | /** |
110 | * Returns type of filepicker element | |
111 | * | |
112 | * @return string | |
113 | */ | |
b8d9c719 | 114 | function getElementTemplateType() { |
c5704ec6 | 115 | if ($this->_flagFrozen){ |
116 | return 'nodisplay'; | |
117 | } else { | |
118 | return 'default'; | |
119 | } | |
120 | } | |
4287fc0d | 121 | |
6c1fd304 RT |
122 | /** |
123 | * Returns HTML for filepicker form element. | |
124 | * | |
125 | * @return string | |
126 | */ | |
c5704ec6 | 127 | function toHtml() { |
bb496de7 DC |
128 | global $CFG, $COURSE, $USER, $PAGE, $OUTPUT; |
129 | $id = $this->_attributes['id']; | |
130 | $elname = $this->_attributes['name']; | |
dd070162 | 131 | |
c5704ec6 | 132 | if ($this->_flagFrozen) { |
133 | return $this->getFrozenHtml(); | |
b8d9c719 | 134 | } |
bb496de7 | 135 | if (!$draftitemid = (int)$this->getValue()) { |
2289c0e4 | 136 | // no existing area info provided - let's use fresh new draft area |
842f2914 PS |
137 | $draftitemid = file_get_unused_draft_itemid(); |
138 | $this->setValue($draftitemid); | |
b8d9c719 | 139 | } |
bb496de7 | 140 | |
b8d9c719 | 141 | if ($COURSE->id == SITEID) { |
142 | $context = get_context_instance(CONTEXT_SYSTEM); | |
143 | } else { | |
144 | $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); | |
145 | } | |
99eaca9d | 146 | |
e189ec00 | 147 | $client_id = uniqid(); |
4287fc0d | 148 | |
6bdfef5d | 149 | $args = new stdClass(); |
99eaca9d DC |
150 | // need these three to filter repositories list |
151 | $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*'; | |
2d7c1117 | 152 | $args->return_types = $this->_options['return_types']; |
bb496de7 | 153 | $args->itemid = $draftitemid; |
b817205b | 154 | $args->maxbytes = $this->_options['maxbytes']; |
99eaca9d | 155 | $args->context = $PAGE->context; |
4b72f9eb | 156 | $args->buttonname = $elname.'choose'; |
63d5c4ac | 157 | $args->elementname = $elname; |
99eaca9d | 158 | |
7e074670 | 159 | $html = $this->_getTabs(); |
bb496de7 DC |
160 | $fp = new file_picker($args); |
161 | $options = $fp->options; | |
be85f7ab | 162 | $options->context = $PAGE->context; |
7e074670 | 163 | $html .= $OUTPUT->render($fp); |
4b72f9eb | 164 | $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>'; |
7e074670 | 165 | |
f08fac7c | 166 | $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload')); |
7e074670 DC |
167 | $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module); |
168 | ||
563d0417 DC |
169 | $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array( |
170 | 'env'=>'filepicker', | |
171 | 'action'=>'browse', | |
172 | 'itemid'=>$draftitemid, | |
173 | 'subdirs'=>0, | |
174 | 'maxbytes'=>$options->maxbytes, | |
175 | 'maxfiles'=>1, | |
176 | 'ctx_id'=>$PAGE->context->id, | |
177 | 'course'=>$PAGE->course->id, | |
71267723 | 178 | 'sesskey'=>sesskey(), |
563d0417 DC |
179 | )); |
180 | ||
181 | // non js file picker | |
182 | $html .= '<noscript>'; | |
6ef1402e | 183 | $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>"; |
563d0417 DC |
184 | $html .= '</noscript>'; |
185 | ||
7e074670 | 186 | return $html; |
c5704ec6 | 187 | } |
4287fc0d | 188 | |
6c1fd304 RT |
189 | /** |
190 | * export uploaded file | |
191 | * | |
192 | * @param array $submitValues values submitted. | |
193 | * @param bool $assoc specifies if returned array is associative | |
194 | * @return array | |
195 | */ | |
21599d8c | 196 | function exportValue(&$submitValues, $assoc = false) { |
b7335412 | 197 | global $USER; |
198 | ||
b1eca344 JP |
199 | $draftitemid = $this->_findValue($submitValues); |
200 | if (null === $draftitemid) { | |
201 | $draftitemid = $this->getValue(); | |
202 | } | |
203 | ||
b7335412 | 204 | // make sure max one file is present and it is not too big |
b1eca344 | 205 | if (!is_null($draftitemid)) { |
b7335412 | 206 | $fs = get_file_storage(); |
207 | $usercontext = get_context_instance(CONTEXT_USER, $USER->id); | |
64f93798 | 208 | if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) { |
b7335412 | 209 | $file = array_shift($files); |
210 | if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) { | |
211 | // bad luck, somebody tries to sneak in oversized file | |
212 | $file->delete(); | |
213 | } | |
214 | foreach ($files as $file) { | |
215 | // only one file expected | |
216 | $file->delete(); | |
217 | } | |
218 | } | |
219 | } | |
5b5206e1 JP |
220 | |
221 | return $this->_prepareValue($draftitemid, true); | |
21599d8c | 222 | } |
c5704ec6 | 223 | } |