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 * Utility class for browsing of user files.
22 * @copyright 2008 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Represents a user context in the tree navigated by {@link file_browser}.
32 * @copyright 2008 Petr Skoda (http://skodak.org)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class file_info_context_user
extends file_info
{
36 /** @var stdClass User object */
42 * @param file_browser $browser
43 * @param stdClass $context
44 * @param stdClass $user
46 public function __construct($browser, $context, $user) {
47 parent
::__construct($browser, $context);
52 * Return information about this specific context level
54 * @param string $component componet
55 * @param string $filearea file area
56 * @param int $itemid item ID
57 * @param string $filepath file path
58 * @param string $filename file name
59 * @return file_info|null
61 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
64 if (!isloggedin() or isguestuser()) {
68 if (empty($component)) {
69 // access control: list areas only for myself
70 if ($this->user
->id
!= $USER->id
) {
71 // no list of areas for other users
77 $methodname = "get_area_{$component}_{$filearea}";
78 if (method_exists($this, $methodname)) {
79 return $this->$methodname($itemid, $filepath, $filename);
86 * Get a file from user private area
88 * @todo MDL-31070 this method should respect $CFG->userquota
89 * @param int $itemid item ID
90 * @param string $filepath file path
91 * @param string $filename file name
92 * @return file_info|null
94 protected function get_area_user_private($itemid, $filepath, $filename) {
97 // access control: only my files, nobody else
98 if ($this->user
->id
!= $USER->id
) {
102 if (is_null($itemid)) {
103 // go to parent, we do not use itemids here in private area
107 $fs = get_file_storage();
109 $filepath = is_null($filepath) ?
'/' : $filepath;
110 $filename = is_null($filename) ?
'.' : $filename;
112 if (!$storedfile = $fs->get_file($this->context
->id
, 'user', 'private', 0, $filepath, $filename)) {
113 if ($filepath === '/' and $filename === '.') {
114 // root dir does not exist yet
115 $storedfile = new virtual_root_file($this->context
->id
, 'user', 'private', 0);
121 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
123 //TODO: user quota from $CFG->userquota
125 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
129 * Get a file from user profile area
131 * @param int $itemid item ID
132 * @param string $filepath file path
133 * @param string $filename file name
134 * @return file_info|null
136 protected function get_area_user_profile($itemid, $filepath, $filename) {
139 $readaccess = has_capability('moodle/user:update', $this->context
);
140 $writeaccess = has_capability('moodle/user:viewalldetails', $this->context
);
142 if (!$readaccess and !$writeaccess) {
143 // the idea here is that only admins should be able to list/modify files in user profile, the rest has to use profile page
147 if (is_null($itemid)) {
148 // go to parent, we do not use itemids here in profile area
152 $fs = get_file_storage();
154 $filepath = is_null($filepath) ?
'/' : $filepath;
155 $filename = is_null($filename) ?
'.' : $filename;
157 if (!$storedfile = $fs->get_file($this->context
->id
, 'user', 'profile', 0, $filepath, $filename)) {
158 if ($filepath === '/' and $filename === '.') {
159 $storedfile = new virtual_root_file($this->context
->id
, 'user', 'profile', 0);
165 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
166 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase,
167 get_string('areauserprofile', 'repository'), false, $readaccess, $writeaccess, false);
171 * Get a file from user draft area
173 * @param int $itemid item ID
174 * @param string $filepath file path
175 * @param string $filename file name
176 * @return file_info|null
178 protected function get_area_user_draft($itemid, $filepath, $filename) {
181 // access control: only my files
182 if ($this->user
->id
!= $USER->id
) {
186 if (empty($itemid)) {
187 // do not browse itemids - you must know the draftid to see what is there
191 $fs = get_file_storage();
193 $filepath = is_null($filepath) ?
'/' : $filepath;
194 $filename = is_null($filename) ?
'.' : $filename;
196 if (!$storedfile = $fs->get_file($this->context
->id
, 'user', 'draft', $itemid, $filepath, $filename)) {
197 if ($filepath === '/' and $filename === '.') {
198 $storedfile = new virtual_root_file($this->context
->id
, 'user', 'draft', $itemid);
204 $urlbase = $CFG->wwwroot
.'/draftfile.php';
205 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
209 * Get a file from user backup area
211 * @todo MDL-31091 maybe we need new caability for access control
212 * @param int $itemid item ID
213 * @param string $filepath file path
214 * @param string $filename file name
215 * @return file_info|null
217 protected function get_area_user_backup($itemid, $filepath, $filename) {
220 // access control: only my files, nobody else - TODO: maybe we need new capability here
221 if ($this->context
->instanceid
!= $USER->id
) {
225 if (is_null($itemid)) {
226 // go to parent, we do not use itemids here in profile area
230 $fs = get_file_storage();
232 $filepath = is_null($filepath) ?
'/' : $filepath;
233 $filename = is_null($filename) ?
'.' : $filename;
235 if (!$storedfile = $fs->get_file($this->context
->id
, 'user', 'backup', $itemid, $filepath, $filename)) {
236 if ($filepath === '/' and $filename === '.') {
237 $storedfile = new virtual_root_file($this->context
->id
, 'user', 'backup', 0);
243 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
244 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('areauserbackup', 'repository'), false, true, true, false);
248 * Returns localised visible name.
252 public function get_visible_name() {
253 return fullname($this->user
, true);
257 * Whether or not new files or directories can be added
261 public function is_writable() {
266 * Whether or not this is a directory
270 public function is_directory() {
275 * Returns list of children.
277 * @return array of file_info instances
279 public function get_children() {
282 if ($child = $this->get_area_user_private(0, '/', '.')) {
283 $children[] = $child;
286 if ($child = $this->get_area_user_profile(0, '/', '.')) {
287 $children[] = $child;
290 if ($child = $this->get_area_user_backup(0, '/', '.')) {
291 $children[] = $child;
293 // do not list draft area here - it is browsable only if you know the draft itemid ;-)
299 * Returns parent file_info instance
301 * @return file_info|null file_info instance or null for root
303 public function get_parent() {
304 return $this->browser
->get_file_info();