lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * This plugin is used to access flickr pictures
  18. *
  19. * @since Moodle 2.0
  20. * @package repository_flickr
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. require_once($CFG->libdir.'/flickrlib.php');
  26. /**
  27. * This plugin is used to access user's private flickr repository
  28. *
  29. * @since Moodle 2.0
  30. * @package repository_flickr
  31. * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class repository_flickr extends repository {
  35. private $flickr;
  36. public $photos;
  37. /**
  38. * Stores sizes of images to prevent multiple API call
  39. */
  40. static private $sizes = array();
  41. /**
  42. *
  43. * @param int $repositoryid
  44. * @param object $context
  45. * @param array $options
  46. */
  47. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  48. global $SESSION, $CFG;
  49. $options['page'] = optional_param('p', 1, PARAM_INT);
  50. parent::__construct($repositoryid, $context, $options);
  51. $this->setting = 'flickr_';
  52. $this->api_key = $this->get_option('api_key');
  53. $this->secret = $this->get_option('secret');
  54. $this->token = get_user_preferences($this->setting, '');
  55. $this->nsid = get_user_preferences($this->setting.'_nsid', '');
  56. $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
  57. $frob = optional_param('frob', '', PARAM_RAW);
  58. if (empty($this->token) && !empty($frob)) {
  59. $auth_info = $this->flickr->auth_getToken($frob);
  60. $this->token = $auth_info['token'];
  61. $this->nsid = $auth_info['user']['nsid'];
  62. set_user_preference($this->setting, $auth_info['token']);
  63. set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
  64. }
  65. }
  66. /**
  67. *
  68. * @return bool
  69. */
  70. public function check_login() {
  71. return !empty($this->token);
  72. }
  73. /**
  74. *
  75. * @return mixed
  76. */
  77. public function logout() {
  78. set_user_preference($this->setting, '');
  79. set_user_preference($this->setting.'_nsid', '');
  80. $this->token = '';
  81. $this->nsid = '';
  82. return $this->print_login();
  83. }
  84. /**
  85. *
  86. * @param array $options
  87. * @return mixed
  88. */
  89. public function set_option($options = array()) {
  90. if (!empty($options['api_key'])) {
  91. set_config('api_key', trim($options['api_key']), 'flickr');
  92. }
  93. if (!empty($options['secret'])) {
  94. set_config('secret', trim($options['secret']), 'flickr');
  95. }
  96. unset($options['api_key']);
  97. unset($options['secret']);
  98. $ret = parent::set_option($options);
  99. return $ret;
  100. }
  101. /**
  102. *
  103. * @param string $config
  104. * @return mixed
  105. */
  106. public function get_option($config = '') {
  107. if ($config==='api_key') {
  108. return trim(get_config('flickr', 'api_key'));
  109. } elseif ($config ==='secret') {
  110. return trim(get_config('flickr', 'secret'));
  111. } else {
  112. $options['api_key'] = trim(get_config('flickr', 'api_key'));
  113. $options['secret'] = trim(get_config('flickr', 'secret'));
  114. }
  115. $options = parent::get_option($config);
  116. return $options;
  117. }
  118. /**
  119. *
  120. * @return bool
  121. */
  122. public function global_search() {
  123. if (empty($this->token)) {
  124. return false;
  125. } else {
  126. return true;
  127. }
  128. }
  129. /**
  130. *
  131. * @return null
  132. */
  133. public function print_login() {
  134. if ($this->options['ajax']) {
  135. $ret = array();
  136. $popup_btn = new stdClass();
  137. $popup_btn->type = 'popup';
  138. $popup_btn->url = $this->flickr->auth();
  139. $ret['login'] = array($popup_btn);
  140. return $ret;
  141. } else {
  142. echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
  143. }
  144. }
  145. /**
  146. * Converts result received from phpFlickr::photo_search to Filepicker/repository format
  147. *
  148. * @param mixed $photos
  149. * @return array
  150. */
  151. private function build_list($photos) {
  152. $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
  153. $ret = array();
  154. $ret['manage'] = $photos_url;
  155. $ret['list'] = array();
  156. $ret['pages'] = $photos['pages'];
  157. $ret['total'] = $photos['total'];
  158. $ret['perpage'] = $photos['perpage'];
  159. $ret['page'] = $photos['page'];
  160. if (!empty($photos['photo'])) {
  161. foreach ($photos['photo'] as $p) {
  162. if(empty($p['title'])) {
  163. $p['title'] = get_string('notitle', 'repository_flickr');
  164. }
  165. if (isset($p['originalformat'])) {
  166. $format = $p['originalformat'];
  167. } else {
  168. $format = 'jpg';
  169. }
  170. $format = '.'.$format;
  171. // append extensions to the files
  172. if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
  173. $p['title'] .= $format;
  174. }
  175. $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
  176. 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
  177. 'thumbnail_width'=>75, 'thumbnail_height'=>75,
  178. 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
  179. }
  180. }
  181. return $ret;
  182. }
  183. /**
  184. *
  185. * @param string $search_text
  186. * @param int $page
  187. * @return array
  188. */
  189. public function search($search_text, $page = 0) {
  190. $photos = $this->flickr->photos_search(array(
  191. 'user_id'=>$this->nsid,
  192. 'per_page'=>24,
  193. 'extras'=>'original_format',
  194. 'page'=>$page,
  195. 'text'=>$search_text
  196. ));
  197. $ret = $this->build_list($photos);
  198. $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
  199. return $ret;
  200. }
  201. /**
  202. *
  203. * @param string $path
  204. * @param int $page
  205. * @return array
  206. */
  207. public function get_listing($path = '', $page = '') {
  208. return $this->search('', $page);
  209. }
  210. /**
  211. * Return photo url by given photo id
  212. * @param string $photoid
  213. * @return string
  214. */
  215. private function build_photo_url($photoid) {
  216. $bestsize = $this->get_best_size($photoid);
  217. if (!isset($bestsize['source'])) {
  218. throw new repository_exception('cannotdownload', 'repository');
  219. }
  220. return $bestsize['source'];
  221. }
  222. /**
  223. * Returns the best size for a photo
  224. *
  225. * @param string $photoid the photo identifier
  226. * @return array of information provided by the API
  227. */
  228. protected function get_best_size($photoid) {
  229. if (!isset(self::$sizes[$photoid])) {
  230. // Sizes are returned from smallest to greatest.
  231. self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid);
  232. }
  233. $sizes = self::$sizes[$photoid];
  234. $bestsize = array();
  235. if (is_array($sizes)) {
  236. while ($bestsize = array_pop($sizes)) {
  237. // Make sure the source is set. Exit the loop if found.
  238. if (isset($bestsize['source'])) {
  239. break;
  240. }
  241. }
  242. }
  243. return $bestsize;
  244. }
  245. public function get_link($photoid) {
  246. return $this->build_photo_url($photoid);
  247. }
  248. /**
  249. *
  250. * @param string $photoid
  251. * @param string $file
  252. * @return string
  253. */
  254. public function get_file($photoid, $file = '') {
  255. $url = $this->build_photo_url($photoid);
  256. return parent::get_file($url, $file);
  257. }
  258. /**
  259. * Add Plugin settings input to Moodle form
  260. * @param object $mform
  261. */
  262. public static function type_config_form($mform, $classname = 'repository') {
  263. global $CFG;
  264. $api_key = get_config('flickr', 'api_key');
  265. $secret = get_config('flickr', 'secret');
  266. if (empty($api_key)) {
  267. $api_key = '';
  268. }
  269. if (empty($secret)) {
  270. $secret = '';
  271. }
  272. parent::type_config_form($mform);
  273. $strrequired = get_string('required');
  274. $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
  275. $mform->setType('api_key', PARAM_RAW_TRIMMED);
  276. $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
  277. $mform->setType('secret', PARAM_RAW_TRIMMED);
  278. //retrieve the flickr instances
  279. $params = array();
  280. $params['context'] = array();
  281. //$params['currentcontext'] = $this->context;
  282. $params['onlyvisible'] = false;
  283. $params['type'] = 'flickr';
  284. $instances = repository::get_instances($params);
  285. if (empty($instances)) {
  286. $callbackurl = get_string('callbackwarning', 'repository_flickr');
  287. $mform->addElement('static', null, '', $callbackurl);
  288. } else {
  289. $instance = array_shift($instances);
  290. $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
  291. $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
  292. }
  293. $mform->addRule('api_key', $strrequired, 'required', null, 'client');
  294. $mform->addRule('secret', $strrequired, 'required', null, 'client');
  295. }
  296. /**
  297. * Names of the plugin settings
  298. * @return array
  299. */
  300. public static function get_type_option_names() {
  301. return array('api_key', 'secret', 'pluginname');
  302. }
  303. public function supported_filetypes() {
  304. return array('web_image');
  305. }
  306. public function supported_returntypes() {
  307. return (FILE_INTERNAL | FILE_EXTERNAL);
  308. }
  309. /**
  310. * Return the source information
  311. *
  312. * @param string $photoid
  313. * @return string|null
  314. */
  315. public function get_file_source_info($photoid) {
  316. return $this->build_photo_url($photoid);
  317. }
  318. }