From ecfac8087843a56890d36db90231992c2c3b3da4 Mon Sep 17 00:00:00 2001 From: moodler Date: Sun, 12 Aug 2007 16:53:24 +0000 Subject: [PATCH] Added new blocks for flickr and youtube to tags (thanks, Luiz!) MDL-10169 --- blocks/tag_flickr/block_tag_flickr.php | 172 ++++++++++++++++++++++ blocks/tag_flickr/config_instance.html | 58 ++++++++ blocks/tag_youtube/block_tag_youtube.php | 177 +++++++++++++++++++++++ blocks/tag_youtube/config_instance.html | 51 +++++++ lang/en_utf8/block_tag_flickr.php | 18 +++ lang/en_utf8/block_tag_youtube.php | 22 +++ lang/en_utf8/search.php | 57 ++++++++ 7 files changed, 555 insertions(+) create mode 100644 blocks/tag_flickr/block_tag_flickr.php create mode 100644 blocks/tag_flickr/config_instance.html create mode 100644 blocks/tag_youtube/block_tag_youtube.php create mode 100644 blocks/tag_youtube/config_instance.html create mode 100644 lang/en_utf8/block_tag_flickr.php create mode 100644 lang/en_utf8/block_tag_youtube.php create mode 100644 lang/en_utf8/search.php diff --git a/blocks/tag_flickr/block_tag_flickr.php b/blocks/tag_flickr/block_tag_flickr.php new file mode 100644 index 0000000000..981f68eaee --- /dev/null +++ b/blocks/tag_flickr/block_tag_flickr.php @@ -0,0 +1,172 @@ +dirroot.'/tag/lib.php'); +require_once($CFG->libdir . '/magpie/rss_cache.inc'); + +define('FLICKR_DEV_KEY', '4764a9d4fb1e1468f114158a7904901e'); +define('DEFAULT_NUMBER_OF_PHOTOS', 6); +define('FLICKR_CACHE_EXPIRATION', 1800); + +class block_tag_flickr extends block_base { + + function init() { + $this->title = get_string('defaulttile','block_tag_flickr'); + $this->version = 2007080800; + } + + function applicable_formats() { + return array('tag' => true); + } + + function specialization() { + $this->title = !empty($this->config->title) ? $this->config->title : get_string('blockname', 'block_tag_flickr'); + } + + function instance_allow_multiple() { + return true; + } + + function get_content() { + + global $CFG, $USER, $PAGE; + + if ($this->content !== NULL) { + return $this->content; + } + + $tagid = optional_param('id', 0, PARAM_INT); // tag id + + //include related tags in the photo query ? + $tags_csv = tag_display_name(tag_by_id($tagid)); + if (!empty($this->config->includerelatedtags)) { + $tags_csv .= ',' . tag_names_csv( get_item_tags('tag',$tagid)); + } + $tags_csv = urlencode($tags_csv); + + //number of photos to display + $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS; + if( !empty($this->config->numberofphotos)) { + $numberofphotos = $this->config->numberofphotos; + } + + //sort search results by + $sortby = 'relevance'; + if( !empty($this->config->sortby)) { + $sortby = $this->config->sortby; + } + + + //pull photos from a specific photoset + if(!empty($this->config->photoset)){ + + $request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos'; + $request .= '&api_key='.FLICKR_DEV_KEY; + $request .= '&photoset_id='.$this->config->photoset; + $request .= '&per_page='.$numberofphotos; + $request .= '&format=php_serial'; + + $response = $this->fetch_request($request); + + $search = unserialize($response); + + foreach ($search['photoset']['photo'] as &$p){ + $p['owner'] = $search['photoset']['owner']; + } + + $photos = array_values($search['photoset']['photo']); + + } + //search for photos tagged with $tags_csv + else{ + + $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search'; + $request .= '&api_key='.FLICKR_DEV_KEY; + $request .= '&tags='.$tags_csv; + $request .= '&per_page='.$numberofphotos; + $request .= '&sort='.$sortby; + $request .= '&format=php_serial'; + + $response = $this->fetch_request($request); + + $search = unserialize($response); + $photos = array_values($search['photos']['photo']); + + + } + + + if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit... + + //render the list of photos + $text = ''; + foreach ($photos as $photo) { + $text .= ""; + $text .= '' ; + } + + + $this->content = new stdClass; + $this->content->text = $text; + $this->content->footer = ''; + + return $this->content; + + } + + function fetch_request($request){ + + global $CFG; + + $cache = new RSSCache($CFG->dataroot . '/cache', FLICKR_CACHE_EXPIRATION); + $cache_status = $cache->check_cache( $request); + + if ( $cache_status == 'HIT' ) { + $cached_response = $cache->get( $request ); + + return $cached_response; + } + + if ( $cache_status == 'STALE' ) { + $cached_response = $cache->get( $request ); + } + + $response = file_get_contents($request); + + if(empty($response)){ + $response = $cached_response; + } + else{ + $cache->set($request, $response); + } + + return $response; + } + + function build_photo_url ($photo, $size = "Medium") + { + //receives an array (can use the individual photo data returned + //from an API call) and returns a URL (doesn't mean that the + //file size exists) + $sizes = array( + "square" => "_s", + "thumbnail" => "_t", + "small" => "_m", + "medium" => "", + "large" => "_b", + "original" => "_o" + ); + + $size = strtolower($size); + if (!array_key_exists($size, $sizes)) { + $size = "medium"; + } + + if ($size == "original") { + $url = "http://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . "/" . $photo['id'] . "_" . $photo['originalsecret'] . "_o" . "." . $photo['originalformat']; + } else { + $url = "http://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . "/" . $photo['id'] . "_" . $photo['secret'] . $sizes[$size] . ".jpg"; + } + return $url; + } +} +?> diff --git a/blocks/tag_flickr/config_instance.html b/blocks/tag_flickr/config_instance.html new file mode 100644 index 0000000000..2e9fde89c2 --- /dev/null +++ b/blocks/tag_flickr/config_instance.html @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
:
: + +
: + config) && isset($this->config->includerelatedtags)) { + $selected = $this->config->includerelatedtags; + } else { + $selected = '0'; + } + $options = array ( '0' => get_string('no'), + '1' => get_string('yes') ); + + choose_from_menu ($options, 'includerelatedtags', $selected); + ?> +
: + config) && isset($this->config->sortby)) { + $selected = $this->config->sortby; + } else { + $selected = 'relevance'; + } + $options = array ( 'date-posted-asc' => get_string('date-posted-asc', 'block_tag_flickr'), + 'date-posted-desc' => get_string('date-posted-desc', 'block_tag_flickr'), + 'date-taken-asc' => get_string('date-taken-asc', 'block_tag_flickr'), + 'date-taken-desc' => get_string('date-taken-desc', 'block_tag_flickr'), + 'interestingness-asc' => get_string('interestingness-asc', 'block_tag_flickr'), + 'interestingness-desc' => get_string('interestingness-desc', 'block_tag_flickr'), + 'relevance' => get_string('relevance', 'block_tag_flickr'), + ); + + choose_from_menu ($options, 'sortby', $selected); + ?> +
:
+
diff --git a/blocks/tag_youtube/block_tag_youtube.php b/blocks/tag_youtube/block_tag_youtube.php new file mode 100644 index 0000000000..5061f30796 --- /dev/null +++ b/blocks/tag_youtube/block_tag_youtube.php @@ -0,0 +1,177 @@ +dirroot.'/tag/lib.php'); +require_once($CFG->libdir . '/magpie/rss_cache.inc'); + +define('YOUTUBE_DEV_KEY', 'PGm8FdJXS8Q'); +define('DEFAULT_NUMBER_OF_VIDEOS', 5); +define('YOUTUBE_CACHE_EXPIRATION', 1800); + +class block_tag_youtube extends block_base { + + function init() { + $this->title = get_string('blockname','block_tag_youtube'); + $this->version = 2007080800; + } + + 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; + } + + function get_content() { + + + if ($this->content !== NULL) { + return $this->content; + } + + + 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(){ + + $tagid = optional_param('id', 0, PARAM_INT); // tag id + + $query_tag = tag_display_name(tag_by_id($tagid)); + $query_tag = urlencode($query_tag); + + $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_tag'; + $request .= '&dev_id=' . YOUTUBE_DEV_KEY; + $request .= "&tag={$query_tag}"; + $request .= "&page=1"; + $request .= "&per_page={$numberofvideos}"; + + return $this->fetch_request($request); + + } + + function get_videos_by_tag_and_category(){ + + $tagid = optional_param('id', 0, PARAM_INT); // tag id + + $query_tag = tag_display_name(tag_by_id($tagid)); + $query_tag = urlencode($query_tag); + + $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; + $request .= "&tag={$query_tag}"; + $request .= "&page=1"; + $request .= "&per_page={$numberofvideos}"; + + return $this->fetch_request($request); + } + + function fetch_request($request){ + + global $CFG; + + $cache = new RSSCache($CFG->dataroot . '/cache',YOUTUBE_CACHE_EXPIRATION); + $cache_status = $cache->check_cache( $request); + + if ( $cache_status == 'HIT' ) { + $cached_response = $cache->get( $request ); + + $simplexmlobj = new SimpleXMLElement($cached_response); + return $this->render_video_list($simplexmlobj); + } + + if ( $cache_status == 'STALE' ) { + $cached_response = $cache->get( $request ); + } + + $response = file_get_contents($request); + + if(empty($response)){ + $response = $cached_response; + } + else{ + $cache->set($request, $response); + } + + $simplexmlobj = new SimpleXMLElement($response); + return $this->render_video_list($simplexmlobj); + } + + function render_video_list($simplexmlobj){ + + $text = ''; + $text .= ''; + foreach($simplexmlobj->video_list->video as $video){ + $text .= ''; + $text .= '"; + $text .= ''; + $text .= ''; + } + $text .= '
'; + $text .= ''; + $text .= '' ; + $text .= ""; + $text .= "'; + $text .= ''.$video->title. ''; + $text .= '
'; + $text .= format_time($video->length_seconds); + $text .= '
'; + $text .= 'views: ' . $video->view_count ; + $text .= '
'; + + return $text; + } +} +?> diff --git a/blocks/tag_youtube/config_instance.html b/blocks/tag_youtube/config_instance.html new file mode 100644 index 0000000000..174425c8df --- /dev/null +++ b/blocks/tag_youtube/config_instance.html @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + +
:
: + +
: + config) && isset($this->config->category)) { + $selected = $this->config->category; + } else { + $selected = 0; + } + /* http://youtube.com/dev_api_ref?m=youtube.videos.list_by_category_and_tag */ + $options = array ( 0 => get_string('anycategory', 'block_tag_youtube'), + 1 => get_string('filmsanimation', 'block_tag_youtube'), + 2 => get_string('autosvehicles', 'block_tag_youtube'), + 23 => get_string('comedy', 'block_tag_youtube'), + 24 => get_string('entertainment', 'block_tag_youtube'), + 10 => get_string('music', 'block_tag_youtube'), + 25 => get_string('newspolitics', 'block_tag_youtube'), + 22 => get_string('peopleblogs', 'block_tag_youtube'), + 15 => get_string('petsanimals', 'block_tag_youtube'), + 26 => get_string('howtodiy', 'block_tag_youtube'), + 17 => get_string('sports', 'block_tag_youtube'), + 19 => get_string('travel', 'block_tag_youtube'), + 20 => get_string('gadgetsgames', 'block_tag_youtube'), + ); + + choose_from_menu ($options, 'category', $selected); + ?> +
+ +
+
diff --git a/lang/en_utf8/block_tag_flickr.php b/lang/en_utf8/block_tag_flickr.php new file mode 100644 index 0000000000..af5eb8c0ff --- /dev/null +++ b/lang/en_utf8/block_tag_flickr.php @@ -0,0 +1,18 @@ + diff --git a/lang/en_utf8/block_tag_youtube.php b/lang/en_utf8/block_tag_youtube.php new file mode 100644 index 0000000000..afc3a27a85 --- /dev/null +++ b/lang/en_utf8/block_tag_youtube.php @@ -0,0 +1,22 @@ + diff --git a/lang/en_utf8/search.php b/lang/en_utf8/search.php new file mode 100644 index 0000000000..2a08a33dc1 --- /dev/null +++ b/lang/en_utf8/search.php @@ -0,0 +1,57 @@ + -- 2.39.5