glossary MDL-23423 improved ajax error display
[moodle.git] / repository / filepicker.php
CommitLineData
761265ad 1<?php
6f2cd52a 2
761265ad 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/>.
17//
18
6f2cd52a
DC
19/**
20 * This file is used to browse repositories in non-javascript mode
21 *
22 * @since 2.0
23 * @package moodlecore
24 * @subpackage repository
25 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
761265ad 29require_once('../config.php');
30require_once($CFG->libdir.'/filelib.php');
31require_once('lib.php');
b6fd5aee 32/// Wait as long as it takes for this script to finish
33set_time_limit(0);
34
35require_login();
761265ad 36
8ea068a2 37// disable blocks in this page
78946b9b 38$PAGE->set_pagelayout('embedded');
8ea068a2 39
40// general parameters
41$action = optional_param('action', '', PARAM_ALPHA);
d10c92f3 42$client_id = optional_param('client_id', '', PARAM_RAW); // client ID
761265ad 43$itemid = optional_param('itemid', '', PARAM_INT);
8ea068a2 44
45// parameters for repository
761265ad 46$callback = optional_param('callback', '', PARAM_CLEANHTML);
ad137107 47$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
a14a933c 48$courseid = optional_param('course', SITEID, PARAM_INT); // course ID
8ea068a2 49$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
50$filename = optional_param('filename', '', PARAM_FILE);
e5051df3 51$fileurl = optional_param('fileurl', '', PARAM_RAW);
8ea068a2 52$thumbnail = optional_param('thumbnail', '', PARAM_RAW);
af804e3e 53$targetpath = optional_param('targetpath', '', PARAM_PATH);
8ea068a2 54$repo_id = optional_param('repo_id', 0, PARAM_INT); // repository ID
55$req_path = optional_param('p', '', PARAM_RAW); // the path in repository
6f2cd52a 56$curr_page = optional_param('page', '', PARAM_RAW); // What page in repository?
761265ad 57$search_text = optional_param('s', '', PARAM_CLEANHTML);
563d0417
DC
58$maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
59$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
60$subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
761265ad 61
563d0417
DC
62// the path to save files
63$savepath = optional_param('savepath', '/', PARAM_PATH);
8ea068a2 64// path in draft area
563d0417 65$draftpath = optional_param('draftpath', '/', PARAM_PATH);
8ea068a2 66
67
68// user context
563d0417 69$user_context = get_context_instance(CONTEXT_USER, $USER->id);
8ea068a2 70
563d0417 71$PAGE->set_context($user_context);
a14a933c
DC
72if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
73 print_error('invalidcourseid');
74}
75$PAGE->set_course($course);
c33aa23b 76
761265ad 77// init repository plugin
78$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
79 'WHERE i.id=? AND i.typeid=r.id';
009a97ce 80if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
761265ad 81 $type = $repository->type;
e35194be
DC
82 if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
83 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
009a97ce 84 $classname = 'repository_' . $type;
85 try {
563d0417 86 $repo = new $classname($repo_id, $contextid, array('ajax'=>false, 'name'=>$repository->name, 'type'=>$type));
009a97ce 87 } catch (repository_exception $e){
88 print_error('pluginerror', 'repository');
89 }
90 } else {
91 print_error('invalidplugin', 'repository');
761265ad 92 }
761265ad 93}
8ea068a2 94
563d0417
DC
95$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs);
96$params['action'] = 'browse';
97$params['draftpath'] = $draftpath;
98$home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
99
100$params['savepath'] = $savepath;
bf1873c6 101$params['repo_id'] = $repo_id;
563d0417
DC
102$url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
103$PAGE->set_url('/repository/filepicker.php', $params);
b6fd5aee 104
14f3c882 105switch ($action) {
ab9cdbb9 106case 'upload':
009a97ce 107 // The uploaded file has been processed in plugin construct function
8ea068a2 108 // redirect to default page
9cbced1d 109 $repo->upload();
563d0417 110 redirect($home_url, get_string('uploadsucc','repository'));
14f3c882 111 break;
8ea068a2 112
78ff2983 113case 'search':
c33aa23b 114 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
78ff2983 115 try {
116 $search_result = $repo->search($search_text);
117 $search_result['search_result'] = true;
118 $search_result['repo_id'] = $repo_id;
009a97ce 119
120 // TODO: need a better solution
ef3e1fbc 121 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
929d7a83
PS
122 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
123 echo $OUTPUT->render($pagingbar);
009a97ce 124
78ff2983 125 echo '<table>';
126 foreach ($search_result['list'] as $item) {
127 echo '<tr>';
128 echo '<td><img src="'.$item['thumbnail'].'" />';
129 echo '</td><td>';
130 if (!empty($item['url'])) {
131 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
132 } else {
133 echo $item['title'];
134 }
135 echo '</td>';
136 echo '<td>';
009a97ce 137 echo '<form method="post">';
8ea068a2 138 echo '<input type="hidden" name="fileurl" value="'.$item['source'].'"/>';
009a97ce 139 echo '<input type="hidden" name="action" value="confirm"/>';
8ea068a2 140 echo '<input type="hidden" name="filename" value="'.$item['title'].'"/>';
141 echo '<input type="hidden" name="thumbnail" value="'.$item['thumbnail'].'"/>';
009a97ce 142 echo '<input type="submit" value="'.get_string('select','repository').'" />';
143 echo '</form>';
78ff2983 144 echo '</td>';
009a97ce 145 echo '</tr>';
78ff2983 146 }
147 echo '</table>';
148 } catch (repository_exception $e) {
149 }
150 break;
8ea068a2 151
14f3c882 152case 'list':
153case 'sign':
af804e3e 154 echo $OUTPUT->header();
563d0417 155 echo '<div><a href="' . $url->out() . '">'.get_string('back', 'repository')."</a></div>";
761265ad 156 if ($repo->check_login()) {
6f2cd52a 157 $list = $repo->get_listing($req_path, $curr_page);
761265ad 158 $dynload = !empty($list['dynload'])?true:false;
159 if (!empty($list['upload'])) {
47ce714b 160 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
ab9cdbb9 161 echo '<label>'.$list['upload']['label'].': </label>';
761265ad 162 echo '<input type="file" name="repo_upload_file" /><br />';
ab9cdbb9 163 echo '<input type="hidden" name="action" value="upload" /><br />';
8ea068a2 164 echo '<input type="hidden" name="draftpath" value="'.$draftpath.'" /><br />';
563d0417 165 echo '<input type="hidden" name="savepath" value="'.$savepath.'" /><br />';
ab9cdbb9 166 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" /><br />';
b6fd5aee 167 echo '<input type="submit" value="'.get_string('upload', 'repository').'" />';
761265ad 168 echo '</form>';
169 } else {
5e98ab96 170 if (!empty($list['path'])) {
171 foreach ($list['path'] as $p) {
bf1873c6
DC
172 //echo '<form method="post" style="display:inline">';
173 //echo '<input type="hidden" name="p" value="'.$p['path'].'"';
174 //echo '<input type="hidden" name="action" value="list"';
175 //echo '<input type="hidden" name="draftpath" value="'.$draftpath.'" /><br />';
176 //echo '<input type="hidden" name="savepath" value="'.$savepath.'" /><br />';
177 //echo '<input style="display:inline" type="submit" value="'.$p['name'].'" />';
178 //echo '</form>';
179
180 $pathurl = new moodle_url($url, array(
181 'p'=>$p['path'],
182 'action'=>'list',
183 'draftpath'=>$draftpath,
184 'savepath'=>$savepath
185 ));
186 echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
187 echo '<span> / </span>';
5e98ab96 188 }
761265ad 189 }
009a97ce 190 if (!empty($list['page'])) {
191 // TODO: need a better solution
a14a933c 192 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid");
929d7a83 193 echo $OUTPUT->paging_bar($list['total'], $list['page'] - 1, $list['perpage'], $pagingurl);
009a97ce 194 }
761265ad 195 echo '<table>';
196 foreach ($list['list'] as $item) {
197 echo '<tr>';
198 echo '<td><img src="'.$item['thumbnail'].'" />';
199 echo '</td><td>';
200 if (!empty($item['url'])) {
201 echo '<a href="'.$item['url'].'" target="_blank">'.$item['title'].'</a>';
202 } else {
203 echo $item['title'];
204 }
205 echo '</td>';
206 echo '<td>';
207 if (!isset($item['children'])) {
208 echo '<form method="post">';
8ea068a2 209 echo '<input type="hidden" name="fileurl" value="'.$item['source'].'"/>';
761265ad 210 echo '<input type="hidden" name="action" value="confirm"/>';
8ea068a2 211 echo '<input type="hidden" name="draftpath" value="'.$draftpath.'" /><br />';
563d0417 212 echo '<input type="hidden" name="savepath" value="'.$savepath.'" /><br />';
8ea068a2 213 echo '<input type="hidden" name="filename" value="'.$item['title'].'"/>';
214 echo '<input type="hidden" name="thumbnail" value="'.$item['thumbnail'].'"/>';
b6fd5aee 215 echo '<input type="submit" value="'.get_string('select','repository').'" />';
761265ad 216 echo '</form>';
217 } else {
218 echo '<form method="post">';
219 echo '<input type="hidden" name="p" value="'.$item['path'].'"/>';
b6fd5aee 220 echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
761265ad 221 echo '</form>';
222 }
223 echo '</td>';
009a97ce 224 echo '</tr>';
761265ad 225 }
226 echo '</table>';
227 }
228 } else {
229 echo '<form method="post">';
761265ad 230 echo '<input type="hidden" name="action" value="sign" />';
231 echo '<input type="hidden" name="repo_id" value="'.$repo_id.'" />';
78ff2983 232 $repo->print_login();
761265ad 233 echo '</form>';
234 }
6fd42112 235 echo $OUTPUT->footer();
14f3c882 236 break;
8ea068a2 237
14f3c882 238case 'download':
14469892 239 $thefile = $repo->get_file($fileurl, $filename, $itemid);
563d0417
DC
240 $filesize = filesize($thefile['path']);
241 if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
242 print_error('maxbytes');
243 }
14469892 244 if (!empty($thefile)) {
e5051df3 245 $record = new stdclass;
563d0417 246 $record->filepath = $savepath;
e5051df3 247 $record->filename = $filename;
64f93798
PS
248 $record->component = 'user';
249 $record->filearea = 'draft';
e5051df3
DC
250 $record->itemid = $itemid;
251 $record->license = '';
252 $record->author = '';
253 $record->source = $thefile['url'];
254 $info = repository::move_to_filepool($thefile['path'], $record);
563d0417 255 redirect($home_url, get_string('downloadsucc','repository'));
009a97ce 256 } else {
257 print_error('cannotdownload', 'repository');
14f3c882 258 }
259
260 break;
8ea068a2 261
14f3c882 262case 'confirm':
af804e3e 263 echo $OUTPUT->header();
b6fd5aee 264 echo '<div><a href="'.me().'">'.get_string('back', 'repository').'</a></div>';
8ea068a2 265 echo '<img src="'.$thumbnail.'" />';
266 echo '<form method="post">';
267 echo '<table>';
268 echo ' <tr>';
269 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
270 echo ' <td><input type="text" name="filename" value="'.$filename.'" /></td>';
271 echo ' <td><input type="hidden" name="fileurl" value="'.$fileurl.'" /></td>';
272 echo ' <td><input type="hidden" name="action" value="download" /></td>';
273 echo ' <td><input type="hidden" name="itemid" value="'.$itemid.'" /></td>';
274 echo ' </tr>';
14f3c882 275 echo '</table>';
276 echo '<div>';
8ea068a2 277 // the save path
278 echo ' <input name="draftpath" type="hidden" value="'.$draftpath.'" />';
563d0417 279 echo ' <input name="savepath" type="hidden" value="'.$savepath.'" />';
8ea068a2 280 echo ' <input type="submit" value="'.get_string('download', 'repository').'" />';
14f3c882 281 echo '</div>';
282 echo '</form>';
6fd42112 283 echo $OUTPUT->footer();
14f3c882 284 break;
8ea068a2 285
563d0417 286default:
ab9cdbb9 287case 'plugins':
6f2cd52a
DC
288 $params = array();
289 $params['context'] = array($user_context, get_system_context());
290 $params['currentcontext'] = $PAGE->context;
563d0417
DC
291 $params['return_types'] = FILE_INTERNAL;
292
6f2cd52a 293 $repos = repository::get_instances($params);
af804e3e 294 echo $OUTPUT->header();
563d0417 295 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
8ea068a2 296 echo '<div>';
297 echo '<ul>';
ab9cdbb9 298 foreach($repos as $repo) {
299 $info = $repo->get_meta();
8ae8bf8a
PS
300
301 $aurl = clone($url);
563d0417 302 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
8ae8bf8a 303
e5051df3 304 echo '<li>';
563d0417 305 echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
e5051df3
DC
306 echo html_writer::link($aurl, $info->name);
307 echo '</li>';
ab9cdbb9 308 }
8ea068a2 309 echo '</ul>';
310 echo '</div>';
311 echo $OUTPUT->footer();
312 break;
af804e3e 313}