Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/tag_flickr/block_tag_flickr.php
Go to the documentation of this file.
00001 <?php
00002 
00003 define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
00004 define('DEFAULT_NUMBER_OF_PHOTOS', 6);
00005 
00006 class block_tag_flickr extends block_base {
00007 
00008     function init() {
00009         $this->title = get_string('pluginname','block_tag_flickr');
00010     }
00011 
00012     function applicable_formats() {
00013         return array('tag' => true);
00014     }
00015 
00016     function specialization() {
00017         $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_flickr');
00018     }
00019 
00020     function instance_allow_multiple() {
00021         return true;
00022     }
00023 
00024     function preferred_width() {
00025         return 170;
00026     }
00027 
00028     function get_content() {
00029         global $CFG, $USER;
00030 
00031         //note: do NOT include files at the top of this file
00032         require_once($CFG->dirroot.'/tag/lib.php');
00033         require_once($CFG->libdir . '/filelib.php');
00034 
00035         if ($this->content !== NULL) {
00036             return $this->content;
00037         }
00038 
00039         $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
00040         $tag = optional_param('tag', '', PARAM_TAG); // tag
00041 
00042         if ($tag) {
00043             $tagobject = tag_get('name', $tag);
00044         } else if ($tagid) {
00045             $tagobject = tag_get('id', $tagid);
00046         }
00047 
00048         if (empty($tagobject)) {
00049             $this->content = new stdClass;
00050             $this->content->text = '';
00051             $this->content->footer = '';
00052             return $this->content;
00053         }
00054 
00055         //include related tags in the photo query ?
00056         $tagscsv = $tagobject->name;
00057         if (!empty($this->config->includerelatedtags)) {
00058             $tagscsv .= ',' . tag_get_related_tags_csv(tag_get_related_tags($tagobject->id), TAG_RETURN_TEXT);
00059         }
00060         $tagscsv = urlencode($tagscsv);
00061 
00062         //number of photos to display
00063         $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
00064         if( !empty($this->config->numberofphotos)) {
00065             $numberofphotos = $this->config->numberofphotos;
00066         }
00067 
00068         //sort search results by
00069         $sortby = 'relevance';
00070         if( !empty($this->config->sortby)) {
00071             $sortby = $this->config->sortby;
00072         }
00073 
00074         //pull photos from a specific photoset
00075         if(!empty($this->config->photoset)){
00076 
00077             $request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
00078             $request .= '&api_key='.FLICKR_DEV_KEY;
00079             $request .= '&photoset_id='.$this->config->photoset;
00080             $request .= '&per_page='.$numberofphotos;
00081             $request .= '&format=php_serial';
00082 
00083             $response = $this->fetch_request($request);
00084 
00085             $search = unserialize($response);
00086 
00087             foreach ($search['photoset']['photo'] as $p){
00088                 $p['owner'] = $search['photoset']['owner'];
00089             }
00090 
00091             $photos = array_values($search['photoset']['photo']);
00092 
00093         }
00094         //search for photos tagged with $tagscsv
00095         else{
00096 
00097             $request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
00098             $request .= '&api_key='.FLICKR_DEV_KEY;
00099             $request .= '&tags='.$tagscsv;
00100             $request .= '&per_page='.$numberofphotos;
00101             $request .= '&sort='.$sortby;
00102             $request .= '&format=php_serial';
00103 
00104             $response = $this->fetch_request($request);
00105 
00106             $search = unserialize($response);
00107             $photos = array_values($search['photos']['photo']);
00108         }
00109 
00110 
00111         if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
00112 
00113         //Accessibility: render the list of photos
00114         $text = '<ul class="inline-list">';
00115          foreach ($photos as $photo) {
00116             $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
00117             $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
00118          }
00119         $text .= "</ul>\n";
00120 
00121         $this->content = new stdClass;
00122         $this->content->text = $text;
00123         $this->content->footer = '';
00124 
00125         return $this->content;
00126     }
00127 
00128     function fetch_request($request){
00129         $c =  new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
00130 
00131         $response = $c->get($request);
00132 
00133         return $response;
00134     }
00135 
00136     function build_photo_url ($photo, $size='medium') {
00137         //receives an array (can use the individual photo data returned
00138         //from an API call) and returns a URL (doesn't mean that the
00139         //file size exists)
00140         $sizes = array(
00141             'square' => '_s',
00142             'thumbnail' => '_t',
00143             'small' => '_m',
00144             'medium' => '',
00145             'large' => '_b',
00146             'original' => '_o'
00147         );
00148 
00149         $size = strtolower($size);
00150         if (!array_key_exists($size, $sizes)) {
00151             $size = 'medium';
00152         }
00153 
00154         if ($size == 'original') {
00155             $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
00156         } else {
00157             $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
00158         }
00159         return $url;
00160     }
00161 }
00162 
00163 
 All Data Structures Namespaces Files Functions Variables Enumerations