1441 |
ariadna |
1 |
<?php
|
|
|
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 |
namespace core\output;
|
|
|
18 |
|
|
|
19 |
use core\context\user as context_user;
|
|
|
20 |
use moodle_url;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Data structure representing a file picker.
|
|
|
25 |
*
|
|
|
26 |
* @copyright 2010 Dongsheng Cai
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @since Moodle 2.0
|
|
|
29 |
* @package core
|
|
|
30 |
* @category output
|
|
|
31 |
*/
|
|
|
32 |
class file_picker implements renderable {
|
|
|
33 |
/**
|
|
|
34 |
* @var stdClass An object containing options for the file picker
|
|
|
35 |
*/
|
|
|
36 |
public $options;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Constructs a file picker object.
|
|
|
40 |
*
|
|
|
41 |
* The following are possible options for the filepicker:
|
|
|
42 |
* - accepted_types (*)
|
|
|
43 |
* - return_types (FILE_INTERNAL)
|
|
|
44 |
* - env (filepicker)
|
|
|
45 |
* - client_id (uniqid)
|
|
|
46 |
* - itemid (0)
|
|
|
47 |
* - maxbytes (-1)
|
|
|
48 |
* - maxfiles (1)
|
|
|
49 |
* - buttonname (false)
|
|
|
50 |
*
|
|
|
51 |
* @param stdClass $options An object containing options for the file picker.
|
|
|
52 |
*/
|
|
|
53 |
public function __construct(stdClass $options) {
|
|
|
54 |
global $CFG, $USER, $PAGE;
|
|
|
55 |
require_once($CFG->dirroot . '/repository/lib.php');
|
|
|
56 |
$defaults = [
|
|
|
57 |
'accepted_types' => '*',
|
|
|
58 |
'return_types' => FILE_INTERNAL,
|
|
|
59 |
'env' => 'filepicker',
|
|
|
60 |
'client_id' => uniqid(),
|
|
|
61 |
'itemid' => 0,
|
|
|
62 |
'maxbytes' => -1,
|
|
|
63 |
'maxfiles' => 1,
|
|
|
64 |
'buttonname' => false,
|
|
|
65 |
];
|
|
|
66 |
foreach ($defaults as $key => $value) {
|
|
|
67 |
if (empty($options->$key)) {
|
|
|
68 |
$options->$key = $value;
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$options->currentfile = '';
|
|
|
73 |
if (!empty($options->itemid)) {
|
|
|
74 |
$fs = get_file_storage();
|
|
|
75 |
$usercontext = context_user::instance($USER->id);
|
|
|
76 |
if (empty($options->filename)) {
|
|
|
77 |
if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) {
|
|
|
78 |
$file = reset($files);
|
|
|
79 |
}
|
|
|
80 |
} else {
|
|
|
81 |
$file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename);
|
|
|
82 |
}
|
|
|
83 |
if (!empty($file)) {
|
|
|
84 |
$options->currentfile = html_writer::link(moodle_url::make_draftfile_url(
|
|
|
85 |
$file->get_itemid(),
|
|
|
86 |
$file->get_filepath(),
|
|
|
87 |
$file->get_filename(),
|
|
|
88 |
), $file->get_filename());
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Initialise options, getting files in root path.
|
|
|
93 |
$this->options = initialise_filepicker($options);
|
|
|
94 |
|
|
|
95 |
// Copying other options.
|
|
|
96 |
foreach ($options as $name => $value) {
|
|
|
97 |
if (!isset($this->options->$name)) {
|
|
|
98 |
$this->options->$name = $value;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Alias this class to the old name.
|
|
|
105 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
106 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
107 |
class_alias(file_picker::class, \file_picker::class);
|