3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 2008 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/portfolio/plugin.php');
28 require_once($CFG->libdir.'/filelib.php');
29 require_once($CFG->libdir.'/flickrlib.php');
31 class portfolio_plugin_flickr extends portfolio_plugin_push_base {
37 public function supported_formats() {
38 return array(PORTFOLIO_FORMAT_IMAGE);
41 public static function get_name() {
42 return get_string('pluginname', 'portfolio_flickr');
45 public function prepare_package() {
49 public function send_package() {
50 foreach ($this->exporter->get_tempfiles() as $file) {
51 // @TODO get max size from flickr people_getUploadStatus
52 $filesize = $file->get_filesize();
54 if ($file->is_valid_image()) {
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')));
66 // Attach photo to a set if requested
67 if ($this->get_export_config('set')) {
68 $this->flickr->photosets_addPhoto($this->get_export_config('set'),
69 $this->flickr->parsed_response['photoid']);
72 throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr',
73 $this->flickr->error_code . ': ' . $this->flickr->error_msg);
79 public static function allows_multiple_instances() {
83 public function get_interactive_continue_url() {
84 return $this->flickr->urls_getUserPhotos();
87 public function expected_time($callertime) {
91 public static function get_allowed_config() {
92 return array('apikey', 'sharedsecret');
95 public static function has_admin_config() {
99 public function admin_config_form(&$mform) {
102 $strrequired = get_string('required');
103 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'), array('size' => 30));
104 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
105 $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
106 $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
108 $a->applyurl = 'http://www.flickr.com/services/api/keys/apply/';
109 $a->keysurl = 'http://www.flickr.com/services/api/keys/';
110 $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=flickr';
111 $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_flickr'),
112 get_string('setupinfodetails', 'portfolio_flickr', $a));
115 public function has_export_config() {
119 public function get_allowed_user_config() {
120 return array('authtoken', 'nsid');
123 public function steal_control($stage) {
124 if ($stage != PORTFOLIO_STAGE_CONFIG) {
131 $token = $this->get_user_config('authtoken', $this->get('user')->id);
132 $nsid = $this->get_user_config('nsid', $this->get('user')->id);
134 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
136 if (!empty($token)) {
137 $this->token = $token;
138 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
141 return $this->flickr->auth('write');
144 public function post_control($stage, $params) {
145 if ($stage != PORTFOLIO_STAGE_CONFIG) {
148 if (!array_key_exists('frob', $params) || empty($params['frob'])) {
149 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
152 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
154 $auth_info = $this->flickr->auth_getToken($params['frob']);
156 $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
159 public function export_config_form(&$mform) {
160 $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
161 $mform->addElement('textarea', 'plugin_description', get_string('description'));
162 $mform->addElement('text', 'plugin_tags', get_string('tags'));
163 $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
164 $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
165 $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
167 $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
168 $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
170 $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
171 2 => $this->get_export_value_name('safety_level', 2),
172 3 => $this->get_export_value_name('safety_level', 3));
174 $content_types = array(1 => $this->get_export_value_name('content_type', 1),
175 2 => $this->get_export_value_name('content_type', 2),
176 3 => $this->get_export_value_name('content_type', 3));
178 $hidden_values = array(1,2);
180 $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
181 $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
182 $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
184 $mform->setDefaults(array('plugin_is_public' => true));
186 $sets = $this->get_sets();
190 $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
194 private function get_sets() {
195 if (empty($this->raw_sets)) {
196 $this->raw_sets = $this->flickr->photosets_getList();
200 foreach ($this->raw_sets['photoset'] as $set_data) {
201 $sets[$set_data['id']] = $set_data['title'];
206 public function get_allowed_export_config() {
207 return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
210 public function get_export_summary() {
211 return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
212 get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
213 get_string('description') => $this->get_export_config('description'),
214 get_string('tags') => $this->get_export_config('tags'),
215 get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
216 get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
217 get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
218 get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
219 get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
220 get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
223 private function get_export_value_name($param, $value) {
224 $params = array('set' => $this->get_sets(),
225 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
226 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
227 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
228 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
229 2 => get_string('moderate', 'portfolio_flickr'),
230 3 => get_string('restricted', 'portfolio_flickr')),
231 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
232 2 => get_string('screenshot', 'portfolio_flickr'),
233 3 => get_string('other', 'portfolio_flickr')),
234 'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
236 if (isset($params[$param][$value])) {
237 return $params[$param][$value];
244 * For now, flickr doesn't support this because we can't dynamically construct callbackurl
246 public static function allows_multiple_exports() {