MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / tag_youtube / block_tag_youtube.php
CommitLineData
4ca6cfbf 1<?php
ecfac808 2
3require_once($CFG->dirroot.'/tag/lib.php');
83f8b304 4require_once($CFG->libdir . '/filelib.php');
ecfac808 5
ecfac808 6define('DEFAULT_NUMBER_OF_VIDEOS', 5);
ecfac808 7
8class block_tag_youtube extends block_base {
9
10 function init() {
9b7b2dae 11 $this->title = get_string('pluginname','block_tag_youtube');
433c242f 12 $this->version = 2007101509;
ecfac808 13 }
14
15 function applicable_formats() {
16 return array('tag' => true);
17 }
18
19 function specialization() {
9b7b2dae 20 $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_youtube');
26a8a8c8
EL
21 // Convert numeric categories (old YouTube API) to
22 // textual ones (new Google Data API)
23 $this->config->category = !empty($this->config->category) ? $this->category_map_old2new($this->config->category) : '0';
ecfac808 24 }
25
26 function instance_allow_multiple() {
27 return true;
28 }
29
331362c8 30 function preferred_width() {
31 return 140;
becf869c 32 }
331362c8 33
ecfac808 34 function get_content() {
35
ecfac808 36 if ($this->content !== NULL) {
37 return $this->content;
38 }
39
03f3a434 40 $text = '';
ecfac808 41 if(!empty($this->config->playlist)){
42 //videos from a playlist
43 $text = $this->get_videos_by_playlist();
44 }
45 else{
46 if(!empty($this->config->category)){
47 //videos from category with tag
48 $text = $this->get_videos_by_tag_and_category();
49 }
50 else {
51 //videos with tag
52 $text = $this->get_videos_by_tag();
53 }
54 }
55
56 $this->content = new stdClass;
57 $this->content->text = $text;
58 $this->content->footer = '';
59
60 return $this->content;
ecfac808 61 }
62
63 function get_videos_by_playlist(){
64
65 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
66 if( !empty($this->config->numberofvideos)) {
67 $numberofvideos = $this->config->numberofvideos;
68 }
69
4f42e466
EL
70 $request = 'http://gdata.youtube.com/feeds/api/playlists/' .
71 $this->config->playlist .
72 '?start-index=1&max-results=' .
73 $numberofvideos .
74 '&format=5';
ecfac808 75
76 return $this->fetch_request($request);
ecfac808 77 }
78
79 function get_videos_by_tag(){
80
3d535996 81 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
becf869c 82 $tag = optional_param('tag', '', PARAM_TAG); // tag
3d535996 83
84 if ($tag) {
0c9bba1c 85 $tagobject = tag_get('name', $tag);
8479c2e0 86 } else if ($tagid) {
0c9bba1c 87 $tagobject = tag_get('id', $tagid);
8479c2e0 88 }
89
0c9bba1c 90 if (empty($tagobject)) {
f1ec6a63 91 return '';
3d535996 92 }
ecfac808 93
0c9bba1c 94 $querytag = urlencode($tagobject->name);
ecfac808 95
96 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
3d535996 97 if ( !empty($this->config->numberofvideos) ) {
ecfac808 98 $numberofvideos = $this->config->numberofvideos;
99 }
100
4f42e466
EL
101 $request = 'http://gdata.youtube.com/feeds/api/videos?vq=' .
102 $querytag .
103 '&start-index=1&max-results=' .
104 $numberofvideos .
105 '&format=5';
ecfac808 106
107 return $this->fetch_request($request);
ecfac808 108 }
109
110 function get_videos_by_tag_and_category(){
111
3d535996 112 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
becf869c 113 $tag = optional_param('tag', '', PARAM_TAG); // tag
3d535996 114
115 if ($tag) {
0c9bba1c 116 $tagobject = tag_get('name', $tag);
8479c2e0 117 } else if ($tagid) {
0c9bba1c 118 $tagobject = tag_get('id', $tagid);
8479c2e0 119 }
120
0c9bba1c 121 if (empty($tagobject)) {
f1ec6a63 122 return '';
3d535996 123 }
ecfac808 124
0c9bba1c 125 $querytag = urlencode($tagobject->name);
ecfac808 126
127 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
128 if( !empty($this->config->numberofvideos)) {
129 $numberofvideos = $this->config->numberofvideos;
130 }
131
4f42e466 132 $request = 'http://gdata.youtube.com/feeds/api/videos?category=' .
26a8a8c8 133 $this->config->category .
4f42e466
EL
134 '&vq=' .
135 $querytag .
136 '&start-index=1&max-results=' .
137 $numberofvideos .
138 '&format=5';
139
ecfac808 140
d4c603db 141 return $this->fetch_request($request);
ecfac808 142 }
143
d4c603db 144 function fetch_request($request){
03f3a434 145 $c = new curl(array('cache' => true, 'module_cache'=>'tag_youtube'));
146 $c->setopt(array('CURLOPT_TIMEOUT' => 3, 'CURLOPT_CONNECTTIMEOUT' => 3));
d4c603db 147
becf869c 148 $response = $c->get($request);
ecfac808 149
03f3a434 150 $xml = new SimpleXMLElement($response);
151 return $this->render_video_list($xml);
ecfac808 152 }
d4c603db 153
03f3a434 154 function render_video_list(SimpleXMLElement $xml){
ecfac808 155
156 $text = '';
42131376 157 $text .= '<ul class="yt-video-entry unlist img-text">';
03f3a434 158
4f42e466
EL
159 foreach($xml->entry as $entry){
160 $media = $entry->children('http://search.yahoo.com/mrss/');
161 $playerattrs = $media->group->player->attributes();
162 $url = s($playerattrs['url']);
163 $thumbattrs = $media->group->thumbnail[0]->attributes();
164 $thumbnail = s($thumbattrs['url']);
165 $title = s($media->group->title);
166 $yt = $media->children('http://gdata.youtube.com/schemas/2007');
167 $secattrs = $yt->duration->attributes();
168 $seconds = $secattrs['seconds'];
169
03f3a434 170 $text .= '<li>';
171 $text .= '<div class="clearfix">';
4f42e466
EL
172 $text .= '<a href="'. $url . '">';
173 $text .= '<img alt="" class="youtube-thumb" src="'. $thumbnail .'" /></a>';
174 $text .= '</div><span><a href="'. $url . '">'. $title .'</a></span>';
03f3a434 175 $text .= '<div>';
4f42e466 176 $text .= format_time($seconds);
03f3a434 177 $text .= "</div></li>\n";
ecfac808 178 }
17a6161d 179 $text .= "</ul><div class=\"clearer\"></div>\n";
ecfac808 180 return $text;
181 }
4f42e466
EL
182
183 function get_categories() {
184 // TODO: Right now using sticky categories from
185 // http://gdata.youtube.com/schemas/2007/categories.cat
186 // This should be performed from time to time by the block insead
187 // and cached somewhere, avoiding deprecated ones and observing regions
188 return array (
189 '0' => get_string('anycategory', 'block_tag_youtube'),
190 'Film' => get_string('filmsanimation', 'block_tag_youtube'),
191 'Autos' => get_string('autosvehicles', 'block_tag_youtube'),
192 'Music' => get_string('music', 'block_tag_youtube'),
193 'Animals'=> get_string('petsanimals', 'block_tag_youtube'),
194 'Sports' => get_string('sports', 'block_tag_youtube'),
195 'Travel' => get_string('travel', 'block_tag_youtube'),
196 'Games' => get_string('gadgetsgames', 'block_tag_youtube'),
197 'Comedy' => get_string('comedy', 'block_tag_youtube'),
198 'People' => get_string('peopleblogs', 'block_tag_youtube'),
199 'News' => get_string('newspolitics', 'block_tag_youtube'),
200 'Entertainment' => get_string('entertainment', 'block_tag_youtube'),
201 'Education' => get_string('education', 'block_tag_youtube'),
202 'Howto' => get_string('howtodiy', 'block_tag_youtube'),
203 'Tech' => get_string('scienceandtech', 'block_tag_youtube')
204 );
205 }
206
207 /**
208 * Provide conversion from old numeric categories available in youtube API
209 * to the new ones available in the Google API
210 *
211 * @param int $oldcat old category code
212 * @return mixed new category code or 0 (if no match found)
213 *
214 * TODO: Someday this should be applied on upgrade for all the existing
215 * block instances so we won't need the mapping any more. That would imply
216 * to implement restore handling to perform the conversion of old blocks.
217 */
218 function category_map_old2new($oldcat) {
219 $oldoptions = array (
220 0 => '0',
221 1 => 'Film',
222 2 => 'Autos',
223 23 => 'Comedy',
224 24 => 'Entertainment',
225 10 => 'Music',
226 25 => 'News',
227 22 => 'People',
228 15 => 'Animals',
229 26 => 'Howto',
230 17 => 'Sports',
231 19 => 'Travel',
232 20 => 'Games'
233 );
234 if (array_key_exists($oldcat, $oldoptions)) {
235 return $oldoptions[$oldcat];
236 } else {
237 return $oldcat;
238 }
239 }
ecfac808 240}
4ca6cfbf 241