2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @package block_tag_flickr
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
26 define('DEFAULT_NUMBER_OF_PHOTOS', 6);
28 class block_tag_flickr extends block_base {
31 $this->title = get_string('pluginname','block_tag_flickr');
34 function applicable_formats() {
35 return array('tag' => true);
38 function specialization() {
39 $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_flickr');
42 function instance_allow_multiple() {
46 function get_content() {
49 //note: do NOT include files at the top of this file
50 require_once($CFG->libdir . '/filelib.php');
52 if ($this->content !== NULL) {
53 return $this->content;
56 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
57 $tag = optional_param('tag', '', PARAM_TAG); // tag
58 $tc = optional_param('tc', 0, PARAM_INT); // Tag collection id.
61 $tagobject = core_tag_tag::get($tagid);
63 $tagobject = core_tag_tag::get_by_name($tc, $tag);
66 if (empty($tagobject)) {
67 $this->content = new stdClass;
68 $this->content->text = '';
69 $this->content->footer = '';
70 return $this->content;
73 //include related tags in the photo query ?
74 $tagscsv = $tagobject->name;
75 if (!empty($this->config->includerelatedtags)) {
76 foreach ($tagobject->get_related_tags() as $t) {
77 $tagscsv .= ',' . $t->get_display_name(false);
80 $tagscsv = urlencode($tagscsv);
82 //number of photos to display
83 $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
84 if( !empty($this->config->numberofphotos)) {
85 $numberofphotos = $this->config->numberofphotos;
88 //sort search results by
89 $sortby = 'relevance';
90 if( !empty($this->config->sortby)) {
91 $sortby = $this->config->sortby;
94 //pull photos from a specific photoset
95 if(!empty($this->config->photoset)){
97 $request = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
98 $request .= '&api_key='.FLICKR_DEV_KEY;
99 $request .= '&photoset_id='.$this->config->photoset;
100 $request .= '&per_page='.$numberofphotos;
101 $request .= '&format=php_serial';
103 $response = $this->fetch_request($request);
105 $search = unserialize($response);
107 foreach ($search['photoset']['photo'] as $p){
108 $p['owner'] = $search['photoset']['owner'];
111 $photos = array_values($search['photoset']['photo']);
114 //search for photos tagged with $tagscsv
117 $request = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
118 $request .= '&api_key='.FLICKR_DEV_KEY;
119 $request .= '&tags='.$tagscsv;
120 $request .= '&per_page='.$numberofphotos;
121 $request .= '&sort='.$sortby;
122 $request .= '&format=php_serial';
124 $response = $this->fetch_request($request);
126 $search = unserialize($response);
127 $photos = array_values($search['photos']['photo']);
131 if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
133 //Accessibility: render the list of photos
134 $text = '<ul class="inline-list">';
135 foreach ($photos as $photo) {
136 $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
137 $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
141 $this->content = new stdClass;
142 $this->content->text = $text;
143 $this->content->footer = '';
145 return $this->content;
148 function fetch_request($request){
149 $c = new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
151 $response = $c->get($request);
156 function build_photo_url ($photo, $size='medium') {
157 //receives an array (can use the individual photo data returned
158 //from an API call) and returns a URL (doesn't mean that the
169 $size = strtolower($size);
170 if (!array_key_exists($size, $sizes)) {
174 if ($size == 'original') {
175 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
177 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';