MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / tag_flickr / block_tag_flickr.php
CommitLineData
4ca6cfbf 1<?php
ecfac808 2
3require_once($CFG->dirroot.'/tag/lib.php');
83f8b304 4require_once($CFG->libdir . '/filelib.php');
ecfac808 5
57a260d9 6define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
ecfac808 7define('DEFAULT_NUMBER_OF_PHOTOS', 6);
ecfac808 8
9class block_tag_flickr extends block_base {
10
11 function init() {
b2fe457c 12 $this->title = get_string('pluginname','block_tag_flickr');
433c242f 13 $this->version = 2007101509;
ecfac808 14 }
15
16 function applicable_formats() {
17 return array('tag' => true);
18 }
19
20 function specialization() {
b2fe457c 21 $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_flickr');
ecfac808 22 }
23
24 function instance_allow_multiple() {
25 return true;
26 }
27
331362c8 28 function preferred_width() {
837aae26 29 return 170;
becf869c 30 }
331362c8 31
ecfac808 32 function get_content() {
33
c4740390 34 global $CFG, $USER;
ecfac808 35
36 if ($this->content !== NULL) {
37 return $this->content;
38 }
53420696 39
3d535996 40 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
becf869c 41 $tag = optional_param('tag', '', PARAM_TAG); // tag
3d535996 42
43 if ($tag) {
0c9bba1c 44 $tagobject = tag_get('name', $tag);
8479c2e0 45 } else if ($tagid) {
0c9bba1c 46 $tagobject = tag_get('id', $tagid);
8479c2e0 47 }
48
0c9bba1c 49 if (empty($tagobject)) {
f1ec6a63 50 $this->content = new stdClass;
51 $this->content->text = '';
52 $this->content->footer = '';
53 return $this->content;
3d535996 54 }
ecfac808 55
56 //include related tags in the photo query ?
0c9bba1c 57 $tagscsv = $tagobject->name;
ecfac808 58 if (!empty($this->config->includerelatedtags)) {
0c9bba1c 59 $tagscsv .= ',' . tag_get_related_tags_csv(tag_get_related_tags($tagobject->id), TAG_RETURN_TEXT);
ecfac808 60 }
0c9bba1c 61 $tagscsv = urlencode($tagscsv);
53420696 62
ecfac808 63 //number of photos to display
64 $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
65 if( !empty($this->config->numberofphotos)) {
66 $numberofphotos = $this->config->numberofphotos;
67 }
68
69 //sort search results by
70 $sortby = 'relevance';
71 if( !empty($this->config->sortby)) {
72 $sortby = $this->config->sortby;
73 }
74
ecfac808 75 //pull photos from a specific photoset
76 if(!empty($this->config->photoset)){
77
78 $request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
79 $request .= '&api_key='.FLICKR_DEV_KEY;
80 $request .= '&photoset_id='.$this->config->photoset;
81 $request .= '&per_page='.$numberofphotos;
82 $request .= '&format=php_serial';
53420696 83
ecfac808 84 $response = $this->fetch_request($request);
53420696 85
ecfac808 86 $search = unserialize($response);
87
8ee42537 88 foreach ($search['photoset']['photo'] as $p){
ecfac808 89 $p['owner'] = $search['photoset']['owner'];
90 }
53420696 91
ecfac808 92 $photos = array_values($search['photoset']['photo']);
53420696 93
ecfac808 94 }
0c9bba1c 95 //search for photos tagged with $tagscsv
ecfac808 96 else{
97
98 $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
99 $request .= '&api_key='.FLICKR_DEV_KEY;
0c9bba1c 100 $request .= '&tags='.$tagscsv;
ecfac808 101 $request .= '&per_page='.$numberofphotos;
102 $request .= '&sort='.$sortby;
103 $request .= '&format=php_serial';
53420696 104
ecfac808 105 $response = $this->fetch_request($request);
53420696 106
ecfac808 107 $search = unserialize($response);
becf869c 108 $photos = array_values($search['photos']['photo']);
ecfac808 109 }
53420696 110
ecfac808 111
112 if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
113
42131376 114 //Accessibility: render the list of photos
115 $text = '<ul class="inline-list">';
116 foreach ($photos as $photo) {
117 $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
118 $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
119 }
120 $text .= "</ul>\n";
ecfac808 121
122 $this->content = new stdClass;
123 $this->content->text = $text;
124 $this->content->footer = '';
125
126 return $this->content;
ecfac808 127 }
53420696 128
129 function fetch_request($request){
becf869c 130 $c = new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
ecfac808 131
becf869c 132 $response = $c->get($request);
ecfac808 133
becf869c 134 return $response;
ecfac808 135 }
53420696 136
e62950ca 137 function build_photo_url ($photo, $size='medium') {
ecfac808 138 //receives an array (can use the individual photo data returned
139 //from an API call) and returns a URL (doesn't mean that the
140 //file size exists)
141 $sizes = array(
e62950ca 142 'square' => '_s',
143 'thumbnail' => '_t',
144 'small' => '_m',
145 'medium' => '',
146 'large' => '_b',
147 'original' => '_o'
ecfac808 148 );
53420696 149
ecfac808 150 $size = strtolower($size);
151 if (!array_key_exists($size, $sizes)) {
e62950ca 152 $size = 'medium';
ecfac808 153 }
53420696 154
e62950ca 155 if ($size == 'original') {
156 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
ecfac808 157 } else {
e62950ca 158 $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
ecfac808 159 }
160 return $url;
becf869c 161 }
ecfac808 162}
53420696 163
4ca6cfbf 164