"MDL-22583, ignore empty directory"
[moodle.git] / repository / local / repository.class.php
CommitLineData
6a391ebf 1<?php
aedb0396
DC
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/>.
6a391ebf 17
96297ca2 18/**
aedb0396 19 * repository_local class is used to browse moodle files
96297ca2 20 *
aedb0396
DC
21 * @since 2.0
22 * @package moodlecore
23 * @subpackage repository
10d53fd3
DC
24 * @copyright 2009 Dongsheng Cai
25 * @author Dongsheng Cai <dongsheng@moodle.com>
aedb0396 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96297ca2 27 */
aedb0396 28
be236ef5 29class repository_local extends repository {
6a391ebf 30
96297ca2 31 /**
5f9087f3 32 * initialize local plugin
cd312233 33 * @param int $repositoryid
34 * @param int $context
35 * @param array $options
96297ca2 36 */
447c7a19 37 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
6a391ebf 38 parent::__construct($repositoryid, $context, $options);
6a391ebf 39 }
4a65c39a 40
96297ca2 41 /**
acb70a9b 42 * local plugin doesn't require login, so list all files
cd312233 43 * @return mixed
96297ca2 44 */
5f9087f3 45 public function print_login() {
6a391ebf 46 return $this->get_listing();
47 }
5fd3e8f7 48
96297ca2 49 /**
5f9087f3 50 * Not supported by File API yet
cd312233 51 * @param string $search_text
52 * @return mixed
96297ca2 53 */
353d5cf3 54 public function search($search_text) {
5f9087f3 55 return array();
353d5cf3 56 }
57
96297ca2 58 /**
aedb0396 59 * Get file listing
96297ca2 60 *
cd312233 61 * @param string $encodedpath
cd312233 62 * @return mixed
96297ca2 63 */
acb70a9b 64 public function get_listing($encodedpath = '') {
390baf46 65 global $CFG, $USER, $OUTPUT;
cd312233 66 $ret = array();
67 $ret['dynload'] = true;
d1bfc05e 68 $ret['nosearch'] = true;
03896eb7 69 $ret['nologin'] = true;
cd312233 70 $list = array();
21942470 71
a6058fed 72 if (!empty($encodedpath)) {
73 $params = unserialize(base64_decode($encodedpath));
74 if (is_array($params)) {
75 $itemid = $params['itemid'];
76 $filename = $params['filename'];
77 $filearea = $params['filearea'];
78 $filepath = $params['filepath'];
79 $context = get_context_instance_by_id($params['contextid']);
80 }
81 } else {
82 $itemid = null;
83 $filename = null;
84 $filearea = null;
85 $filepath = null;
86 $context = get_system_context();
a6058fed 87 }
88
e3ca0f3a 89 try {
cd312233 90 $browser = get_file_browser();
cd312233 91
4ba3169d 92 if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) {
acb70a9b
DC
93 // build path navigation
94 $pathnodes = array();
95 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
96 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
cd312233 97 $level = $fileinfo->get_parent();
98 while ($level) {
acb70a9b
DC
99 $encodedpath = base64_encode(serialize($level->get_params()));
100 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
cd312233 101 $level = $level->get_parent();
102 }
acb70a9b
DC
103 if (!empty($pathnodes) && is_array($pathnodes)) {
104 $pathnodes = array_reverse($pathnodes);
105 $ret['path'] = $pathnodes;
78f7e0c6 106 }
acb70a9b 107 // build file tree
cd312233 108 $children = $fileinfo->get_children();
109 foreach ($children as $child) {
110 if ($child->is_directory()) {
87628a67 111 $params = $child->get_params();
7d573936
DC
112 $subdir_children = $child->get_children();
113 if (empty($subdir_children)) {
114 continue;
115 }
6bf197b3
DC
116 $encodedpath = base64_encode(serialize($params));
117 // hide user_private area from local plugin, user should
118 // use private file plugin to access private files
119 if ($params['filearea'] == 'user_private') {
120 continue;
121 }
cd312233 122 $node = array(
123 'title' => $child->get_visible_name(),
124 'size' => 0,
125 'date' => '',
acb70a9b 126 'path' => $encodedpath,
cd312233 127 'children'=>array(),
b5d0cafc 128 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . ''
cd312233 129 );
130 $list[] = $node;
131 } else {
acb70a9b 132 $encodedpath = base64_encode(serialize($child->get_params()));
5f9087f3 133 $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32';
cd312233 134 $node = array(
135 'title' => $child->get_visible_name(),
136 'size' => 0,
137 'date' => '',
acb70a9b 138 'source'=> $encodedpath,
7f288e50 139 'thumbnail' => $OUTPUT->pix_url($icon) . '',
cd312233 140 );
141 $list[] = $node;
142 }
143 }
144 }
157d65c0 145 } catch (Exception $e) {
6a391ebf 146 throw new repository_exception('emptyfilelist', 'repository_local');
6a391ebf 147 }
cd312233 148 $ret['list'] = $list;
87628a67 149 $ret['list'] = array_filter($list, array($this, 'filter'));
cd312233 150 return $ret;
6a391ebf 151 }
dbc01944 152
96297ca2 153 /**
aedb0396
DC
154 * Set repository name
155 *
156 * @return string repository name
96297ca2 157 */
d31af46a 158 public function get_name(){
159 return get_string('repositoryname', 'repository_local');;
160 }
aedb0396
DC
161
162 /**
163 * Local file don't support to link to external links
164 *
2f67a9b3 165 * @return int
aedb0396 166 */
41076c58
DC
167 public function supported_returntypes() {
168 return FILE_INTERNAL;
169 }
c7e4621e 170
acb70a9b 171 /**
c7e4621e 172 * Copy a file to file area
acb70a9b
DC
173 *
174 * @global object $USER
175 * @global object $DB
176 * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data
177 * @param string $new_filename The intended name of file
178 * @param string $new_itemid itemid
179 * @param string $new_filepath the new path in draft area
180 * @return array The information of file
181 */
c7e4621e 182 public function copy_to_area($encoded, $new_filearea='user_draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') {
acb70a9b
DC
183 global $USER, $DB;
184 $info = array();
4317f92f 185
acb70a9b
DC
186 $browser = get_file_browser();
187 $params = unserialize(base64_decode($encoded));
188 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
189 // the final file
190 $contextid = $params['contextid'];
191 $filearea = $params['filearea'];
192 $filepath = $params['filepath'];
193 $filename = $params['filename'];
194 $fileitemid = $params['itemid'];
195 $context = get_context_instance_by_id($contextid);
196 try {
197 $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
c7e4621e 198 $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename);
acb70a9b
DC
199 } catch (Exception $e) {
200 throw $e;
201 }
202
203 $info['itemid'] = $new_itemid;
204 $info['title'] = $new_filename;
205 $info['contextid'] = $user_context->id;
206 $info['filesize'] = $file_info->get_filesize();
207
208 return $info;
209 }
210}