timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / blocks / tag_flickr / block_tag_flickr.php
blob43271d4c0cc9eedd14e0e493283bb128f669a289
1 <?php // $id$
3 require_once($CFG->dirroot.'/tag/lib.php');
4 require_once($CFG->libdir . '/filelib.php');
5 require_once($CFG->libdir . '/magpie/rss_cache.inc');
7 define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
8 define('DEFAULT_NUMBER_OF_PHOTOS', 6);
9 define('FLICKR_CACHE_EXPIRATION', 1800);
11 class block_tag_flickr extends block_base {
13 function init() {
14 $this->title = get_string('defaulttile','block_tag_flickr');
15 $this->version = 2007101509;
18 function applicable_formats() {
19 return array('tag' => true);
22 function specialization() {
23 $this->title = !empty($this->config->title) ? $this->config->title : get_string('blockname', 'block_tag_flickr');
26 function instance_allow_multiple() {
27 return true;
30 function preferred_width() {
31 return 170;
34 function get_content() {
36 global $CFG, $USER, $PAGE;
38 if ($this->content !== NULL) {
39 return $this->content;
42 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
43 $tag = optional_param('tag', '', PARAM_TAG); // tag
45 if ($tag) {
46 $tagobject = tag_get('name', $tag);
47 } else if ($tagid) {
48 $tagobject = tag_get('id', $tagid);
51 if (empty($tagobject)) {
52 print_error('tagnotfound');
55 //include related tags in the photo query ?
56 $tagscsv = $tagobject->name;
57 if (!empty($this->config->includerelatedtags)) {
58 $tagscsv .= ',' . tag_get_related_tags_csv(tag_get_related_tags($tagobject->id), TAG_RETURN_TEXT);
60 $tagscsv = urlencode($tagscsv);
62 //number of photos to display
63 $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
64 if( !empty($this->config->numberofphotos)) {
65 $numberofphotos = $this->config->numberofphotos;
68 //sort search results by
69 $sortby = 'relevance';
70 if( !empty($this->config->sortby)) {
71 $sortby = $this->config->sortby;
74 //pull photos from a specific photoset
75 if(!empty($this->config->photoset)){
77 $request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
78 $request .= '&api_key='.FLICKR_DEV_KEY;
79 $request .= '&photoset_id='.$this->config->photoset;
80 $request .= '&per_page='.$numberofphotos;
81 $request .= '&format=php_serial';
83 $response = $this->fetch_request($request);
85 $search = unserialize($response);
87 foreach ($search['photoset']['photo'] as $p){
88 $p['owner'] = $search['photoset']['owner'];
91 $photos = array_values($search['photoset']['photo']);
94 //search for photos tagged with $tagscsv
95 else{
97 $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
98 $request .= '&api_key='.FLICKR_DEV_KEY;
99 $request .= '&tags='.$tagscsv;
100 $request .= '&per_page='.$numberofphotos;
101 $request .= '&sort='.$sortby;
102 $request .= '&format=php_serial';
104 $response = $this->fetch_request($request);
106 $search = unserialize($response);
107 $photos = array_values($search['photos']['photo']);
111 if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
113 //Accessibility: render the list of photos
114 $text = '<ul class="inline-list">';
115 foreach ($photos as $photo) {
116 $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
117 $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
119 $text .= "</ul>\n";
121 $this->content = new stdClass;
122 $this->content->text = $text;
123 $this->content->footer = '';
125 return $this->content;
128 function fetch_request($request){
129 global $CFG;
131 make_upload_directory('/cache/flickr');
133 $cache = new RSSCache($CFG->dataroot . '/cache/flickr', FLICKR_CACHE_EXPIRATION);
134 $cache_status = $cache->check_cache( $request);
136 if ( $cache_status == 'HIT' ) {
137 $cached_response = $cache->get( $request );
139 return $cached_response;
142 if ( $cache_status == 'STALE' ) {
143 $cached_response = $cache->get( $request );
146 $response = download_file_content($request);
148 if(empty($response)){
149 $response = $cached_response;
151 else{
152 $cache->set($request, $response);
155 return $response;
158 function build_photo_url ($photo, $size='medium') {
159 //receives an array (can use the individual photo data returned
160 //from an API call) and returns a URL (doesn't mean that the
161 //file size exists)
162 $sizes = array(
163 'square' => '_s',
164 'thumbnail' => '_t',
165 'small' => '_m',
166 'medium' => '',
167 'large' => '_b',
168 'original' => '_o'
171 $size = strtolower($size);
172 if (!array_key_exists($size, $sizes)) {
173 $size = 'medium';
176 if ($size == 'original') {
177 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
178 } else {
179 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
181 return $url;