block_tag_youtube.php 5.01 KB
Newer Older
1
<?php 
2
3

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

moodler's avatar
moodler committed
6
define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
7
8
9
10
11
12
define('DEFAULT_NUMBER_OF_VIDEOS', 5);

class block_tag_youtube extends block_base {

    function init() {
        $this->title = get_string('blockname','block_tag_youtube');
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_youtube');
    }

    function instance_allow_multiple() {
        return true;
    }

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

32
33
34
35
36
37
    function get_content() {

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

38
        $text = '';
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
        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(){

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

        if ($tag) {
83
            $tagobject = tag_get('name', $tag);
84
        } else if ($tagid) {
85
            $tagobject = tag_get('id', $tagid);
86
87
        }

88
        if (empty($tagobject)) {
89
            return '';
90
        }
91

92
        $querytag = urlencode($tagobject->name);
93
94

        $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
95
        if ( !empty($this->config->numberofvideos) ) {
96
97
98
99
            $numberofvideos = $this->config->numberofvideos;
        }

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

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

    function get_videos_by_tag_and_category(){

110
        $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
111
        $tag = optional_param('tag', '', PARAM_TAG); // tag
112
113

        if ($tag) {
114
            $tagobject = tag_get('name', $tag);
115
        } else if ($tagid) {
116
            $tagobject = tag_get('id', $tagid);
117
118
        }

119
        if (empty($tagobject)) {
120
            return '';
121
        }
122

123
        $querytag = urlencode($tagobject->name);
124
125
126
127
128
129
130
131
132

        $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;
133
        $request .= "&tag={$querytag}";
134
135
136
        $request .= "&page=1";
        $request .= "&per_page={$numberofvideos}";

137
        return $this->fetch_request($request);
138
139
    }

140
    function fetch_request($request){
141
142
        $c = new curl(array('cache' => true, 'module_cache'=>'tag_youtube'));
        $c->setopt(array('CURLOPT_TIMEOUT' => 3, 'CURLOPT_CONNECTTIMEOUT' => 3));
143

144
        $response = $c->get($request);
145

146
147
        $xml = new SimpleXMLElement($response);
        return $this->render_video_list($xml);
148
    }
149

150
    function render_video_list(SimpleXMLElement $xml){
151
152

        $text = '';
153
        $text .= '<ul class="yt-video-entry unlist img-text">';
154
155
156
157
158
159
160
161
162
163

        foreach($xml->video_list->video 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";
164
        }
165
        $text .= "</ul><div class=\"clearer\"></div>\n";
166
167
168
169
        return $text;
    }
}
?>