MDL-31014 Integrated
[moodle.git] / repository / user / lib.php
blob38d6d1ba3fcd7c6567dd6006914654afc1fa7e60
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 * repository_user class is used to browse user private files
21 * @since 2.0
22 * @package repository
23 * @subpackage user
24 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class repository_user extends repository {
30 /**
31 * user plugin doesn't require login
32 * @return mixed
34 public function print_login() {
35 return $this->get_listing();
38 /**
39 * Get file listing
41 * @param string $encodedpath
42 * @return mixed
44 public function get_listing($encodedpath = '') {
45 global $CFG, $USER, $OUTPUT;
46 $ret = array();
47 $ret['dynload'] = true;
48 $ret['nosearch'] = true;
49 $ret['nologin'] = true;
50 $list = array();
52 if (!empty($encodedpath)) {
53 $params = unserialize(base64_decode($encodedpath));
54 if (is_array($params)) {
55 $filepath = clean_param($params['filepath'], PARAM_PATH);;
56 $filename = clean_param($params['filename'], PARAM_FILE);
58 } else {
59 $itemid = 0;
60 $filepath = '/';
61 $filename = null;
63 $filearea = 'private';
64 $component = 'user';
65 $itemid = 0;
66 $context = get_context_instance(CONTEXT_USER, $USER->id);
68 try {
69 $browser = get_file_browser();
71 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
72 $pathnodes = array();
73 $level = $fileinfo;
74 $params = $fileinfo->get_params();
75 while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
76 $encodedpath = base64_encode(serialize($level->get_params()));
77 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
78 $level = $level->get_parent();
79 $params = $level->get_params();
81 $ret['path'] = array_reverse($pathnodes);
83 // build file tree
84 $children = $fileinfo->get_children();
85 foreach ($children as $child) {
86 if ($child->is_directory()) {
87 $encodedpath = base64_encode(serialize($child->get_params()));
88 $node = array(
89 'title' => $child->get_visible_name(),
90 'size' => 0,
91 'date' => '',
92 'path' => $encodedpath,
93 'children'=>array(),
94 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
96 $list[] = $node;
97 } else {
98 $encodedpath = base64_encode(serialize($child->get_params()));
99 $node = array(
100 'title' => $child->get_visible_name(),
101 'size' => 0,
102 'date' => '',
103 'source'=> $encodedpath,
104 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
106 $list[] = $node;
110 } catch (Exception $e) {
111 throw new repository_exception('emptyfilelist', 'repository_user');
113 $ret['list'] = $list;
114 $ret['list'] = array_filter($list, array($this, 'filter'));
115 return $ret;
119 * User file don't support to link to external links
121 * @return int
123 public function supported_returntypes() {
124 return FILE_INTERNAL;
128 * Does this repository used to browse moodle files?
130 * @return boolean
132 public function has_moodle_files() {
133 return true;