MDL-34428 Private files repository should have a 'manage' link
[moodle.git] / repository / user / lib.php
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.
14 //
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/>.
18 /**
19  * This plugin is used to access user's private files
20  *
21  * @since 2.0
22  * @package    repository_user
23  * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
26 require_once($CFG->dirroot . '/repository/lib.php');
28 /**
29  * repository_user class is used to browse user private files
30  *
31  * @since     2.0
32  * @package   repository_user
33  * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class repository_user extends repository {
38     /**
39      * user plugin doesn't require login
40      *
41      * @return mixed
42      */
43     public function print_login() {
44         return $this->get_listing();
45     }
47     /**
48      * Get file listing
49      *
50      * @param string $encodedpath
51      * @return mixed
52      */
53     public function get_listing($encodedpath = '', $page = '') {
54         global $CFG, $USER, $OUTPUT;
55         $ret = array();
56         $ret['dynload'] = true;
57         $ret['nosearch'] = true;
58         $ret['nologin'] = true;
59         $manageurl = new moodle_url('/user/files.php');
60         $ret['manage'] = $manageurl->out();
61         $list = array();
63         if (!empty($encodedpath)) {
64             $params = unserialize(base64_decode($encodedpath));
65             if (is_array($params)) {
66                 $filepath = clean_param($params['filepath'], PARAM_PATH);;
67                 $filename = clean_param($params['filename'], PARAM_FILE);
68             }
69         } else {
70             $itemid   = 0;
71             $filepath = '/';
72             $filename = null;
73         }
74         $filearea = 'private';
75         $component = 'user';
76         $itemid  = 0;
77         $context = get_context_instance(CONTEXT_USER, $USER->id);
79         try {
80             $browser = get_file_browser();
82             if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
83                 $pathnodes = array();
84                 $level = $fileinfo;
85                 $params = $fileinfo->get_params();
86                 while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
87                     $encodedpath = base64_encode(serialize($level->get_params()));
88                     $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
89                     $level = $level->get_parent();
90                     $params = $level->get_params();
91                 }
92                 $ret['path'] = array_reverse($pathnodes);
94                 // build file tree
95                 $children = $fileinfo->get_children();
96                 foreach ($children as $child) {
97                     if ($child->is_directory()) {
98                         $encodedpath = base64_encode(serialize($child->get_params()));
99                         $node = array(
100                             'title' => $child->get_visible_name(),
101                             'datemodified' => $child->get_timemodified(),
102                             'datecreated' => $child->get_timecreated(),
103                             'path' => $encodedpath,
104                             'children'=>array(),
105                             'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
106                         );
107                         $list[] = $node;
108                     } else {
109                         $encodedpath = base64_encode(serialize($child->get_params()));
110                         $node = array(
111                             'title' => $child->get_visible_name(),
112                             'size' => $child->get_filesize(),
113                             'datemodified' => $child->get_timemodified(),
114                             'datecreated' => $child->get_timecreated(),
115                             'author' => $child->get_author(),
116                             'license' => $child->get_license(),
117                             'isref' => $child->is_external_file(),
118                             'source'=> $encodedpath,
119                             'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
120                             'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
121                         );
122                         if ($child->get_status() == 666) {
123                             $node['originalmissing'] = true;
124                         }
125                         if ($imageinfo = $child->get_imageinfo()) {
126                             $fileurl = new moodle_url($child->get_url());
127                             $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
128                             $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
129                             $node['image_width'] = $imageinfo['width'];
130                             $node['image_height'] = $imageinfo['height'];
131                         }
132                         $list[] = $node;
133                     }
134                 }
135             }
136         } catch (Exception $e) {
137             throw new repository_exception('emptyfilelist', 'repository_user');
138         }
139         $ret['list'] = $list;
140         $ret['list'] = array_filter($list, array($this, 'filter'));
141         return $ret;
142     }
144     /**
145      * Does this repository used to browse moodle files?
146      *
147      * @return boolean
148      */
149     public function has_moodle_files() {
150         return true;
151     }
153     /**
154      * User cannot use the external link to dropbox
155      *
156      * @return int
157      */
158     public function supported_returntypes() {
159         return FILE_INTERNAL | FILE_REFERENCE;
160     }
162     /**
163      * Return reference file life time
164      *
165      * @param string $ref
166      * @return int
167      */
168     public function get_reference_file_lifetime($ref) {
169         // this should be realtime
170         return 0;
171     }