MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / tag_flickr / block_tag_flickr.php
CommitLineData
4ca6cfbf 1<?php
f25a6839
SH
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/**
18 * Flickr tag block.
19 *
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
23 */
ecfac808 24
57a260d9 25define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
ecfac808 26define('DEFAULT_NUMBER_OF_PHOTOS', 6);
ecfac808 27
28class block_tag_flickr extends block_base {
29
30 function init() {
b2fe457c 31 $this->title = get_string('pluginname','block_tag_flickr');
ecfac808 32 }
33
34 function applicable_formats() {
35 return array('tag' => true);
36 }
37
38 function specialization() {
b2fe457c 39 $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_flickr');
ecfac808 40 }
41
42 function instance_allow_multiple() {
43 return true;
44 }
45
46 function get_content() {
c4740390 47 global $CFG, $USER;
ecfac808 48
8571833f 49 //note: do NOT include files at the top of this file
8571833f
PS
50 require_once($CFG->libdir . '/filelib.php');
51
ecfac808 52 if ($this->content !== NULL) {
53 return $this->content;
54 }
53420696 55
3d535996 56 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
becf869c 57 $tag = optional_param('tag', '', PARAM_TAG); // tag
ac840cf4 58 $tc = optional_param('tc', 0, PARAM_INT); // Tag collection id.
3d535996 59
ac840cf4
MG
60 if ($tagid) {
61 $tagobject = core_tag_tag::get($tagid);
62 } else if ($tag) {
63 $tagobject = core_tag_tag::get_by_name($tc, $tag);
8479c2e0 64 }
65
0c9bba1c 66 if (empty($tagobject)) {
f1ec6a63 67 $this->content = new stdClass;
68 $this->content->text = '';
69 $this->content->footer = '';
70 return $this->content;
3d535996 71 }
ecfac808 72
73 //include related tags in the photo query ?
0c9bba1c 74 $tagscsv = $tagobject->name;
ecfac808 75 if (!empty($this->config->includerelatedtags)) {
ac840cf4
MG
76 foreach ($tagobject->get_related_tags() as $t) {
77 $tagscsv .= ',' . $t->get_display_name(false);
78 }
ecfac808 79 }
0c9bba1c 80 $tagscsv = urlencode($tagscsv);
53420696 81
ecfac808 82 //number of photos to display
83 $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
84 if( !empty($this->config->numberofphotos)) {
85 $numberofphotos = $this->config->numberofphotos;
86 }
87
88 //sort search results by
89 $sortby = 'relevance';
90 if( !empty($this->config->sortby)) {
91 $sortby = $this->config->sortby;
92 }
93
ecfac808 94 //pull photos from a specific photoset
95 if(!empty($this->config->photoset)){
96
6f6345e3 97 $request = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
ecfac808 98 $request .= '&api_key='.FLICKR_DEV_KEY;
99 $request .= '&photoset_id='.$this->config->photoset;
100 $request .= '&per_page='.$numberofphotos;
101 $request .= '&format=php_serial';
53420696 102
ecfac808 103 $response = $this->fetch_request($request);
53420696 104
ecfac808 105 $search = unserialize($response);
106
8ee42537 107 foreach ($search['photoset']['photo'] as $p){
ecfac808 108 $p['owner'] = $search['photoset']['owner'];
109 }
53420696 110
ecfac808 111 $photos = array_values($search['photoset']['photo']);
53420696 112
ecfac808 113 }
0c9bba1c 114 //search for photos tagged with $tagscsv
ecfac808 115 else{
116
6f6345e3 117 $request = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
ecfac808 118 $request .= '&api_key='.FLICKR_DEV_KEY;
0c9bba1c 119 $request .= '&tags='.$tagscsv;
ecfac808 120 $request .= '&per_page='.$numberofphotos;
121 $request .= '&sort='.$sortby;
122 $request .= '&format=php_serial';
53420696 123
ecfac808 124 $response = $this->fetch_request($request);
53420696 125
ecfac808 126 $search = unserialize($response);
becf869c 127 $photos = array_values($search['photos']['photo']);
ecfac808 128 }
53420696 129
ecfac808 130
131 if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
132
42131376 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";
138 }
139 $text .= "</ul>\n";
ecfac808 140
141 $this->content = new stdClass;
142 $this->content->text = $text;
143 $this->content->footer = '';
144
145 return $this->content;
ecfac808 146 }
53420696 147
148 function fetch_request($request){
becf869c 149 $c = new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
ecfac808 150
becf869c 151 $response = $c->get($request);
ecfac808 152
becf869c 153 return $response;
ecfac808 154 }
53420696 155
e62950ca 156 function build_photo_url ($photo, $size='medium') {
ecfac808 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
159 //file size exists)
160 $sizes = array(
e62950ca 161 'square' => '_s',
162 'thumbnail' => '_t',
163 'small' => '_m',
164 'medium' => '',
165 'large' => '_b',
166 'original' => '_o'
ecfac808 167 );
53420696 168
ecfac808 169 $size = strtolower($size);
170 if (!array_key_exists($size, $sizes)) {
e62950ca 171 $size = 'medium';
ecfac808 172 }
53420696 173
e62950ca 174 if ($size == 'original') {
175 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
ecfac808 176 } else {
e62950ca 177 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
ecfac808 178 }
179 return $url;
becf869c 180 }
ecfac808 181}
53420696 182
4ca6cfbf 183