MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / local / repository.class.php
CommitLineData
6a391ebf 1<?php
2/**
3 * repository_local class
4 * This is a subclass of repository class
5 *
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
9
96297ca2 10/**
11 *
12 */
be236ef5 13class repository_local extends repository {
6a391ebf 14
96297ca2 15 /**
16 *
17 * @global <type> $SESSION
18 * @global <type> $action
19 * @global <type> $CFG
20 * @param <type> $repositoryid
21 * @param <type> $context
22 * @param <type> $options
23 */
353d5cf3 24 public function __construct($repositoryid, $context = SITEID, $options = array()) {
6a391ebf 25 global $SESSION, $action, $CFG;
26 parent::__construct($repositoryid, $context, $options);
5fd3e8f7 27 // TODO:
6a391ebf 28 // get the parameter from client side
29 // $this->context can be used here.
5fd3e8f7 30 // When user upload a file, $action == 'upload'
31 // You can use $_FILES to find that file
6a391ebf 32 }
4a65c39a 33
96297ca2 34 /**
35 *
36 * @global <type> $SESSION
37 * @param <type> $ajax
38 * @return <type>
39 */
be236ef5 40 public function print_login($ajax = true) {
6a391ebf 41 global $SESSION;
42 // TODO
43 // Return file list in moodle
6a391ebf 44 return $this->get_listing();
45 }
5fd3e8f7 46
96297ca2 47 /**
48 *
49 * @param <type> $path
50 * @return <type>
51 */
991c9c35 52 private function _decode_path($path) {
53 $filearea = '';
54 $path = '';
55 if (($file = unserialize($path)) !== false) {
56 $filearea = $file[0];
57 $path = $file[1];
58 }
59 return array('filearea' => $filearea, 'path' => $path);
60 }
61
96297ca2 62 /**
63 *
64 * @param <type> $search_text
65 * @return <type>
66 */
353d5cf3 67 public function search($search_text) {
68 return $this->get_listing('', $search_text);
69 }
70
96297ca2 71 /**
72 *
73 * @global <type> $CFG
74 * @param <type> $encodedpath
75 * @param <type> $search
76 * @return <type>
77 */
991c9c35 78 public function get_listing($encodedpath = '', $search = '') {
be236ef5 79 global $CFG;
dbc01944 80
e3ca0f3a 81 try {
dbc01944 82 return repository::get_user_file_tree($search);
be236ef5 83 }
e3ca0f3a 84 catch (Exception $e) {
6a391ebf 85 throw new repository_exception('emptyfilelist', 'repository_local');
6a391ebf 86 }
87 }
dbc01944 88
b953d4a4 89 /**
90 * Download a file, this function can be overridden by
91 * subclass.
92 *
93 * @global object $CFG
94 * @param string $url the url of file
95 * @param string $file save location
96 * @return string the location of the file
97 * @see curl package
98 */
99 public function get_file($url, $file = '') {
100 global $CFG;
101 if (!file_exists($CFG->dataroot.'/temp/download')) {
102 mkdir($CFG->dataroot.'/temp/download/', 0777, true);
103 }
104 if (is_dir($CFG->dataroot.'/temp/download')) {
105 $dir = $CFG->dataroot.'/temp/download/';
106 }
107 if (empty($file)) {
108 $file = uniqid('repo').'_'.time().'.tmp';
109 }
110 if (file_exists($dir.$file)) {
111 $file = uniqid('m').$file;
112 }
113
114 ///retrieve the file
115 $fileparams = unserialize(base64_decode($url));
116 $contextid = $fileparams[0];
117 $filearea = $fileparams[1];
118 $itemid = $fileparams[2];
119 $filepath = $fileparams[3];
120 $filename = $fileparams[4];
121 $fs = get_file_storage();
122 $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename);
123 $contents = $sf->get_content();
dbc01944 124 $fp = fopen($dir.$file, 'w');
b953d4a4 125 fwrite($fp,$contents);
126 fclose($fp);
dbc01944 127
b953d4a4 128 return $dir.$file;
129 }
130
96297ca2 131 /**
132 *
133 */
be236ef5 134 public function print_listing() {
6a391ebf 135 // will be used in non-javascript file picker
136 }
96297ca2 137
138 /**
139 *
140 * @return <type>
141 */
d31af46a 142 public function get_name(){
143 return get_string('repositoryname', 'repository_local');;
144 }
6a391ebf 145}
dbc01944 146?>