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 * FileManager form element
21 * Contains HTML class for a filemanager 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/element.php');
31 require_once($CFG->dirroot
.'/lib/filelib.php');
32 require_once($CFG->dirroot
.'/repository/lib.php');
35 * Filemanager form element
37 * FilemaneManager lets user to upload/manage multiple files
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_filemanager
extends HTML_QuickForm_element
{
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('mainfile' => '', 'subdirs' => 1, 'maxbytes' => -1, 'maxfiles' => -1,
52 'accepted_types' => '*', 'return_types' => null, 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED
);
57 * @param string $elementName (optional) name of the filemanager
58 * @param string $elementLabel (optional) filemanager label
59 * @param array $attributes (optional) Either a typical HTML attribute string
60 * or an associative array
61 * @param array $options set of options to initalize filemanager
63 function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
66 $options = (array)$options;
67 foreach ($options as $name=>$value) {
68 if (array_key_exists($name, $this->_options
)) {
69 $this->_options
[$name] = $value;
72 if (!empty($options['maxbytes'])) {
73 $this->_options
['maxbytes'] = get_user_max_upload_file_size($PAGE->context
, $CFG->maxbytes
, $options['maxbytes']);
75 if (empty($options['return_types'])) {
76 $this->_options
['return_types'] = (FILE_INTERNAL | FILE_REFERENCE
);
78 $this->_type
= 'filemanager';
79 parent
::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
83 * Sets name of filemanager
85 * @param string $name name of the filemanager
87 function setName($name) {
88 $this->updateAttributes(array('name'=>$name));
92 * Returns name of filemanager
97 return $this->getAttribute('name');
101 * Updates filemanager attribute value
103 * @param string $value value to set
105 function setValue($value) {
106 $this->updateAttributes(array('value'=>$value));
110 * Returns filemanager attribute value
114 function getValue() {
115 return $this->getAttribute('value');
119 * Returns maximum file size which can be uploaded
123 function getMaxbytes() {
124 return $this->_options
['maxbytes'];
128 * Sets maximum file size which can be uploaded
130 * @param int $maxbytes file size
132 function setMaxbytes($maxbytes) {
134 $this->_options
['maxbytes'] = get_user_max_upload_file_size($PAGE->context
, $CFG->maxbytes
, $maxbytes);
138 * Returns the maximum size of the area.
142 function getAreamaxbytes() {
143 return $this->_options
['areamaxbytes'];
147 * Sets the maximum size of the area.
149 * @param int $areamaxbytes size limit
151 function setAreamaxbytes($areamaxbytes) {
152 $this->_options
['areamaxbytes'] = $areamaxbytes;
156 * Returns true if subdirectoy can be created, else false
160 function getSubdirs() {
161 return $this->_options
['subdirs'];
165 * Set option to create sub directory, while uploading file
167 * @param bool $allow true if sub directory can be created.
169 function setSubdirs($allow) {
170 $this->_options
['subdirs'] = $allow;
174 * Returns maximum number of files which can be uploaded
178 function getMaxfiles() {
179 return $this->_options
['maxfiles'];
183 * Sets maximum number of files which can be uploaded.
185 * @param int $num number of files
187 function setMaxfiles($num) {
188 $this->_options
['maxfiles'] = $num;
192 * Returns html for help button.
194 * @return string html for help button
196 function getHelpButton() {
197 return $this->_helpbutton
;
201 * Returns type of filemanager element
205 function getElementTemplateType() {
206 if ($this->_flagFrozen
){
214 * Returns HTML for filemanager form element.
219 global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
220 require_once("$CFG->dirroot/repository/lib.php");
222 // security - never ever allow guest/not logged in user to upload anything or use this element!
223 if (isguestuser() or !isloggedin()) {
224 print_error('noguest');
227 if ($this->_flagFrozen
) {
228 return $this->getFrozenHtml();
231 $id = $this->_attributes
['id'];
232 $elname = $this->_attributes
['name'];
233 $subdirs = $this->_options
['subdirs'];
234 $maxbytes = $this->_options
['maxbytes'];
235 $draftitemid = $this->getValue();
236 $accepted_types = $this->_options
['accepted_types'];
238 if (empty($draftitemid)) {
239 // no existing area info provided - let's use fresh new draft area
240 require_once("$CFG->libdir/filelib.php");
241 $this->setValue(file_get_unused_draft_itemid());
242 $draftitemid = $this->getValue();
245 $client_id = uniqid();
247 // filemanager options
248 $options = new stdClass();
249 $options->mainfile
= $this->_options
['mainfile'];
250 $options->maxbytes
= $this->_options
['maxbytes'];
251 $options->maxfiles
= $this->getMaxfiles();
252 $options->client_id
= $client_id;
253 $options->itemid
= $draftitemid;
254 $options->subdirs
= $this->_options
['subdirs'];
255 $options->target
= $id;
256 $options->accepted_types
= $accepted_types;
257 $options->return_types
= $this->_options
['return_types'];
258 $options->context
= $PAGE->context
;
259 $options->areamaxbytes
= $this->_options
['areamaxbytes'];
261 $html = $this->_getTabs();
262 $fm = new form_filemanager($options);
263 $output = $PAGE->get_renderer('core', 'files');
264 $html .= $output->render($fm);
266 $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
267 // label element needs 'for' attribute work
268 $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
275 * Data structure representing a file manager.
277 * This class defines the data structure for file mnager
280 * @copyright 2010 Dongsheng Cai
281 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
282 * @todo do not use this abstraction (skodak)
284 class form_filemanager
implements renderable
{
285 /** @var stdClass $options options for filemanager */
291 * @param stdClass $options options for filemanager
292 * default options are:
294 * areamaxbytes => FILE_AREA_MAX_BYTES_UNLIMITED,
298 * client_id=>uniqid(),
299 * acepted_types=>'*',
300 * return_types=>FILE_INTERNAL,
301 * context=>$PAGE->context,
302 * author=>fullname($USER),
303 * licenses=>array build from $CFG->licenses,
304 * defaultlicense=>$CFG->sitedefaultlicense
306 public function __construct(stdClass
$options) {
307 global $CFG, $USER, $PAGE;
308 require_once($CFG->dirroot
. '/repository/lib.php');
311 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED
,
315 'client_id'=>uniqid(),
316 'accepted_types'=>'*',
317 'return_types'=>FILE_INTERNAL
,
318 'context'=>$PAGE->context
,
319 'author'=>fullname($USER),
322 if (!empty($CFG->licenses
)) {
323 $array = explode(',', $CFG->licenses
);
324 foreach ($array as $license) {
326 $l->shortname
= $license;
327 $l->fullname
= get_string($license, 'license');
328 $defaults['licenses'][] = $l;
331 if (!empty($CFG->sitedefaultlicense
)) {
332 $defaults['defaultlicense'] = $CFG->sitedefaultlicense
;
334 foreach ($defaults as $key=>$value) {
335 // Using !isset() prevents us from overwriting falsey values with defaults (as empty() did).
336 if (!isset($options->$key)) {
337 $options->$key = $value;
341 $fs = get_file_storage();
343 // initilise options, getting files in root path
344 $this->options
= file_get_drafarea_files($options->itemid
, '/');
346 // calculate file count
347 $usercontext = context_user
::instance($USER->id
);
348 $files = $fs->get_area_files($usercontext->id
, 'user', 'draft', $options->itemid
, 'id', false);
349 $filecount = count($files);
350 $this->options
->filecount
= $filecount;
352 // copying other options
353 foreach ($options as $name=>$value) {
354 $this->options
->$name = $value;
357 // calculate the maximum file size as minimum from what is specified in filepicker options,
358 // course options, global configuration and php settings
359 $coursebytes = $maxbytes = 0;
360 list($context, $course, $cm) = get_context_info_array($this->options
->context
->id
);
361 if (is_object($course)) {
362 $coursebytes = $course->maxbytes
;
364 if (!empty($this->options
->maxbytes
) && $this->options
->maxbytes
> 0) {
365 $maxbytes = $this->options
->maxbytes
;
367 $this->options
->maxbytes
= get_user_max_upload_file_size($context, $CFG->maxbytes
, $coursebytes, $maxbytes);
369 // building file picker options
370 $params = new stdClass();
371 $params->accepted_types
= $options->accepted_types
;
372 $params->return_types
= $options->return_types
;
373 $params->context
= $options->context
;
374 $params->env
= 'filemanager';
375 $params->disable_types
= !empty($options->disable_types
)?
$options->disable_types
:array();
376 $filepicker_options = initialise_filepicker($params);
377 $this->options
->filepicker
= $filepicker_options;
380 public function get_nonjsurl() {
382 return new moodle_url('/repository/draftfiles_manager.php', array(
383 'env'=>'filemanager',
385 'itemid'=>$this->options
->itemid
,
386 'subdirs'=>$this->options
->subdirs
,
387 'maxbytes'=>$this->options
->maxbytes
,
388 'areamaxbytes' => $this->options
->areamaxbytes
,
389 'maxfiles'=>$this->options
->maxfiles
,
390 'ctx_id'=>$PAGE->context
->id
, // TODO ?
391 'course'=>$PAGE->course
->id
, // TODO ?
392 'sesskey'=>sesskey(),