PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/portfolio/flickr/lib.php

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