Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/filebrowser/file_info_context_user.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00028 defined('MOODLE_INTERNAL') || die();
00029 
00038 class file_info_context_user extends file_info {
00039     protected $user;
00040 
00041     public function __construct($browser, $context, $user) {
00042         parent::__construct($browser, $context);
00043         $this->user = $user;
00044     }
00045 
00055     public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
00056         global $USER;
00057 
00058         if (!isloggedin() or isguestuser()) {
00059             return null;
00060         }
00061 
00062         if (empty($component)) {
00063             // access control: list areas only for myself
00064             if ($this->user->id != $USER->id) {
00065                 // no list of areas for other users
00066                 return null;
00067             }
00068             return $this;
00069         }
00070 
00071         $methodname = "get_area_{$component}_{$filearea}";
00072         if (method_exists($this, $methodname)) {
00073             return $this->$methodname($itemid, $filepath, $filename);
00074         }
00075 
00076         return null;
00077     }
00078 
00079     protected function get_area_user_private($itemid, $filepath, $filename) {
00080         global $USER, $CFG;
00081 
00082         // access control: only my files, nobody else
00083         if ($this->user->id != $USER->id) {
00084             return null;
00085         }
00086 
00087         if (is_null($itemid)) {
00088             // go to parent, we do not use itemids here in private area
00089             return $this;
00090         }
00091 
00092         $fs = get_file_storage();
00093 
00094         $filepath = is_null($filepath) ? '/' : $filepath;
00095         $filename = is_null($filename) ? '.' : $filename;
00096 
00097         if (!$storedfile = $fs->get_file($this->context->id, 'user', 'private', 0, $filepath, $filename)) {
00098             if ($filepath === '/' and $filename === '.') {
00099                 // root dir does not exist yet
00100                 $storedfile = new virtual_root_file($this->context->id, 'user', 'private', 0);
00101             } else {
00102                 // not found
00103                 return null;
00104             }
00105         }
00106         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00107 
00108         //TODO: user quota from $CFG->userquota
00109 
00110         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
00111     }
00112 
00113     protected function get_area_user_profile($itemid, $filepath, $filename) {
00114         global $CFG;
00115 
00116         $readaccess = has_capability('moodle/user:update', $this->context);
00117         $writeaccess = has_capability('moodle/user:viewalldetails', $this->context);
00118 
00119         if (!$readaccess and !$writeaccess) {
00120             // the idea here is that only admins should be able to list/modify files in user profile, the rest has to use profile page
00121             return null;
00122         }
00123 
00124         if (is_null($itemid)) {
00125             // go to parent, we do not use itemids here in profile area
00126             return $this;
00127         }
00128 
00129         $fs = get_file_storage();
00130 
00131         $filepath = is_null($filepath) ? '/' : $filepath;
00132         $filename = is_null($filename) ? '.' : $filename;
00133 
00134         if (!$storedfile = $fs->get_file($this->context->id, 'user', 'profile', 0, $filepath, $filename)) {
00135             if ($filepath === '/' and $filename === '.') {
00136                 $storedfile = new virtual_root_file($this->context->id, 'user', 'profile', 0);
00137             } else {
00138                 // not found
00139                 return null;
00140             }
00141         }
00142         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00143         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, 
00144                 get_string('areauserprofile', 'repository'), false, $readaccess, $writeaccess, false);
00145     }
00146 
00147     protected function get_area_user_draft($itemid, $filepath, $filename) {
00148         global $USER, $CFG;
00149 
00150         // access control: only my files
00151         if ($this->user->id != $USER->id) {
00152             return null;
00153         }
00154 
00155         if (empty($itemid)) {
00156             // do not browse itemids - you must know the draftid to see what is there
00157             return null;
00158         }
00159 
00160         $fs = get_file_storage();
00161 
00162         $filepath = is_null($filepath) ? '/' : $filepath;
00163         $filename = is_null($filename) ? '.' : $filename;
00164 
00165         if (!$storedfile = $fs->get_file($this->context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
00166             if ($filepath === '/' and $filename === '.') {
00167                 $storedfile = new virtual_root_file($this->context->id, 'user', 'draft', $itemid);
00168             } else {
00169                 // not found
00170                 return null;
00171             }
00172         }
00173         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00174         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
00175     }
00176 
00177     protected function get_area_user_backup($itemid, $filepath, $filename) {
00178         global $USER, $CFG;
00179 
00180         // access control: only my files, nobody else - TODO: maybe we need new capability here
00181         if ($this->context->instanceid != $USER->id) {
00182             return null;
00183         }
00184 
00185         if (is_null($itemid)) {
00186             // go to parent, we do not use itemids here in profile area
00187             return $this;
00188         }
00189 
00190         $fs = get_file_storage();
00191 
00192         $filepath = is_null($filepath) ? '/' : $filepath;
00193         $filename = is_null($filename) ? '.' : $filename;
00194 
00195         if (!$storedfile = $fs->get_file($this->context->id, 'user', 'backup', $itemid, $filepath, $filename)) {
00196             if ($filepath === '/' and $filename === '.') {
00197                 $storedfile = new virtual_root_file($this->context->id, 'user', 'backup', 0);
00198             } else {
00199                 // not found
00200                 return null;
00201             }
00202         }
00203         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00204         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areauserbackup', 'repository'), false, true, true, false);
00205     }
00206 
00211     public function get_visible_name() {
00212         return fullname($this->user, true);
00213     }
00214 
00219     public function is_writable() {
00220         return false;
00221     }
00222 
00227     public function is_directory() {
00228         return true;
00229     }
00230 
00235     public function get_children() {
00236         $children = array();
00237 
00238         if ($child = $this->get_area_user_private(0, '/', '.')) {
00239             $children[] = $child;
00240         }
00241 /*
00242         if ($child = $this->get_area_user_profile(0, '/', '.')) {
00243             $children[] = $child;
00244         }
00245 */
00246         if ($child = $this->get_area_user_backup(0, '/', '.')) {
00247             $children[] = $child;
00248         }
00249         // do not list draft area here - it is browsable only if you know the draft itemid ;-)
00250 
00251         return $children;
00252     }
00253 
00258     public function get_parent() {
00259         return $this->browser->get_file_info();
00260     }
00261 }
 All Data Structures Namespaces Files Functions Variables Enumerations