block_rss_client.php 10.7 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16

dhawes's avatar
dhawes committed
17
/**
18
19
 * A block which displays Remote feeds
 *
20
 * @package   block_rss_client
21
22
 * @copyright  Daryl Hawes
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
dhawes's avatar
dhawes committed
23
 */
24

dhawes's avatar
dhawes committed
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 applicable_formats() {
32
        return array('all' => true, 'tag' => false);   // Needs work to make it work on tags MDL-11960
33
34
    }

35
    function specialization() {
36
        // After the block has been loaded we customize the block's title display
37
        if (!empty($this->config) && !empty($this->config->title)) {
38
            // There is a customized block title, display it
39
            $this->title = $this->config->title;
40
41
        } else {
            // No customized block title, use localized remote news feed string
42
            $this->title = get_string('remotenewsfeed', 'block_rss_client');
43
44
        }
    }
dhawes's avatar
dhawes committed
45

46
    function get_content() {
47
        global $CFG, $DB;
moodler's avatar
moodler committed
48

49
        if ($this->content !== NULL) {
50
51
52
            return $this->content;
        }

53
        // initalise block content object
54
        $this->content = new stdClass;
defacer's avatar
   
defacer committed
55
56
57
        $this->content->text   = '';
        $this->content->footer = '';

moodler's avatar
moodler committed
58
        if (empty($this->instance)) {
59
60
61
            return $this->content;
        }

62
63
64
65
66
67
68
        if (!isset($this->config)) {
            // The block has yet to be configured - just display configure message in
            // the block if user has permission to configure it

            if (has_capability('block/rss_client:manageanyfeeds', $this->context)) {
                $this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client');
            }
69

70
            return $this->content;
71
        }
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        // How many feed items should we display?
        $maxentries = 5;
        if ( !empty($this->config->shownumentries) ) {
            $maxentries = intval($this->config->shownumentries);
        }elseif( isset($CFG->block_rss_client_num_entries) ) {
            $maxentries = intval($CFG->block_rss_client_num_entries);
        }


        /* ---------------------------------
         * Begin Normal Display of Block Content
         * --------------------------------- */

        $output = '';


89
        if (!empty($this->config->rssid)) {
90
91
92
93
94
95
96
97
            list($rss_ids_sql, $params) = $DB->get_in_or_equal($this->config->rssid);

            $rss_feeds = $DB->get_records_select('block_rss_client', "id $rss_ids_sql", $params);

            $showtitle = false;
            if (count($rss_feeds) > 1) {
                // when many feeds show the title for each feed
                $showtitle = true;
98
            }
99

100
101
            foreach($rss_feeds as $feed){
                $output.= $this->get_feed_html($feed, $maxentries, $showtitle);
dhawes's avatar
dhawes committed
102
            }
103
        }
dhawes's avatar
dhawes committed
104

105
        $this->content->text = $output;
106

107
108
        return $this->content;
    }
dhawes's avatar
dhawes committed
109

110

111
112
113
114
115
116
117
118
119
120
121
    function instance_allow_multiple() {
        return true;
    }

    function has_config() {
        return true;
    }

    function instance_allow_config() {
        return true;
    }
dhawes's avatar
dhawes committed
122

123
    /**
124
125
126
127
128
129
     * Returns the html of a feed to be displaed in the block
     *
     * @param mixed feedrecord The feed record from the database
     * @param int maxentries The maximum number of entries to be displayed
     * @param boolean showtitle Should the feed title be displayed in html
     * @return string html representing the rss feed content
130
     */
131
132
133
    function get_feed_html($feedrecord, $maxentries, $showtitle){
        global $CFG;
        require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
134

135
        $feed = new moodle_simplepie($feedrecord->url);
moodler's avatar
moodler committed
136

137
138
139
        if(isset($CFG->block_rss_client_timeout)){
            $feed->set_cache_duration($CFG->block_rss_client_timeout*60);
        }
140

141
142
143
        if(debugging() && $feed->error()){
            return '<p>'. $feedrecord->url .' Failed with code: '.$feed->error().'</p>';
        }
144

145
        $r = ''; // return string
146

147
148
149
150
        if($this->config->block_rss_client_show_channel_image){
            if($image = $feed->get_image_url()){
                $imagetitle = s($feed->get_image_title());
                $imagelink  = $feed->get_image_link();
151

152
153
154
155
156
157
158
159
160
                $r.='<div class="image" title="'.$imagetitle.'">'."\n";
                if($imagelink){
                    $r.='<a href="'.$imagelink.'">';
                }
                $r.='<img src="'.$image.'" alt="'.$imagetitle.'" />'."\n";
                if($imagelink){
                    $r.='</a>';
                }
                $r.= '</div>';
161
            }
162
        }
163

164
165
166
167
168
        if(empty($feedrecord->preferredtitle)){
            $feedtitle = $this->format_title($feed->get_title());
        }else{
            $feedtitle = $this->format_title($feedrecord->preferredtitle);
        }
169

170
171
172
        if($showtitle){
            $r.='<div class="title">'.$feedtitle.'</div>';
        }
dhawes's avatar
dhawes committed
173

dhawes's avatar
dhawes committed
174

175
        $r.='<ul class="list no-overflow">'."\n";
176

177
178
179
180
        $feeditems = $feed->get_items(0, $maxentries);
        foreach($feeditems as $item){
            $r.= $this->get_item_html($item);
        }
dhawes's avatar
dhawes committed
181

182
        $r.='</ul>';
183

184

185
        if ($this->config->block_rss_client_show_channel_link) {
186

187
            $channellink = $feed->get_link();
188
189
190
191

            if (!empty($channellink)){
                //NOTE: this means the 'last feed' display wins the block title - but
                //this is exiting behaviour..
192
                $this->content->footer = '<a href="'.htmlspecialchars(clean_param($channellink,PARAM_URL)).'">'. get_string('clientchannellink', 'block_rss_client') .'</a>';
193
            }
194
        }
195

196
197
198
199
200
        if (empty($this->config->title)){
            //NOTE: this means the 'last feed' displayed wins the block title - but
            //this is exiting behaviour..
            $this->title = strip_tags($feedtitle);
        }
201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        return $r;
    }


    /**
     * Returns the html list item of a feed item
     *
     * @param mixed item simplepie_item representing the feed item
     * @return string html li representing the rss feed item
     */
    function get_item_html($item){

        $link        = $item->get_link();
        $title       = $item->get_title();
        $description = $item->get_description();


        if(empty($title)){
            // no title present, use portion of description
221
            $title = core_text::substr(strip_tags($description), 0, 20) . '...';
222
223
        }else{
            $title = break_up_long_words($title, 30);
224
225
        }

226
227
        if(empty($link)){
            $link = $item->get_id();
228
        } else {
229
230
231
232
233
234
235
236
237
            try {
                // URLs in our RSS cache will be escaped (correctly as theyre store in XML)
                // html_writer::link() will re-escape them. To prevent double escaping unescape here.
                // This can by done using htmlspecialchars_decode() but moodle_url also has that effect.
                $link = new moodle_url($link);
            } catch (moodle_exception $e) {
                // Catching the exception to prevent the whole site to crash in case of malformed RSS feed
                $link = '';
            }
238
        }
239

240
241
        $r = html_writer::start_tag('li');
            $r.= html_writer::start_tag('div',array('class'=>'link'));
242
                $r.= html_writer::link($link, s($title), array('onclick'=>'this.target="_blank"'));
243
            $r.= html_writer::end_tag('div');
244

245
            if($this->config->display_description && !empty($description)){
246

247
                $formatoptions = new stdClass();
248
                $formatoptions->para = false;
249

250
                $r.= html_writer::start_tag('div',array('class'=>'description'));
251
252
253
                    $description = format_text($description, FORMAT_HTML, $formatoptions, $this->page->course->id);
                    $description = break_up_long_words($description, 30);
                    $r.= $description;
254
255
256
                $r.= html_writer::end_tag('div');
            }
        $r.= html_writer::end_tag('li');
257
258

        return $r;
259
    }
260

261
262
263
264
265
266
267
    /**
     * Strips a large title to size and adds ... if title too long
     *
     * @param string title to shorten
     * @param int max character length of title
     * @return string title s() quoted and shortened if necessary
     */
268
269
    function format_title($title,$max=64) {

270
        if (core_text::strlen($title) <= $max) {
271
272
            return s($title);
        } else {
273
            return s(core_text::substr($title,0,$max-3).'...');
274
275
276
        }
    }

277
    /**
278
     * cron - goes through all feeds and retrieves them with the cache
279
280
281
282
283
     * duration set to 0 in order to force the retrieval of the item and
     * refresh the cache
     *
     * @return boolean true if all feeds were retrieved succesfully
     */
284
    function cron() {
skodak's avatar
skodak committed
285
        global $CFG, $DB;
286
        require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
287

288
        // We are going to measure execution times
289
290
        $starttime =  microtime();

291
        // And we have one initial $status
292
293
        $status = true;

294
        // Fetch all site feeds.
skodak's avatar
skodak committed
295
        $rs = $DB->get_recordset('block_rss_client');
296
297
        $counter = 0;
        mtrace('');
skodak's avatar
skodak committed
298
        foreach ($rs as $rec) {
299
            mtrace('    ' . $rec->url . ' ', '');
300
301
            // Fetch the rss feed, using standard simplepie caching
            // so feeds will be renewed only if cache has expired
302
            core_php_time_limit::raise(60);
303
304

            $feed =  new moodle_simplepie();
305
            // set timeout for longer than normal to be agressive at
306
307
            // fetching feeds if possible..
            $feed->set_timeout(40);
308
309
310
311
312
            $feed->set_cache_duration(0);
            $feed->set_feed_url($rec->url);
            $feed->init();

            if ($feed->error()) {
313
                mtrace ('error');
314
                mtrace ('SimplePie failed with error:'.$feed->error());
315
                $status = false;
316
317
            } else {
                mtrace ('ok');
318
319
320
            }
            $counter ++;
        }
skodak's avatar
skodak committed
321
        $rs->close();
322

323
        // Show times
324
325
        mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');

326
        // And return $status
327
328
        return $status;
    }
329
}
330

331