|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 define('DEFAULT_NUMBER_OF_VIDEOS', 5); 00004 00005 class block_tag_youtube extends block_base { 00006 00007 function init() { 00008 $this->title = get_string('pluginname','block_tag_youtube'); 00009 } 00010 00011 function applicable_formats() { 00012 return array('tag' => true); 00013 } 00014 00015 function specialization() { 00016 $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_youtube'); 00017 // Convert numeric categories (old YouTube API) to 00018 // textual ones (new Google Data API) 00019 $this->config->category = !empty($this->config->category) ? $this->category_map_old2new($this->config->category) : '0'; 00020 } 00021 00022 function instance_allow_multiple() { 00023 return true; 00024 } 00025 00026 function preferred_width() { 00027 return 140; 00028 } 00029 00030 function get_content() { 00031 global $CFG; 00032 00033 //note: do NOT include files at the top of this file 00034 require_once($CFG->dirroot.'/tag/lib.php'); 00035 require_once($CFG->libdir . '/filelib.php'); 00036 00037 if ($this->content !== NULL) { 00038 return $this->content; 00039 } 00040 00041 $text = ''; 00042 if(!empty($this->config->playlist)){ 00043 //videos from a playlist 00044 $text = $this->get_videos_by_playlist(); 00045 } 00046 else{ 00047 if(!empty($this->config->category)){ 00048 //videos from category with tag 00049 $text = $this->get_videos_by_tag_and_category(); 00050 } 00051 else { 00052 //videos with tag 00053 $text = $this->get_videos_by_tag(); 00054 } 00055 } 00056 00057 $this->content = new stdClass; 00058 $this->content->text = $text; 00059 $this->content->footer = ''; 00060 00061 return $this->content; 00062 } 00063 00064 function get_videos_by_playlist(){ 00065 00066 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS; 00067 if( !empty($this->config->numberofvideos)) { 00068 $numberofvideos = $this->config->numberofvideos; 00069 } 00070 00071 $request = 'http://gdata.youtube.com/feeds/api/playlists/' . 00072 $this->config->playlist . 00073 '?start-index=1&max-results=' . 00074 $numberofvideos . 00075 '&format=5'; 00076 00077 return $this->fetch_request($request); 00078 } 00079 00080 function get_videos_by_tag(){ 00081 00082 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility 00083 $tag = optional_param('tag', '', PARAM_TAG); // tag 00084 00085 if ($tag) { 00086 $tagobject = tag_get('name', $tag); 00087 } else if ($tagid) { 00088 $tagobject = tag_get('id', $tagid); 00089 } 00090 00091 if (empty($tagobject)) { 00092 return ''; 00093 } 00094 00095 $querytag = urlencode($tagobject->name); 00096 00097 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS; 00098 if ( !empty($this->config->numberofvideos) ) { 00099 $numberofvideos = $this->config->numberofvideos; 00100 } 00101 00102 $request = 'http://gdata.youtube.com/feeds/api/videos?vq=' . 00103 $querytag . 00104 '&start-index=1&max-results=' . 00105 $numberofvideos . 00106 '&format=5'; 00107 00108 return $this->fetch_request($request); 00109 } 00110 00111 function get_videos_by_tag_and_category(){ 00112 00113 $tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility 00114 $tag = optional_param('tag', '', PARAM_TAG); // tag 00115 00116 if ($tag) { 00117 $tagobject = tag_get('name', $tag); 00118 } else if ($tagid) { 00119 $tagobject = tag_get('id', $tagid); 00120 } 00121 00122 if (empty($tagobject)) { 00123 return ''; 00124 } 00125 00126 $querytag = urlencode($tagobject->name); 00127 00128 $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS; 00129 if( !empty($this->config->numberofvideos)) { 00130 $numberofvideos = $this->config->numberofvideos; 00131 } 00132 00133 $request = 'http://gdata.youtube.com/feeds/api/videos?category=' . 00134 $this->config->category . 00135 '&vq=' . 00136 $querytag . 00137 '&start-index=1&max-results=' . 00138 $numberofvideos . 00139 '&format=5'; 00140 00141 00142 return $this->fetch_request($request); 00143 } 00144 00145 function fetch_request($request){ 00146 $c = new curl(array('cache' => true, 'module_cache'=>'tag_youtube')); 00147 $c->setopt(array('CURLOPT_TIMEOUT' => 3, 'CURLOPT_CONNECTTIMEOUT' => 3)); 00148 00149 $response = $c->get($request); 00150 00151 $xml = new SimpleXMLElement($response); 00152 return $this->render_video_list($xml); 00153 } 00154 00155 function render_video_list(SimpleXMLElement $xml){ 00156 00157 $text = ''; 00158 $text .= '<ul class="yt-video-entry unlist img-text">'; 00159 00160 foreach($xml->entry as $entry){ 00161 $media = $entry->children('http://search.yahoo.com/mrss/'); 00162 $playerattrs = $media->group->player->attributes(); 00163 $url = s($playerattrs['url']); 00164 $thumbattrs = $media->group->thumbnail[0]->attributes(); 00165 $thumbnail = s($thumbattrs['url']); 00166 $title = s($media->group->title); 00167 $yt = $media->children('http://gdata.youtube.com/schemas/2007'); 00168 $secattrs = $yt->duration->attributes(); 00169 $seconds = $secattrs['seconds']; 00170 00171 $text .= '<li>'; 00172 $text .= '<div class="clearfix">'; 00173 $text .= '<a href="'. $url . '">'; 00174 $text .= '<img alt="" class="youtube-thumb" src="'. $thumbnail .'" /></a>'; 00175 $text .= '</div><span><a href="'. $url . '">'. $title .'</a></span>'; 00176 $text .= '<div>'; 00177 $text .= format_time($seconds); 00178 $text .= "</div></li>\n"; 00179 } 00180 $text .= "</ul><div class=\"clearer\"></div>\n"; 00181 return $text; 00182 } 00183 00184 function get_categories() { 00185 // TODO: Right now using sticky categories from 00186 // http://gdata.youtube.com/schemas/2007/categories.cat 00187 // This should be performed from time to time by the block insead 00188 // and cached somewhere, avoiding deprecated ones and observing regions 00189 return array ( 00190 '0' => get_string('anycategory', 'block_tag_youtube'), 00191 'Film' => get_string('filmsanimation', 'block_tag_youtube'), 00192 'Autos' => get_string('autosvehicles', 'block_tag_youtube'), 00193 'Music' => get_string('music', 'block_tag_youtube'), 00194 'Animals'=> get_string('petsanimals', 'block_tag_youtube'), 00195 'Sports' => get_string('sports', 'block_tag_youtube'), 00196 'Travel' => get_string('travel', 'block_tag_youtube'), 00197 'Games' => get_string('gadgetsgames', 'block_tag_youtube'), 00198 'Comedy' => get_string('comedy', 'block_tag_youtube'), 00199 'People' => get_string('peopleblogs', 'block_tag_youtube'), 00200 'News' => get_string('newspolitics', 'block_tag_youtube'), 00201 'Entertainment' => get_string('entertainment', 'block_tag_youtube'), 00202 'Education' => get_string('education', 'block_tag_youtube'), 00203 'Howto' => get_string('howtodiy', 'block_tag_youtube'), 00204 'Tech' => get_string('scienceandtech', 'block_tag_youtube') 00205 ); 00206 } 00207 00219 function category_map_old2new($oldcat) { 00220 $oldoptions = array ( 00221 0 => '0', 00222 1 => 'Film', 00223 2 => 'Autos', 00224 23 => 'Comedy', 00225 24 => 'Entertainment', 00226 10 => 'Music', 00227 25 => 'News', 00228 22 => 'People', 00229 15 => 'Animals', 00230 26 => 'Howto', 00231 17 => 'Sports', 00232 19 => 'Travel', 00233 20 => 'Games' 00234 ); 00235 if (array_key_exists($oldcat, $oldoptions)) { 00236 return $oldoptions[$oldcat]; 00237 } else { 00238 return $oldcat; 00239 } 00240 } 00241 } 00242