gradebook MDL-15457 hide grade item when activity is hidden in the course
[moodle.git] / repository / repository_ajax.php
CommitLineData
aa6c1ced 1<?php
5bce5972 2
6f2cd52a
DC
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/**
20 * The Web service script that is called from the filepicker front end
21 *
22 * @since 2.0
23 * @package moodlecore
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
27 */
455860ce 28
e35194be
DC
29define('AJAX_SCRIPT', true);
30
14469892
DC
31require_once(dirname(dirname(__FILE__)).'/config.php');
32require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
33require_once(dirname(__FILE__).'/lib.php');
99eaca9d 34
dd0566eb
DC
35$err = new stdclass;
36
d0f8585d 37/// Parameters
9d4ef80f 38$action = optional_param('action', '', PARAM_ALPHA);
4277133e 39$repo_id = optional_param('repo_id', 0, PARAM_INT); // Repository ID
3e123368
DC
40$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID
41$env = optional_param('env', 'filepicker', PARAM_ALPHA); // Opened in editor or moodleform
1dce6261 42$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
3e123368
DC
43$author = optional_param('author', '', PARAM_TEXT); // File author
44$source = optional_param('source', '', PARAM_RAW); // File to download
45$itemid = optional_param('itemid', 0, PARAM_INT); // Itemid
46$page = optional_param('page', '', PARAM_RAW); // Page
47$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes
48$req_path = optional_param('p', '', PARAM_RAW); // Path
e35194be 49$accepted_types = optional_param('accepted_types', '*', PARAM_RAW);
3e123368
DC
50$saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name
51$saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path
9d4ef80f
DC
52$search_text = optional_param('s', '', PARAM_CLEANHTML);
53$linkexternal = optional_param('linkexternal', '', PARAM_ALPHA);
455860ce 54
196c0451
PS
55list($context, $course, $cm) = get_context_info_array($contextid);
56require_login($course, false, $cm);
57$PAGE->set_context($context);
58
bce08d9a 59echo $OUTPUT->header(); // send headers
9d4ef80f 60
6127179b
DC
61if (!confirm_sesskey()) {
62 $err->error = get_string('invalidsesskey');
63 die(json_encode($err));
64}
65
be85f7ab
DC
66/// Get repository instance information
67$sql = 'SELECT i.name, i.typeid, r.type FROM {repository} r, {repository_instances} i WHERE i.id=? AND i.typeid=r.id';
68
69if (!$repository = $DB->get_record_sql($sql, array($repo_id))) {
70 $err->error = get_string('invalidrepositoryid', 'repository');
71 die(json_encode($err));
72} else {
73 $type = $repository->type;
74}
75
76/// Check permissions
77repository::check_capability($contextid, $repository);
78
3e123368
DC
79$moodle_maxbytes = get_max_upload_file_size();
80// to prevent maxbytes greater than moodle maxbytes setting
81if ($maxbytes == 0 || $maxbytes>=$moodle_maxbytes) {
82 $maxbytes = $moodle_maxbytes;
83}
84
577aab9b 85/// Wait as long as it takes for this script to finish
9d4ef80f 86set_time_limit(0);
577aab9b 87
4277133e 88// Early actions which need to be done before repository instances initialised
9d4ef80f
DC
89switch ($action) {
90 // global search
91 case 'gsearch':
92 $params = array();
93 $params['context'] = array(get_context_instance_by_id($contextid), get_system_context());
94 $params['currentcontext'] = get_context_instance_by_id($contextid);
95 $repos = repository::get_instances($params);
96 $list = array();
97 foreach($repos as $repo){
98 if ($repo->global_search()) {
e35194be
DC
99 $ret = $repo->search($search_text);
100 array_walk($ret['list'], 'repository_attach_id', $repo->id); // See function below
101 $tmp = array_merge($list, $ret['list']);
102 $list = $tmp;
d0f8585d 103 }
9d4ef80f
DC
104 }
105 $listing = array('list'=>$list);
106 $listing['gsearch'] = true;
9d4ef80f
DC
107 die(json_encode($listing));
108 break;
d0f8585d 109
9d4ef80f
DC
110 // remove the cache files & logout
111 case 'ccache':
112 $cache = new curl_cache;
113 $cache->refresh();
114 $action = 'list';
115 break;
116}
d0f8585d 117
e35194be
DC
118if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
119 require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
9d4ef80f 120 $classname = 'repository_' . $type;
e35194be 121 $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type));
a06878d3 122} else {
e35194be 123 $err->error = get_string('invalidplugin', 'repository', $type);
9d4ef80f 124 die(json_encode($err));
a06878d3 125}
9d4ef80f 126
d0f8585d 127/// These actions all occur on the currently active repository instance
9d4ef80f
DC
128switch ($action) {
129 case 'sign':
130 case 'signin':
131 case 'list':
132 if ($repo->check_login()) {
e35194be
DC
133 $listing = $repo->get_listing($req_path, $page);
134 $listing['repo_id'] = $repo_id;
135 echo json_encode($listing);
d0f8585d 136 break;
9d4ef80f
DC
137 } else {
138 $action = 'login';
139 }
140 case 'login':
e35194be
DC
141 $listing = $repo->print_login();
142 $listing['repo_id'] = $repo_id;
143 echo json_encode($listing);
9d4ef80f
DC
144 break;
145 case 'logout':
146 $logout = $repo->logout();
9d4ef80f
DC
147 $logout['repo_id'] = $repo_id;
148 echo json_encode($logout);
149 break;
150 case 'searchform':
e35194be 151 $search_form['form'] = $repo->print_search();
9d4ef80f
DC
152 echo json_encode($search_form);
153 break;
154 case 'search':
e35194be
DC
155 $search_result = $repo->search($search_text, (int)$page);
156 $search_result['repo_id'] = $repo_id;
157 $search_result['search_result'] = true;
158 echo json_encode($search_result);
9d4ef80f
DC
159 break;
160 case 'download':
e35194be
DC
161 // validate mimetype
162 $mimetypes = array();
2385e6de 163 if ((is_array($accepted_types) and in_array('*', $accepted_types)) or $accepted_types == '*') {
e35194be
DC
164 $mimetypes = '*';
165 } else {
166 foreach ($accepted_types as $type) {
16f61c70 167 $mimetypes[] = mimeinfo('type', $type);
e35194be
DC
168 }
169 if (!in_array(mimeinfo('type', $saveas_filename), $mimetypes)) {
033aad78 170 throw new moodle_exception('invalidfiletype', 'repository', '', get_string(mimeinfo('type', $saveas_filename), 'mimetypes'));
e35194be
DC
171 }
172 }
173
4277133e 174 // We have two special repository type need to deal with
e35194be
DC
175 // local and recent plugins don't added new files to moodle, just add new records to database
176 // so we don't check user quota and maxbytes here
d3959c26 177 if (in_array($repo->options['type'], array('local', 'recent', 'user', 'coursefiles'))) {
3b61c7e6 178 $fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
e35194be
DC
179 $info = array();
180 $info['file'] = $fileinfo['title'];
181 $info['id'] = $itemid;
16f61c70 182 $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
e35194be
DC
183 $filesize = $fileinfo['filesize'];
184 if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
185 throw new file_exception('maxbytes');
186 }
187 echo json_encode($info);
188 die; // ends here!!
189 } else {
190 $allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
191 if (!empty($allowexternallink)) {
192 $allowexternallink = true;
193 } else {
194 $allowexternallink = false;
195 }
196 // allow external links in url element all the time
197 $allowexternallink = ($allowexternallink || ($env == 'url'));
198
199 // Use link of the files
200 if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
201 // use external link
202 $link = $repo->get_link($source);
9d4ef80f 203 $info = array();
e35194be
DC
204 $info['filename'] = $saveas_filename;
205 $info['type'] = 'link';
206 $info['url'] = $link;
acb70a9b 207 echo json_encode($info);
e35194be 208 die;
9d4ef80f 209 } else {
e35194be
DC
210 // Download file to moodle
211 $file = $repo->get_file($source, $saveas_filename);
212 if ($file['path'] === false) {
213 $err->error = get_string('cannotdownload', 'repository');
214 die(json_encode($err));
99d52655 215 }
ea1780ad 216
e35194be
DC
217 // check if exceed maxbytes
218 if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
219 throw new file_exception('maxbytes');
220 }
14469892 221
e35194be
DC
222 $record = new stdclass;
223 $record->filepath = $saveas_path;
224 $record->filename = $saveas_filename;
225 $record->component = 'user';
226 $record->filearea = 'draft';
227 $record->itemid = $itemid;
14469892 228
e35194be
DC
229 if (!empty($file['license'])) {
230 $record->license = $file['license'];
231 } else {
232 $record->license = $license;
233 }
234 if (!empty($file['author'])) {
235 $record->author = $file['author'];
236 } else {
237 $record->author = $author;
238 }
239 $record->source = !empty($file['url']) ? $file['url'] : '';
1dce6261 240
e35194be
DC
241 $info = repository::move_to_filepool($file['path'], $record);
242 if (empty($info)) {
243 $info['e'] = get_string('error', 'moodle');
acb70a9b 244 }
e35194be
DC
245 echo json_encode($info);
246 die;
9d4ef80f 247 }
9d4ef80f
DC
248 }
249 break;
250 case 'upload':
ac1870e3
DC
251 // handle exception here instead moodle default exception handler
252 // see MDL-23407
253 try {
254 // TODO: add file scanning MDL-19380 into each plugin
255 $result = $repo->upload();
256 echo json_encode($result);
257 } catch (Exception $e) {
258 $err->error = $e->getMessage();
259 echo json_encode($err);
260 die;
261 }
9d4ef80f
DC
262 break;
263}
d0f8585d 264
265/**
266 * Small function to walk an array to attach repository ID
9d4ef80f
DC
267 * @param array $value
268 * @param string $key
269 * @param int $id
d0f8585d 270 */
271function repository_attach_id(&$value, $key, $id){
272 $value['repo_id'] = $id;
0eb58cf4 273}