MDL-29248 backup: take rid of the setting and code handling the 'private' user filearea
[moodle.git] / lib / filebrowser / file_info_context_user.php
bloba75efed1a2688f3737924b8cbb7eec0aba64ee7f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 /**
20 * Utility class for browsing of user files.
22 * @package core
23 * @subpackage filebrowser
24 * @copyright 2008 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * Represents a user context in the tree navigated by @see{file_browser}.
33 * @package core
34 * @subpackage filebrowser
35 * @copyright 2008 Petr Skoda (http://skodak.org)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class file_info_context_user extends file_info {
39 protected $user;
41 public function __construct($browser, $context, $user) {
42 parent::__construct($browser, $context);
43 $this->user = $user;
46 /**
47 * Return information about this specific context level
49 * @param $component
50 * @param $filearea
51 * @param $itemid
52 * @param $filepath
53 * @param $filename
55 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
56 global $USER;
58 if (!isloggedin() or isguestuser()) {
59 return null;
62 if (empty($component)) {
63 // access control: list areas only for myself
64 if ($this->user->id != $USER->id) {
65 // no list of areas for other users
66 return null;
68 return $this;
71 $methodname = "get_area_{$component}_{$filearea}";
72 if (method_exists($this, $methodname)) {
73 return $this->$methodname($itemid, $filepath, $filename);
76 return null;
79 protected function get_area_user_private($itemid, $filepath, $filename) {
80 global $USER, $CFG;
82 // access control: only my files, nobody else
83 if ($this->user->id != $USER->id) {
84 return null;
87 if (is_null($itemid)) {
88 // go to parent, we do not use itemids here in private area
89 return $this;
92 $fs = get_file_storage();
94 $filepath = is_null($filepath) ? '/' : $filepath;
95 $filename = is_null($filename) ? '.' : $filename;
97 if (!$storedfile = $fs->get_file($this->context->id, 'user', 'private', 0, $filepath, $filename)) {
98 if ($filepath === '/' and $filename === '.') {
99 // root dir does not exist yet
100 $storedfile = new virtual_root_file($this->context->id, 'user', 'private', 0);
101 } else {
102 // not found
103 return null;
106 $urlbase = $CFG->wwwroot.'/pluginfile.php';
108 //TODO: user quota from $CFG->userquota
110 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
113 protected function get_area_user_profile($itemid, $filepath, $filename) {
114 global $CFG;
116 $readaccess = has_capability('moodle/user:update', $this->context);
117 $writeaccess = has_capability('moodle/user:viewalldetails', $this->context);
119 if (!$readaccess and !$writeaccess) {
120 // the idea here is that only admins should be able to list/modify files in user profile, the rest has to use profile page
121 return null;
124 if (is_null($itemid)) {
125 // go to parent, we do not use itemids here in profile area
126 return $this;
129 $fs = get_file_storage();
131 $filepath = is_null($filepath) ? '/' : $filepath;
132 $filename = is_null($filename) ? '.' : $filename;
134 if (!$storedfile = $fs->get_file($this->context->id, 'user', 'profile', 0, $filepath, $filename)) {
135 if ($filepath === '/' and $filename === '.') {
136 $storedfile = new virtual_root_file($this->context->id, 'user', 'profile', 0);
137 } else {
138 // not found
139 return null;
142 $urlbase = $CFG->wwwroot.'/pluginfile.php';
143 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase,
144 get_string('areauserprofile', 'repository'), false, $readaccess, $writeaccess, false);
147 protected function get_area_user_draft($itemid, $filepath, $filename) {
148 global $USER, $CFG;
150 // access control: only my files
151 if ($this->user->id != $USER->id) {
152 return null;
155 if (empty($itemid)) {
156 // do not browse itemids - you must know the draftid to see what is there
157 return null;
160 $fs = get_file_storage();
162 $filepath = is_null($filepath) ? '/' : $filepath;
163 $filename = is_null($filename) ? '.' : $filename;
165 if (!$storedfile = $fs->get_file($this->context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
166 if ($filepath === '/' and $filename === '.') {
167 $storedfile = new virtual_root_file($this->context->id, 'user', 'draft', $itemid);
168 } else {
169 // not found
170 return null;
173 $urlbase = $CFG->wwwroot.'/pluginfile.php';
174 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
177 protected function get_area_user_backup($itemid, $filepath, $filename) {
178 global $USER, $CFG;
180 // access control: only my files, nobody else - TODO: maybe we need new capability here
181 if ($this->context->instanceid != $USER->id) {
182 return null;
185 if (is_null($itemid)) {
186 // go to parent, we do not use itemids here in profile area
187 return $this;
190 $fs = get_file_storage();
192 $filepath = is_null($filepath) ? '/' : $filepath;
193 $filename = is_null($filename) ? '.' : $filename;
195 if (!$storedfile = $fs->get_file($this->context->id, 'user', 'backup', $itemid, $filepath, $filename)) {
196 if ($filepath === '/' and $filename === '.') {
197 $storedfile = new virtual_root_file($this->context->id, 'user', 'backup', 0);
198 } else {
199 // not found
200 return null;
203 $urlbase = $CFG->wwwroot.'/pluginfile.php';
204 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserbackup', 'repository'), false, true, true, false);
208 * Returns localised visible name.
209 * @return string
211 public function get_visible_name() {
212 return fullname($this->user, true);
216 * Can I add new files or directories?
217 * @return bool
219 public function is_writable() {
220 return false;
224 * Is directory?
225 * @return bool
227 public function is_directory() {
228 return true;
232 * Returns list of children.
233 * @return array of file_info instances
235 public function get_children() {
236 $children = array();
238 if ($child = $this->get_area_user_private(0, '/', '.')) {
239 $children[] = $child;
242 if ($child = $this->get_area_user_profile(0, '/', '.')) {
243 $children[] = $child;
246 if ($child = $this->get_area_user_backup(0, '/', '.')) {
247 $children[] = $child;
249 // do not list draft area here - it is browsable only if you know the draft itemid ;-)
251 return $children;
255 * Returns parent file_info instance
256 * @return file_info or null for root
258 public function get_parent() {
259 return $this->browser->get_file_info();