MDL-16341 Almost finished portfolio flickr plugin. Modified phpflickr->request method...
[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()) {
29 $return = $this->flickr->request ('upload', array('photo' => $file->get_content(),
30 'title' => $this->get_export_config('title'),
31 'description' => $this->get_export_config('description'),
32 'tags' => $this->get_export_config('tags'),
33 'is_public' => $this->get_export_config('is_public'),
34 'is_friend' => $this->get_export_config('is_friend'),
35 'is_family' => $this->get_export_config('is_family')));
36
37 // TODO if a set was requested, attach the photo to that set
38 if ($return && $this->get_export_config('set')) {
39 //
40 }
41 // DEBUG!!!
42 var_dump($return);die();
43 }
44 }
7e95408b 45 }
46
237d80e0 47 public static function allows_multiple() {
48 return false;
49 }
50
7e95408b 51 public function get_continue_url() {
a55d94d4 52 // return $this->flickr->urls_getUserPhotos();
53 return "http://www.flickr.com/tools/uploader_edit.gne?ids="; // Add ids of uploaded files
7e95408b 54 }
55
56 public function expected_time($callertime) {
57 return $callertime;
58 }
59
60 public static function get_allowed_config() {
61 return array('apikey', 'sharedsecret');
62 }
63
64 public static function has_admin_config() {
65 return true;
66 }
67
68 public function admin_config_form(&$mform) {
69 $strrequired = get_string('required');
70 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'));
71 $mform->addRule('apikey', $strrequired, 'required', null, 'client');
72 $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
73 $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
74 }
75
a55d94d4 76 public function has_export_config() {
77 return true;
78 }
79
80 public function get_allowed_user_config() {
81 return array('authtoken', 'nsid');
82 }
83
84 public function steal_control($stage) {
85 if ($stage != PORTFOLIO_STAGE_CONFIG) {
86 return false;
87 }
88 if ($this->token) {
89 return false;
90 }
91
92 $token = $this->get_user_config('authtoken', $this->get('user')->id);
93 $nsid = $this->get_user_config('nsid', $this->get('user')->id);
94
95 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
96
97 if (!empty($token)) {
98 $this->token = $token;
99 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
100 return false;
101 }
102 return $this->flickr->auth('write');
103 }
104
105 public function post_control($stage, $params) {
106 if ($stage != PORTFOLIO_STAGE_CONFIG) {
107 return;
108 }
109 if (!array_key_exists('frob', $params) || empty($params['frob'])) {
110 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
111 }
112
113 $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'));
114
115 $auth_info = $this->flickr->auth_getToken($params['frob']);
116
117 $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
118 }
119
120 public function export_config_form(&$mform) {
121 $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
122 $mform->addElement('textarea', 'plugin_description', get_string('description'));
123 $mform->addElement('text', 'plugin_tags', get_string('tags'));
124 $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
125 $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
126 $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
127
128 $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
129 $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
130
131 $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
132 2 => $this->get_export_value_name('safety_level', 2),
133 3 => $this->get_export_value_name('safety_level', 3));
134
135 $content_types = array(1 => $this->get_export_value_name('content_type', 1),
136 2 => $this->get_export_value_name('content_type', 2),
137 3 => $this->get_export_value_name('content_type', 3));
138
139 $hidden_values = array(1,2);
140
141 $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
142 $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
143 $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
144
145 $mform->setDefaults(array('plugin_is_public' => true));
146
147 $sets = $this->get_sets();
148
149 if (!empty($sets)) {
150 $sets[0] = '----';
151 $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
152 }
153 }
154
155 private function get_sets() {
156 if (empty($this->raw_sets)) {
157 $this->raw_sets = $this->flickr->photosets_getList();
158 }
159
160 $sets = array();
161 foreach ($this->raw_sets['photoset'] as $set_data) {
162 $sets[$set_data['id']] = $set_data['title'];
163 }
164 return $sets;
165 }
166
167 public function get_allowed_export_config() {
168 return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
169 }
170
171 public function get_export_summary() {
172 return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
173 get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
174 get_string('description') => $this->get_export_config('description'),
175 get_string('tags') => $this->get_export_config('tags'),
176 get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
177 get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
178 get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
179 get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
180 get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
181 get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
182 }
183
184 private function get_export_value_name($param, $value) {
185 $params = array('set' => $this->get_sets(),
186 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
187 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
188 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
189 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
190 2 => get_string('moderate', 'portfolio_flickr'),
191 3 => get_string('restricted', 'portfolio_flickr')),
192 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
193 2 => get_string('screenshot', 'portfolio_flickr'),
194 3 => get_string('other', 'portfolio_flickr')),
195 'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
196
197 if (isset($params[$param][$value])) {
198 return $params[$param][$value];
199 } else {
200 return '-';
201 }
202 }
7e95408b 203}