Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Flickr tag block.
  19   *
  20   * @package    block_tag_flickr
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('FLICKR_DEV_KEY', '4fddbdd7ff2376beec54d7f6afad425e');
  26  define('DEFAULT_NUMBER_OF_PHOTOS', 6);
  27  
  28  class block_tag_flickr extends block_base {
  29  
  30      function init() {
  31          $this->title = get_string('pluginname','block_tag_flickr');
  32      }
  33  
  34      function applicable_formats() {
  35          return array('tag' => true);
  36      }
  37  
  38      function specialization() {
  39          $this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_flickr');
  40      }
  41  
  42      function instance_allow_multiple() {
  43          return true;
  44      }
  45  
  46      function get_content() {
  47          global $CFG, $USER;
  48  
  49          //note: do NOT include files at the top of this file
  50          require_once($CFG->libdir . '/filelib.php');
  51  
  52          if ($this->content !== NULL) {
  53              return $this->content;
  54          }
  55  
  56          $tagid = optional_param('id', 0, PARAM_INT);   // tag id - for backware compatibility
  57          $tag = optional_param('tag', '', PARAM_TAG); // tag
  58          $tc = optional_param('tc', 0, PARAM_INT); // Tag collection id.
  59  
  60          if ($tagid) {
  61              $tagobject = core_tag_tag::get($tagid);
  62          } else if ($tag) {
  63              $tagobject = core_tag_tag::get_by_name($tc, $tag);
  64          }
  65  
  66          if (empty($tagobject)) {
  67              $this->content = new stdClass;
  68              $this->content->text = '';
  69              $this->content->footer = '';
  70              return $this->content;
  71          }
  72  
  73          //include related tags in the photo query ?
  74          $tagscsv = $tagobject->name;
  75          if (!empty($this->config->includerelatedtags)) {
  76              foreach ($tagobject->get_related_tags() as $t) {
  77                  $tagscsv .= ',' . $t->get_display_name(false);
  78              }
  79          }
  80          $tagscsv = urlencode($tagscsv);
  81  
  82          //number of photos to display
  83          $numberofphotos = DEFAULT_NUMBER_OF_PHOTOS;
  84          if( !empty($this->config->numberofphotos)) {
  85              $numberofphotos = $this->config->numberofphotos;
  86          }
  87  
  88          //sort search results by
  89          $sortby = 'relevance';
  90          if( !empty($this->config->sortby)) {
  91              $sortby = $this->config->sortby;
  92          }
  93  
  94          //pull photos from a specific photoset
  95          if(!empty($this->config->photoset)){
  96  
  97              $request = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos';
  98              $request .= '&api_key='.FLICKR_DEV_KEY;
  99              $request .= '&photoset_id='.$this->config->photoset;
 100              $request .= '&per_page='.$numberofphotos;
 101              $request .= '&format=php_serial';
 102  
 103              $response = $this->fetch_request($request);
 104  
 105              $search = unserialize($response);
 106  
 107              foreach ($search['photoset']['photo'] as $p){
 108                  $p['owner'] = $search['photoset']['owner'];
 109              }
 110  
 111              $photos = array_values($search['photoset']['photo']);
 112  
 113          }
 114          //search for photos tagged with $tagscsv
 115          else{
 116  
 117              $request = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
 118              $request .= '&api_key='.FLICKR_DEV_KEY;
 119              $request .= '&tags='.$tagscsv;
 120              $request .= '&per_page='.$numberofphotos;
 121              $request .= '&sort='.$sortby;
 122              $request .= '&format=php_serial';
 123  
 124              $response = $this->fetch_request($request);
 125  
 126              $search = unserialize($response);
 127              $photos = array_values($search['photos']['photo']);
 128          }
 129  
 130  
 131          if(strcmp($search['stat'], 'ok') != 0) return; //if no results were returned, exit...
 132  
 133          //Accessibility: render the list of photos
 134          $text = '<ul class="inline-list">';
 135           foreach ($photos as $photo) {
 136              $text .= '<li><a href="http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'] . '/" title="'.s($photo['title']).'">';
 137              $text .= '<img alt="'.s($photo['title']).'" class="flickr-photos" src="'. $this->build_photo_url($photo, 'square') ."\" /></a></li>\n";
 138           }
 139          $text .= "</ul>\n";
 140  
 141          $this->content = new stdClass;
 142          $this->content->text = $text;
 143          $this->content->footer = '';
 144  
 145          return $this->content;
 146      }
 147  
 148      function fetch_request($request){
 149          $c =  new curl(array('cache' => true, 'module_cache'=> 'tag_flickr'));
 150  
 151          $response = $c->get($request);
 152  
 153          return $response;
 154      }
 155  
 156      function build_photo_url ($photo, $size='medium') {
 157          //receives an array (can use the individual photo data returned
 158          //from an API call) and returns a URL (doesn't mean that the
 159          //file size exists)
 160          $sizes = array(
 161              'square' => '_s',
 162              'thumbnail' => '_t',
 163              'small' => '_m',
 164              'medium' => '',
 165              'large' => '_b',
 166              'original' => '_o'
 167          );
 168  
 169          $size = strtolower($size);
 170          if (!array_key_exists($size, $sizes)) {
 171              $size = 'medium';
 172          }
 173  
 174          if ($size == 'original') {
 175              $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['originalsecret'] . '_o' . '.' . $photo['originalformat'];
 176          } else {
 177              $url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . $sizes[$size] . '.jpg';
 178          }
 179          return $url;
 180      }
 181  
 182      /**
 183       * Return the plugin config settings for external functions.
 184       *
 185       * @return stdClass the configs for both the block instance and plugin
 186       * @since Moodle 3.8
 187       */
 188      public function get_config_for_external() {
 189          // Return all settings for all users since it is safe (no private keys, etc..).
 190          $configs = !empty($this->config) ? $this->config : new stdClass();
 191  
 192          return (object) [
 193              'instance' => $configs,
 194              'plugin' => new stdClass(),
 195          ];
 196      }
 197  }
 198  
 199