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