weekly release 2.3dev
[moodle.git] / repository / local / lib.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 21 * @since 2.0
d078f6d3
PS
22 * @package repository
23 * @subpackage local
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96297ca2 26 */
aedb0396 27
be236ef5 28class repository_local extends repository {
6a391ebf 29
96297ca2 30 /**
acb70a9b 31 * local plugin doesn't require login, so list all files
cd312233 32 * @return mixed
96297ca2 33 */
5f9087f3 34 public function print_login() {
6a391ebf 35 return $this->get_listing();
36 }
5fd3e8f7 37
96297ca2 38 /**
aedb0396 39 * Get file listing
96297ca2 40 *
cd312233 41 * @param string $encodedpath
cd312233 42 * @return mixed
96297ca2 43 */
acb70a9b 44 public function get_listing($encodedpath = '') {
390baf46 45 global $CFG, $USER, $OUTPUT;
cd312233 46 $ret = array();
47 $ret['dynload'] = true;
d1bfc05e 48 $ret['nosearch'] = true;
03896eb7 49 $ret['nologin'] = true;
cd312233 50 $list = array();
21942470 51
a6058fed 52 if (!empty($encodedpath)) {
53 $params = unserialize(base64_decode($encodedpath));
54 if (is_array($params)) {
aff24313
PS
55 $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
56 $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
3136dc16
PS
57 $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
58 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
59 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
60 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
a6058fed 61 }
62 } else {
63 $itemid = null;
64 $filename = null;
65 $filearea = null;
66 $filepath = null;
16f61c70 67 $component = null;
be85f7ab
DC
68 if (!empty($this->context)) {
69 list($context, $course, $cm) = get_context_info_array($this->context->id);
70 $courseid = is_object($course) ? $course->id : SITEID;
71 $context = get_context_instance(CONTEXT_COURSE, $courseid);
72 } else {
73 $context = get_system_context();
74 }
a6058fed 75 }
76
955b6e09
DC
77 $browser = get_file_browser();
78
16f61c70 79 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
955b6e09
DC
80 // build path navigation
81 $pathnodes = array();
82 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
83 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
84 $level = $fileinfo->get_parent();
85 while ($level) {
86 $encodedpath = base64_encode(serialize($level->get_params()));
87 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
88 $level = $level->get_parent();
89 }
90 if (!empty($pathnodes) && is_array($pathnodes)) {
91 $pathnodes = array_reverse($pathnodes);
92 $ret['path'] = $pathnodes;
93 }
94 // build file tree
95 $children = $fileinfo->get_children();
96 foreach ($children as $child) {
955b6e09 97 if ($child->is_directory()) {
16741cac
PS
98 if ($child->is_empty_area()) {
99 continue;
100 }
955b6e09 101 $params = $child->get_params();
955b6e09
DC
102 $encodedpath = base64_encode(serialize($params));
103 // hide user_private area from local plugin, user should
104 // use private file plugin to access private files
105 //if ($params['filearea'] == 'user_private') {
106 //continue;
107 //}
108 $node = array(
109 'title' => $child->get_visible_name(),
955b6e09
DC
110 'size' => 0,
111 'date' => '',
112 'path' => $encodedpath,
113 'children'=>array(),
ede72522 114 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
955b6e09
DC
115 );
116 $list[] = $node;
117 } else {
118 $encodedpath = base64_encode(serialize($child->get_params()));
955b6e09
DC
119 $node = array(
120 'title' => $child->get_visible_name(),
955b6e09
DC
121 'size' => 0,
122 'date' => '',
123 'source'=> $encodedpath,
ede72522 124 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
955b6e09
DC
125 );
126 $list[] = $node;
cd312233 127 }
128 }
eed6f2e7
DC
129 } else {
130 // if file doesn't exist, build path nodes root of current context
131 $pathnodes = array();
132 $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
133 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
134 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
135 $level = $fileinfo->get_parent();
136 while ($level) {
137 $encodedpath = base64_encode(serialize($level->get_params()));
138 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
139 $level = $level->get_parent();
140 }
141 if (!empty($pathnodes) && is_array($pathnodes)) {
142 $pathnodes = array_reverse($pathnodes);
143 $ret['path'] = $pathnodes;
144 }
145 $list = array();
6a391ebf 146 }
87628a67 147 $ret['list'] = array_filter($list, array($this, 'filter'));
cd312233 148 return $ret;
6a391ebf 149 }
dbc01944 150
aedb0396
DC
151 /**
152 * Local file don't support to link to external links
153 *
2f67a9b3 154 * @return int
aedb0396 155 */
41076c58
DC
156 public function supported_returntypes() {
157 return FILE_INTERNAL;
158 }
c7e4621e 159
acb70a9b 160 /**
f392caba 161 * Does this repository used to browse moodle files?
acb70a9b 162 *
f392caba 163 * @return boolean
acb70a9b 164 */
f392caba
DC
165 public function has_moodle_files() {
166 return true;
f892da72 167 }
acb70a9b 168}