98c738457402eec5bd5f91fdf66682de25657fb7
[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         $list = array();
61         if (!empty($encodedpath)) {
62             $params = unserialize(base64_decode($encodedpath));
63             if (is_array($params)) {
64                 $filepath = clean_param($params['filepath'], PARAM_PATH);;
65                 $filename = clean_param($params['filename'], PARAM_FILE);
66             }
67         } else {
68             $itemid   = 0;
69             $filepath = '/';
70             $filename = null;
71         }
72         $filearea = 'private';
73         $component = 'user';
74         $itemid  = 0;
75         $context = get_context_instance(CONTEXT_USER, $USER->id);
77         try {
78             $browser = get_file_browser();
80             if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
81                 $pathnodes = array();
82                 $level = $fileinfo;
83                 $params = $fileinfo->get_params();
84                 while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
85                     $encodedpath = base64_encode(serialize($level->get_params()));
86                     $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
87                     $level = $level->get_parent();
88                     $params = $level->get_params();
89                 }
90                 $ret['path'] = array_reverse($pathnodes);
92                 // build file tree
93                 $children = $fileinfo->get_children();
94                 foreach ($children as $child) {
95                     if ($child->is_directory()) {
96                         $encodedpath = base64_encode(serialize($child->get_params()));
97                         $node = array(
98                             'title' => $child->get_visible_name(),
99                             'datemodified' => $child->get_timemodified(),
100                             'datecreated' => $child->get_timecreated(),
101                             'path' => $encodedpath,
102                             'children'=>array(),
103                             'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
104                         );
105                         $list[] = $node;
106                     } else {
107                         $encodedpath = base64_encode(serialize($child->get_params()));
108                         $node = array(
109                             'title' => $child->get_visible_name(),
110                             'size' => $child->get_filesize(),
111                             'datemodified' => $child->get_timemodified(),
112                             'datecreated' => $child->get_timecreated(),
113                             'author' => $child->get_author(),
114                             'license' => $child->get_license(),
115                             'isref' => $child->is_external_file(),
116                             'source'=> $encodedpath,
117                             'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
118                             'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
119                         );
120                         if ($child->get_status() == 666) {
121                             $node['originalmissing'] = true;
122                         }
123                         if ($imageinfo = $child->get_imageinfo()) {
124                             $fileurl = new moodle_url($child->get_url());
125                             $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
126                             $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
127                             $node['image_width'] = $imageinfo['width'];
128                             $node['image_height'] = $imageinfo['height'];
129                         }
130                         $list[] = $node;
131                     }
132                 }
133             }
134         } catch (Exception $e) {
135             throw new repository_exception('emptyfilelist', 'repository_user');
136         }
137         $ret['list'] = $list;
138         $ret['list'] = array_filter($list, array($this, 'filter'));
139         return $ret;
140     }
142     /**
143      * Does this repository used to browse moodle files?
144      *
145      * @return boolean
146      */
147     public function has_moodle_files() {
148         return true;
149     }
151     /**
152      * User cannot use the external link to dropbox
153      *
154      * @return int
155      */
156     public function supported_returntypes() {
157         return FILE_INTERNAL | FILE_REFERENCE;
158     }
160     /**
161      * Return reference file life time
162      *
163      * @param string $ref
164      * @return int
165      */
166     public function get_reference_file_lifetime($ref) {
167         // this should be realtime
168         return 0;
169     }