MDL-16521- 'share' parameter must be int for create folder in box.net
[moodle.git] / portfolio / type / boxnet / lib.php
CommitLineData
b3abd2c6 1<?php
67a87e7d 2require_once($CFG->libdir.'/filelib.php');
615a2859 3require_once($CFG->libdir.'/boxlib.php');
67a87e7d 4
d96a1acc 5class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
67a87e7d 6
7e95408b 7 public $boxclient;
67a87e7d 8 private $ticket;
9 private $authtoken;
67a87e7d 10 private $folders;
a226b58d 11 private $accounttree;
67a87e7d 12
0f71f48b 13 public static function get_name() {
14 return get_string('pluginname', 'portfolio_boxnet');
15 }
16
d67bfc32 17 public function prepare_package() {
13e7e765 18 // don't do anything for this plugin, we want to send all files as they are.
19 }
20
21 public function send_package() {
53aa4841 22 // if we need to create the folder, do it now
23 if ($newfolder = $this->get_export_config('newfolder')) {
5d37f13c 24 if (!$created = $this->boxclient->createFolder($newfolder, array('share' => (int)$this->get_export_config('sharefolder')))) {
53aa4841 25 throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
26 }
2e6a355d 27 $this->folders[$created['folder_id']] = $created['folder_name'];
53aa4841 28 $this->set_export_config(array('folder' => $created['folder_id']));
29 }
d67bfc32 30 foreach ($this->exporter->get_tempfiles() as $file) {
31 $return = $this->boxclient->uploadFile(
67a87e7d 32 array(
33 'file' => $file,
53aa4841 34 'folder_id' => $this->get_export_config('folder'),
fd64f098 35 'share' => $this->get_export_config('sharefile'),
67a87e7d 36 )
37 );
d67bfc32 38 if (array_key_exists('status', $return) && $return['status'] == 'upload_ok'
39 && array_key_exists('id', $return) && count($return['id']) == 1) {
37f03ea0 40 $this->rename_file($return['id'][array_pop(array_keys($return['id']))], $file->get_filename());
41 // if this fails, the file was sent but not renamed - this triggers a warning but is not fatal.
d67bfc32 42 }
67a87e7d 43 }
44 if ($this->boxclient->isError()) {
37f03ea0 45 throw new portfolio_plugin_exception('sendfailed', 'portfolio_boxnet', $this->boxclient->getErrorMsg());
67a87e7d 46 }
67a87e7d 47 }
48
67a87e7d 49 public function get_export_summary() {
50 $allfolders = $this->get_folder_list();
53aa4841 51 if ($newfolder = $this->get_export_config('newfolder')) {
52 $foldername = $newfolder . ' (' . get_string('tobecreated', 'portfolio_boxnet') . ')';
53 } else {
54 $foldername = $allfolders[$this->get_export_config('folder')];
55 }
67a87e7d 56 return array(
53aa4841 57 get_string('targetfolder', 'portfolio_boxnet') => $foldername
67a87e7d 58 );
59 }
60
61 public function get_continue_url() {
67a87e7d 62 return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
63 }
64
65 public function expected_time($callertime) {
66 return $callertime;
67 }
68
69 public static function has_admin_config() {
70 return true;
71 }
72
73 public static function get_allowed_config() {
74 return array('apikey');
75 }
76
77 public function has_export_config() {
78 return true;
79 }
80
81 public function get_allowed_user_config() {
82 return array('authtoken', 'authtokenctime');
83 }
84
85 public function get_allowed_export_config() {
fd64f098 86 return array('folder', 'newfolder', 'sharefile', 'sharefolder');
67a87e7d 87 }
88
89 public function export_config_form(&$mform) {
90 $folders = $this->get_folder_list();
91 $strrequired = get_string('required');
fd64f098 92 $mform->addElement('checkbox', 'plugin_sharefile', get_string('sharefile', 'portfolio_boxnet'));
67a87e7d 93 $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
fd64f098 94 $mform->addElement('checkbox', 'plugin_sharefolder', get_string('sharefolder', 'portfolio_boxnet'));
67a87e7d 95 if (empty($folders)) {
96 $mform->addRule('plugin_newfolder', $strrequired, 'required', null, 'client');
97 }
98 else {
99 $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
100 }
101 }
102
103 public function export_config_validation($data) {
104 if ((!array_key_exists('plugin_folder', $data) || empty($data['plugin_folder']))
105 && (!array_key_exists('plugin_newfolder', $data) || empty($data['plugin_newfolder']))) {
106 return array(
107 'plugin_folder' => get_string('notarget', 'portfolio_boxnet'),
108 'plugin_newfolder' => get_string('notarget', 'portfolio_boxnet'));
109 }
110 $allfolders = $this->get_folder_list();
111 if (in_array($data['plugin_newfolder'], $allfolders)) {
112 return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
113 }
114 }
115
116 public function admin_config_form(&$mform) {
89e12bd5 117 global $CFG;
67a87e7d 118 $strrequired = get_string('required');
119 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
89e12bd5 120 $helpparams = array(
121 'boxnet_apikey',
122 get_string('apikeyhelp', 'portfolio_boxnet'),
123 'portfolio',
124 );
125 $mform->setHelpButton('apikey', $helpparams);
67a87e7d 126 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
127 }
128
129 public function steal_control($stage) {
130 if ($stage != PORTFOLIO_STAGE_CONFIG) {
131 return false;
132 }
133 if ($this->authtoken) {
134 return false;
135 }
136 if (!$this->ensure_ticket()) {
34035201 137 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
67a87e7d 138 }
139 $token = $this->get_user_config('authtoken', $this->get('user')->id);
140 $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
141 if (!empty($token) && (($ctime + 60*60*20) > time())) {
142 $this->authtoken = $token;
143 $this->boxclient->auth_token = $token;
144 return false;
145 }
146 return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
147 }
148
149 public function post_control($stage, $params) {
150 if ($stage != PORTFOLIO_STAGE_CONFIG) {
151 return;
152 }
153 if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
34035201 154 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
67a87e7d 155 }
156 $this->authtoken = $params['auth_token'];
157 $this->boxclient->auth_token = $this->authtoken;
158 $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
159 }
160
161 private function ensure_ticket() {
162 if (!empty($this->boxclient)) {
163 return true;
164 }
165 $this->boxclient = new boxclient($this->get_config('apikey'), '');
166 $ticket_return = $this->boxclient->getTicket();
167 if ($this->boxclient->isError() || empty($ticket_return)) {
34035201 168 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
67a87e7d 169 }
170 $this->ticket = $ticket_return['ticket'];
8801ab06 171 return $this->ticket;
67a87e7d 172 }
173
a226b58d 174 private function ensure_account_tree() {
175 if (!empty($this->accounttree)) {
176 return;
67a87e7d 177 }
178 if (empty($this->ticket)
179 || empty($this->authtoken)
180 || empty($this->boxclient)) {
181 // if we don't have these we're pretty much screwed
34035201 182 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
67a87e7d 183 return false;
184 }
a226b58d 185 $this->accounttree = $this->boxclient->getAccountTree();
67a87e7d 186 if ($this->boxclient->isError()) {
34035201 187 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
67a87e7d 188 }
a226b58d 189 if (!is_array($this->accounttree)) {
67a87e7d 190 return false;
191 }
a226b58d 192 }
193
194 private function get_folder_list() {
195 if (!empty($this->folders)) {
196 return $this->folders;
197 }
198 $this->ensure_account_tree();
67a87e7d 199 $folders = array();
a226b58d 200 foreach ($this->accounttree['folder_id'] as $key => $id) {
67a87e7d 201 if (empty($id)) {
202 continue;
203 }
a226b58d 204 $name = $this->accounttree['folder_name'][$key];
205 if (!empty($this->accounttree['shared'][$key])) {
67a87e7d 206 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
207 }
208 $folders[$id] = $name;
209 }
210 $this->folders = $folders;
211 return $folders;
212 }
213
a226b58d 214 private function rename_file($fileid, $newname) {
215 // look at moving this to the boxnet client class
216 $this->ensure_account_tree();
217 $count = 1;
218 $bits = explode('.', $newname);
219 $suffix = '';
220 if (count($bits) == 1) {
68b7eed0 221 $prefix = $newname;
a226b58d 222 } else {
223 $suffix = '.' . array_pop($bits);
224 $prefix = implode('.', $bits);
225 }
226 while (true) {
899ffdfb 227 if (!array_key_exists('file_name', $this->accounttree) || !in_array($newname, $this->accounttree['file_name'])) {
9b69a0ce 228 for ($i = 0; $i < 2; $i++) {
229 if ($this->boxclient->renameFile($fileid, $newname)) {
230 return true;
231 }
232 }
233 debugging("tried three times to rename file and failed");
234 return false;
a226b58d 235 }
236 $newname = $prefix . '(' . $count . ')' . $suffix;
237 $count++;
238 }
239 return false;
240 }
241
67a87e7d 242 public function instance_sanity_check() {
243 if (!$this->get_config('apikey')) {
244 return 'err_noapikey';
245 }
246 //@TODO see if we can verify the api key without actually getting an authentication token
247 }
2f68e760 248
249 public static function allows_multiple() {
250 return false;
251 }
67a87e7d 252}