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