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->dirroot.'/tag/lib.php');
51 require_once($CFG->libdir . '/filelib.php');
53 if ($this->content !== NULL) {
54 return $this->content;
57 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
58 $tag = optional_param('tag', '', PARAM_TAG); // tag
61 $tagobject = tag_get('name', $tag);
63 $tagobject = tag_get('id', $tagid);
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 $tagscsv .= ',' . tag_get_related_tags_csv(tag_get_related_tags($tagobject->id), TAG_RETURN_TEXT);
78 $tagscsv = urlencode($tagscsv);
80 //number of photos to display
81 $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
82 if( !empty($this->config->numberofphotos)) {
83 $numberofphotos = $this->config->numberofphotos;
86 //sort search results by
87 $sortby = 'relevance';
88 if( !empty($this->config->sortby)) {
89 $sortby = $this->config->sortby;
92 //pull photos from a specific photoset
93 if(!empty($this->config->photoset)){
95 $request = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
96 $request .= '&api_key='.FLICKR_DEV_KEY;
97 $request .= '&photoset_id='.$this->config->photoset;
98 $request .= '&per_page='.$numberofphotos;
99 $request .= '&format=php_serial';
101 $response = $this->fetch_request($request);
103 $search = unserialize($response);
105 foreach ($search['photoset']['photo'] as $p){
106 $p['owner'] = $search['photoset']['owner'];
109 $photos = array_values($search['photoset']['photo']);
112 //search for photos tagged with $tagscsv
115 $request = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
116 $request .= '&api_key='.FLICKR_DEV_KEY;
117 $request .= '&tags='.$tagscsv;
118 $request .= '&per_page='.$numberofphotos;
119 $request .= '&sort='.$sortby;
120 $request .= '&format=php_serial';
122 $response = $this->fetch_request($request);
124 $search = unserialize($response);
125 $photos = array_values($search['photos']['photo']);
129 if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
131 //Accessibility: render the list of photos
132 $text = '<ul class="inline-list">';
133 foreach ($photos as $photo) {
134 $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
135 $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
139 $this->content = new stdClass;
140 $this->content->text = $text;
141 $this->content->footer = '';
143 return $this->content;
146 function fetch_request($request){
147 $c = new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
149 $response = $c->get($request);
154 function build_photo_url ($photo, $size='medium') {
155 //receives an array (can use the individual photo data returned
156 //from an API call) and returns a URL (doesn't mean that the
167 $size = strtolower($size);
168 if (!array_key_exists($size, $sizes)) {
172 if ($size == 'original') {
173 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
175 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';