Merge branch 'MDL-31557_master' of git://github.com/markn86/moodle
[moodle.git] / repository / local / lib.php
CommitLineData
6a391ebf 1<?php
aedb0396
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
6a391ebf 16
96297ca2 17/**
67233725 18 * This plugin is used to access local files
96297ca2 19 *
67233725 20 * @since 2.0
e2652f1a 21 * @package repository_local
67233725 22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96297ca2 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
aedb0396 26
67233725
DC
27/**
28 * repository_local class is used to browse moodle files
29 *
30 * @since 2.0
31 * @package repository_local
b8de2621 32 * @copyright 2012 Marina Glancy
67233725
DC
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
be236ef5 36class repository_local extends repository {
96297ca2 37 /**
aedb0396 38 * Get file listing
96297ca2 39 *
cd312233 40 * @param string $encodedpath
b8de2621 41 * @param string $page no paging is used in repository_local
cd312233 42 * @return mixed
96297ca2 43 */
58eb9b9f 44 public function get_listing($encodedpath = '', $page = '') {
390baf46 45 global $CFG, $USER, $OUTPUT;
cd312233 46 $ret = array();
47 $ret['dynload'] = true;
d1bfc05e 48 $ret['nosearch'] = true;
03896eb7 49 $ret['nologin'] = true;
b8de2621
MG
50 $ret['list'] = array();
51
52 $itemid = null;
53 $filename = null;
54 $filearea = null;
55 $filepath = null;
56 $component = null;
21942470 57
a6058fed 58 if (!empty($encodedpath)) {
59 $params = unserialize(base64_decode($encodedpath));
b8de2621 60 if (is_array($params) && isset($params['contextid'])) {
aff24313
PS
61 $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
62 $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
3136dc16 63 $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
0e35ba6f 64 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
3136dc16 65 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
d197ea43 66 $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
a6058fed 67 }
b8de2621
MG
68 }
69 if (empty($context) && !empty($this->context)) {
43aadf04 70 $context = $this->context->get_course_context(false);
b8de2621
MG
71 }
72 if (empty($context)) {
73 $context = context_system::instance();
74 }
75
76 // prepare list of allowed extensions: $extensions is either string '*'
77 // or array of lowercase extensions, i.e. array('.gif','.jpg')
78 $extensions = optional_param_array('accepted_types', '', PARAM_RAW);
79 if (empty($extensions) || $extensions === '*' || (is_array($extensions) && in_array('*', $extensions))) {
80 $extensions = '*';
a6058fed 81 } else {
b8de2621
MG
82 if (!is_array($extensions)) {
83 $extensions = array($extensions);
be85f7ab 84 }
43aadf04 85 $extensions = array_map('textlib::strtolower', $extensions);
a6058fed 86 }
87
b8de2621 88 // build file tree
955b6e09 89 $browser = get_file_browser();
b8de2621 90 if (!($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename))) {
eed6f2e7 91 // if file doesn't exist, build path nodes root of current context
eed6f2e7 92 $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
e2652f1a 93 }
b8de2621
MG
94 $ret['list'] = $this->get_non_empty_children($fileinfo, $extensions);
95
e2652f1a 96 // build path navigation
b8de2621
MG
97 $path = array();
98 for ($level = $fileinfo; $level; $level = $level->get_parent()) {
99 array_unshift($path, $level);
100 }
101 array_unshift($path, null);
e2652f1a 102 $ret['path'] = array();
b8de2621
MG
103 for ($i=1; $i<count($path); $i++) {
104 if ($path[$i] == $fileinfo || !$this->can_skip($path[$i], $extensions, $path[$i-1])) {
105 $ret['path'][] = $this->get_node_path($path[$i]);
eed6f2e7 106 }
6a391ebf 107 }
cd312233 108 return $ret;
6a391ebf 109 }
dbc01944 110
aedb0396 111 /**
b8de2621 112 * Tells how the file can be picked from this repository
aedb0396 113 *
2f67a9b3 114 * @return int
aedb0396 115 */
41076c58 116 public function supported_returntypes() {
be920f9a 117 return FILE_INTERNAL | FILE_REFERENCE;
41076c58 118 }
c7e4621e 119
acb70a9b 120 /**
f392caba 121 * Does this repository used to browse moodle files?
acb70a9b 122 *
f392caba 123 * @return boolean
acb70a9b 124 */
f392caba
DC
125 public function has_moodle_files() {
126 return true;
f892da72 127 }
be920f9a
MG
128
129 /**
130 * Return reference file life time
131 *
132 * @param string $ref
133 * @return int
134 */
135 public function get_reference_file_lifetime($ref) {
136 // this should be realtime
137 return 0;
138 }
e2652f1a
MG
139
140 /**
b8de2621
MG
141 * Returns all children elements that have one of the specified extensions
142 *
43aadf04 143 * This function may skip subfolders and recursively add their children
b8de2621 144 * {@link repository_local::can_skip()}
e2652f1a
MG
145 *
146 * @param file_info $fileinfo
b8de2621
MG
147 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
148 * @return array array of file_info elements
e2652f1a 149 */
b8de2621
MG
150 private function get_non_empty_children(file_info $fileinfo, $extensions) {
151 $nonemptychildren = $fileinfo->get_non_empty_children($extensions);
152 $list = array();
153 foreach ($nonemptychildren as $child) {
154 if ($this->can_skip($child, $extensions, $fileinfo)) {
155 $list = array_merge($list, $this->get_non_empty_children($child, $extensions));
156 } else {
157 $list[] = $this->get_node($child);
158 }
e2652f1a 159 }
b8de2621 160 return $list;
e2652f1a
MG
161 }
162
163 /**
43aadf04 164 * Whether this folder may be skipped in folder hierarchy
b8de2621
MG
165 *
166 * 1. Skip the name of a single filearea in a module
167 * 2. Skip course categories for non-admins who do not have navshowmycoursecategories setting
e2652f1a
MG
168 *
169 * @param file_info $fileinfo
b8de2621
MG
170 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
171 * @param file_info|int $parent specify parent here if we know it to avoid creating extra objects
172 * @return bool
e2652f1a 173 */
b8de2621
MG
174 private function can_skip(file_info $fileinfo, $extensions, $parent = -1) {
175 global $CFG;
b8de2621
MG
176 if (!$fileinfo->is_directory()) {
177 // do not skip files
178 return false;
e2652f1a 179 }
b8de2621
MG
180 if ($fileinfo instanceof file_info_context_coursecat) {
181 // This is a course category. For non-admins we do not display categories
182 return empty($CFG->navshowmycoursecategories) &&
183 !has_capability('moodle/course:update', context_system::instance());
184 } else if ($fileinfo instanceof file_info_context_course ||
185 $fileinfo instanceof file_info_context_user ||
186 $fileinfo instanceof file_info_area_course_legacy ||
187 $fileinfo instanceof file_info_context_module ||
188 $fileinfo instanceof file_info_context_system) {
189 // these instances can never be filearea inside an activity, they will never be skipped
190 return false;
191 } else {
192 $params = $fileinfo->get_params();
193 if (strlen($params['filearea']) &&
194 ($params['filepath'] === '/' || empty($params['filepath'])) &&
43aadf04
MG
195 ($params['filename'] === '.' || empty($params['filename'])) &&
196 context::instance_by_id($params['contextid'])->contextlevel == CONTEXT_MODULE) {
b8de2621
MG
197 if ($parent === -1) {
198 $parent = $fileinfo->get_parent();
199 }
200 // This is a filearea inside an activity, it can be skipped if it has no non-empty siblings
201 if ($parent && ($parent instanceof file_info_context_module)) {
43aadf04 202 if ($parent->count_non_empty_children($extensions, 2) <= 1) {
b8de2621
MG
203 return true;
204 }
205 }
206 }
207 }
208 return false;
e2652f1a
MG
209 }
210
211 /**
b8de2621 212 * Converts file_info object to element of repository return list
e2652f1a 213 *
b8de2621 214 * @param file_info $fileinfo
e2652f1a
MG
215 * @return array
216 */
b8de2621 217 private function get_node(file_info $fileinfo) {
e2652f1a 218 global $OUTPUT;
b8de2621 219 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
e2652f1a 220 $node = array(
b8de2621
MG
221 'title' => $fileinfo->get_visible_name(),
222 'datemodified' => $fileinfo->get_timemodified(),
223 'datecreated' => $fileinfo->get_timecreated()
5bdf63cc 224 );
b8de2621 225 if ($fileinfo->is_directory()) {
e2652f1a 226 $node['path'] = $encodedpath;
559276b1 227 $node['thumbnail'] = $OUTPUT->pix_url(file_folder_icon(90))->out(false);
e2652f1a
MG
228 $node['children'] = array();
229 } else {
b8de2621
MG
230 $node['size'] = $fileinfo->get_filesize();
231 $node['author'] = $fileinfo->get_author();
232 $node['license'] = $fileinfo->get_license();
233 $node['isref'] = $fileinfo->is_external_file();
234 if ($fileinfo->get_status() == 666) {
1778f310
MG
235 $node['originalmissing'] = true;
236 }
e2652f1a 237 $node['source'] = $encodedpath;
b8de2621
MG
238 $node['thumbnail'] = $OUTPUT->pix_url(file_file_icon($fileinfo, 90))->out(false);
239 $node['icon'] = $OUTPUT->pix_url(file_file_icon($fileinfo, 24))->out(false);
240 if ($imageinfo = $fileinfo->get_imageinfo()) {
dfad252c 241 // what a beautiful picture, isn't it
b8de2621
MG
242 $fileurl = new moodle_url($fileinfo->get_url());
243 $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
244 $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
dfad252c
DM
245 $node['image_width'] = $imageinfo['width'];
246 $node['image_height'] = $imageinfo['height'];
247 }
e2652f1a
MG
248 }
249 return $node;
250 }
251
252 /**
b8de2621 253 * Converts file_info object to element of repository return path
e2652f1a 254 *
b8de2621 255 * @param file_info $fileinfo
e2652f1a
MG
256 * @return array
257 */
b8de2621
MG
258 private function get_node_path(file_info $fileinfo) {
259 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
e2652f1a
MG
260 return array(
261 'path' => $encodedpath,
b8de2621 262 'name' => $fileinfo->get_visible_name()
e2652f1a
MG
263 );
264 }
727dac2f 265}