MDL-13766, fixed treeview
[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
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
e35194be 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) {
f00340e2 110 $shorttitle = $this->get_short_filename($child->get_visible_name(), 12);
cd312233 111 if ($child->is_directory()) {
87628a67 112 $params = $child->get_params();
7d573936
DC
113 $subdir_children = $child->get_children();
114 if (empty($subdir_children)) {
115 continue;
116 }
6bf197b3
DC
117 $encodedpath = base64_encode(serialize($params));
118 // hide user_private area from local plugin, user should
119 // use private file plugin to access private files
e35194be 120 if ($params['filearea'] == 'user_private') {
6bf197b3
DC
121 continue;
122 }
cd312233 123 $node = array(
124 'title' => $child->get_visible_name(),
f00340e2 125 'shorttitle'=>$shorttitle,
cd312233 126 'size' => 0,
127 'date' => '',
acb70a9b 128 'path' => $encodedpath,
cd312233 129 'children'=>array(),
b5d0cafc 130 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . ''
cd312233 131 );
132 $list[] = $node;
133 } else {
acb70a9b 134 $encodedpath = base64_encode(serialize($child->get_params()));
5f9087f3 135 $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32';
cd312233 136 $node = array(
137 'title' => $child->get_visible_name(),
f00340e2 138 'shorttitle'=>$shorttitle,
cd312233 139 'size' => 0,
140 'date' => '',
acb70a9b 141 'source'=> $encodedpath,
7f288e50 142 'thumbnail' => $OUTPUT->pix_url($icon) . '',
cd312233 143 );
144 $list[] = $node;
145 }
146 }
147 }
157d65c0 148 } catch (Exception $e) {
6a391ebf 149 throw new repository_exception('emptyfilelist', 'repository_local');
6a391ebf 150 }
cd312233 151 $ret['list'] = $list;
87628a67 152 $ret['list'] = array_filter($list, array($this, 'filter'));
cd312233 153 return $ret;
6a391ebf 154 }
dbc01944 155
96297ca2 156 /**
aedb0396
DC
157 * Set repository name
158 *
159 * @return string repository name
96297ca2 160 */
d31af46a 161 public function get_name(){
614d18d2 162 return get_string('pluginname', 'repository_local');;
d31af46a 163 }
aedb0396
DC
164
165 /**
166 * Local file don't support to link to external links
167 *
2f67a9b3 168 * @return int
aedb0396 169 */
41076c58
DC
170 public function supported_returntypes() {
171 return FILE_INTERNAL;
172 }
c7e4621e 173
acb70a9b 174 /**
c7e4621e 175 * Copy a file to file area
acb70a9b
DC
176 *
177 * @global object $USER
178 * @global object $DB
179 * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data
180 * @param string $new_filename The intended name of file
181 * @param string $new_itemid itemid
182 * @param string $new_filepath the new path in draft area
183 * @return array The information of file
184 */
e35194be 185 public function copy_to_area($encoded, $new_filearea='user_draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') {
acb70a9b
DC
186 global $USER, $DB;
187 $info = array();
4317f92f 188
acb70a9b
DC
189 $browser = get_file_browser();
190 $params = unserialize(base64_decode($encoded));
191 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
192 // the final file
193 $contextid = $params['contextid'];
194 $filearea = $params['filearea'];
195 $filepath = $params['filepath'];
196 $filename = $params['filename'];
197 $fileitemid = $params['itemid'];
198 $context = get_context_instance_by_id($contextid);
199 try {
e35194be
DC
200 $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
201 $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename);
acb70a9b
DC
202 } catch (Exception $e) {
203 throw $e;
204 }
205
206 $info['itemid'] = $new_itemid;
207 $info['title'] = $new_filename;
208 $info['contextid'] = $user_context->id;
209 $info['filesize'] = $file_info->get_filesize();
210
211 return $info;
212 }
213}