block_tag_youtube.php 6.2 KB
Newer Older
1
<?php // $id$
2
3

require_once($CFG->dirroot.'/tag/lib.php');
4
require_once($CFG->libdir . '/filelib.php');
5
require_once($CFG->libdir . '/magpie/rss_cache.inc');
6
require_once($CFG->libdir . '/phpxml/xml.php');
7

moodler's avatar
moodler committed
8
define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
9
10
11
12
13
14
15
define('DEFAULT_NUMBER_OF_VIDEOS', 5);
define('YOUTUBE_CACHE_EXPIRATION', 1800);

class block_tag_youtube extends block_base {

    function init() {
        $this->title = get_string('blockname','block_tag_youtube');
moodler's avatar
moodler committed
16
        $this->version = 2007101509;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    }

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

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

    function instance_allow_multiple() {
        return true;
    }

31
32
33
34
    function preferred_width() {
        return 140;
    } 

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    function get_content() {

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

        if(!empty($this->config->playlist)){
            //videos from a playlist
            $text = $this->get_videos_by_playlist();
        }
        else{
            if(!empty($this->config->category)){
                //videos from category with tag
                $text = $this->get_videos_by_tag_and_category();
            }
            else {
                //videos with tag
                $text = $this->get_videos_by_tag();
            }
        }

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

        return $this->content;
    }

    function get_videos_by_playlist(){

        $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
        if( !empty($this->config->numberofvideos)) {
            $numberofvideos = $this->config->numberofvideos;
        }

        $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_playlist';
        $request .= '&dev_id=' . YOUTUBE_DEV_KEY;
        $request .= "&id={$this->config->playlist}";
        $request .= "&page=1";
        $request .= "&per_page={$numberofvideos}";

        return $this->fetch_request($request);
    }

    function get_videos_by_tag(){

81
82
83
84
        $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
        $tag = optional_param('tag', '', PARAM_TAG); // tag 

        if ($tag) {
85
86
87
88
89
90
            $tag_object = tag_get('name', $tag);
        } else if ($tagid) {
            $tag_object = tag_get('id', $tagid);
        }

        if (empty($tag_object)) {
moodler's avatar
moodler committed
91
            print_error('tagnotfound');
92
        }
93

94
        $query_tag = html_entity_decode(tag_display_name($tag_object));
95
96
97
        $query_tag = urlencode($query_tag);

        $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
98
        if ( !empty($this->config->numberofvideos) ) {
99
100
101
102
            $numberofvideos = $this->config->numberofvideos;
        }

        $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag';
103
        $request .= '&dev_id='. YOUTUBE_DEV_KEY;
104
105
106
107
108
109
110
111
112
        $request .= "&tag={$query_tag}";
        $request .= "&page=1";
        $request .= "&per_page={$numberofvideos}";

        return $this->fetch_request($request);
    }

    function get_videos_by_tag_and_category(){

113
114
115
116
        $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
        $tag = optional_param('tag', '', PARAM_TAG); // tag 

        if ($tag) {
117
118
119
120
121
122
            $tag_object = tag_get('name', $tag);
        } else if ($tagid) {
            $tag_object = tag_get('id', $tagid);
        }

        if (empty($tag_object)) {
moodler's avatar
moodler committed
123
            print_error('tagnotfound');
124
        }
125

126
        $query_tag = html_entity_decode(tag_display_name($tag_object));
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        $query_tag = urlencode($query_tag);

        $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
        if( !empty($this->config->numberofvideos)) {
            $numberofvideos = $this->config->numberofvideos;
        }

        $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_category_and_tag';
        $request .= '&category_id='.$this->config->category;
        $request .= '&dev_id=' . YOUTUBE_DEV_KEY;
        $request .= "&tag={$query_tag}";
        $request .= "&page=1";
        $request .= "&per_page={$numberofvideos}";

141
        return $this->fetch_request($request);
142
143
    }

144
145
    function fetch_request($request){

146
        global $CFG;
147

148
149
150
        make_upload_directory('/cache/youtube');

        $cache = new RSSCache($CFG->dataroot . '/cache/youtube',YOUTUBE_CACHE_EXPIRATION);
151
152
153
154
155
        $cache_status = $cache->check_cache( $request);

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

156
157
            $xmlobj = XML_unserialize($cached_response);
            return $this->render_video_list($xmlobj);
158
159
160
161
162
163
        }

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

164
        $response = download_file_content($request);
165
166
167
168
169

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

173
174
        $xmlobj = XML_unserialize($response);
        return $this->render_video_list($xmlobj);
175
    }
176
177

    function render_video_list($xmlobj){
178
179

        $text = '';
180
        $text .= '<ul class="yt-video-entry unlist img-text">';
181
182
        $videos = $xmlobj['ut_response']['video_list']['video'];

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        if (is_array($videos) ) {
            foreach($videos as $video){
                $text .= '<li>';
                $text .= '<div class="clearfix">';
                $text .= '<a href="'. s($video['url']) . '">';
                $text .= '<img alt="" class="youtube-thumb" src="'. $video['thumbnail_url'] .'" /></a>';
                $text .= '</div><span><a href="'. s($video['url']) . '">'.s($video['title']).'</a></span>';
                $text .= '<div>';
                $text .= format_time($video['length_seconds']);
                $text .= "</div></li>\n";
            }
        } else { 
            // if youtube is offline, or for whatever reason the previous
            // call doesn't work... 
            //add_to_log(SITEID, 'blocks/tag_youtube', 'problem in getting videos off youtube');
198
        }
199
        $text .= "</ul><div class=\"clearer\"></div>\n";
200
201
202
203
        return $text;
    }
}
?>