course/lib.php: MDL-23401 Fix regression introduced in MDL-23255 that prevents course...
[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
955b6e09 24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
aedb0396 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)) {
955b6e09
DC
55 $itemid = clean_param($params['itemid'], PARAM_INT);
56 $filename = clean_param($params['filename'], PARAM_FILE);
57 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
58 $filepath = clean_param($params['filepath'], PARAM_PATH);;
16f61c70 59 $component = clean_param($params['component'], PARAM_ALPHAEXT);
955b6e09 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;
a6058fed 68 $context = get_system_context();
a6058fed 69 }
70
955b6e09
DC
71 $browser = get_file_browser();
72
16f61c70 73 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
955b6e09
DC
74 // build path navigation
75 $pathnodes = array();
76 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
77 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
78 $level = $fileinfo->get_parent();
79 while ($level) {
80 $encodedpath = base64_encode(serialize($level->get_params()));
81 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
82 $level = $level->get_parent();
83 }
84 if (!empty($pathnodes) && is_array($pathnodes)) {
85 $pathnodes = array_reverse($pathnodes);
86 $ret['path'] = $pathnodes;
87 }
88 // build file tree
89 $children = $fileinfo->get_children();
90 foreach ($children as $child) {
91 $shorttitle = $this->get_short_filename($child->get_visible_name(), 12);
92 if ($child->is_directory()) {
93 $params = $child->get_params();
94 $subdir_children = $child->get_children();
95 //if (empty($subdir_children)) {
96 //continue;
97 //}
98 $encodedpath = base64_encode(serialize($params));
99 // hide user_private area from local plugin, user should
100 // use private file plugin to access private files
101 //if ($params['filearea'] == 'user_private') {
102 //continue;
103 //}
104 $node = array(
105 'title' => $child->get_visible_name(),
106 'shorttitle'=>$shorttitle,
107 'size' => 0,
108 'date' => '',
109 'path' => $encodedpath,
110 'children'=>array(),
111 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . ''
112 );
113 $list[] = $node;
114 } else {
115 $encodedpath = base64_encode(serialize($child->get_params()));
116 $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32';
117 $node = array(
118 'title' => $child->get_visible_name(),
119 'shorttitle'=>$shorttitle,
120 'size' => 0,
121 'date' => '',
122 'source'=> $encodedpath,
123 'thumbnail' => $OUTPUT->pix_url($icon) . '',
124 );
125 $list[] = $node;
cd312233 126 }
127 }
6a391ebf 128 }
87628a67 129 $ret['list'] = array_filter($list, array($this, 'filter'));
cd312233 130 return $ret;
6a391ebf 131 }
dbc01944 132
96297ca2 133 /**
aedb0396
DC
134 * Set repository name
135 *
136 * @return string repository name
96297ca2 137 */
d31af46a 138 public function get_name(){
614d18d2 139 return get_string('pluginname', 'repository_local');;
d31af46a 140 }
aedb0396
DC
141
142 /**
143 * Local file don't support to link to external links
144 *
2f67a9b3 145 * @return int
aedb0396 146 */
41076c58
DC
147 public function supported_returntypes() {
148 return FILE_INTERNAL;
149 }
c7e4621e 150
acb70a9b 151 /**
c7e4621e 152 * Copy a file to file area
acb70a9b
DC
153 *
154 * @global object $USER
155 * @global object $DB
156 * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data
157 * @param string $new_filename The intended name of file
158 * @param string $new_itemid itemid
159 * @param string $new_filepath the new path in draft area
160 * @return array The information of file
161 */
955b6e09 162 public function copy_to_area($encoded, $new_filearea='draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') {
acb70a9b
DC
163 global $USER, $DB;
164 $info = array();
4317f92f 165
acb70a9b 166 $browser = get_file_browser();
acb70a9b 167 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
955b6e09 168
acb70a9b 169 // the final file
955b6e09
DC
170 $params = unserialize(base64_decode($encoded));
171 $contextid = clean_param($params['contextid'], PARAM_INT);
172 $fileitemid = clean_param($params['itemid'], PARAM_INT);
173 $filename = clean_param($params['filename'], PARAM_FILE);
174 $filepath = clean_param($params['filepath'], PARAM_PATH);;
175 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
16f61c70 176 $component = clean_param($params['component'], PARAM_ALPHAEXT);
955b6e09
DC
177
178 $context = get_context_instance_by_id($contextid);
179
16f61c70
DC
180 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
181 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename);
acb70a9b
DC
182
183 $info['itemid'] = $new_itemid;
184 $info['title'] = $new_filename;
185 $info['contextid'] = $user_context->id;
186 $info['filesize'] = $file_info->get_filesize();
187
188 return $info;
189 }
190}