MDL-42605 portfolio_flickr: Photos can be added to a set on export
[moodle.git] / portfolio / flickr / lib.php
CommitLineData
7e95408b 1<?php
76116d2e
DM
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package portfolio
20 * @subpackage flickr
21 * @copyright 2008 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
f675815e 27require_once($CFG->libdir.'/portfolio/plugin.php');
7e95408b 28require_once($CFG->libdir.'/filelib.php');
539b3d13 29require_once($CFG->libdir.'/flickrlib.php');
7e95408b 30
31class portfolio_plugin_flickr extends portfolio_plugin_push_base {
32
33 private $flickr;
a55d94d4 34 private $token;
35 private $raw_sets;
7e95408b 36
38652d90 37 public function supported_formats() {
ea0de12f 38 return array(PORTFOLIO_FORMAT_IMAGE);
39 }
40
0f71f48b 41 public static function get_name() {
42 return get_string('pluginname', 'portfolio_flickr');
43 }
44
7e95408b 45 public function prepare_package() {
a55d94d4 46
7e95408b 47 }
48
49 public function send_package() {
a55d94d4 50 foreach ($this->exporter->get_tempfiles() as $file) {
51 // @TODO get max size from flickr people_getUploadStatus
52 $filesize = $file->get_filesize();
53
54 if ($file->is_valid_image()) {
76116d2e
DM
55 $return = $this->flickr->upload($file, array(
56 'title' => $this->get_export_config('title'),
57 'description' => $this->get_export_config('description'),
58 'tags' => $this->get_export_config('tags'),
59 'is_public' => $this->get_export_config('is_public'),
60 'is_friend' => $this->get_export_config('is_friend'),
61 'is_family' => $this->get_export_config('is_family'),
62 'safety_level' => $this->get_export_config('safety_level'),
63 'content_type' => $this->get_export_config('content_type'),
64 'hidden' => $this->get_export_config('hidden')));
0d4231f0 65 if ($return) {
66 // Attach photo to a set if requested
16dc4c13 67 if ($this->get_export_config('set') && !empty($this->flickr->parsed_response['photoid'])) {
76116d2e
DM
68 $this->flickr->photosets_addPhoto($this->get_export_config('set'),
69 $this->flickr->parsed_response['photoid']);
0d4231f0 70 }
71 } else {
76116d2e
DM
72 throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr',
73 $this->flickr->error_code . ': ' . $this->flickr->error_msg);
a55d94d4 74 }
a55d94d4 75 }
76 }
7e95408b 77 }
78
384ba38a 79 public static function allows_multiple_instances() {
237d80e0 80 return false;
81 }
82
5d0dbf13 83 public function get_interactive_continue_url() {
76116d2e 84 return $this->flickr->urls_getUserPhotos();
7e95408b 85 }
86
87 public function expected_time($callertime) {
88 return $callertime;
89 }
90
91 public static function get_allowed_config() {
92 return array('apikey', 'sharedsecret');
93 }
94
95 public static function has_admin_config() {
96 return true;
97 }
98
c17ec774 99 public static function admin_config_form(&$mform) {
76116d2e
DM
100 global $CFG;
101
7e95408b 102 $strrequired = get_string('required');
76116d2e 103 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'), array('size' => 30));
7e95408b 104 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
962b1baf 105 $mform->setType('apikey', PARAM_RAW_TRIMMED);
7e95408b 106 $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
107 $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
962b1baf 108 $mform->setType('sharedsecret', PARAM_RAW_TRIMMED);
76116d2e
DM
109 $a = new stdClass();
110 $a->applyurl = 'http://www.flickr.com/services/api/keys/apply/';
111 $a->keysurl = 'http://www.flickr.com/services/api/keys/';
dd2645d3 112 $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=flickr';
76116d2e
DM
113 $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_flickr'),
114 get_string('setupinfodetails', 'portfolio_flickr', $a));
7e95408b 115 }
116
a55d94d4 117 public function has_export_config() {
118 return true;
119 }
120
121 public function get_allowed_user_config() {
122 return array('authtoken', 'nsid');
123 }
124
125 public function steal_control($stage) {
126 if ($stage != PORTFOLIO_STAGE_CONFIG) {
127 return false;
128 }
129 if ($this->token) {
130 return false;
131 }
132
133 $token = $this->get_user_config('authtoken', $this->get('user')->id);
134 $nsid = $this->get_user_config('nsid', $this->get('user')->id);
135
136 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
137
138 if (!empty($token)) {
139 $this->token = $token;
140 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
141 return false;
142 }
143 return $this->flickr->auth('write');
144 }
145
146 public function post_control($stage, $params) {
147 if ($stage != PORTFOLIO_STAGE_CONFIG) {
148 return;
149 }
150 if (!array_key_exists('frob', $params) || empty($params['frob'])) {
151 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
152 }
153
154 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
155
156 $auth_info = $this->flickr->auth_getToken($params['frob']);
157
158 $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
159 }
160
161 public function export_config_form(&$mform) {
162 $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
22138937 163 $mform->setType('plugin_title', PARAM_TEXT);
a55d94d4 164 $mform->addElement('textarea', 'plugin_description', get_string('description'));
22138937 165 $mform->setType('plugin_description', PARAM_CLEANHTML);
a55d94d4 166 $mform->addElement('text', 'plugin_tags', get_string('tags'));
22138937 167 $mform->setType('plugin_tags', PARAM_TAGLIST);
a55d94d4 168 $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
169 $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
170 $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
171
172 $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
173 $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
174
175 $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
176 2 => $this->get_export_value_name('safety_level', 2),
177 3 => $this->get_export_value_name('safety_level', 3));
178
179 $content_types = array(1 => $this->get_export_value_name('content_type', 1),
180 2 => $this->get_export_value_name('content_type', 2),
181 3 => $this->get_export_value_name('content_type', 3));
182
183 $hidden_values = array(1,2);
184
185 $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
186 $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
187 $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
188
189 $mform->setDefaults(array('plugin_is_public' => true));
190
191 $sets = $this->get_sets();
192
193 if (!empty($sets)) {
194 $sets[0] = '----';
195 $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
196 }
197 }
198
199 private function get_sets() {
200 if (empty($this->raw_sets)) {
201 $this->raw_sets = $this->flickr->photosets_getList();
202 }
203
204 $sets = array();
205 foreach ($this->raw_sets['photoset'] as $set_data) {
206 $sets[$set_data['id']] = $set_data['title'];
207 }
208 return $sets;
209 }
210
211 public function get_allowed_export_config() {
212 return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
213 }
214
215 public function get_export_summary() {
216 return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
217 get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
218 get_string('description') => $this->get_export_config('description'),
219 get_string('tags') => $this->get_export_config('tags'),
220 get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
221 get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
222 get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
223 get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
224 get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
225 get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
226 }
227
228 private function get_export_value_name($param, $value) {
229 $params = array('set' => $this->get_sets(),
230 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
231 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
232 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
233 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
234 2 => get_string('moderate', 'portfolio_flickr'),
235 3 => get_string('restricted', 'portfolio_flickr')),
236 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
237 2 => get_string('screenshot', 'portfolio_flickr'),
238 3 => get_string('other', 'portfolio_flickr')),
239 'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
240
241 if (isset($params[$param][$value])) {
242 return $params[$param][$value];
243 } else {
244 return '-';
245 }
246 }
76116d2e
DM
247
248 /**
249 * For now, flickr doesn't support this because we can't dynamically construct callbackurl
250 */
251 public static function allows_multiple_exports() {
252 return false;
253 }
7e95408b 254}