MDL-18167 towareds reimplementing of win installer support
[moodle.git] / portfolio / type / flickr / lib.php
CommitLineData
7e95408b 1<?php
2require_once($CFG->libdir.'/filelib.php');
539b3d13 3require_once($CFG->libdir.'/flickrlib.php');
7e95408b 4
5class portfolio_plugin_flickr extends portfolio_plugin_push_base {
6
7 private $flickr;
a55d94d4 8 private $token;
9 private $raw_sets;
7e95408b 10
ea0de12f 11 public static function supported_formats() {
12 return array(PORTFOLIO_FORMAT_IMAGE);
13 }
14
0f71f48b 15 public static function get_name() {
16 return get_string('pluginname', 'portfolio_flickr');
17 }
18
7e95408b 19 public function prepare_package() {
a55d94d4 20
7e95408b 21 }
22
23 public function send_package() {
a55d94d4 24 foreach ($this->exporter->get_tempfiles() as $file) {
25 // @TODO get max size from flickr people_getUploadStatus
26 $filesize = $file->get_filesize();
27
28 if ($file->is_valid_image()) {
18729a51 29 /**
30 $return = $this->flickr->upload (array('photo' => $file,
0d4231f0 31 'title' => $this->get_export_config('title'),
32 'description' => $this->get_export_config('description'),
33 'tags' => $this->get_export_config('tags'),
34 'is_public' => $this->get_export_config('is_public'),
35 'is_friend' => $this->get_export_config('is_friend'),
36 'is_family' => $this->get_export_config('is_family'),
37 'safety_level' => $this->get_export_config('safety_level'),
38 'content_type' => $this->get_export_config('content_type'),
39 'hidden' => $this->get_export_config('hidden')));
18729a51 40 */
41 $return = $this->flickr->upload($file, $this->get_export_config('title'), $this->get_export_config('description'), $this->get_export_config('tags'), $this->get_export_config('is_public'), $this->get_export_config('is_friend'), $this->get_export_config('is_family'));
0d4231f0 42 if ($return) {
43 // Attach photo to a set if requested
44 if ($this->get_export_config('set')) {
45 $this->flickr->photosets_addPhoto($this->get_export_config('set'), $this->flickr->parsed_response['photoid']);
46 }
47 } else {
48 throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', $this->flickr->error_code . ': ' . $this->flickr->error_msg);
a55d94d4 49 }
a55d94d4 50 }
51 }
7e95408b 52 }
53
237d80e0 54 public static function allows_multiple() {
55 return false;
56 }
57
7e95408b 58 public function get_continue_url() {
a55d94d4 59 // return $this->flickr->urls_getUserPhotos();
60 return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files
7e95408b 61 }
62
63 public function expected_time($callertime) {
64 return $callertime;
65 }
66
67 public static function get_allowed_config() {
68 return array('apikey', 'sharedsecret');
69 }
70
71 public static function has_admin_config() {
72 return true;
73 }
74
75 public function admin_config_form(&$mform) {
76 $strrequired = get_string('required');
77 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'));
78 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
79 $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
80 $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
81 }
82
a55d94d4 83 public function has_export_config() {
84 return true;
85 }
86
87 public function get_allowed_user_config() {
88 return array('authtoken', 'nsid');
89 }
90
91 public function steal_control($stage) {
92 if ($stage != PORTFOLIO_STAGE_CONFIG) {
93 return false;
94 }
95 if ($this->token) {
96 return false;
97 }
98
99 $token = $this->get_user_config('authtoken', $this->get('user')->id);
100 $nsid = $this->get_user_config('nsid', $this->get('user')->id);
101
102 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
103
104 if (!empty($token)) {
105 $this->token = $token;
106 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
107 return false;
108 }
109 return $this->flickr->auth('write');
110 }
111
112 public function post_control($stage, $params) {
113 if ($stage != PORTFOLIO_STAGE_CONFIG) {
114 return;
115 }
116 if (!array_key_exists('frob', $params) || empty($params['frob'])) {
117 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
118 }
119
120 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
121
122 $auth_info = $this->flickr->auth_getToken($params['frob']);
123
124 $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
125 }
126
127 public function export_config_form(&$mform) {
128 $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
129 $mform->addElement('textarea', 'plugin_description', get_string('description'));
130 $mform->addElement('text', 'plugin_tags', get_string('tags'));
131 $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
132 $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
133 $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
134
135 $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
136 $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
137
138 $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
139 2 => $this->get_export_value_name('safety_level', 2),
140 3 => $this->get_export_value_name('safety_level', 3));
141
142 $content_types = array(1 => $this->get_export_value_name('content_type', 1),
143 2 => $this->get_export_value_name('content_type', 2),
144 3 => $this->get_export_value_name('content_type', 3));
145
146 $hidden_values = array(1,2);
147
148 $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
149 $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
150 $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
151
152 $mform->setDefaults(array('plugin_is_public' => true));
153
154 $sets = $this->get_sets();
155
156 if (!empty($sets)) {
157 $sets[0] = '----';
158 $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
159 }
160 }
161
162 private function get_sets() {
163 if (empty($this->raw_sets)) {
164 $this->raw_sets = $this->flickr->photosets_getList();
165 }
166
167 $sets = array();
168 foreach ($this->raw_sets['photoset'] as $set_data) {
169 $sets[$set_data['id']] = $set_data['title'];
170 }
171 return $sets;
172 }
173
174 public function get_allowed_export_config() {
175 return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
176 }
177
178 public function get_export_summary() {
179 return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
180 get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
181 get_string('description') => $this->get_export_config('description'),
182 get_string('tags') => $this->get_export_config('tags'),
183 get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
184 get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
185 get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
186 get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
187 get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
188 get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
189 }
190
191 private function get_export_value_name($param, $value) {
192 $params = array('set' => $this->get_sets(),
193 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
194 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
195 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
196 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
197 2 => get_string('moderate', 'portfolio_flickr'),
198 3 => get_string('restricted', 'portfolio_flickr')),
199 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
200 2 => get_string('screenshot', 'portfolio_flickr'),
201 3 => get_string('other', 'portfolio_flickr')),
202 'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
203
204 if (isset($params[$param][$value])) {
205 return $params[$param][$value];
206 } else {
207 return '-';
208 }
209 }
7e95408b 210}