8e82d8b8a8c7b47ae6cd0ad9d2bb950b3dcf739b
[moodle.git] / files / filebrowser_ajax.php
1 <?php
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/>.
18 /**
19  * File manager support
20  *
21  * @package    core
22  * @subpackage file
23  * @copyright  2010 Dongsheng Cai <dongsheng@moodle.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 define('AJAX_SCRIPT', true);
29 require('../config.php');
30 require_once($CFG->libdir.'/filelib.php');
32 $action = optional_param('action', 'list', PARAM_ALPHA);
34 require_login();
36 $err = new stdclass;
38 if (isguestuser()) {
39     $err->error = get_string('noguest');
40     die(json_encode($err));
41 }
43 switch ($action) {
44     // used by course file tree viewer
45     case 'getfiletree':
47         $contextid  = required_param('contextid', PARAM_INT);
48         $component  = required_param('component', PARAM_ALPHAEXT);
49         $filearea   = required_param('filearea', PARAM_ALPHAEXT);
50         $itemid     = required_param('itemid', PARAM_INT);
51         $filepath   = required_param('filepath', PARAM_PATH);
53         $browser = get_file_browser();
54         $params = $url->params();
55         // fix empty value
56         foreach ($params as $key=>$value) {
57             if ($value==='') {
58                 $params[$key] = null;
59             }
60         }
61         $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $component, $filearea, $itemid, $filepath);
62         $children = $fileinfo->get_children();
63         $tree = array();
64         foreach ($children as $child) {
65             $filedate = $child->get_timemodified();
66             $filesize = $child->get_filesize();
67             $mimetype = $child->get_mimetype();
68             $params = $child->get_params();
69             $url = new moodle_url('/files/index.php', $params);
70             $fileitem = array(
71                     'params'=>$params,
72                     'filename'=>$child->get_visible_name(),
73                     'filedate'=>$filedate ? userdate($filedate) : '',
74                     'filesize'=>$filesize ? display_size($filesize) : '',
75                     );
76             if ($child->is_directory()) {
77                 $fileitem['isdir'] = true;
78                 $fileitem['url'] = $url->out();
79             } else {
80                 $fileitem['url'] = $child->get_url();
81             }
82             $tree[] = $fileitem;
83         }
84         echo json_encode($tree);
86         break;
88     default:
89         break;
90 }