[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/blocks/tag_youtube/ -> block_tag_youtube.php (source)

   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  
   8  define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
   9  define('DEFAULT_NUMBER_OF_VIDEOS', 5);
  10  define('YOUTUBE_CACHE_EXPIRATION', 1800);
  11  
  12  class block_tag_youtube extends block_base {
  13  
  14      function init() {
  15          $this->title = get_string('blockname','block_tag_youtube');
  16          $this->version = 2007101509;
  17      }
  18  
  19      function applicable_formats() {
  20          return array('tag' => true);
  21      }
  22  
  23      function specialization() {
  24          $this->title = !empty($this->config->title) ? $this->config->title : get_string('blockname', 'block_tag_youtube');
  25      }
  26  
  27      function instance_allow_multiple() {
  28          return true;
  29      }
  30  
  31      function preferred_width() {
  32          return 140;
  33      } 
  34  
  35      function get_content() {
  36  
  37          if ($this->content !== NULL) {
  38              return $this->content;
  39          }
  40  
  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;
  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  
  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}";
  75  
  76          return $this->fetch_request($request);
  77      }
  78  
  79      function get_videos_by_tag(){
  80  
  81          $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
  82          $tag = optional_param('tag', '', PARAM_TAG); // tag 
  83  
  84          if ($tag) {
  85              $tagobject = tag_get('name', $tag);
  86          } else if ($tagid) {
  87              $tagobject = tag_get('id', $tagid);
  88          }
  89  
  90          if (empty($tagobject)) {
  91              print_error('tagnotfound');
  92          }
  93  
  94          $querytag = urlencode($tagobject->name);
  95  
  96          $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
  97          if ( !empty($this->config->numberofvideos) ) {
  98              $numberofvideos = $this->config->numberofvideos;
  99          }
 100  
 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}";
 106  
 107          return $this->fetch_request($request);
 108      }
 109  
 110      function get_videos_by_tag_and_category(){
 111  
 112          $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
 113          $tag = optional_param('tag', '', PARAM_TAG); // tag 
 114  
 115          if ($tag) {
 116              $tagobject = tag_get('name', $tag);
 117          } else if ($tagid) {
 118              $tagobject = tag_get('id', $tagid);
 119          }
 120  
 121          if (empty($tagobject)) {
 122              print_error('tagnotfound');
 123          }
 124  
 125          $querytag = urlencode($tagobject->name);
 126  
 127          $numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
 128          if( !empty($this->config->numberofvideos)) {
 129              $numberofvideos = $this->config->numberofvideos;
 130          }
 131  
 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}";
 138  
 139          return $this->fetch_request($request);
 140      }
 141  
 142      function fetch_request($request){
 143  
 144          global $CFG;
 145  
 146          make_upload_directory('/cache/youtube');
 147  
 148          $cache = new RSSCache($CFG->dataroot . '/cache/youtube',YOUTUBE_CACHE_EXPIRATION);
 149          $cache_status = $cache->check_cache( $request);
 150  
 151          if ( $cache_status == 'HIT' ) {
 152              $cached_response = $cache->get( $request );
 153  
 154              $xmlobj = XML_unserialize($cached_response);
 155              return $this->render_video_list($xmlobj);
 156          }
 157  
 158          if ( $cache_status == 'STALE' ) {
 159              $cached_response = $cache->get( $request );
 160          }
 161  
 162          $response = download_file_content($request);
 163  
 164          if(empty($response)){
 165              $response = $cached_response;
 166          }
 167          else{
 168              $cache->set($request, $response);
 169          }
 170  
 171          $xmlobj = XML_unserialize($response);
 172          return $this->render_video_list($xmlobj);
 173      }
 174  
 175      function render_video_list($xmlobj){
 176  
 177          $text = '';
 178          $text .= '<ul class="yt-video-entry unlist img-text">';
 179          $videos = $xmlobj['ut_response']['video_list']['video'];
 180  
 181          if (is_array($videos) ) {
 182              foreach($videos as $video){
 183                  $text .= '<li>';
 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>';
 188                  $text .= '<div>';
 189                  $text .= format_time($video['length_seconds']);
 190                  $text .= "</div></li>\n";
 191              }
 192          } else { 
 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');
 196          }
 197          $text .= "</ul><div class=\"clearer\"></div>\n";
 198          return $text;
 199      }
 200  }
 201  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7