Commit | Line | Data |
---|---|---|
6a391ebf | 1 | <?php |
2 | /** | |
3 | * repository_local class | |
4 | * This is a subclass of repository class | |
5 | * | |
6a391ebf | 6 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
7 | */ | |
8 | ||
96297ca2 | 9 | /** |
10 | * | |
11 | */ | |
be236ef5 | 12 | class repository_local extends repository { |
6a391ebf | 13 | |
96297ca2 | 14 | /** |
5f9087f3 | 15 | * initialize local plugin |
cd312233 | 16 | * @param int $repositoryid |
17 | * @param int $context | |
18 | * @param array $options | |
96297ca2 | 19 | */ |
353d5cf3 | 20 | public function __construct($repositoryid, $context = SITEID, $options = array()) { |
6a391ebf | 21 | parent::__construct($repositoryid, $context, $options); |
6a391ebf | 22 | } |
4a65c39a | 23 | |
96297ca2 | 24 | /** |
5f9087f3 | 25 | * local plugin don't need login, so list all files |
cd312233 | 26 | * @return mixed |
96297ca2 | 27 | */ |
5f9087f3 | 28 | public function print_login() { |
6a391ebf | 29 | return $this->get_listing(); |
30 | } | |
5fd3e8f7 | 31 | |
96297ca2 | 32 | /** |
5f9087f3 | 33 | * Not supported by File API yet |
cd312233 | 34 | * @param string $search_text |
35 | * @return mixed | |
96297ca2 | 36 | */ |
353d5cf3 | 37 | public function search($search_text) { |
5f9087f3 | 38 | return array(); |
353d5cf3 | 39 | } |
40 | ||
96297ca2 | 41 | /** |
42 | * | |
cd312233 | 43 | * @param string $encodedpath |
44 | * @param string $path not used by this plugin | |
cd312233 | 45 | * @return mixed |
96297ca2 | 46 | */ |
5f9087f3 | 47 | public function get_listing($encodedpath = '', $page = '') { |
390baf46 | 48 | global $CFG, $USER, $OUTPUT; |
cd312233 | 49 | $ret = array(); |
50 | $ret['dynload'] = true; | |
d1bfc05e | 51 | $ret['nosearch'] = true; |
cd312233 | 52 | $list = array(); |
21942470 | 53 | |
a6058fed | 54 | if (!empty($encodedpath)) { |
55 | $params = unserialize(base64_decode($encodedpath)); | |
56 | if (is_array($params)) { | |
57 | $itemid = $params['itemid']; | |
58 | $filename = $params['filename']; | |
59 | $filearea = $params['filearea']; | |
60 | $filepath = $params['filepath']; | |
61 | $context = get_context_instance_by_id($params['contextid']); | |
62 | } | |
63 | } else { | |
64 | $itemid = null; | |
65 | $filename = null; | |
66 | $filearea = null; | |
67 | $filepath = null; | |
68 | $context = get_system_context(); | |
a6058fed | 69 | } |
70 | ||
e3ca0f3a | 71 | try { |
cd312233 | 72 | $browser = get_file_browser(); |
cd312233 | 73 | |
4ba3169d | 74 | if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) { |
cd312233 | 75 | $level = $fileinfo->get_parent(); |
76 | while ($level) { | |
8559ea10 | 77 | $params = base64_encode(serialize($level->get_params())); |
cd312233 | 78 | $path[] = array('name'=>$level->get_visible_name(), 'path'=>$params); |
79 | $level = $level->get_parent(); | |
80 | } | |
78f7e0c6 | 81 | if (!empty($path) && is_array($path)) { |
82 | $path = array_reverse($path); | |
83 | $ret['path'] = $path; | |
84 | } | |
cd312233 | 85 | $children = $fileinfo->get_children(); |
86 | foreach ($children as $child) { | |
87 | if ($child->is_directory()) { | |
8559ea10 | 88 | $params = base64_encode(serialize($child->get_params())); |
cd312233 | 89 | $node = array( |
90 | 'title' => $child->get_visible_name(), | |
91 | 'size' => 0, | |
92 | 'date' => '', | |
93 | 'path' => $params, | |
94 | 'children'=>array(), | |
f2a1963c | 95 | 'thumbnail' => $OUTPUT->old_icon_url('f/folder-32') . '' |
cd312233 | 96 | ); |
97 | $list[] = $node; | |
98 | } else { | |
99 | $params = base64_encode(serialize($child->get_params())); | |
5f9087f3 | 100 | $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32'; |
cd312233 | 101 | $node = array( |
102 | 'title' => $child->get_visible_name(), | |
103 | 'size' => 0, | |
104 | 'date' => '', | |
105 | 'source'=> $params, | |
5f9087f3 | 106 | 'thumbnail' => $OUTPUT->old_icon_url($icon) |
cd312233 | 107 | ); |
108 | $list[] = $node; | |
109 | } | |
110 | } | |
111 | } | |
157d65c0 | 112 | } catch (Exception $e) { |
6a391ebf | 113 | throw new repository_exception('emptyfilelist', 'repository_local'); |
6a391ebf | 114 | } |
cd312233 | 115 | $ret['list'] = $list; |
116 | return $ret; | |
6a391ebf | 117 | } |
dbc01944 | 118 | |
b953d4a4 | 119 | /** |
120 | * Download a file, this function can be overridden by | |
121 | * subclass. | |
122 | * | |
123 | * @global object $CFG | |
5f9087f3 | 124 | * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data |
125 | * @param string $title The intended name of file | |
126 | * @param string $itemid itemid | |
127 | * @param string $save_path the new path in draft area | |
128 | * @return array The metainfo of file | |
b953d4a4 | 129 | * @see curl package |
130 | */ | |
41076c58 | 131 | public function move_to_draft($encoded, $title = '', $itemid = '', $save_path = '/') { |
a6058fed | 132 | global $USER, $DB; |
133 | $ret = array(); | |
157d65c0 | 134 | |
a6058fed | 135 | $browser = get_file_browser(); |
cd312233 | 136 | $params = unserialize(base64_decode($encoded)); |
a6058fed | 137 | $user_context = get_context_instance(CONTEXT_USER, $USER->id); |
157d65c0 | 138 | // the final file |
139 | $contextid = $params['contextid']; | |
140 | $filearea = $params['filearea']; | |
141 | $filepath = $params['filepath']; | |
142 | $filename = $params['filename']; | |
143 | $fileitemid = $params['itemid']; | |
144 | $context = get_context_instance_by_id($contextid); | |
145 | $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename); | |
5f9087f3 | 146 | $file_info->copy_to_storage($user_context->id, 'user_draft', $itemid, $save_path, $title); |
157d65c0 | 147 | |
a6058fed | 148 | $ret['itemid'] = $itemid; |
149 | $ret['title'] = $title; | |
150 | $ret['contextid'] = $user_context->id; | |
151 | ||
152 | return $ret; | |
6a391ebf | 153 | } |
96297ca2 | 154 | |
155 | /** | |
cd312233 | 156 | * @return string |
96297ca2 | 157 | */ |
d31af46a | 158 | public function get_name(){ |
159 | return get_string('repositoryname', 'repository_local');; | |
160 | } | |
41076c58 DC |
161 | public function supported_returntypes() { |
162 | return FILE_INTERNAL; | |
163 | } | |
6a391ebf | 164 | } |
4317f92f | 165 |