Skip to content

Commit 702089a

Browse files
committed
Update the plugin and fixes
* Update glide version * Make sure we use cache to prevent fetching the feed at each page display * Small refactoring * Remove image prefix is set
1 parent 8574ef8 commit 702089a

29 files changed

+2014
-1860
lines changed

.travis.yml

Lines changed: 0 additions & 48 deletions
This file was deleted.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# RSS Thumbnails #
2+
3+
[![Moodle plugin CI for Moodle 3.9 and 3.10](https://github.com/call-learning/moodle-block_rss_thumbnails/actions/workflows/main.yml/badge.svg)](https://github.com/call-learning/moodle-block_rss_thumbnails/actions/workflows/main.yml)
4+
5+
Display a given RSS feed in a slider. Mostly used on site frontpage.
6+
7+
## License ##
8+
9+
2021 CALL Learning <[email protected]>
10+
11+
This program is free software: you can redistribute it and/or modify it under
12+
the terms of the GNU General Public License as published by the Free Software
13+
Foundation, either version 3 of the License, or (at your option) any later
14+
version.
15+
16+
This program is distributed in the hope that it will be useful, but WITHOUT ANY
17+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18+
PARTICULAR PURPOSE. See the GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License along with
21+
this program. If not, see <http://www.gnu.org/licenses/>.
22+
23+
## Dependencies ##
24+
25+
Glide.JS: https://glidejs.com/docs/

amd/build/glide.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/glide.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/glide.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
* @copyright 2020 - CALL Learning - Laurent David <laurent@call-learning>
66
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
77
*/
8-
define(['jquery', 'block_rss_thumbnails/config'], function($) {
8+
// eslint-disable-next-line no-unused-vars
9+
define(['block_rss_thumbnails/config'], function() {
910
return function(locator, config) {
1011
require(['glide'], function(Glide) {
1112
// Show the slider now we are initialised.
12-
$(locator).removeClass('d-none');
13-
new Glide(locator, config).mount();
13+
document.querySelector(locator).classList.remove('d-none');
14+
(new Glide(locator, config)).mount();
1415
});
1516
};
1617
});

block_rss_thumbnails.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use block_rss_thumbnails\output\block;
2626
use block_rss_thumbnails\output\feed;
2727
use block_rss_thumbnails\output\footer;
28-
use block_rss_thumbnails\feed_creator;
28+
use block_rss_thumbnails\feed_factory;
2929

3030
defined('MOODLE_INTERNAL') || die();
3131
global $CFG;
@@ -53,7 +53,6 @@ class block_rss_thumbnails extends block_base {
5353

5454
/** @var bool Track whether any of the output feeds have recorded failures */
5555
private $hasfailedfeeds = false;
56-
5756
/** @var int Defines the number of maximum feeds in the thumbnail */
5857
private $maxentries = self::DEFAULT_MAX_ENTRIES;
5958

@@ -103,7 +102,7 @@ public function get_content() {
103102
return $this->content;
104103
}
105104
$carousseldelay = $this->config->carousseldelay ?? self::DEFAULT_CAROUSSEL_DELAY;
106-
$block = new block($carousseldelay);
105+
$block = new block($carousseldelay, $this->config->remove_image_size_suffix ?? false);
107106

108107
if (!empty($this->config->rssid)) {
109108
list($rssidssql, $params) = $DB->get_in_or_equal($this->config->rssid);
@@ -117,14 +116,11 @@ public function get_content() {
117116
}
118117

119118
$footer = $this->get_footer($rssfeeds);
120-
} else {
121-
$rssfeeds = array();
122119
}
123120

124121
$renderer = $this->page->get_renderer('block_rss_thumbnails');
125-
126122
$this->content = (object) [
127-
'text' => $renderer->render($block, $rssfeeds),
123+
'text' => $renderer->render($block),
128124
'footer' => $footer ? $renderer->render($footer) : ''
129125
];
130126
return $this->content;
@@ -180,9 +176,20 @@ public function get_feed($feedrecord, $maxentries, $showtitle = true): ?feed {
180176
$this->hasfailedfeeds = true;
181177
return null;
182178
}
179+
return feed_factory::create_feed_from_url($feedrecord->url, $maxentries, $showtitle);
183180

184-
return feed_creator::create_feed_from_url($feedrecord->url, $maxentries, $showtitle);
181+
}
185182

183+
/**
184+
* Serialize and store config data
185+
*
186+
* @param stdClass $data
187+
* @param false $nolongerused
188+
* @throws coding_exception
189+
*/
190+
public function instance_config_save($data, $nolongerused = false) {
191+
parent::instance_config_save($data, $nolongerused);
192+
cache_helper::purge_by_event('block_rss_thumbnails/expiresfeed');
186193
}
187194

188195
/**
@@ -197,7 +204,6 @@ public function get_feed($feedrecord, $maxentries, $showtitle = true): ?feed {
197204
public function format_title($title, $max = 64): string {
198205
return (core_text::strlen($title) <= $max) ? $title : core_text::substr($title, 0, $max - 3) . '...';
199206
}
200-
201207
/**
202208
* Checks wether the configuration of the block is valid or not.
203209
*

classes/feed_creator.php renamed to classes/feed_factory.php

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use block_rss_thumbnails\output\channel_image;
2828
use block_rss_thumbnails\output\feed;
2929
use block_rss_thumbnails\output\item;
30+
use cache;
3031
use moodle_exception;
3132
use moodle_url;
3233
use SimpleXMLElement;
@@ -38,7 +39,7 @@
3839
* @copyright 202 - CALL Learning - Martin CORNU-MANSUY <martin@call-learning>
3940
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4041
*/
41-
class feed_creator {
42+
class feed_factory {
4243

4344
/** A list of XML entities. */
4445
const XML_ENTITIES = array(
@@ -70,13 +71,23 @@ class feed_creator {
7071
/**
7172
* A function that creates a feed from the given url of an xml file.
7273
*
74+
* Note: we cache the feed content in the block_rss_thumbnails/feeds cache to avoid
75+
* fetching the feed at each page load.
76+
*
7377
* @param string $xmlurl the url of the file
7478
* @param int $maxentries the maximum number of items this feed will have
7579
* @param bool $showtitle should we display the title of the feed ?
80+
* the feed provides the thumbnail image as xxx-80x80.png for example. The real image is xxx.png
7681
* @return feed|null the feed created or null if an error occurred
7782
*/
7883
public static function create_feed_from_url(string $xmlurl, int $maxentries, bool $showtitle = true) {
79-
return self::create_feed(file_get_contents($xmlurl), $maxentries, $showtitle);
84+
$cache = cache::make('block_rss_thumbnails', 'feeds');
85+
if ($cache->has($xmlurl)) {
86+
return $cache->get($xmlurl);
87+
}
88+
$feed = self::create_feed(file_get_contents($xmlurl), $maxentries, $showtitle);
89+
$cache->set($xmlurl, $feed);
90+
return $feed;
8091
}
8192

8293
/**
@@ -101,19 +112,19 @@ public static function create_feed(string $xmlsource, int $maxentries, bool $sho
101112
$image = $channel->image;
102113
if (!empty($image)) {
103114
$image = new channel_image(
104-
new moodle_url($image->url),
105-
$image->title,
106-
new moodle_url($image->link)
115+
new moodle_url($image->url),
116+
$image->title,
117+
new moodle_url($image->link)
107118
);
108119
} else {
109120
$image = null;
110121
}
111122
$feed = new feed(
112-
$channel->title,
113-
new moodle_url($channel->link),
114-
$image,
115-
$showtitle,
116-
$image ? true : false,
123+
(string) $channel->title,
124+
new moodle_url($channel->link),
125+
$image,
126+
$showtitle,
127+
$image ? true : false
117128
);
118129

119130
$counter = 0;
@@ -131,6 +142,16 @@ public static function create_feed(string $xmlsource, int $maxentries, bool $sho
131142
return $feed;
132143
}
133144

145+
/**
146+
* Replaces HTML entities that could be in the xml file so SimpleXML will be able to interpret every entity.
147+
*
148+
* @param string $xmlfile the source file content as a string.
149+
* @return string the source file content with only XML entities.
150+
*/
151+
public static function normalize_xml_file(string $xmlfile): string {
152+
return str_replace(self::HTML_ENTITIES, self::XML_ENTITIES, $xmlfile);
153+
}
154+
134155
/**
135156
* Creates all the items of a feed by calling {@see self::create_item()} and returns them into an array.
136157
*
@@ -161,12 +182,11 @@ private static function create_items(SimpleXMLElement $simplexmlelt): array {
161182
*
162183
* @param SimpleXMLElement $xmlitem The source of the item to be created.
163184
* @return item The item created.
164-
* @throws moodle_exception See the use of {@see moodle_url}
165185
*/
166186
public static function create_item(SimpleXMLElement $xmlitem) {
167187
$categories = [];
168188
foreach ($xmlitem->category as $category) {
169-
$categories[] = $category;
189+
$categories[] = (string) $category;
170190
}
171191

172192
$namespaces = $xmlitem->getNamespaces(true);
@@ -180,22 +200,12 @@ public static function create_item(SimpleXMLElement $xmlitem) {
180200
$imageurl = new moodle_url("");
181201
}
182202

183-
$id = $xmlitem->guid ?? $xmlitem->title;
203+
$id = (string) ($xmlitem->guid ?? $xmlitem->title);
184204
$link = new moodle_url($xmlitem->link);
185-
$description = $xmlitem->description;
205+
$description = (string) $xmlitem->description;
186206
$timestamp = strtotime($xmlitem->pubDate);
187-
$title = $xmlitem->title;
207+
$title = (string) $xmlitem->title;
188208

189209
return new item($id, $link, $description, $timestamp, true, $title, $imageurl, $categories);
190210
}
191-
192-
/**
193-
* Replaces HTML entities that could be in the xml file so SimpleXML will be able to interpret every entity.
194-
*
195-
* @param string $xmlfile the source file content as a string.
196-
* @return string the source file content with only XML entities.
197-
*/
198-
public static function normalize_xml_file(string $xmlfile): string {
199-
return str_replace(self::HTML_ENTITIES, self::XML_ENTITIES, $xmlfile);
200-
}
201211
}

classes/output/block.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,28 @@
3737
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3838
*/
3939
class block implements renderable, templatable {
40-
40+
/**
41+
* @var bool $removeimagesuffix should we remove image url suffix to show the large version of the image ? Sometime
42+
* the feed provides the thumbnail image as xxx-80x80.png for example. The real image is xxx.png.
43+
*/
44+
public $removeimagesuffix = false;
4145
/** @var int The delay between two slides of the caroussel (in milliseconds) */
4246
private $carousseldelay;
4347

4448
/** @var array An array of renderable feeds */
4549
private $feeds;
4650

4751
/**
48-
* Contructor
52+
* Constructor
4953
*
5054
* @param int $carousseldelay An integer representing the speed of the carroussel
51-
* @param array $feeds An array of renderable feeds
55+
* @param bool $removeimagesuffix should we remove the image prefix on export_for_template ?
5256
*/
53-
public function __construct(int $carousseldelay = block_rss_thumbnails::DEFAULT_CAROUSSEL_DELAY, array $feeds = array()) {
54-
$this->feeds = $feeds;
57+
public function __construct(int $carousseldelay = block_rss_thumbnails::DEFAULT_CAROUSSEL_DELAY,
58+
bool $removeimagesuffix = false) {
59+
$this->feeds = [];
5560
$this->carousseldelay = $carousseldelay;
61+
$this->removeimagesuffix = $removeimagesuffix;
5662
}
5763

5864
/**
@@ -67,6 +73,7 @@ public function export_for_template(renderer_base $output): array {
6773
'carousseldelay' => $this->carousseldelay
6874
);
6975
foreach ($this->feeds as $feed) {
76+
$feed->removeimagesuffix = $this->removeimagesuffix;
7077
$data['feeds'][] = $feed->export_for_template($output);
7178
}
7279

classes/output/feed.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3838
*/
3939
class feed implements renderable, templatable {
40+
/**
41+
* @var bool $removeimagesuffix should we remove image url suffix to show the large version of the image ? Sometime
42+
* the feed provides the thumbnail image as xxx-80x80.png for example. The real image is xxx.png.
43+
*/
44+
public $removeimagesuffix = false;
4045

4146
/** @var string|null The feed's title. */
4247
private $title;
@@ -124,7 +129,6 @@ public function get_link() {
124129
public function get_title() {
125130
return $this->title;
126131
}
127-
128132
/**
129133
* Export this for use in a mustache template context.
130134
*
@@ -141,6 +145,7 @@ public function export_for_template(renderer_base $output) {
141145
$data['image'] = ($this->showimage && $this->image) ? $this->image->export_for_template($output) : null;
142146

143147
foreach ($this->items as $item) {
148+
$item->removeimagesuffix = $this->removeimagesuffix;
144149
$data['items'][] = $item->export_for_template($output);
145150
}
146151

0 commit comments

Comments
 (0)