block_tag_flickr.php 5.19 KB
Newer Older
1
<?php // $id$
2
3

require_once($CFG->dirroot.'/tag/lib.php');
4
require_once($CFG->libdir . '/filelib.php');
5

moodler's avatar
moodler committed
6
define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
7
8
9
10
11
12
define('DEFAULT_NUMBER_OF_PHOTOS', 6);

class block_tag_flickr extends block_base {

    function init() {
        $this->title = get_string('defaulttile','block_tag_flickr');
moodler's avatar
moodler committed
13
        $this->version = 2007101509;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    }

    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;
    }

28
    function preferred_width() {
29
        return 170;
30
    }
31

32
33
    function get_content() {

34
        global $CFG, $USER;
35
36
37
38

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

40
        $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
41
        $tag = optional_param('tag', '', PARAM_TAG); // tag
42
43

        if ($tag) {
44
            $tagobject = tag_get('name', $tag);
45
        } else if ($tagid) {
46
            $tagobject = tag_get('id', $tagid);
47
48
        }

49
        if (empty($tagobject)) {
50
51
52
53
            $this->content = new stdClass;
            $this->content->text = '';
            $this->content->footer = '';
            return $this->content;
54
        }
55
56

        //include related tags in the photo query ?
57
        $tagscsv = $tagobject->name;
58
        if (!empty($this->config->includerelatedtags)) {
59
            $tagscsv .= ',' . tag_get_related_tags_csv(tag_get_related_tags($tagobject->id), TAG_RETURN_TEXT);
60
        }
61
        $tagscsv = urlencode($tagscsv);
62

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        //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';
83

84
            $response = $this->fetch_request($request);
85

86
87
            $search = unserialize($response);

moodler's avatar
moodler committed
88
            foreach ($search['photoset']['photo'] as $p){
89
90
                $p['owner'] = $search['photoset']['owner'];
            }
91

92
            $photos = array_values($search['photoset']['photo']);
93

94
        }
95
        //search for photos tagged with $tagscsv
96
97
98
99
        else{

            $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
            $request .= '&api_key='.FLICKR_DEV_KEY;
100
            $request .= '&tags='.$tagscsv;
101
102
103
            $request .= '&per_page='.$numberofphotos;
            $request .= '&sort='.$sortby;
            $request .= '&format=php_serial';
104

105
            $response = $this->fetch_request($request);
106

107
            $search = unserialize($response);
108
            $photos = array_values($search['photos']['photo']);
109
        }
110

111
112
113

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

114
115
116
117
118
119
120
        //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";
121
122
123
124
125
126
127

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

        return $this->content;
    }
128
129

    function fetch_request($request){
130
        $c =  new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
131

132
        $response = $c->get($request);
133

134
        return $response;
135
    }
136

moodler's avatar
moodler committed
137
    function build_photo_url ($photo, $size='medium') {
138
139
140
141
        //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
142
143
144
145
146
147
            'square' => '_s',
            'thumbnail' => '_t',
            'small' => '_m',
            'medium' => '',
            'large' => '_b',
            'original' => '_o'
148
        );
149

150
151
        $size = strtolower($size);
        if (!array_key_exists($size, $sizes)) {
moodler's avatar
moodler committed
152
            $size = 'medium';
153
        }
154

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

164
?>