mnet MDL-20208 Added the string 'allhosts_no_options'
[moodle.git] / portfolio / 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') . ')';
447192d3 53 } elseif ($this->get_export_config('folder')) {
53aa4841 54 $foldername = $allfolders[$this->get_export_config('folder')];
447192d3 55 } else {
56 $foldername = '';
53aa4841 57 }
67a87e7d 58 return array(
53aa4841 59 get_string('targetfolder', 'portfolio_boxnet') => $foldername
67a87e7d 60 );
61 }
62
5d0dbf13 63 public function get_interactive_continue_url() {
67a87e7d 64 return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
65 }
66
67 public function expected_time($callertime) {
68 return $callertime;
69 }
70
71 public static function has_admin_config() {
72 return true;
73 }
74
75 public static function get_allowed_config() {
76 return array('apikey');
77 }
78
79 public function has_export_config() {
80 return true;
81 }
82
83 public function get_allowed_user_config() {
84 return array('authtoken', 'authtokenctime');
85 }
86
87 public function get_allowed_export_config() {
fd64f098 88 return array('folder', 'newfolder', 'sharefile', 'sharefolder');
67a87e7d 89 }
90
91 public function export_config_form(&$mform) {
92 $folders = $this->get_folder_list();
fd64f098 93 $mform->addElement('checkbox', 'plugin_sharefile', get_string('sharefile', 'portfolio_boxnet'));
67a87e7d 94 $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
fd64f098 95 $mform->addElement('checkbox', 'plugin_sharefolder', get_string('sharefolder', 'portfolio_boxnet'));
447192d3 96 $folders[0] = '----';
97 ksort($folders);
98 $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
67a87e7d 99 }
100
101 public function export_config_validation($data) {
67a87e7d 102 $allfolders = $this->get_folder_list();
103 if (in_array($data['plugin_newfolder'], $allfolders)) {
104 return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
105 }
106 }
107
108 public function admin_config_form(&$mform) {
89e12bd5 109 global $CFG;
67a87e7d 110 $strrequired = get_string('required');
111 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
89e12bd5 112 $helpparams = array(
113 'boxnet_apikey',
114 get_string('apikeyhelp', 'portfolio_boxnet'),
115 'portfolio',
116 );
117 $mform->setHelpButton('apikey', $helpparams);
67a87e7d 118 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
ff347f99 119 $mform->addElement('warning', 'apikeyhelp', 'smalltext', get_string('apikeyinlinehelp', 'portfolio_boxnet', $CFG->wwwroot . '/portfolio/add.php?postcontrol=1'));
120
67a87e7d 121 }
122
123 public function steal_control($stage) {
124 if ($stage != PORTFOLIO_STAGE_CONFIG) {
125 return false;
126 }
127 if ($this->authtoken) {
128 return false;
129 }
130 if (!$this->ensure_ticket()) {
34035201 131 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
67a87e7d 132 }
133 $token = $this->get_user_config('authtoken', $this->get('user')->id);
134 $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
135 if (!empty($token) && (($ctime + 60*60*20) > time())) {
136 $this->authtoken = $token;
137 $this->boxclient->auth_token = $token;
138 return false;
139 }
140 return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
141 }
142
143 public function post_control($stage, $params) {
144 if ($stage != PORTFOLIO_STAGE_CONFIG) {
145 return;
146 }
147 if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
34035201 148 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
67a87e7d 149 }
150 $this->authtoken = $params['auth_token'];
151 $this->boxclient->auth_token = $this->authtoken;
152 $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
153 }
154
155 private function ensure_ticket() {
156 if (!empty($this->boxclient)) {
157 return true;
158 }
159 $this->boxclient = new boxclient($this->get_config('apikey'), '');
160 $ticket_return = $this->boxclient->getTicket();
161 if ($this->boxclient->isError() || empty($ticket_return)) {
34035201 162 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
67a87e7d 163 }
164 $this->ticket = $ticket_return['ticket'];
8801ab06 165 return $this->ticket;
67a87e7d 166 }
167
a226b58d 168 private function ensure_account_tree() {
169 if (!empty($this->accounttree)) {
170 return;
67a87e7d 171 }
172 if (empty($this->ticket)
173 || empty($this->authtoken)
174 || empty($this->boxclient)) {
175 // if we don't have these we're pretty much screwed
34035201 176 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
67a87e7d 177 return false;
178 }
a226b58d 179 $this->accounttree = $this->boxclient->getAccountTree();
67a87e7d 180 if ($this->boxclient->isError()) {
34035201 181 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
67a87e7d 182 }
a226b58d 183 if (!is_array($this->accounttree)) {
67a87e7d 184 return false;
185 }
a226b58d 186 }
187
188 private function get_folder_list() {
189 if (!empty($this->folders)) {
190 return $this->folders;
191 }
192 $this->ensure_account_tree();
67a87e7d 193 $folders = array();
a226b58d 194 foreach ($this->accounttree['folder_id'] as $key => $id) {
67a87e7d 195 if (empty($id)) {
196 continue;
197 }
a226b58d 198 $name = $this->accounttree['folder_name'][$key];
199 if (!empty($this->accounttree['shared'][$key])) {
67a87e7d 200 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
201 }
202 $folders[$id] = $name;
203 }
204 $this->folders = $folders;
205 return $folders;
206 }
207
a226b58d 208 private function rename_file($fileid, $newname) {
209 // look at moving this to the boxnet client class
210 $this->ensure_account_tree();
211 $count = 1;
212 $bits = explode('.', $newname);
213 $suffix = '';
214 if (count($bits) == 1) {
68b7eed0 215 $prefix = $newname;
a226b58d 216 } else {
217 $suffix = '.' . array_pop($bits);
218 $prefix = implode('.', $bits);
219 }
220 while (true) {
899ffdfb 221 if (!array_key_exists('file_name', $this->accounttree) || !in_array($newname, $this->accounttree['file_name'])) {
9b69a0ce 222 for ($i = 0; $i < 2; $i++) {
223 if ($this->boxclient->renameFile($fileid, $newname)) {
224 return true;
225 }
226 }
227 debugging("tried three times to rename file and failed");
228 return false;
a226b58d 229 }
230 $newname = $prefix . '(' . $count . ')' . $suffix;
231 $count++;
232 }
233 return false;
234 }
235
67a87e7d 236 public function instance_sanity_check() {
237 if (!$this->get_config('apikey')) {
238 return 'err_noapikey';
239 }
240 //@TODO see if we can verify the api key without actually getting an authentication token
241 }
2f68e760 242
384ba38a 243 public static function allows_multiple_instances() {
2f68e760 244 return false;
245 }
62e71954 246
247 public static function supported_formats() {
248 return array(PORTFOLIO_FORMAT_FILE); // don't support rich html, it breaks links
249 }
67a87e7d 250}