block_tag_flickr.php 5.31 KB
Newer Older
1
2
3
4
5
<?php 

require_once($CFG->dirroot.'/tag/lib.php');
require_once($CFG->libdir . '/magpie/rss_cache.inc');

moodler's avatar
moodler committed
6
define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
define('DEFAULT_NUMBER_OF_PHOTOS', 6);
define('FLICKR_CACHE_EXPIRATION', 1800);

class block_tag_flickr extends block_base {

    function init() {
        $this->title = get_string('defaulttile','block_tag_flickr');
        $this->version = 2007080800;
    }

    function applicable_formats() {
        return array('tag' => true);
    }

    function specialization() {
        $this->title = !empty($this->config->title) ? $this->config->title : get_string('blockname', 'block_tag_flickr');
    }

    function instance_allow_multiple() {
        return true;
    }

    function get_content() {

        global $CFG, $USER, $PAGE;

        if ($this->content !== NULL) {
            return $this->content;
        }
36

37
38
39
40
41
42
43
44
        $tagid       = optional_param('id',     0,      PARAM_INT);   // tag id

        //include related tags in the photo query ?
        $tags_csv = tag_display_name(tag_by_id($tagid));
        if (!empty($this->config->includerelatedtags)) {
            $tags_csv .= ',' . tag_names_csv( get_item_tags('tag',$tagid));
        }
        $tags_csv = urlencode($tags_csv);
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        //number of photos to display
        $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
        if( !empty($this->config->numberofphotos)) {
            $numberofphotos = $this->config->numberofphotos;
        }

        //sort search results by
        $sortby = 'relevance';
        if( !empty($this->config->sortby)) {
            $sortby = $this->config->sortby;
        }

        //pull photos from a specific photoset
        if(!empty($this->config->photoset)){

            $request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
            $request .= '&api_key='.FLICKR_DEV_KEY;
            $request .= '&photoset_id='.$this->config->photoset;
            $request .= '&per_page='.$numberofphotos;
            $request .= '&format=php_serial';
66

67
            $response = $this->fetch_request($request);
68

69
70
            $search = unserialize($response);

moodler's avatar
moodler committed
71
            foreach ($search['photoset']['photo'] as $p){
72
73
                $p['owner'] = $search['photoset']['owner'];
            }
74

75
            $photos = array_values($search['photoset']['photo']);
76

77
78
79
80
81
82
83
84
85
86
        }
        //search for photos tagged with $tags_csv
        else{

            $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
            $request .= '&api_key='.FLICKR_DEV_KEY;
            $request .= '&tags='.$tags_csv;
            $request .= '&per_page='.$numberofphotos;
            $request .= '&sort='.$sortby;
            $request .= '&format=php_serial';
87

88
            $response = $this->fetch_request($request);
89

90
            $search = unserialize($response);
91
            $photos = array_values($search['photos']['photo']);  
92
        }
93

94
95
96

        if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...

97
98
99
100
101
102
103
        //Accessibility: render the list of photos
        $text = '<ul class="inline-list">';
         foreach ($photos as $photo) {
            $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
            $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
         }
        $text .= "</ul>\n";
104
105
106
107
108
109
110

        $this->content = new stdClass;
        $this->content->text = $text;
        $this->content->footer = '';

        return $this->content;
    }
111
112

    function fetch_request($request){
113
        global $CFG;
114
115

        make_upload_directory('/cache/flickr');
116

117
        $cache = new RSSCache($CFG->dataroot . '/cache/flickr', FLICKR_CACHE_EXPIRATION);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        $cache_status = $cache->check_cache( $request);

        if ( $cache_status == 'HIT' ) {
            $cached_response = $cache->get( $request );

            return $cached_response;
        }

        if ( $cache_status == 'STALE' ) {
            $cached_response = $cache->get( $request );
        }

        $response = file_get_contents($request);

        if(empty($response)){
            $response = $cached_response;
        }
        else{
            $cache->set($request, $response);    
        }

        return $response;        
    }
141

moodler's avatar
moodler committed
142
    function build_photo_url ($photo, $size='medium') {
143
144
145
146
        //receives an array (can use the individual photo data returned
        //from an API call) and returns a URL (doesn't mean that the
        //file size exists)
        $sizes = array(
moodler's avatar
moodler committed
147
148
149
150
151
152
            'square' => '_s',
            'thumbnail' => '_t',
            'small' => '_m',
            'medium' => '',
            'large' => '_b',
            'original' => '_o'
153
        );
154

155
156
        $size = strtolower($size);
        if (!array_key_exists($size, $sizes)) {
moodler's avatar
moodler committed
157
            $size = 'medium';
158
        }
159

moodler's avatar
moodler committed
160
161
        if ($size == 'original') {
            $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
162
        } else {
moodler's avatar
moodler committed
163
            $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
164
165
166
167
        }
        return $url;
    }    
}
168

169
?>