Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/rss_client/block_rss_client.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026  class block_rss_client extends block_base {
00027 
00028     function init() {
00029         $this->title = get_string('feedstitle', 'block_rss_client');
00030     }
00031 
00032     function preferred_width() {
00033         return 210;
00034     }
00035 
00036     function applicable_formats() {
00037         return array('all' => true, 'tag' => false);   // Needs work to make it work on tags MDL-11960
00038     }
00039 
00040     function specialization() {
00041         // After the block has been loaded we customize the block's title display
00042         if (!empty($this->config) && !empty($this->config->title)) {
00043             // There is a customized block title, display it
00044             $this->title = $this->config->title;
00045         } else {
00046             // No customized block title, use localized remote news feed string
00047             $this->title = get_string('remotenewsfeed', 'block_rss_client');
00048         }
00049     }
00050 
00051     function get_content() {
00052         global $CFG, $DB;
00053 
00054         if ($this->content !== NULL) {
00055             return $this->content;
00056         }
00057 
00058         // initalise block content object
00059         $this->content = new stdClass;
00060         $this->content->text   = '';
00061         $this->content->footer = '';
00062 
00063         if (empty($this->instance)) {
00064             return $this->content;
00065         }
00066 
00067         if (!isset($this->config)) {
00068             // The block has yet to be configured - just display configure message in
00069             // the block if user has permission to configure it
00070 
00071             if (has_capability('block/rss_client:manageanyfeeds', $this->context)) {
00072                 $this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client');
00073             }
00074 
00075             return $this->content;
00076         }
00077 
00078         // How many feed items should we display?
00079         $maxentries = 5;
00080         if ( !empty($this->config->shownumentries) ) {
00081             $maxentries = intval($this->config->shownumentries);
00082         }elseif( isset($CFG->block_rss_client_num_entries) ) {
00083             $maxentries = intval($CFG->block_rss_client_num_entries);
00084         }
00085 
00086 
00087         /* ---------------------------------
00088          * Begin Normal Display of Block Content
00089          * --------------------------------- */
00090 
00091         $output = '';
00092 
00093 
00094         if (!empty($this->config->rssid)) {
00095             list($rss_ids_sql, $params) = $DB->get_in_or_equal($this->config->rssid);
00096 
00097             $rss_feeds = $DB->get_records_select('block_rss_client', "id $rss_ids_sql", $params);
00098 
00099             $showtitle = false;
00100             if (count($rss_feeds) > 1) {
00101                 // when many feeds show the title for each feed
00102                 $showtitle = true;
00103             }
00104 
00105             foreach($rss_feeds as $feed){
00106                 $output.= $this->get_feed_html($feed, $maxentries, $showtitle);
00107             }
00108         }
00109 
00110         $this->content->text = $output;
00111 
00112         return $this->content;
00113     }
00114 
00115 
00116     function instance_allow_multiple() {
00117         return true;
00118     }
00119 
00120     function has_config() {
00121         return true;
00122     }
00123 
00124     function instance_allow_config() {
00125         return true;
00126     }
00127 
00136     function get_feed_html($feedrecord, $maxentries, $showtitle){
00137         global $CFG;
00138         require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
00139 
00140         $feed = new moodle_simplepie($feedrecord->url);
00141 
00142         if(isset($CFG->block_rss_client_timeout)){
00143             $feed->set_cache_duration($CFG->block_rss_client_timeout*60);
00144         }
00145 
00146         if(debugging() && $feed->error()){
00147             return '<p>'. $feedrecord->url .' Failed with code: '.$feed->error().'</p>';
00148         }
00149 
00150         $r = ''; // return string
00151 
00152         if($this->config->block_rss_client_show_channel_image){
00153             if($image = $feed->get_image_url()){
00154                 $imagetitle = s($feed->get_image_title());
00155                 $imagelink  = $feed->get_image_link();
00156 
00157                 $r.='<div class="image" title="'.$imagetitle.'">'."\n";
00158                 if($imagelink){
00159                     $r.='<a href="'.$imagelink.'">';
00160                 }
00161                 $r.='<img src="'.$image.'" alt="'.$imagetitle.'" />'."\n";
00162                 if($imagelink){
00163                     $r.='</a>';
00164                 }
00165                 $r.= '</div>';
00166             }
00167         }
00168 
00169         if(empty($feedrecord->preferredtitle)){
00170             $feedtitle = $this->format_title($feed->get_title());
00171         }else{
00172             $feedtitle = $this->format_title($feedrecord->preferredtitle);
00173         }
00174 
00175         if($showtitle){
00176             $r.='<div class="title">'.$feedtitle.'</div>';
00177         }
00178 
00179 
00180         $r.='<ul class="list no-overflow">'."\n";
00181 
00182         $feeditems = $feed->get_items(0, $maxentries);
00183         foreach($feeditems as $item){
00184             $r.= $this->get_item_html($item);
00185         }
00186 
00187         $r.='</ul>';
00188 
00189 
00190         if ($this->config->block_rss_client_show_channel_link) {
00191 
00192             $channellink = $feed->get_link();
00193 
00194             if (!empty($channellink)){
00195                 //NOTE: this means the 'last feed' display wins the block title - but
00196                 //this is exiting behaviour..
00197                 $this->content->footer = '<a href="'.htmlspecialchars(clean_param($channellink,PARAM_URL)).'">'. get_string('clientchannellink', 'block_rss_client') .'</a>';
00198             }
00199         }
00200 
00201         if (empty($this->config->title)){
00202             //NOTE: this means the 'last feed' displayed wins the block title - but
00203             //this is exiting behaviour..
00204             $this->title = strip_tags($feedtitle);
00205         }
00206 
00207         return $r;
00208     }
00209 
00210 
00217     function get_item_html($item){
00218 
00219         $link        = $item->get_link();
00220         $title       = $item->get_title();
00221         $description = $item->get_description();
00222 
00223 
00224         if(empty($title)){
00225             // no title present, use portion of description
00226             $title = textlib::substr(strip_tags($description), 0, 20) . '...';
00227         }else{
00228             $title = break_up_long_words($title, 30);
00229         }
00230 
00231         if(empty($link)){
00232             $link = $item->get_id();
00233         } else {
00234             //URLs in our RSS cache will be escaped (correctly as theyre store in XML)
00235             //html_writer::link() will re-escape them. To prevent double escaping unescape here.
00236             //This can by done using htmlspecialchars_decode() but moodle_url also has that effect
00237             $link = new moodle_url($link);
00238         }
00239 
00240         $r = html_writer::start_tag('li');
00241             $r.= html_writer::start_tag('div',array('class'=>'link'));
00242                 $r.= html_writer::link(clean_param($link,PARAM_URL), s($title), array('onclick'=>'this.target="_blank"'));
00243             $r.= html_writer::end_tag('div');
00244 
00245             if($this->config->display_description && !empty($description)){
00246 
00247                 $description = break_up_long_words($description, 30);
00248 
00249                 $formatoptions = new stdClass();
00250                 $formatoptions->para = false;
00251 
00252                 $r.= html_writer::start_tag('div',array('class'=>'description'));
00253                     $r.= format_text($description, FORMAT_HTML, $formatoptions, $this->page->course->id);
00254                 $r.= html_writer::end_tag('div');
00255             }
00256         $r.= html_writer::end_tag('li');
00257 
00258         return $r;
00259     }
00260 
00268     function format_title($title,$max=64) {
00269 
00270         // Loading the textlib singleton instance. We are going to need it.
00271         $textlib = textlib_get_instance();
00272 
00273         if ($textlib->strlen($title) <= $max) {
00274             return s($title);
00275         } else {
00276             return s($textlib->substr($title,0,$max-3).'...');
00277         }
00278     }
00279 
00287     function cron() {
00288         global $CFG, $DB;
00289         require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
00290 
00291         // We are going to measure execution times
00292         $starttime =  microtime();
00293 
00294         // And we have one initial $status
00295         $status = true;
00296 
00297         // Fetch all site feeds.
00298         $rs = $DB->get_recordset('block_rss_client');
00299         $counter = 0;
00300         mtrace('');
00301         foreach ($rs as $rec) {
00302             mtrace('    ' . $rec->url . ' ', '');
00303             // Fetch the rss feed, using standard simplepie caching
00304             // so feeds will be renewed only if cache has expired
00305             @set_time_limit(60);
00306 
00307             $feed =  new moodle_simplepie();
00308             // set timeout for longer than normal to be agressive at
00309             // fetching feeds if possible..
00310             $feed->set_timeout(40);
00311             $feed->set_cache_duration(0);
00312             $feed->set_feed_url($rec->url);
00313             $feed->init();
00314 
00315             if ($feed->error()) {
00316                 mtrace ('error');
00317                 mtrace ('SimplePie failed with error:'.$feed->error());
00318                 $status = false;
00319             } else {
00320                 mtrace ('ok');
00321             }
00322             $counter ++;
00323         }
00324         $rs->close();
00325 
00326         // Show times
00327         mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
00328 
00329         // And return $status
00330         return $status;
00331     }
00332 }
00333 
00334 
 All Data Structures Namespaces Files Functions Variables Enumerations