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');
8 define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
9 define('DEFAULT_NUMBER_OF_VIDEOS', 5);
10 define('YOUTUBE_CACHE_EXPIRATION', 1800);
12 class block_tag_youtube
extends block_base
{
15 $this->title
= get_string('blockname','block_tag_youtube');
16 $this->version
= 2007101509;
19 function applicable_formats() {
20 return array('tag' => true);
23 function specialization() {
24 $this->title
= !empty($this->config
->title
) ?
$this->config
->title
: get_string('blockname', 'block_tag_youtube');
27 function instance_allow_multiple() {
31 function preferred_width() {
35 function get_content() {
37 if ($this->content
!== NULL) {
38 return $this->content
;
41 if(!empty($this->config
->playlist
)){
42 //videos from a playlist
43 $text = $this->get_videos_by_playlist();
46 if(!empty($this->config
->category
)){
47 //videos from category with tag
48 $text = $this->get_videos_by_tag_and_category();
52 $text = $this->get_videos_by_tag();
56 $this->content
= new stdClass
;
57 $this->content
->text
= $text;
58 $this->content
->footer
= '';
60 return $this->content
;
63 function get_videos_by_playlist(){
65 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS
;
66 if( !empty($this->config
->numberofvideos
)) {
67 $numberofvideos = $this->config
->numberofvideos
;
70 $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_playlist';
71 $request .= '&dev_id=' . YOUTUBE_DEV_KEY
;
72 $request .= "&id={$this->config->playlist}";
73 $request .= "&page=1";
74 $request .= "&per_page={$numberofvideos}";
76 return $this->fetch_request($request);
79 function get_videos_by_tag(){
81 $tagid = optional_param('id', 0, PARAM_INT
); // tag id - for backware compatibility
82 $tag = optional_param('tag', '', PARAM_TAG
); // tag
85 $tagobject = tag_get('name', $tag);
87 $tagobject = tag_get('id', $tagid);
90 if (empty($tagobject)) {
91 print_error('tagnotfound');
94 $querytag = urlencode($tagobject->name
);
96 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS
;
97 if ( !empty($this->config
->numberofvideos
) ) {
98 $numberofvideos = $this->config
->numberofvideos
;
101 $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag';
102 $request .= '&dev_id='. YOUTUBE_DEV_KEY
;
103 $request .= "&tag={$querytag}";
104 $request .= "&page=1";
105 $request .= "&per_page={$numberofvideos}";
107 return $this->fetch_request($request);
110 function get_videos_by_tag_and_category(){
112 $tagid = optional_param('id', 0, PARAM_INT
); // tag id - for backware compatibility
113 $tag = optional_param('tag', '', PARAM_TAG
); // tag
116 $tagobject = tag_get('name', $tag);
118 $tagobject = tag_get('id', $tagid);
121 if (empty($tagobject)) {
122 print_error('tagnotfound');
125 $querytag = urlencode($tagobject->name
);
127 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS
;
128 if( !empty($this->config
->numberofvideos
)) {
129 $numberofvideos = $this->config
->numberofvideos
;
132 $request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_category_and_tag';
133 $request .= '&category_id='.$this->config
->category
;
134 $request .= '&dev_id=' . YOUTUBE_DEV_KEY
;
135 $request .= "&tag={$querytag}";
136 $request .= "&page=1";
137 $request .= "&per_page={$numberofvideos}";
139 return $this->fetch_request($request);
142 function fetch_request($request){
146 make_upload_directory('/cache/youtube');
148 $cache = new RSSCache($CFG->dataroot
. '/cache/youtube',YOUTUBE_CACHE_EXPIRATION
);
149 $cache_status = $cache->check_cache( $request);
151 if ( $cache_status == 'HIT' ) {
152 $cached_response = $cache->get( $request );
154 $xmlobj = XML_unserialize($cached_response);
155 return $this->render_video_list($xmlobj);
158 if ( $cache_status == 'STALE' ) {
159 $cached_response = $cache->get( $request );
162 $response = download_file_content($request);
164 if(empty($response)){
165 $response = $cached_response;
168 $cache->set($request, $response);
171 $xmlobj = XML_unserialize($response);
172 return $this->render_video_list($xmlobj);
175 function render_video_list($xmlobj){
178 $text .= '<ul class="yt-video-entry unlist img-text">';
179 $videos = $xmlobj['ut_response']['video_list']['video'];
181 if (is_array($videos) ) {
182 foreach($videos as $video){
184 $text .= '<div class="clearfix">';
185 $text .= '<a href="'. s($video['url']) . '">';
186 $text .= '<img alt="" class="youtube-thumb" src="'. $video['thumbnail_url'] .'" /></a>';
187 $text .= '</div><span><a href="'. s($video['url']) . '">'.s($video['title']).'</a></span>';
189 $text .= format_time($video['length_seconds']);
190 $text .= "</div></li>\n";
193 // if youtube is offline, or for whatever reason the previous
194 // call doesn't work...
195 //add_to_log(SITEID, 'blocks/tag_youtube', 'problem in getting videos off youtube');
197 $text .= "</ul><div class=\"clearer\"></div>\n";