"MDL-13766, repository settings page"
[moodle.git] / repository / ws.php
CommitLineData
0eb58cf4 1<?php
1e28c767 2set_time_limit(0);
0eb58cf4 3require_once('../config.php');
4require_once('lib.php');
bf1fccf0 5// set one hour here
6$CFG->repository_cache_expire = 60*60;
75e7a38c 7// repository id
bf1fccf0 8$id = optional_param('id', 1, PARAM_INT);
75e7a38c 9// action of client
1e28c767 10$action = optional_param('action', '', PARAM_RAW);
75e7a38c 11// Search text
1e28c767 12$search = optional_param('s', '', PARAM_RAW);
13// files to be downloaded
bb2c046d 14$file = optional_param('file', '', PARAM_RAW);
15$title = optional_param('title', '', PARAM_RAW);
16$p = optional_param('p', '', PARAM_RAW);
5bce5972 17
0eb58cf4 18if(!$repository = $DB->get_record('repository', array('id'=>$id))) {
bf1fccf0 19 $err = new stdclass;
20 $err->e = get_string('invalidrepositoryid', 'repository');
21 die(json_encode($err));
0eb58cf4 22}
23
bf1fccf0 24if(file_exists($CFG->dirroot.'/repository/'.
25 $repository->repositorytype.'/repository.class.php'))
26{
27 require_once($CFG->dirroot.'/repository/'.
28 $repository->repositorytype.'/repository.class.php');
0eb58cf4 29 $classname = 'repository_' . $repository->repositorytype;
bf1fccf0 30 try{
31 $repo = new $classname($id, SITEID, array('ajax'=>true));
32 } catch (repository_exception $e){
33 $err = new stdclass;
34 $err->e = $e->getMessage();
19add4c0 35 die(json_encode($err.time()));
bf1fccf0 36 }
0eb58cf4 37} else {
bf1fccf0 38 $err = new stdclass;
39 $err->e = get_string('invalidplugin', 'repository');
40 die(json_encode($err));
0eb58cf4 41}
42
43if($action == 'list') {
bf1fccf0 44 try {
45 if(!empty($p)) {
46 echo json_encode($repo->get_listing($p));
47 } else if(!empty($search)) {
48 echo json_encode($repo->get_listing('', $search));
49 } else {
50 echo json_encode($repo->get_listing());
51 }
52 } catch (repository_exception $e) {
53 $err = new stdclass;
54 $err->e = $e->getMessage();
55 die(json_encode($err));
5bce5972 56 }
75e7a38c 57
1e28c767 58} elseif($action == 'download') {
bb2c046d 59 $ret = $repo->get_file($file, $title);
60 // TODO
61 // Need to communicate with FILE API
62 // Copy the tmp file to final location
bf1fccf0 63 try {
64 echo json_encode($ret);
65 } catch (repository_exception $e){
66 $err = new stdclass;
67 $err->e = $e->getMessage();
68 die(json_encode($err));
69 }
0eb58cf4 70} else {
bf1fccf0 71 try {
72 echo json_encode($repo->print_login());
73 } catch (repository_exception $e){
74 $err = new stdclass;
75 $err->e = $e->getMessage();
76 die(json_encode($err));
77 }
0eb58cf4 78}
79
80?>