Merge branch 'MDL-63698-master' of https://github.com/lucaboesch/moodle
[moodle.git] / repository / user / lib.php
blob288665325d7b923db6daa3b6c7f5e2fd8d3ec139
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/>.
18 /**
19 * This plugin is used to access user's private files
21 * @since Moodle 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
26 require_once($CFG->dirroot . '/repository/lib.php');
28 /**
29 * repository_user class is used to browse user private files
31 * @since Moodle 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
36 class repository_user extends repository {
38 /**
39 * user plugin doesn't require login
41 * @return mixed
43 public function print_login() {
44 return $this->get_listing();
47 /**
48 * Get file listing
50 * @param string $encodedpath
51 * @return mixed
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 = json_decode(base64_decode($encodedpath), true);
65 if (is_array($params)) {
66 $filepath = clean_param($params['filepath'], PARAM_PATH);
67 $filename = clean_param($params['filename'], PARAM_FILE);
69 } else {
70 $itemid = 0;
71 $filepath = '/';
72 $filename = null;
74 $filearea = 'private';
75 $component = 'user';
76 $itemid = 0;
77 $context = context_user::instance($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(json_encode($level->get_params()));
88 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
89 $level = $level->get_parent();
90 $params = $level->get_params();
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(json_encode($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->image_url(file_folder_icon(90))->out(false)
107 $list[] = $node;
108 } else {
109 $encodedpath = base64_encode(json_encode($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->image_url(file_file_icon($child, 24))->out(false),
120 'thumbnail' => $OUTPUT->image_url(file_file_icon($child, 90))->out(false)
122 if ($child->get_status() == 666) {
123 $node['originalmissing'] = true;
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'];
132 $list[] = $node;
136 } catch (Exception $e) {
137 throw new repository_exception('emptyfilelist', 'repository_user');
139 $ret['list'] = $list;
140 $ret['list'] = array_filter($list, array($this, 'filter'));
141 return $ret;
145 * Does this repository used to browse moodle files?
147 * @return boolean
149 public function has_moodle_files() {
150 return true;
154 * User cannot use the external link to dropbox
156 * @return int
158 public function supported_returntypes() {
159 return FILE_INTERNAL | FILE_REFERENCE;
163 * Is this repository accessing private data?
165 * @return bool
167 public function contains_private_data() {
168 return false;