MDL-15777 - whole database module export rejigged to use files api.
[moodle.git] / portfolio / type / boxnet / lib.php
CommitLineData
b3abd2c6 1<?php
67a87e7d 2require_once($CFG->libdir.'/filelib.php');
3require_once($CFG->dirroot.'/repository/boxnet/boxlibphp5.php');
4
5class portfolio_plugin_boxnet extends portfolio_plugin_base {
6
7 private $boxclient;
8 private $ticket;
9 private $authtoken;
67a87e7d 10 private $folders;
11
d67bfc32 12 public function prepare_package() {
67a87e7d 13 return true; // don't do anything else for this plugin, we want to send all files as they are.
14 }
15
16 public function send_package() {
17 $ret = array();
d67bfc32 18 foreach ($this->exporter->get_tempfiles() as $file) {
19 $return = $this->boxclient->uploadFile(
67a87e7d 20 array(
21 'file' => $file,
22 'folder_id' => $this->get_export_config('folder')
23 )
24 );
d67bfc32 25 if (array_key_exists('status', $return) && $return['status'] == 'upload_ok'
26 && array_key_exists('id', $return) && count($return['id']) == 1) {
27 $return['rename'] = $this->boxclient->renameFile($return['id'][array_pop(array_keys($return['id']))], $file->get_filename());
28 $ret[] = $return;
29 }
67a87e7d 30 }
31 if ($this->boxclient->isError()) {
32 return false;
33 }
34 return is_array($ret) && !empty($ret);
35 }
36
37 public function set_export_config($config) {
38 parent::set_export_config($config);
39 if (array_key_exists('newfolder', $config) && !empty($config['newfolder'])) {
40 if (!$created = $this->boxclient->createFolder($config['newfolder'])) {
41 portfolio_exporter::raise_error('foldercreatefailed', 'portfolio_boxnet');
42 }
43 $this->folders[$created['folder_id']] = $created['folder_type'];
44 parent::set_export_config(array('folder' => $created['folder_id']));
45 }
46 }
47
48 public function get_export_summary() {
49 $allfolders = $this->get_folder_list();
50 return array(
51 get_string('targetfolder', 'portfolio_boxnet') => $allfolders[$this->get_export_config('folder')]
52 );
53 }
54
55 public function get_continue_url() {
56 // @todo this was a *guess* based on what urls I got clicking around the interface.
57 // the #0:f:<folderid> part seems fragile...
58 // but I couldn't find a documented permalink scheme.
59 return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
60 }
61
62 public function expected_time($callertime) {
63 return $callertime;
64 }
65
66 public static function has_admin_config() {
67 return true;
68 }
69
70 public static function get_allowed_config() {
71 return array('apikey');
72 }
73
74 public function has_export_config() {
75 return true;
76 }
77
78 public function get_allowed_user_config() {
79 return array('authtoken', 'authtokenctime');
80 }
81
82 public function get_allowed_export_config() {
83 return array('folder', 'newfolder');
84 }
85
86 public function export_config_form(&$mform) {
87 $folders = $this->get_folder_list();
88 $strrequired = get_string('required');
89 $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
90 if (empty($folders)) {
91 $mform->addRule('plugin_newfolder', $strrequired, 'required', null, 'client');
92 }
93 else {
94 $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
95 }
96 }
97
98 public function export_config_validation($data) {
99 if ((!array_key_exists('plugin_folder', $data) || empty($data['plugin_folder']))
100 && (!array_key_exists('plugin_newfolder', $data) || empty($data['plugin_newfolder']))) {
101 return array(
102 'plugin_folder' => get_string('notarget', 'portfolio_boxnet'),
103 'plugin_newfolder' => get_string('notarget', 'portfolio_boxnet'));
104 }
105 $allfolders = $this->get_folder_list();
106 if (in_array($data['plugin_newfolder'], $allfolders)) {
107 return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
108 }
109 }
110
111 public function admin_config_form(&$mform) {
112 $strrequired = get_string('required');
113 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
114 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
115 }
116
117 public function steal_control($stage) {
118 if ($stage != PORTFOLIO_STAGE_CONFIG) {
119 return false;
120 }
121 if ($this->authtoken) {
122 return false;
123 }
124 if (!$this->ensure_ticket()) {
125 portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
126 }
127 $token = $this->get_user_config('authtoken', $this->get('user')->id);
128 $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
129 if (!empty($token) && (($ctime + 60*60*20) > time())) {
130 $this->authtoken = $token;
131 $this->boxclient->auth_token = $token;
132 return false;
133 }
134 return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
135 }
136
137 public function post_control($stage, $params) {
138 if ($stage != PORTFOLIO_STAGE_CONFIG) {
139 return;
140 }
141 if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
142 portfolio_exporter::raise_error('noauthtoken', 'portfolio_boxnet');
143 }
144 $this->authtoken = $params['auth_token'];
145 $this->boxclient->auth_token = $this->authtoken;
146 $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
147 }
148
149 private function ensure_ticket() {
150 if (!empty($this->boxclient)) {
151 return true;
152 }
153 $this->boxclient = new boxclient($this->get_config('apikey'), '');
154 $ticket_return = $this->boxclient->getTicket();
155 if ($this->boxclient->isError() || empty($ticket_return)) {
156 portfolio_exporter::raise_error('noticket', 'portfolio_boxnet');
157 }
158 $this->ticket = $ticket_return['ticket'];
8801ab06 159 return $this->ticket;
67a87e7d 160 }
161
162 private function get_folder_list() {
163 if (!empty($this->folders)) {
164 return $this->folders;
165 }
166 if (empty($this->ticket)
167 || empty($this->authtoken)
168 || empty($this->boxclient)) {
169 // if we don't have these we're pretty much screwed
170 portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
171 return false;
172 }
173 $rawfolders = $this->boxclient->getAccountTree();
174 if ($this->boxclient->isError()) {
175 portfolio_exporter::raise_error('folderlistfailed', 'portfolio_boxnet');
176 }
177 if (!is_array($rawfolders)) {
178 return false;
179 }
180 $folders = array();
181 foreach ($rawfolders['folder_id'] as $key => $id) {
182 if (empty($id)) {
183 continue;
184 }
185 $name = $rawfolders['folder_name'][$key];
186 if (!empty($rawfolders['shared'][$key])) {
187 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
188 }
189 $folders[$id] = $name;
190 }
191 $this->folders = $folders;
192 return $folders;
193 }
194
195 public function instance_sanity_check() {
196 if (!$this->get_config('apikey')) {
197 return 'err_noapikey';
198 }
199 //@TODO see if we can verify the api key without actually getting an authentication token
200 }
2f68e760 201
202 public static function allows_multiple() {
203 return false;
204 }
67a87e7d 205}