[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blocks/rss_client/ -> block_rss_client.php (source)

   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   * A block which displays Remote feeds
  19   *
  20   * @package   block_rss_client
  21   * @copyright  Daryl Hawes
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
  23   */
  24  
  25   class block_rss_client extends block_base {
  26  
  27      function init() {
  28          $this->title = get_string('pluginname', 'block_rss_client');
  29      }
  30  
  31      function preferred_width() {
  32          return 210;
  33      }
  34  
  35      function applicable_formats() {
  36          return array('all' => true, 'tag' => false);   // Needs work to make it work on tags MDL-11960
  37      }
  38  
  39      function specialization() {
  40          // After the block has been loaded we customize the block's title display
  41          if (!empty($this->config) && !empty($this->config->title)) {
  42              // There is a customized block title, display it
  43              $this->title = $this->config->title;
  44          } else {
  45              // No customized block title, use localized remote news feed string
  46              $this->title = get_string('remotenewsfeed', 'block_rss_client');
  47          }
  48      }
  49  
  50      function get_content() {
  51          global $CFG, $DB;
  52  
  53          if ($this->content !== NULL) {
  54              return $this->content;
  55          }
  56  
  57          // initalise block content object
  58          $this->content = new stdClass;
  59          $this->content->text   = '';
  60          $this->content->footer = '';
  61  
  62          if (empty($this->instance)) {
  63              return $this->content;
  64          }
  65  
  66          if (!isset($this->config)) {
  67              // The block has yet to be configured - just display configure message in
  68              // the block if user has permission to configure it
  69  
  70              if (has_capability('block/rss_client:manageanyfeeds', $this->context)) {
  71                  $this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client');
  72              }
  73  
  74              return $this->content;
  75          }
  76  
  77          // How many feed items should we display?
  78          $maxentries = 5;
  79          if ( !empty($this->config->shownumentries) ) {
  80              $maxentries = intval($this->config->shownumentries);
  81          }elseif( isset($CFG->block_rss_client_num_entries) ) {
  82              $maxentries = intval($CFG->block_rss_client_num_entries);
  83          }
  84  
  85  
  86          /* ---------------------------------
  87           * Begin Normal Display of Block Content
  88           * --------------------------------- */
  89  
  90          $output = '';
  91  
  92  
  93          if (!empty($this->config->rssid)) {
  94              list($rss_ids_sql, $params) = $DB->get_in_or_equal($this->config->rssid);
  95  
  96              $rss_feeds = $DB->get_records_select('block_rss_client', "id $rss_ids_sql", $params);
  97  
  98              $showtitle = false;
  99              if (count($rss_feeds) > 1) {
 100                  // when many feeds show the title for each feed
 101                  $showtitle = true;
 102              }
 103  
 104              foreach($rss_feeds as $feed){
 105                  $output.= $this->get_feed_html($feed, $maxentries, $showtitle);
 106              }
 107          }
 108  
 109          $this->content->text = $output;
 110  
 111          return $this->content;
 112      }
 113  
 114  
 115      function instance_allow_multiple() {
 116          return true;
 117      }
 118  
 119      function has_config() {
 120          return true;
 121      }
 122  
 123      function instance_allow_config() {
 124          return true;
 125      }
 126  
 127      /**
 128       * Returns the html of a feed to be displaed in the block
 129       *
 130       * @param mixed feedrecord The feed record from the database
 131       * @param int maxentries The maximum number of entries to be displayed
 132       * @param boolean showtitle Should the feed title be displayed in html
 133       * @return string html representing the rss feed content
 134       */
 135      function get_feed_html($feedrecord, $maxentries, $showtitle){
 136          global $CFG;
 137          require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
 138  
 139          $feed = new moodle_simplepie($feedrecord->url);
 140  
 141          if(isset($CFG->block_rss_client_timeout)){
 142              $feed->set_cache_duration($CFG->block_rss_client_timeout*60);
 143          }
 144  
 145          if(debugging() && $feed->error()){
 146              return '<p>'. $feedrecord->url .' Failed with code: '.$feed->error().'</p>';
 147          }
 148  
 149          $r = ''; // return string
 150  
 151          if($this->config->block_rss_client_show_channel_image){
 152              if($image = $feed->get_image_url()){
 153                  $imagetitle = s($feed->get_image_title());
 154                  $imagelink  = $feed->get_image_link();
 155  
 156                  $r.='<div class="image" title="'.$imagetitle.'">'."\n";
 157                  if($imagelink){
 158                      $r.='<a href="'.$imagelink.'">';
 159                  }
 160                  $r.='<img src="'.$image.'" alt="'.$imagetitle.'" />'."\n";
 161                  if($imagelink){
 162                      $r.='</a>';
 163                  }
 164                  $r.= '</div>';
 165              }
 166          }
 167  
 168          if(empty($feedrecord->preferredtitle)){
 169              $feedtitle = $this->format_title($feed->get_title());
 170          }else{
 171              $feedtitle = $this->format_title($feedrecord->preferredtitle);
 172          }
 173  
 174          if($showtitle){
 175              $r.='<div class="title">'.$feedtitle.'</div>';
 176          }
 177  
 178  
 179          $r.='<ul class="list no-overflow">'."\n";
 180  
 181          $feeditems = $feed->get_items(0, $maxentries);
 182          foreach($feeditems as $item){
 183              $r.= $this->get_item_html($item);
 184          }
 185  
 186          $r.='</ul>';
 187  
 188  
 189          if ($this->config->block_rss_client_show_channel_link) {
 190  
 191              $channellink = $feed->get_link();
 192  
 193              if (!empty($channellink)){
 194                  //NOTE: this means the 'last feed' display wins the block title - but
 195                  //this is exiting behaviour..
 196                  $this->content->footer = '<a href="'.htmlspecialchars(clean_param($channellink,PARAM_URL)).'">'. get_string('clientchannellink', 'block_rss_client') .'</a>';
 197              }
 198          }
 199  
 200          if (empty($this->config->title)){
 201              //NOTE: this means the 'last feed' displayed wins the block title - but
 202              //this is exiting behaviour..
 203              $this->title = strip_tags($feedtitle);
 204          }
 205  
 206          return $r;
 207      }
 208  
 209  
 210      /**
 211       * Returns the html list item of a feed item
 212       *
 213       * @param mixed item simplepie_item representing the feed item
 214       * @return string html li representing the rss feed item
 215       */
 216      function get_item_html($item){
 217  
 218          $link        = $item->get_link();
 219          $title       = $item->get_title();
 220          $description = $item->get_description();
 221  
 222  
 223          if(empty($title)){
 224              // no title present, use portion of description
 225              $title = core_text::substr(strip_tags($description), 0, 20) . '...';
 226          }else{
 227              $title = break_up_long_words($title, 30);
 228          }
 229  
 230          if(empty($link)){
 231              $link = $item->get_id();
 232          } else {
 233              try {
 234                  // URLs in our RSS cache will be escaped (correctly as theyre store in XML)
 235                  // html_writer::link() will re-escape them. To prevent double escaping unescape here.
 236                  // This can by done using htmlspecialchars_decode() but moodle_url also has that effect.
 237                  $link = new moodle_url($link);
 238              } catch (moodle_exception $e) {
 239                  // Catching the exception to prevent the whole site to crash in case of malformed RSS feed
 240                  $link = '';
 241              }
 242          }
 243  
 244          $r = html_writer::start_tag('li');
 245              $r.= html_writer::start_tag('div',array('class'=>'link'));
 246                  $r.= html_writer::link($link, s($title), array('onclick'=>'this.target="_blank"'));
 247              $r.= html_writer::end_tag('div');
 248  
 249              if($this->config->display_description && !empty($description)){
 250  
 251                  $formatoptions = new stdClass();
 252                  $formatoptions->para = false;
 253  
 254                  $r.= html_writer::start_tag('div',array('class'=>'description'));
 255                      $description = format_text($description, FORMAT_HTML, $formatoptions, $this->page->course->id);
 256                      $description = break_up_long_words($description, 30);
 257                      $r.= $description;
 258                  $r.= html_writer::end_tag('div');
 259              }
 260          $r.= html_writer::end_tag('li');
 261  
 262          return $r;
 263      }
 264  
 265      /**
 266       * Strips a large title to size and adds ... if title too long
 267       *
 268       * @param string title to shorten
 269       * @param int max character length of title
 270       * @return string title s() quoted and shortened if necessary
 271       */
 272      function format_title($title,$max=64) {
 273  
 274          if (core_text::strlen($title) <= $max) {
 275              return s($title);
 276          } else {
 277              return s(core_text::substr($title,0,$max-3).'...');
 278          }
 279      }
 280  
 281      /**
 282       * cron - goes through all feeds and retrieves them with the cache
 283       * duration set to 0 in order to force the retrieval of the item and
 284       * refresh the cache
 285       *
 286       * @return boolean true if all feeds were retrieved succesfully
 287       */
 288      function cron() {
 289          global $CFG, $DB;
 290          require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
 291  
 292          // We are going to measure execution times
 293          $starttime =  microtime();
 294  
 295          // And we have one initial $status
 296          $status = true;
 297  
 298          // Fetch all site feeds.
 299          $rs = $DB->get_recordset('block_rss_client');
 300          $counter = 0;
 301          mtrace('');
 302          foreach ($rs as $rec) {
 303              mtrace('    ' . $rec->url . ' ', '');
 304              // Fetch the rss feed, using standard simplepie caching
 305              // so feeds will be renewed only if cache has expired
 306              core_php_time_limit::raise(60);
 307  
 308              $feed =  new moodle_simplepie();
 309              // set timeout for longer than normal to be agressive at
 310              // fetching feeds if possible..
 311              $feed->set_timeout(40);
 312              $feed->set_cache_duration(0);
 313              $feed->set_feed_url($rec->url);
 314              $feed->init();
 315  
 316              if ($feed->error()) {
 317                  mtrace ('error');
 318                  mtrace ('SimplePie failed with error:'.$feed->error());
 319                  $status = false;
 320              } else {
 321                  mtrace ('ok');
 322              }
 323              $counter ++;
 324          }
 325          $rs->close();
 326  
 327          // Show times
 328          mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
 329  
 330          // And return $status
 331          return $status;
 332      }
 333  }
 334  
 335  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1