MDL-34052 assignment - Disable submission and feedback plugins based on assign type...
[moodle.git] / repository / draftfiles_ajax.php
blob3857a0da965eedd7f7ea192abdd14e95b7703b69
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.
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 * Draft file ajax file manager
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
27 define('AJAX_SCRIPT', true);
29 require('../config.php');
30 require_once($CFG->libdir.'/filelib.php');
31 require_once($CFG->libdir.'/adminlib.php');
32 require_once($CFG->dirroot.'/repository/lib.php');
33 $PAGE->set_context(get_system_context());
34 require_login();
35 if (isguestuser()) {
36 print_error('noguest');
38 require_sesskey();
40 $action = required_param('action', PARAM_ALPHA);
41 $draftid = required_param('itemid', PARAM_INT);
42 $filepath = optional_param('filepath', '/', PARAM_PATH);
44 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
46 echo $OUTPUT->header(); // send headers
49 //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!!
52 switch ($action) {
53 case 'dir':
54 $data = new stdClass();
55 file_get_drafarea_folders($draftid, $filepath, $data);
56 echo json_encode($data);
57 die;
59 case 'list':
60 $filepath = optional_param('filepath', '/', PARAM_PATH);
62 $data = repository::prepare_listing(file_get_drafarea_files($draftid, $filepath));
63 $info = file_get_draft_area_info($draftid);
64 $data->filecount = $info['filecount'];
65 $data->filesize = $info['filesize'];
66 $data->tree = new stdClass();
67 file_get_drafarea_folders($draftid, '/', $data->tree);
68 echo json_encode($data);
69 die;
71 case 'mkdir':
72 $filepath = required_param('filepath', PARAM_PATH);
73 $newdirname = required_param('newdirname', PARAM_FILE);
75 $fs = get_file_storage();
76 $fs->create_directory($user_context->id, 'user', 'draft', $draftid, file_correct_filepath(file_correct_filepath($filepath).$newdirname));
77 $return = new stdClass();
78 $return->filepath = $filepath;
79 echo json_encode($return);
80 die;
82 case 'delete':
83 $filename = required_param('filename', PARAM_FILE);
84 $filepath = required_param('filepath', PARAM_PATH);
86 $fs = get_file_storage();
87 $filepath = file_correct_filepath($filepath);
88 $return = new stdClass();
89 if ($stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename)) {
90 $parent_path = $stored_file->get_parent_directory()->get_filepath();
91 if ($stored_file->is_directory()) {
92 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $filepath, true);
93 foreach ($files as $file) {
94 $file->delete();
96 $stored_file->delete();
97 $return->filepath = $parent_path;
98 echo json_encode($return);
99 } else {
100 if($result = $stored_file->delete()) {
101 $return->filepath = $parent_path;
102 echo json_encode($return);
103 } else {
104 echo json_encode(false);
107 } else {
108 echo json_encode(false);
110 die;
112 case 'setmainfile':
113 $filename = required_param('filename', PARAM_FILE);
114 $filepath = required_param('filepath', PARAM_PATH);
116 $filepath = file_correct_filepath($filepath);
117 // reset sort order
118 file_reset_sortorder($user_context->id, 'user', 'draft', $draftid);
119 // set main file
120 $return = file_set_sortorder($user_context->id, 'user', 'draft', $draftid, $filepath, $filename, 1);
121 echo json_encode($return);
122 die;
124 case 'updatefile':
125 // Allows to Rename file, move it to another directory, change it's license and author information in one request
126 $filename = required_param('filename', PARAM_FILE);
127 $filepath = required_param('filepath', PARAM_PATH);
129 $fs = get_file_storage();
130 if (!($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename))) {
131 die(json_encode((object)array('error' => get_string('filenotfound', 'error'))));
134 $updatedata = array();
135 $updatedata['filename'] = $newfilename = optional_param('newfilename', $file->get_filename(), PARAM_FILE);
136 $updatedata['filepath'] = $newfilepath = optional_param('newfilepath', $file->get_filepath(), PARAM_PATH);
137 $updatedata['license'] = optional_param('newlicense', $file->get_license(), PARAM_TEXT);
138 $updatedata['author'] = optional_param('newauthor', $file->get_author(), PARAM_TEXT);
139 foreach ($updatedata as $key => $value) {
140 if (''.$value === ''.$file->{'get_'.$key}()) {
141 unset($updatedata[$key]);
145 if (!empty($updatedata)) {
146 if (array_key_exists('filename', $updatedata) || array_key_exists('filepath', $updatedata)) {
147 // check that target file name does not exist
148 if ($fs->file_exists($user_context->id, 'user', 'draft', $draftid, $newfilepath, $newfilename)) {
149 die(json_encode((object)array('error' => get_string('fileexists', 'repository'))));
151 $file->rename($newfilepath, $newfilename);
153 if (array_key_exists('license', $updatedata)) {
154 $file->set_license($updatedata['license']);
156 if (array_key_exists('author', $updatedata)) {
157 $file->set_author($updatedata['author']);
159 $changes = array_diff(array_keys($updatedata), array('filepath'));
160 if (!empty($changes)) {
161 // any change except for the moving to another folder alters 'Date modified' of the file
162 $file->set_timemodified(time());
166 die(json_encode((object)array('filepath' => $newfilepath)));
168 case 'updatedir':
169 $filepath = required_param('filepath', PARAM_PATH);
170 $fs = get_file_storage();
171 if (!$dir = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) {
172 die(json_encode((object)array('error' => get_string('foldernotfound', 'repository'))));
174 $parts = explode('/', trim($dir->get_filepath(), '/'));
175 $dirname = end($parts);
176 $newdirname = required_param('newdirname', PARAM_FILE);
177 $parent = required_param('newfilepath', PARAM_PATH);
178 $newfilepath = clean_param($parent . '/' . $newdirname . '/', PARAM_PATH);
179 if ($newfilepath == $filepath) {
180 // no action required
181 die(json_encode((object)array('filepath' => $parent)));
183 if ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $newfilepath, true)) {
184 //bad luck, we can not rename if something already exists there
185 die(json_encode((object)array('error' => get_string('folderexists', 'repository'))));
187 $xfilepath = preg_quote($filepath, '|');
188 if (preg_match("|^$xfilepath|", $parent)) {
189 // we can not move folder to it's own subfolder
190 die(json_encode((object)array('error' => get_string('folderrecurse', 'repository'))));
193 //we must update directory and all children too
194 $files = $fs->get_area_files($user_context->id, 'user', 'draft', $draftid);
195 foreach ($files as $file) {
196 if (!preg_match("|^$xfilepath|", $file->get_filepath())) {
197 continue;
199 // move one by one
200 $path = preg_replace("|^$xfilepath|", $newfilepath, $file->get_filepath());
201 if ($dirname !== $newdirname && $file->get_filepath() === $filepath && $file->get_filename() === '.') {
202 // this is the main directory we move/rename AND it has actually been renamed
203 $file->set_timemodified(time());
205 $file->rename($path, $file->get_filename());
208 $return = new stdClass();
209 $return->filepath = $parent;
210 echo json_encode($return);
211 die;
213 case 'zip':
214 $filepath = required_param('filepath', PARAM_PATH);
216 $zipper = get_file_packer('application/zip');
217 $fs = get_file_storage();
219 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
221 $parent_path = $file->get_parent_directory()->get_filepath();
223 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $draftid, $parent_path, $filepath.'.zip', $USER->id)) {
224 $return = new stdClass();
225 $return->filepath = $parent_path;
226 echo json_encode($return);
227 } else {
228 echo json_encode(false);
230 die;
232 case 'downloaddir':
233 $filepath = required_param('filepath', PARAM_PATH);
235 $zipper = get_file_packer('application/zip');
236 $fs = get_file_storage();
237 $area = file_get_draft_area_info($draftid);
238 if ($area['filecount'] == 0) {
239 echo json_encode(false);
240 die;
243 $stored_file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.');
244 if ($filepath === '/') {
245 $parent_path = '/';
246 $filename = get_string('files').'.zip';
247 } else {
248 $parent_path = $stored_file->get_parent_directory()->get_filepath();
249 $filename = trim($filepath, '/').'.zip';
252 // archive compressed file to an unused draft area
253 $newdraftitemid = file_get_unused_draft_itemid();
254 if ($newfile = $zipper->archive_to_storage(array($stored_file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
255 $return = new stdClass();
256 $return->fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
257 $return->filepath = $parent_path;
258 echo json_encode($return);
259 } else {
260 echo json_encode(false);
262 die;
264 case 'unzip':
265 $filename = required_param('filename', PARAM_FILE);
266 $filepath = required_param('filepath', PARAM_PATH);
268 $zipper = get_file_packer('application/zip');
270 $fs = get_file_storage();
272 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
274 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $filepath, $USER->id)) {
275 $return = new stdClass();
276 $return->filepath = $filepath;
277 echo json_encode($return);
278 } else {
279 echo json_encode(false);
281 die;
283 case 'getoriginal':
284 $filename = required_param('filename', PARAM_FILE);
285 $filepath = required_param('filepath', PARAM_PATH);
287 $fs = get_file_storage();
288 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
289 if (!$file) {
290 echo json_encode(false);
291 } else {
292 $return = array('filename' => $filename, 'filepath' => $filepath, 'original' => $file->get_reference_details());
293 echo json_encode((object)$return);
295 die;
297 case 'getreferences':
298 $filename = required_param('filename', PARAM_FILE);
299 $filepath = required_param('filepath', PARAM_PATH);
301 $fs = get_file_storage();
302 $file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, $filename);
303 if (!$file) {
304 echo json_encode(false);
305 } else {
306 $source = unserialize($file->get_source());
307 $return = array('filename' => $filename, 'filepath' => $filepath, 'references' => array());
308 $browser = get_file_browser();
309 if (isset($source->original)) {
310 $reffiles = $fs->search_references($source->original);
311 foreach ($reffiles as $reffile) {
312 $refcontext = get_context_instance_by_id($reffile->get_contextid());
313 $fileinfo = $browser->get_file_info($refcontext, $reffile->get_component(), $reffile->get_filearea(), $reffile->get_itemid(), $reffile->get_filepath(), $reffile->get_filename());
314 if (empty($fileinfo)) {
315 $return['references'][] = get_string('undisclosedreference', 'repository');
316 } else {
317 $return['references'][] = $fileinfo->get_readable_fullname();
321 echo json_encode((object)$return);
323 die;
325 default:
326 // no/unknown action?
327 echo json_encode(false);
328 die;