MDL-22991, fixed file api calls
[moodle.git] / repository / user / lib.php
CommitLineData
6bf197b3
DC
1<?php
2
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 * repository_user class is used to browse user private files
20 *
21 * @since 2.0
22 * @package moodlecore
23 * @subpackage repository
955b6e09
DC
24 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6bf197b3
DC
26 */
27
28class repository_user extends repository {
29
6bf197b3
DC
30 /**
31 * user plugin doesn't require login
32 * @return mixed
33 */
34 public function print_login() {
35 return $this->get_listing();
36 }
37
6bf197b3
DC
38 /**
39 * Get file listing
40 *
41 * @param string $encodedpath
42 * @return mixed
43 */
44 public function get_listing($encodedpath = '') {
45 global $CFG, $USER, $OUTPUT;
46 $ret = array();
47 $ret['dynload'] = true;
48 $ret['nosearch'] = true;
49 $ret['nologin'] = true;
50 $list = array();
51
52 if (!empty($encodedpath)) {
53 $params = unserialize(base64_decode($encodedpath));
54 if (is_array($params)) {
955b6e09
DC
55 $itemid = clean_param($params['itemid'], PARAM_INT);
56 $filename = clean_param($params['filename'], PARAM_FILE);
57 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
58 $filepath = clean_param($params['filepath'], PARAM_PATH);;
16f61c70 59 $component = clean_param($params['component'], PARAM_ALPHAEXT);
955b6e09 60 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
6bf197b3
DC
61 }
62 } else {
63 $itemid = 0;
64 $filename = null;
16f61c70 65 $filearea = 'private';
6bf197b3 66 $filepath = '/';
16f61c70 67 $component = 'user';
6bf197b3
DC
68 $context = get_context_instance(CONTEXT_USER, $USER->id);
69 }
70
71 try {
72 $browser = get_file_browser();
73
16f61c70 74 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
6bf197b3
DC
75 $pathnodes = array();
76 $level = $fileinfo;
77 $params = $fileinfo->get_params();
e35194be 78 while ($level && $params['filearea'] == 'user_private') {
6bf197b3
DC
79 $encodedpath = base64_encode(serialize($level->get_params()));
80 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
81 $level = $level->get_parent();
82 $params = $level->get_params();
83 }
84 $ret['path'] = array_reverse($pathnodes);
85
86 // build file tree
87 $children = $fileinfo->get_children();
88 foreach ($children as $child) {
89 if ($child->is_directory()) {
90 $encodedpath = base64_encode(serialize($child->get_params()));
91 $node = array(
92 'title' => $child->get_visible_name(),
93 'size' => 0,
94 'date' => '',
95 'path' => $encodedpath,
96 'children'=>array(),
97 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . ''
98 );
99 $list[] = $node;
100 } else {
101 $encodedpath = base64_encode(serialize($child->get_params()));
102 $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32';
103 $node = array(
104 'title' => $child->get_visible_name(),
105 'size' => 0,
106 'date' => '',
107 'source'=> $encodedpath,
108 'thumbnail' => $OUTPUT->pix_url($icon) . '',
109 );
110 $list[] = $node;
111 }
112 }
113 }
114 } catch (Exception $e) {
115 throw new repository_exception('emptyfilelist', 'repository_user');
116 }
117 $ret['list'] = $list;
87628a67 118 $ret['list'] = array_filter($list, array($this, 'filter'));
6bf197b3
DC
119 return $ret;
120 }
121
122 /**
123 * Set repository name
124 *
125 * @return string repository name
126 */
127 public function get_name(){
128 return get_string('areauserpersonal', 'repository');;
129 }
130
131 /**
132 * User file don't support to link to external links
133 *
134 * @return int
135 */
136 public function supported_returntypes() {
137 return FILE_INTERNAL;
138 }
139
140 /**
141 * Copy a file to file area
142 *
143 * @global object $USER
144 * @global object $DB
145 * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data
146 * @param string $new_filename The intended name of file
147 * @param string $new_itemid itemid
148 * @param string $new_filepath the new path in draft area
149 * @return array The information of file
150 */
955b6e09 151 public function copy_to_area($encoded, $new_filearea='draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') {
6bf197b3 152 global $USER, $DB;
6bf197b3
DC
153
154 $browser = get_file_browser();
155 $params = unserialize(base64_decode($encoded));
156 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
955b6e09
DC
157
158 $contextid = clean_param($params['contextid'], PARAM_INT);
159 $fileitemid = clean_param($params['itemid'], PARAM_INT);
160 $filename = clean_param($params['filename'], PARAM_FILE);
161 $filepath = clean_param($params['filepath'], PARAM_PATH);;
162 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
16f61c70 163 $component = clean_param($params['component'], PARAM_ALPHAEXT);
955b6e09 164
6bf197b3 165 $context = get_context_instance_by_id($contextid);
16f61c70
DC
166 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
167 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $new_itemid, $new_filepath, $new_filename);
6bf197b3 168
955b6e09 169 $info = array();
6bf197b3
DC
170 $info['itemid'] = $new_itemid;
171 $info['title'] = $new_filename;
172 $info['contextid'] = $user_context->id;
173 $info['filesize'] = $file_info->get_filesize();
174
175 return $info;
176 }
177}