Commit ecfac808 authored by moodler's avatar moodler
Browse files

Added new blocks for flickr and youtube to tags (thanks, Luiz!) MDL-10169

parent e3ff328b
<?php
require_once($CFG->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 .= "<a href='http://www.flickr.com/photos/" . $photo['owner'] . "/" . $photo['id'] . "/'>";
$text .= '<img title="'.$photo['title'].'" class="flickr-photos" src="' . $this->build_photo_url($photo, 'square') . '"/>' ;
}
$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;
}
}
?>
<table cellpadding="9" cellspacing="0">
<tr valign="top">
<td align="right"><?php print_string('configtitle', 'block_tag_flickr'); ?>:</td>
<td><input type="text" name="title" size="30" value="<?php echo isset($this->config->title)?p($this->config->title):''; ?>" /></td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('numberofphotos', 'block_tag_flickr'); ?>:</td>
<td>
<input type="text" name="numberofphotos" size="5" value="<?php echo isset($this->config->numberofphotos)?p($this->config->numberofphotos):''; ?>" />
</td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('includerelatedtags', 'block_tag_flickr'); ?>:</td>
<td>
<?php
if (isset($this->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);
?>
</td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('sortby', 'block_tag_flickr'); ?>:</td>
<td>
<?php
if (isset($this->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);
?>
</td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('getfromphotoset', 'block_tag_flickr'); ?>:</td>
<td><input type="text" name="photoset" size="20" value="<?php echo isset($this->config->photoset)?p($this->config->photoset):''; ?>" /></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="<?php print_string('savechanges') ?>" /></td>
</tr>
</table>
<?php
require_once($CFG->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 .= '<table class="yt-video-entry">';
foreach($simplexmlobj->video_list->video as $video){
$text .= '<tr>';
$text .= '<td>';
$text .= '<a href="'. $video->url . '">';
$text .= '<img class="youtube-thumb" title="'.$video->title.'" style="padding:3px;" src="' . $video->thumbnail_url . '"/>' ;
$text .= "</a>";
$text .= "</td>";
$text .= '<td>';
$text .= '<a href="'. $video->url . '">'.$video->title. '</a>';
$text .= '<br/>';
$text .= format_time($video->length_seconds);
$text .= '<br/>';
$text .= 'views: ' . $video->view_count ;
$text .= '</td>';
$text .= '</tr>';
}
$text .= '</table>';
return $text;
}
}
?>
<table cellpadding="9" cellspacing="0">
<tr valign="top">
<td align="right"><?php print_string('configtitle', 'block_tag_youtube'); ?>:</td>
<td><input type="text" name="title" size="30" value="<?php echo isset($this->config->title)?p($this->config->title):''; ?>" /></td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('numberofvideos', 'block_tag_youtube'); ?>:</td>
<td>
<input type="text" name="numberofvideos" size="5" value="<?php echo isset($this->config->numberofvideos)?p($this->config->numberofvideos):''; ?>" />
</td>
</tr>
<tr valign="top">
<td align="right"><?php print_string('category', 'block_tag_youtube'); ?>:</td>
<td>
<?php
if (isset($this->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);
?>
</td>
</tr>
<tr valign="top">
<td align="right">
<?php print_string('includeonlyvideosfromplaylist', 'block_tag_youtube'); ?>
</td>
<td><input type="text" name="playlist" size="20" value="<?php echo isset($this->config->playlist)?p($this->config->playlist):''; ?>" /></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="<?php print_string('savechanges') ?>" /></td>
</tr>
</table>
<?php
$string['blockname'] = 'Flickr';
$string['configtitle'] = 'Title';
$string['getfromphotoset'] = 'Get photos from photoset with id';
$string['date-posted-asc'] = 'Date Posted ASC';
$string['date-posted-desc'] = 'Date Posted DESC';
$string['date-taken-asc'] = 'Date Taken ASC';
$string['date-taken-desc'] = 'Date Taken DESC';
$string['defaulttile'] = 'Flickr';
$string['includerelatedtags'] = 'Include related tags in query';
$string['interestingness-asc'] = 'Interestingness ASC';
$string['interestingness-desc'] = 'Interestingness DESC';
$string['numberofphotos'] = 'Number of photos';
$string['relevance'] = 'Relevance';
$string['sortby'] = 'Sort by';
?>
<?php
$string['blockname'] = 'Youtube';
$string['configtitle'] = 'Title';
$string['numberofvideos'] = 'Number of videos';
$string['category'] = 'Category';
$string['anycategory'] = 'Any Category';
$string['includeonlyvideosfromplaylist'] = 'Include only videos from the playlist with id';
$string['filmsanimation'] = 'Films & Animation';
$string['autosvehicles'] = 'Autos & Vehicles';
$string['comedy'] = 'Comedy';
$string['entertainment'] = 'Entertainment';
$string['music'] = 'Music';
$string['newspolitics'] = 'News & Politics';
$string['peopleblogs'] = 'People & Blogs';
$string['petsanimals'] = 'Pets & Animals';
$string['howtodiy'] = 'How-to & DIY';
$string['sports'] = 'Sports';
$string['travel'] = 'Travel & Places';
$string['gadgetsgames'] = 'Gadgets & Games';
?>
<?php // $Id$
$string['advancedsearch'] = 'Advanced search';
$string['all'] = 'All';
$string['author'] = 'Author';
$string['authorname'] = 'Author name';
$string['back'] = 'Back';
$string['beadmin'] = 'You need to be an admin user to use this page.';
$string['checkdb'] = 'Check Database';
$string['checkdbadvice'] = 'Check your database for any problems.';
$string['checkdir'] = 'Check dir';
$string['checkdiradvice'] = 'Ensure the data directory exists and is writable.';
$string['commenton'] = 'Comment on ';
$string['createanindex'] = 'create an index';
$string['createdon'] = 'Created on';
$string['database'] = 'Database';
$string['databasestate'] = 'Indexing database state';
$string['datadirectory'] = 'Data directory';
$string['deletionsinindex'] = 'Deletions in index';
$string['doctype'] = 'Doctype';
$string['documents'] = 'documents';
$string['documentsfor'] = 'Documents for ';
$string['documentsindatabase'] = 'Documents in database';
$string['documentsinindex'] = 'Documents in index';
$string['duration'] = 'Duration';
$string['emptydatabaseerror'] = 'Database table is not present, or contains no index records.';
$string['enteryoursearchquery'] = 'Enter your search query';
$string['errors'] = 'Errors ';
$string['filesinindexdirectory'] = 'Files in index directory';
$string['globalsearchdisabled'] = 'Global searching is not enabled.';
$string['invalidindexerror'] = 'Index directory either contains an invalid index, or nothing at all.';
$string['ittook'] = 'It took';
$string['next'] = 'Next';
$string['noindexmessage'] = 'Admin: There appears to be no search index. Please';
$string['normalsearch'] = 'Normal search';
$string['openedon'] = 'opened on';
$string['resultsreturnedfor'] = ' results returned for ';
$string['runindexer'] = 'Run indexer (real)';
$string['runindexertest'] = 'Run indexer test';
$string['score'] = 'Score';
$string['search'] = 'Search';
$string['searching'] = 'Searching in ...';
$string['seconds'] = ' seconds ';
$string['solutions'] = 'Solutions';
$string['statistics'] = 'Statistics';
$string['thesewordshelpimproverank'] = 'These words help improve rank';
$string['thesewordsmustappear'] = 'These words must appear';
$string['thesewordsmustnotappear'] = 'These words must not appear';
$string['title'] = 'Title';
$string['tofetchtheseresults'] = 'to fetch these results';
$string['totalsize'] = 'Total Size ';
$string['type'] = 'Type';
$string['uncompleteindexingerror'] = 'Indexing was not successfully completed, please restart it.';
$string['versiontoolow'] = 'Sorry, global search requires PHP 5.0.0 or later (currently using version '.$phpversion.')';
$string['whichmodulestosearch?'] = 'Which modules to search?';
$string['wordsintitle'] = 'Words in title';
?>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment