3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * This file is used to browse repositories in non-javascript mode
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
29 require_once('../config.php');
30 require_once($CFG->libdir
.'/filelib.php');
31 require_once('lib.php');
32 /// Wait as long as it takes for this script to finish
38 // disable blocks in this page
39 $PAGE->set_pagelayout('embedded');
42 $action = optional_param('action', '', PARAM_ALPHA
);
43 $client_id = optional_param('client_id', '', PARAM_RAW
); // client ID
44 $itemid = optional_param('itemid', '', PARAM_INT
);
46 // parameters for repository
47 $callback = optional_param('callback', '', PARAM_CLEANHTML
);
48 $contextid = optional_param('ctx_id', SYSCONTEXTID
, PARAM_INT
); // context ID
49 $courseid = optional_param('course', SITEID
, PARAM_INT
); // course ID
50 $env = optional_param('env', 'filepicker', PARAM_ALPHA
); // opened in file picker, file manager or html editor
51 $filename = optional_param('filename', '', PARAM_FILE
);
52 $fileurl = optional_param('fileurl', '', PARAM_RAW
);
53 $thumbnail = optional_param('thumbnail', '', PARAM_RAW
);
54 $targetpath = optional_param('targetpath', '', PARAM_PATH
);
55 $repo_id = optional_param('repo_id', 0, PARAM_INT
); // repository ID
56 $req_path = optional_param('p', '', PARAM_RAW
); // the path in repository
57 $curr_page = optional_param('page', '', PARAM_RAW
); // What page in repository?
58 $search_text = optional_param('s', '', PARAM_CLEANHTML
);
59 $maxfiles = optional_param('maxfiles', -1, PARAM_INT
); // maxfiles
60 $maxbytes = optional_param('maxbytes', 0, PARAM_INT
); // maxbytes
61 $subdirs = optional_param('subdirs', 0, PARAM_INT
); // maxbytes
62 $accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW
);
64 // the path to save files
65 $savepath = optional_param('savepath', '/', PARAM_PATH
);
67 $draftpath = optional_param('draftpath', '/', PARAM_PATH
);
71 $user_context = context_user
::instance($USER->id
);
73 $PAGE->set_context($user_context);
74 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
75 print_error('invalidcourseid');
77 $PAGE->set_course($course);
80 // Get repository instance information
83 'mimetypes' => $accepted_types
85 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
88 $repo->check_capability();
91 $context = context
::instance_by_id($contextid);
93 // Make sure maxbytes passed is within site filesize limits.
94 $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes
, $course->maxbytes
, $maxbytes);
96 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
97 $params['action'] = 'browse';
98 $params['draftpath'] = $draftpath;
99 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
101 $params['savepath'] = $savepath;
102 $params['repo_id'] = $repo_id;
103 $url = new moodle_url($CFG->httpswwwroot
."/repository/filepicker.php", $params);
104 $PAGE->set_url('/repository/filepicker.php', $params);
108 // The uploaded file has been processed in plugin construct function
109 // redirect to default page
111 $repo->upload('', $maxbytes);
112 redirect($home_url, get_string('uploadsucc','repository'));
113 } catch (moodle_exception
$e) {
115 $e->link
= $PAGE->url
->out();
116 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
122 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
124 $search_result = $repo->search($search_text);
125 $search_result['issearchresult'] = true;
126 $search_result['repo_id'] = $repo_id;
128 // TODO: need a better solution
129 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
130 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
131 echo $OUTPUT->render($pagingbar);
134 foreach ($search_result['list'] as $item) {
138 if (isset($item['thumbnail_height'])) {
139 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
141 if (isset($item['thumbnail_width'])) {
142 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
144 echo html_writer
::empty_tag('img', array('src' => $item['thumbnail'], 'alt' => '', 'style' => $style));
146 if (!empty($item['url'])) {
147 echo html_writer
::link($item['url'], s($item['title']), array('target'=>'_blank'));
149 echo s($item['title']);
153 echo '<form method="post">';
154 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
155 echo '<input type="hidden" name="action" value="confirm"/>';
156 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
157 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
158 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
164 } catch (repository_exception
$e) {
170 echo $OUTPUT->header();
172 echo $OUTPUT->container_start();
173 echo html_writer
::link($url, get_string('back', 'repository'));
174 echo $OUTPUT->container_end();
176 if ($repo->check_login()) {
177 $list = $repo->get_listing($req_path, $curr_page);
178 $dynload = !empty($list['dynload'])?
true:false;
179 if (!empty($list['upload'])) {
180 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
181 echo '<label>'.$list['upload']['label'].': </label>';
182 echo '<input type="file" name="repo_upload_file" /><br />';
183 echo '<input type="hidden" name="action" value="upload" /><br />';
184 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
185 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
186 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
187 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
190 if (!empty($list['path'])) {
191 foreach ($list['path'] as $p) {
192 //echo '<form method="post" style="display:inline">';
193 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
194 //echo '<input type="hidden" name="action" value="list"';
195 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
196 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
197 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
200 $pathurl = new moodle_url($url, array(
203 'draftpath'=>$draftpath,
204 'savepath'=>$savepath
206 echo '<strong>' . html_writer
::link($pathurl, s($p['name'])) . '</strong>';
207 echo '<span> / </span>';
210 if (!empty($list['page'])) {
211 // TODO MDL-28482: need a better solution
212 // paging_bar is not a good option because it starts page numbering from 0 and
213 // repositories number pages starting from 1.
214 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
215 if (!isset($list['perpage']) && !isset($list['total'])) {
216 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
218 if (empty($list['total'])) {
219 if ($list['pages'] == -1) {
220 $total = ($list['page'] +
2) * $list['perpage'];
222 $total = $list['pages'] * $list['perpage'];
225 $total = $list['total'];
227 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
230 foreach ($list['list'] as $item) {
234 if (isset($item['thumbnail_height'])) {
235 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
237 if (isset($item['thumbnail_width'])) {
238 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
240 echo html_writer
::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style));
242 if (!empty($item['url'])) {
243 echo html_writer
::link($item['url'], s($item['title']), array('target'=>'_blank'));
245 echo s($item['title']);
249 if (!isset($item['children'])) {
250 echo '<form method="post">';
251 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
252 echo '<input type="hidden" name="action" value="confirm"/>';
253 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
254 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
255 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
256 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
257 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
260 echo '<form method="post">';
261 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
262 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
271 echo '<form method="post">';
272 echo '<input type="hidden" name="action" value="sign" />';
273 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
274 $repo->print_login();
277 echo $OUTPUT->footer();
281 // Check that user has permission to access this file
282 if (!$repo->file_is_accessible($fileurl)) {
283 print_error('storedfilecannotread');
285 $record = new stdClass();
286 $reference = $repo->get_file_reference($fileurl);
288 $sourcefield = $repo->get_file_source_info($fileurl);
289 $record->source
= repository
::build_source_field($sourcefield);
291 // If file is already a reference, set $fileurl = file source, $repo = file repository
292 // note that in this case user may not have permission to access the source file directly
293 // so no file_browser/file_info can be used below
294 if ($repo->has_moodle_files()) {
295 $file = repository
::get_moodle_file($fileurl);
296 if ($file && $file->is_external_file()) {
297 $sourcefield = $file->get_source(); // remember the original source
298 $record->source
= $repo::build_source_field($sourcefield);
299 $reference = $file->get_reference();
300 $repo_id = $file->get_repository_id();
301 $repo = repository
::get_repository_by_id($repo_id, $contextid, $repooptions);
305 $record->filepath
= $savepath;
306 $record->filename
= $filename;
307 $record->component
= 'user';
308 $record->filearea
= 'draft';
309 $record->itemid
= $itemid;
310 $record->license
= '';
311 $record->author
= '';
314 $record->timecreated
= $now;
315 $record->timemodified
= $now;
316 $record->userid
= $USER->id
;
317 $record->contextid
= $user_context->id
;
318 $record->sortorder
= 0;
320 if ($repo->has_moodle_files()) {
321 $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes);
322 redirect($home_url, get_string('downloadsucc', 'repository'));
324 $thefile = $repo->get_file($reference, $filename);
325 if (!empty($thefile['path'])) {
326 $filesize = filesize($thefile['path']);
327 if ($maxbytes != -1 && $filesize>$maxbytes) {
328 unlink($thefile['path']);
329 print_error('maxbytes');
332 $info = repository
::move_to_filepool($thefile['path'], $record);
333 redirect($home_url, get_string('downloadsucc', 'repository'));
334 } catch (moodle_exception
$e) {
336 $e->link
= $PAGE->url
->out();
337 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
341 print_error('cannotdownload', 'repository');
348 echo $OUTPUT->header();
349 echo '<div><a href="'.s($PAGE->url
->out(false)).'">'.get_string('back', 'repository').'</a></div>';
350 echo '<img src="'.$thumbnail.'" />';
351 echo '<form method="post">';
354 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
355 echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
356 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
357 echo ' <td><input type="hidden" name="action" value="download" /></td>';
358 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
363 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
364 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
365 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
368 echo $OUTPUT->footer();
374 $params['context'] = array($user_context, get_system_context());
375 $params['currentcontext'] = $PAGE->context
;
376 $params['return_types'] = FILE_INTERNAL
;
378 $repos = repository
::get_instances($params);
379 echo $OUTPUT->header();
380 echo html_writer
::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
383 foreach($repos as $repo) {
384 $info = $repo->get_meta();
387 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id
, 'draftpath'=>$draftpath));
390 echo html_writer
::empty_tag('img', array('src'=>$info->icon
, 'alt'=>$info->name
, 'class'=>'icon icon-pre'));
391 echo html_writer
::link($aurl, s($info->name
));
396 echo $OUTPUT->footer();