on-demand release 4.0dev+
[moodle.git] / repository / user / lib.php
CommitLineData
6bf197b3
DC
1<?php
2
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/>.
17
18/**
67233725 19 * This plugin is used to access user's private files
6bf197b3 20 *
5bcfd504 21 * @since Moodle 2.0
67233725
DC
22 * @package repository_user
23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6bf197b3 25 */
67233725 26require_once($CFG->dirroot . '/repository/lib.php');
6bf197b3 27
67233725
DC
28/**
29 * repository_user class is used to browse user private files
30 *
5bcfd504 31 * @since Moodle 2.0
67233725
DC
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 */
6bf197b3
DC
36class repository_user extends repository {
37
6bf197b3
DC
38 /**
39 * user plugin doesn't require login
67233725 40 *
6bf197b3
DC
41 * @return mixed
42 */
43 public function print_login() {
44 return $this->get_listing();
45 }
46
6bf197b3
DC
47 /**
48 * Get file listing
49 *
50 * @param string $encodedpath
51 * @return mixed
52 */
58eb9b9f 53 public function get_listing($encodedpath = '', $page = '') {
6bf197b3
DC
54 global $CFG, $USER, $OUTPUT;
55 $ret = array();
56 $ret['dynload'] = true;
57 $ret['nosearch'] = true;
58 $ret['nologin'] = true;
c4b64a06
MG
59 $manageurl = new moodle_url('/user/files.php');
60 $ret['manage'] = $manageurl->out();
6bf197b3
DC
61 $list = array();
62
63 if (!empty($encodedpath)) {
56964b79 64 $params = json_decode(base64_decode($encodedpath), true);
6bf197b3 65 if (is_array($params)) {
0e35ba6f 66 $filepath = clean_param($params['filepath'], PARAM_PATH);
3136dc16 67 $filename = clean_param($params['filename'], PARAM_FILE);
6bf197b3
DC
68 }
69 } else {
70 $itemid = 0;
6bf197b3 71 $filepath = '/';
3136dc16 72 $filename = null;
6bf197b3 73 }
3136dc16
PS
74 $filearea = 'private';
75 $component = 'user';
692fedb0 76 $itemid = 0;
21c08c63 77 $context = context_user::instance($USER->id);
6bf197b3
DC
78
79 try {
80 $browser = get_file_browser();
81
16f61c70 82 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
6bf197b3
DC
83 $pathnodes = array();
84 $level = $fileinfo;
85 $params = $fileinfo->get_params();
692fedb0 86 while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
56964b79 87 $encodedpath = base64_encode(json_encode($level->get_params()));
6bf197b3
DC
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);
93
94 // build file tree
95 $children = $fileinfo->get_children();
96 foreach ($children as $child) {
97 if ($child->is_directory()) {
56964b79 98 $encodedpath = base64_encode(json_encode($child->get_params()));
6bf197b3
DC
99 $node = array(
100 'title' => $child->get_visible_name(),
5bdf63cc
MG
101 'datemodified' => $child->get_timemodified(),
102 'datecreated' => $child->get_timecreated(),
6bf197b3
DC
103 'path' => $encodedpath,
104 'children'=>array(),
663640f5 105 'thumbnail' => $OUTPUT->image_url(file_folder_icon(90))->out(false)
6bf197b3
DC
106 );
107 $list[] = $node;
108 } else {
56964b79 109 $encodedpath = base64_encode(json_encode($child->get_params()));
6bf197b3
DC
110 $node = array(
111 'title' => $child->get_visible_name(),
5bdf63cc
MG
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(),
1778f310 117 'isref' => $child->is_external_file(),
6bf197b3 118 'source'=> $encodedpath,
663640f5
DW
119 'icon' => $OUTPUT->image_url(file_file_icon($child, 24))->out(false),
120 'thumbnail' => $OUTPUT->image_url(file_file_icon($child, 90))->out(false)
6bf197b3 121 );
1778f310
MG
122 if ($child->get_status() == 666) {
123 $node['originalmissing'] = true;
124 }
dfad252c
DM
125 if ($imageinfo = $child->get_imageinfo()) {
126 $fileurl = new moodle_url($child->get_url());
3333e7e2
DM
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()));
dfad252c
DM
129 $node['image_width'] = $imageinfo['width'];
130 $node['image_height'] = $imageinfo['height'];
131 }
6bf197b3
DC
132 $list[] = $node;
133 }
134 }
135 }
136 } catch (Exception $e) {
137 throw new repository_exception('emptyfilelist', 'repository_user');
138 }
139 $ret['list'] = $list;
87628a67 140 $ret['list'] = array_filter($list, array($this, 'filter'));
6bf197b3
DC
141 return $ret;
142 }
143
6bf197b3 144 /**
67233725
DC
145 * Does this repository used to browse moodle files?
146 *
147 * @return boolean
148 */
149 public function has_moodle_files() {
150 return true;
151 }
152
153 /**
154 * User cannot use the external link to dropbox
6bf197b3
DC
155 *
156 * @return int
157 */
158 public function supported_returntypes() {
67233725 159 return FILE_INTERNAL | FILE_REFERENCE;
6bf197b3
DC
160 }
161
31581ae6
FM
162 /**
163 * Is this repository accessing private data?
164 *
165 * @return bool
166 */
167 public function contains_private_data() {
168 return false;
169 }
6bf197b3 170}