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
63 // the path to save files
64 $savepath = optional_param('savepath', '/', PARAM_PATH);
66 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
70 $user_context = context_user::instance($USER->id);
72 $PAGE->set_context($user_context);
73 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
74 print_error('invalidcourseid');
76 $PAGE->set_course($course);
78 // init repository plugin
79 $sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i '.
80 'WHERE i.id=? AND i.typeid=r.id';
81 if ($repository = $DB->get_record_sql($sql, array($repo_id))) {
82 $type = $repository->type;
83 if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
84 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
85 $classname = 'repository_' . $type;
87 $repo = new $classname($repo_id, $contextid, array('ajax'=>false, 'name'=>$repository->name, 'type'=>$type));
88 } catch (repository_exception $e){
89 print_error('pluginerror', 'repository');
92 print_error('invalidplugin', 'repository');
96 $context = context::instance_by_id($contextid);
97 $moodle_maxbytes = get_user_max_upload_file_size($context);
98 // to prevent maxbytes greater than moodle maxbytes setting
99 if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
100 $maxbytes = $moodle_maxbytes;
103 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
104 $params['action'] = 'browse';
105 $params['draftpath'] = $draftpath;
106 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
108 $params['savepath'] = $savepath;
109 $params['repo_id'] = $repo_id;
110 $url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
111 $PAGE->set_url('/repository/filepicker.php', $params);
115 // The uploaded file has been processed in plugin construct function
116 // redirect to default page
118 $repo->upload('', $maxbytes);
119 redirect($home_url, get_string('uploadsucc','repository'));
120 } catch (moodle_exception $e) {
122 $e->link = $PAGE->url->out();
123 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
129 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
131 $search_result = $repo->search($search_text);
132 $search_result['issearchresult'] = true;
133 $search_result['repo_id'] = $repo_id;
135 // TODO: need a better solution
136 $purl = new moodle_url($url, array('search_paging' => 1, 'action' => 'search', 'repo_id' => $repo_id));
137 $pagingbar = new paging_bar($search_result['total'], $search_result['page'] - 1, $search_result['perpage'], $purl, 'p');
138 echo $OUTPUT->render($pagingbar);
141 foreach ($search_result['list'] as $item) {
145 if (isset($item['thumbnail_height'])) {
146 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
148 if (isset($item['thumbnail_width'])) {
149 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
151 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style));
153 if (!empty($item['url'])) {
154 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
160 echo '<form method="post">';
161 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
162 echo '<input type="hidden" name="action" value="confirm"/>';
163 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
164 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
165 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
171 } catch (repository_exception $e) {
177 echo $OUTPUT->header();
179 echo $OUTPUT->container_start();
180 echo html_writer::link($url, get_string('back', 'repository'));
181 echo $OUTPUT->container_end();
183 if ($repo->check_login()) {
184 $list = $repo->get_listing($req_path, $curr_page);
185 $dynload = !empty($list['dynload'])?true:false;
186 if (!empty($list['upload'])) {
187 echo '<form action="'.$url->out().'" method="post" enctype="multipart/form-data" style="display:inline">';
188 echo '<label>'.$list['upload']['label'].': </label>';
189 echo '<input type="file" name="repo_upload_file" /><br />';
190 echo '<input type="hidden" name="action" value="upload" /><br />';
191 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
192 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
193 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" /><br />';
194 echo '<input type="submit" value="'.s(get_string('upload', 'repository')).'" />';
197 if (!empty($list['path'])) {
198 foreach ($list['path'] as $p) {
199 //echo '<form method="post" style="display:inline">';
200 //echo '<input type="hidden" name="p" value="'.s($p['path']).'"';
201 //echo '<input type="hidden" name="action" value="list"';
202 //echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
203 //echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
204 //echo '<input style="display:inline" type="submit" value="'.s($p['name']).'" />';
207 $pathurl = new moodle_url($url, array(
210 'draftpath'=>$draftpath,
211 'savepath'=>$savepath
213 echo '<strong>' . html_writer::link($pathurl, $p['name']) . '</strong>';
214 echo '<span> / </span>';
217 if (!empty($list['page'])) {
218 // TODO MDL-28482: need a better solution
219 // paging_bar is not a good option because it starts page numbering from 0 and
220 // repositories number pages starting from 1.
221 $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
222 if (!isset($list['perpage']) && !isset($list['total'])) {
223 $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
225 if (empty($list['total'])) {
226 if ($list['pages'] == -1) {
227 $total = ($list['page'] + 2) * $list['perpage'];
229 $total = $list['pages'] * $list['perpage'];
232 $total = $list['total'];
234 echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
237 foreach ($list['list'] as $item) {
241 if (isset($item['thumbnail_height'])) {
242 $style .= 'max-height:'.$item['thumbnail_height'].'px;';
244 if (isset($item['thumbnail_width'])) {
245 $style .= 'max-width:'.$item['thumbnail_width'].'px;';
247 echo html_writer::empty_tag('img', array('src' => $item['thumbnail'], 'style' => $style));
249 if (!empty($item['url'])) {
250 echo html_writer::link($item['url'], $item['title'], array('target'=>'_blank'));
256 if (!isset($item['children'])) {
257 echo '<form method="post">';
258 echo '<input type="hidden" name="fileurl" value="'.s($item['source']).'"/>';
259 echo '<input type="hidden" name="action" value="confirm"/>';
260 echo '<input type="hidden" name="draftpath" value="'.s($draftpath).'" /><br />';
261 echo '<input type="hidden" name="savepath" value="'.s($savepath).'" /><br />';
262 echo '<input type="hidden" name="filename" value="'.s($item['title']).'"/>';
263 echo '<input type="hidden" name="thumbnail" value="'.s($item['thumbnail']).'"/>';
264 echo '<input type="submit" value="'.s(get_string('select','repository')).'" />';
267 echo '<form method="post">';
268 echo '<input type="hidden" name="p" value="'.s($item['path']).'"/>';
269 echo '<input type="submit" value="'.s(get_string('enter', 'repository')).'" />';
278 echo '<form method="post">';
279 echo '<input type="hidden" name="action" value="sign" />';
280 echo '<input type="hidden" name="repo_id" value="'.s($repo_id).'" />';
281 $repo->print_login();
284 echo $OUTPUT->footer();
288 $thefile = $repo->get_file($fileurl, $filename);
289 $filesize = filesize($thefile['path']);
290 if (($maxbytes!=-1) && ($filesize>$maxbytes)) {
291 print_error('maxbytes');
293 if (!empty($thefile)) {
294 $record = new stdClass();
295 $record->filepath = $savepath;
296 $record->filename = $filename;
297 $record->component = 'user';
298 $record->filearea = 'draft';
299 $record->itemid = $itemid;
300 $record->license = '';
301 $record->author = '';
304 $record->timecreated = $now;
305 $record->timemodified = $now;
306 $record->userid = $USER->id;
307 $record->contextid = $user_context->id;
309 $sourcefield = $repo->get_file_source_info($thefile['url']);
310 $record->source = repository::build_source_field($sourcefield);
312 $info = repository::move_to_filepool($thefile['path'], $record);
313 redirect($home_url, get_string('downloadsucc', 'repository'));
314 } catch (moodle_exception $e) {
316 $e->link = $PAGE->url->out();
317 echo $OUTPUT->header(); // hack: we need the embedded header here, standard error printing would not use it
321 print_error('cannotdownload', 'repository');
327 echo $OUTPUT->header();
328 echo '<div><a href="'.s($PAGE->url->out(false)).'">'.get_string('back', 'repository').'</a></div>';
329 echo '<img src="'.$thumbnail.'" />';
330 echo '<form method="post">';
333 echo ' <td><label>'.get_string('filename', 'repository').'</label></td>';
334 echo ' <td><input type="text" name="filename" value="'.s($filename).'" /></td>';
335 echo ' <td><input type="hidden" name="fileurl" value="'.s($fileurl).'" /></td>';
336 echo ' <td><input type="hidden" name="action" value="download" /></td>';
337 echo ' <td><input type="hidden" name="itemid" value="'.s($itemid).'" /></td>';
342 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
343 echo ' <input name="savepath" type="hidden" value="'.s($savepath).'" />';
344 echo ' <input type="submit" value="'.s(get_string('download', 'repository')).'" />';
347 echo $OUTPUT->footer();
353 $params['context'] = array($user_context, get_system_context());
354 $params['currentcontext'] = $PAGE->context;
355 $params['return_types'] = FILE_INTERNAL;
357 $repos = repository::get_instances($params);
358 echo $OUTPUT->header();
359 echo html_writer::link($home_url->out(false), get_string('backtodraftfiles', 'repository'));
362 foreach($repos as $repo) {
363 $info = $repo->get_meta();
366 $aurl->params(array('savepath'=>$savepath, 'action' => 'list', 'repo_id' => $info->id, 'draftpath'=>$draftpath));
369 echo '<img src="'.$info->icon.'" alt="'.$info->name.'" width="16" height="16" /> ';
370 echo html_writer::link($aurl, $info->name);
375 echo $OUTPUT->footer();