MDL-22950 adding new component column to the files table, unfortunately this change...
[moodle.git] / repository / draftfiles_ajax.php
CommitLineData
64f93798
PS
1<?php
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/>.
17
18/**
19 * Draft file ajax file manager
20 *
21 * @package core
22 * @subpackage repository
23 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27define('AJAX_SCRIPT', true);
28
29require('../config.php');
30require_once($CFG->libdir.'/filelib.php');
31require_once($CFG->libdir.'/adminlib.php');
32
33require_login();
34if (isguestuser()) {
35 print_error('noguest');
36}
37require_sesskey();
38
39$action = required_param('action', PARAM_ALPHA);
40$draftid = required_param('itemid', PARAM_INT);
41
42$user_context = get_context_instance(CONTEXT_USER, $USER->id);
43
44//
45//NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!!
46//
47
48switch ($action) {
49 case 'dir':
50 $data = new stdclass;
51 file_get_drafarea_folders($draftid, $filepath, $data);
52 echo json_encode($data);
53 die;
54
55 case 'list':
56 $filepath = optional_param('filepath', '/', PARAM_PATH);
57
58 $data = file_get_drafarea_files($draftid, $filepath);
59 echo json_encode($data);
60 die;
61
62 case 'mkdir':
63 $filepath = required_param('filepath', PARAM_PATH);
64 $newdirname = required_param('newdirname', PARAM_FILE);
65
66 $fs = get_file_storage();
67 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
68 $return = new stdclass;
69 $return->filepath = $filepath;
70 echo json_encode($return);
71 die;
72
73 case 'delete':
74 $filename = required_param('filename', PARAM_FILE);
75 $filepath = required_param('filepath', PARAM_PATH);
76
77 $fs = get_file_storage();
78 $filepath = file_correct_filepath($filepath);
79 $return = new stdclass;
80 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
81 $parent_path = $stored_file->get_parent_directory()->get_filepath();
82 if($result = $stored_file->delete()) {
83 $return->filepath = $parent_path;
84 echo json_encode($return);
85 } else {
86 echo json_encode(false);
87 }
88 } else {
89 echo json_encode(false);
90 }
91 die;
92
93 case 'setmainfile':
94 $filename = required_param('filename', PARAM_FILE);
95 $filepath = required_param('filepath', PARAM_PATH);
96
97 $filepath = file_correct_filepath($filepath);
98 // reset sort order
99 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
100 // set main file
101 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
102 echo json_encode($return);
103 die;
104
105 case 'rename':
106 $filename = required_param('filename', PARAM_FILE);
107 $filepath = required_param('filepath', PARAM_PATH);
108 $newfilename = required_param('newfilename', PARAM_FILE);
109
110 $fs = get_file_storage();
111 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $filepath, $newfilename)) {
112 //bad luck, we can not rename!
113 echo json_encode(false);
114 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
115 $return = new object();
116 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
117 $file->delete();
118 $return->filepath = $newfile->get_filepath();
119 echo json_encode($return);
120 } else {
121 echo json_encode(false);
122 }
123 die;
124
125 case 'renamedir':
126 $filepath = required_param('filepath', PARAM_PATH);
127 $newdirname = required_param('newdirname', PARAM_FILE);
128
129 //TODO: we must update all children too - I am going to implement low level move in file_storage (skodak)
130 echo json_encode(false);
131 die;
132
133 case 'movedir':
134 $filepath = required_param('filepath', PARAM_PATH);
135 $newfilepath = required_param('newfilepath', PARAM_PATH);
136
137 //TODO: we must update all children too - I am going to implement low level move in file_storage (skodak)
138 echo json_encode(false);
139 die;
140
141 case 'movefile':
142 $filename = required_param('filename', PARAM_FILE);
143 $filepath = required_param('filepath', PARAM_PATH);
144 $newfilepath = required_param('newfilepath', PARAM_PATH);
145
146 $fs = get_file_storage();
147 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $filename)) {
148 //bad luck, we can not rename!
149 echo json_encode(false);
150 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
151 $return = new object();
152 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$newfilepath), $file);
153 $file->delete();
154 $return->filepath = $newfile->get_filepath();
155 echo json_encode($return);
156 } else {
157 echo json_encode(false);
158 }
159 die;
160
161 case 'zip':
162 $filepath = required_param('filepath', PARAM_PATH);
163
164 $zipper = get_file_packer('application/zip');
165 $fs = get_file_storage();
166
167 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
168
169 $parent_path = $file->get_parent_directory()->get_filepath();
170
171 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
172 $return = new stdclass;
173 $return->filepath = $parent_path;
174 echo json_encode($return);
175 } else {
176 echo json_encode(false);
177 }
178 die;
179
180 case 'downloaddir':
181 $filepath = required_param('filepath', PARAM_PATH);
182
183 $zipper = get_file_packer('application/zip');
184 $fs = get_file_storage();
185 $area = file_get_draft_area_info($draftid);
186 if ($area['filecount'] == 0) {
187 echo json_encode(false);
188 die;
189 }
190
191 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
192 if ($filepath === '/') {
193 $parent_path = '/';
194 $filename = get_string('files').'.zip';
195 } else {
196 $parent_path = $stored_file->get_parent_directory()->get_filepath();
197 $filename = trim($filepath, '/').'.zip';
198 }
199
200 // archive compressed file to an unused draft area
201 $newdraftitemid = file_get_unused_draft_itemid();
202 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
203 $return = new stdclass;
204 $return->fileurl = file_draftfile_url($newdraftitemid, '/', $filename);
205 $return->filepath = $parent_path;
206 echo json_encode($return);
207 } else {
208 echo json_encode(false);
209 }
210 die;
211
212 case 'unzip':
213 $filename = required_param('filename', PARAM_FILE);
214 $filepath = required_param('filepath', PARAM_PATH);
215
216 $zipper = get_file_packer('application/zip');
217
218 $fs = get_file_storage();
219
220 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
221
222 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
223 $return = new stdclass;
224 $return->filepath = $filepath;
225 echo json_encode($return);
226 } else {
227 echo json_encode(false);
228 }
229 die;
230
231 default:
232 // no/unknown action?
233 echo json_encode(false);
234 die;
235}