From 04fb95aa9e90b67990eedea5edf763f75e34a402 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Wed, 26 Oct 2022 15:23:51 +0200 Subject: [PATCH 01/10] fix invalid config, still have to implement the test --- .travis.yml | 1 + block_rss_thumbnails.php | 25 +++++++++++++++++++++++++ editfeed.php | 2 +- lang/en/block_rss_thumbnails.php | 1 + tests/block_rss_thumbnails_test.php | 8 ++++++++ version.php | 2 +- 6 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8a255b5..b52b493 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ cache: php: - 7.2 - 7.4 + - 8.0 env: matrix: diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 65b2e17..3b90eac 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -95,6 +95,11 @@ public function get_content() { new moodle_url('/blocks/rss_thumbnails/js/glide/dist/css/glide.core' . (debugging() ? '.min' : '') . '.css')); + if (!$this->config_is_valid()) { + $this->content->text = get_string("invalidconfig", "block_rss_thumbnails"); + return $this->content; + } + 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. @@ -107,9 +112,11 @@ public function get_content() { } // We need this if an user deletes a field in the configuration of the block. + /* $this->title = $this->config->title ?? self::DEFAULT_TITLE; $this->carousseldelay = $this->config->carousseldelay ?? self::DEFAULT_CAROUSSEL_DELAY; $this->maxentries = $this->config->numentries ?? self::DEFAULT_MAX_ENTRIES; + */ $block = new block($this->get_carousseldelay()); @@ -215,4 +222,22 @@ public function format_title($title, $max = 64): string { public function get_carousseldelay(): int { return $this->carousseldelay; } + + /** + * Checks wether the configuration of the block is valid or not. + * + * @return bool true if the configuration of the block is valid, false if it's not. + */ + private function config_is_valid(): bool { + if (empty($this->config)) { + return false; + } + if (!is_integer($this->config->carousseldelay) || !is_integer($this->config->numentries)) { + return false; + } + if (!$this->config->title) { + return false; + } + return true; + } } diff --git a/editfeed.php b/editfeed.php index 0238583..b06ea05 100644 --- a/editfeed.php +++ b/editfeed.php @@ -37,7 +37,7 @@ */ class feed_edit_form extends moodleform { - // TODO review this file because a lot of things are just none sense. + // TODO review this file. /** @var bool $isadding checks whether the user is adding a new feed or not. */ protected $isadding; diff --git a/lang/en/block_rss_thumbnails.php b/lang/en/block_rss_thumbnails.php index 988f09a..214d36d 100644 --- a/lang/en/block_rss_thumbnails.php +++ b/lang/en/block_rss_thumbnails.php @@ -47,3 +47,4 @@ $string['enableautodiscovery_help'] = 'By checking this, the RSS feed will reload automatically'; // TODO make sure it is correct. $string['feeddeleted'] = 'The feed has been successfully deleted'; $string['configureblock'] = "Click the edit icon above to configure this block to display RSS thumbnails ."; +$string['invalidconfig'] = "It looks like your block configuration is invalid.
Make sure you did not lay some empty fields."; diff --git a/tests/block_rss_thumbnails_test.php b/tests/block_rss_thumbnails_test.php index 001d3df..ecf8330 100644 --- a/tests/block_rss_thumbnails_test.php +++ b/tests/block_rss_thumbnails_test.php @@ -179,6 +179,14 @@ public function test_get_feed_items() { } + /** + * @return void + * @covers \block_rss_thumbnails::config_is_valid + */ + public function test_config_is_valid() { + // TODO implement test config is valid. + } + /** * Get expected items to be returned by the get_feed. * diff --git a/version.php b/version.php index f500cd8..e3e2a2e 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022102500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2022102501; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012112900; // Requires this Moodle version. $plugin->component = 'block_rss_thumbnails'; // Full name of the plugin (used for diagnostics). $plugin->dependencies = []; From a99fc83d13ebb85aa1facd25d5b63249637487ce Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Thu, 27 Oct 2022 09:43:26 +0200 Subject: [PATCH 02/10] isvalid --- block_rss_thumbnails.php | 2 +- tests/block_rss_thumbnails_test.php | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 3b90eac..b4ae41f 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -228,7 +228,7 @@ public function get_carousseldelay(): int { * * @return bool true if the configuration of the block is valid, false if it's not. */ - private function config_is_valid(): bool { + public function config_is_valid(): bool { if (empty($this->config)) { return false; } diff --git a/tests/block_rss_thumbnails_test.php b/tests/block_rss_thumbnails_test.php index ecf8330..001d3df 100644 --- a/tests/block_rss_thumbnails_test.php +++ b/tests/block_rss_thumbnails_test.php @@ -179,14 +179,6 @@ public function test_get_feed_items() { } - /** - * @return void - * @covers \block_rss_thumbnails::config_is_valid - */ - public function test_config_is_valid() { - // TODO implement test config is valid. - } - /** * Get expected items to be returned by the get_feed. * From f204654f255c33d9a26cbed243dbedc489d1e25b Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Thu, 10 Nov 2022 16:06:20 +0100 Subject: [PATCH 03/10] Resolved Dan Marsden's issues: * Separated the class edit into a single class name feed_edit -> fixes #6 * Added namespaces for editfeed and form/feed_edit -> fixes #5 * Added thirdpartylibs.xml -> fixes #3 * Added a check at the start of block_rss_thumbnails::get_content so the method will no longer compute twice -> fixes #4 --- block_rss_thumbnails.php | 4 + classes/form/feed_edit.php | 192 ++++++++++++++++++++++++++++ edit_form.php | 4 +- editfeed.php | 174 +------------------------ lang/en/block_rss_thumbnails.php | 1 + tests/block_rss_thumbnails_test.php | 2 - thirdpartylibs.xml | 10 ++ version.php | 3 +- 8 files changed, 218 insertions(+), 172 deletions(-) create mode 100644 classes/form/feed_edit.php create mode 100644 thirdpartylibs.xml diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index b4ae41f..294670a 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -91,6 +91,10 @@ public function init(?int $carousseldelay = null): void { public function get_content() { global $DB; + if ($this->content != null && !empty($this->content->text)) { + return $this->content; + } + $this->page->requires->css( new moodle_url('/blocks/rss_thumbnails/js/glide/dist/css/glide.core' . (debugging() ? '.min' : '') . '.css')); diff --git a/classes/form/feed_edit.php b/classes/form/feed_edit.php new file mode 100644 index 0000000..129cdd0 --- /dev/null +++ b/classes/form/feed_edit.php @@ -0,0 +1,192 @@ +. + + +namespace block_rss_thumbnails\form; + +defined('MOODLE_INTERNAL') || die(); + +require_login(); +require_once($CFG->libdir . '/formslib.php'); +require_once($CFG->libdir .'/simplepie/moodle_simplepie.php'); + +use moodle_simplepie; +use moodle_url; +use moodleform; + +/** + * A class to be able to edit the feeds we import in the plugin. + * + * @package block_rss_thumbnails + * @copyright 2022 - CALL Learning - Martin CORNU-MANSUY + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class feed_edit extends moodleform { + + // TODO review this file. + + /** @var bool $isadding checks whether the user is adding a new feed or not. */ + protected $isadding; + + /** @var bool $caneditshared checks whether the user has the capability to edit a shared feed or not. */ + protected $caneditshared; + + /** @var string $title The title */ + protected $title = ''; + + /** @var string $description The description */ + protected $description = ''; + + /** + * Constructor. + * + * @param string $actionurl the url of the action. + * @param bool $isadding to know if the user is adding a new feed or not. + * @param bool $caneditshared to know if the user has the ability to edit a shared feed or not. + */ + public function __construct($actionurl, $isadding, $caneditshared) { + $this->isadding = $isadding; + $this->caneditshared = $caneditshared; + parent::__construct($actionurl); + } + + /** + * Defines the form allowing to edit a feed. + * + * @return void + */ + public function definition() { + $mform =& $this->_form; + + // Then show the fields about where this block appears. + $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_thumbnails')); + + $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_thumbnails'), array('size' => 60)); + $mform->setType('url', PARAM_URL); + $mform->addRule('url', null, 'required'); + + $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_thumbnails')); + $mform->setDefault('autodiscovery', 1); + $mform->setAdvanced('autodiscovery'); + $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_thumbnails'); + + $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_thumbnails'), array('size' => 60)); + $mform->setType('preferredtitle', PARAM_NOTAGS); + + if ($this->caneditshared) { + $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_thumbnails')); + $mform->setDefault('shared', 0); + } + + $submitlabal = null; // Default. + if ($this->isadding) { + $submitlabal = get_string('addnewfeed', 'block_rss_thumbnails'); + } + $this->add_action_buttons(true, $submitlabal); + } + + /** + * Defines the form after the discovery of data + * + * @return void + */ + public function definition_after_data() { + $mform =& $this->_form; + + if ($mform->getElementValue('autodiscovery')) { + $mform->applyFilter('url', self::class . '::autodiscover_feed_url'); + } + } + + /** + * Validates the edition form. + * + * @param array $data Datas of the form. + * @param array $files Files of the form. + * @return array + */ + public function validation($data, $files): array { + $errors = parent::validation($data, $files); + + $rss = new moodle_simplepie(); + // Set timeout for longer than normal to try and grab the feed. + $rss->set_timeout(); + $rss->set_feed_url($data['url']); + $rss->set_autodiscovery_cache_duration(0); + $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); + $rss->init(); + + if ($rss->error()) { + $errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_thumbnails'); + } else { + $this->title = $rss->get_title(); + $this->description = $rss->get_description(); + } + + return $errors; + } + + /** + * Gets data of the form. + * + * @return object|null + */ + public function get_data(): ?object { + $data = parent::get_data(); + if ($data) { + $data->title = ''; + $data->description = ''; + + if ($this->title) { + $data->title = $this->title; + } + + if ($this->description) { + $data->description = $this->description; + } + } + return $data; + } + + /** + * Autodiscovers a feed url from a given url, to be used by the formslibs + * filter function + * + * Uses simplepie with autodiscovery set to maximum level to try and find + * a feed to subscribe to. + * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level + * + * @param string $url URL to autodiscover a url + * @return string URL of feed or original url if none found + */ + public static function autodiscover_feed_url($url): string { + $rss = new moodle_simplepie(); + $rss->set_feed_url($url); + $rss->set_autodiscovery_level(); + // When autodiscovering an RSS feed, simplepie will try lots of + // rss links on a page, so set the timeout high. + $rss->set_timeout(20); + $rss->init(); + + if ($rss->error()) { + return $url; + } + + // Return URL without quoting.. + $discoveredurl = new moodle_url($rss->subscribe_url()); + return $discoveredurl->out(false); + } +} diff --git a/edit_form.php b/edit_form.php index 7929fd8..7da72d5 100644 --- a/edit_form.php +++ b/edit_form.php @@ -45,7 +45,7 @@ class block_rss_thumbnails_edit_form extends block_edit_form { * @return void */ protected function specific_definition($mform) { - global $CFG, $DB, $USER, $SESSION; + global $CFG, $DB, $USER; // Fields for editing block contents. $mform->addElement('header', 'configheader', get_string('blocksettings', 'block')); @@ -96,7 +96,7 @@ protected function specific_definition($mform) { get_string('choosefeedlabel', 'block_rss_thumbnails'), $rssfeeds ); - $select->setMultiple(true); + $select->setMultiple(false); } else { $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_thumbnails'), get_string('nofeeds', 'block_rss_thumbnails')); diff --git a/editfeed.php b/editfeed.php index b06ea05..fefa26d 100644 --- a/editfeed.php +++ b/editfeed.php @@ -22,175 +22,15 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +namespace block_rss_thumbnails; require_once(__DIR__ . '/../../config.php'); require_login(); -require_once($CFG->libdir . '/formslib.php'); -require_once($CFG->libdir .'/simplepie/moodle_simplepie.php'); -/** - * A class to be able to edit the feeds we import in the plugin. - * - * @package block_rss_thumbnails - * @copyright 2022 - CALL Learning - Martin CORNU-MANSUY - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class feed_edit_form extends moodleform { - - // TODO review this file. - - /** @var bool $isadding checks whether the user is adding a new feed or not. */ - protected $isadding; - - /** @var bool $caneditshared checks whether the user has the capability to edit a shared feed or not. */ - protected $caneditshared; - - /** @var string $title The title */ - protected $title = ''; - - /** @var string $description The description */ - protected $description = ''; - - /** - * Constructor. - * - * @param string $actionurl the url of the action. - * @param bool $isadding to know if the user is adding a new feed or not. - * @param bool $caneditshared to know if the user has the ability to edit a shared feed or not. - */ - public function __construct($actionurl, $isadding, $caneditshared) { - $this->isadding = $isadding; - $this->caneditshared = $caneditshared; - parent::__construct($actionurl); - } - - /** - * Defines the form allowing to edit a feed. - * - * @return void - */ - public function definition() { - $mform =& $this->_form; - - // Then show the fields about where this block appears. - $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_thumbnails')); - - $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_thumbnails'), array('size' => 60)); - $mform->setType('url', PARAM_URL); - $mform->addRule('url', null, 'required'); - - $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_thumbnails')); - $mform->setDefault('autodiscovery', 1); - $mform->setAdvanced('autodiscovery'); - $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_thumbnails'); - - $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_thumbnails'), array('size' => 60)); - $mform->setType('preferredtitle', PARAM_NOTAGS); - - if ($this->caneditshared) { - $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_thumbnails')); - $mform->setDefault('shared', 0); - } - - $submitlabal = null; // Default. - if ($this->isadding) { - $submitlabal = get_string('addnewfeed', 'block_rss_thumbnails'); - } - $this->add_action_buttons(true, $submitlabal); - } - - /** - * Defines the form after the discovery of data - * - * @return void - */ - public function definition_after_data() { - $mform =& $this->_form; - - if ($mform->getElementValue('autodiscovery')) { - $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url'); - } - } - - /** - * Validates the edition form. - * - * @param array $data Datas of the form. - * @param array $files Files of the form. - * @return array - */ - public function validation($data, $files): array { - $errors = parent::validation($data, $files); - - $rss = new moodle_simplepie(); - // Set timeout for longer than normal to try and grab the feed. - $rss->set_timeout(10); - $rss->set_feed_url($data['url']); - $rss->set_autodiscovery_cache_duration(0); - $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); - $rss->init(); - - if ($rss->error()) { - $errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_thumbnails'); - } else { - $this->title = $rss->get_title(); - $this->description = $rss->get_description(); - } - - return $errors; - } - - /** - * Gets data of the form. - * - * @return object|null - */ - public function get_data(): ?object { - $data = parent::get_data(); - if ($data) { - $data->title = ''; - $data->description = ''; - - if ($this->title) { - $data->title = $this->title; - } - - if ($this->description) { - $data->description = $this->description; - } - } - return $data; - } - - /** - * Autodiscovers a feed url from a given url, to be used by the formslibs - * filter function - * - * Uses simplepie with autodiscovery set to maximum level to try and find - * a feed to subscribe to. - * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level - * - * @param string $url URL to autodiscover a url - * @return string URL of feed or original url if none found - */ - public static function autodiscover_feed_url($url) { - $rss = new moodle_simplepie(); - $rss->set_feed_url($url); - $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL); - // When autodiscovering an RSS feed, simplepie will try lots of - // rss links on a page, so set the timeout high. - $rss->set_timeout(20); - $rss->init(); - - if ($rss->error()) { - return $url; - } - - // Return URL without quoting.. - $discoveredurl = new moodle_url($rss->subscribe_url()); - return $discoveredurl->out(false); - } -} +use block_rss_thumbnails\form\feed_edit; +use context_system; +use moodle_url; +use stdClass; $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); $courseid = optional_param('courseid', 0, PARAM_INT); @@ -230,10 +70,10 @@ public static function autodiscover_feed_url($url) { $rssrecord = $DB->get_record('block_rss_thumbnails', array('id' => $rssid), '*', MUST_EXIST); } else { $isadding = true; - $rssrecord = new stdClass; + $rssrecord = new stdClass(); } -$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds); +$mform = new feed_edit($PAGE->url, $isadding, $managesharedfeeds); $mform->set_data($rssrecord); if ($mform->is_cancelled()) { diff --git a/lang/en/block_rss_thumbnails.php b/lang/en/block_rss_thumbnails.php index 214d36d..5ab77d4 100644 --- a/lang/en/block_rss_thumbnails.php +++ b/lang/en/block_rss_thumbnails.php @@ -36,6 +36,7 @@ $string['clientshowchannellinklabel'] = 'Should a link to the original site (channel link) be displayed? (Note that if no feed link is supplied in the news feed then no link will be shown) :'; $string['managefeeds'] = 'Manage feeds'; $string['addnewfeed'] = 'Add a new feed'; +$string['editafeed'] = 'Edit a feed'; $string['feedurl'] = 'URL of the feed'; $string['feed'] = 'Feed'; $string['customtitlelabel'] = 'Enter feed\'s title'; diff --git a/tests/block_rss_thumbnails_test.php b/tests/block_rss_thumbnails_test.php index 001d3df..24a7120 100644 --- a/tests/block_rss_thumbnails_test.php +++ b/tests/block_rss_thumbnails_test.php @@ -39,8 +39,6 @@ */ class block_rss_thumbnails_test extends advanced_testcase { - // TODO IMPORTANT TO CHANGE CAROUSSEL SPEED INTO SMTHING LIKE CAROUSSEL DELAY (in ms). - /** * Expected config data. */ diff --git a/thirdpartylibs.xml b/thirdpartylibs.xml new file mode 100644 index 0000000..b49d63f --- /dev/null +++ b/thirdpartylibs.xml @@ -0,0 +1,10 @@ + + + + js/glide/dist/glide.js + Glide + 3.5.2 + MIT + 1.0 + + \ No newline at end of file diff --git a/version.php b/version.php index e3e2a2e..0168afd 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022102501; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2022111004; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012112900; // Requires this Moodle version. +$plugin->release = 1.0; $plugin->component = 'block_rss_thumbnails'; // Full name of the plugin (used for diagnostics). $plugin->dependencies = []; From 7dbe77ac7e36cf39f8ba51a2a0069ce61e487bb6 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Thu, 10 Nov 2022 16:18:19 +0100 Subject: [PATCH 04/10] moodle-plugin-ci validation --- block_rss_thumbnails.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 294670a..1c9b4b2 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -115,13 +115,6 @@ public function get_content() { return $this->content; } - // We need this if an user deletes a field in the configuration of the block. - /* - $this->title = $this->config->title ?? self::DEFAULT_TITLE; - $this->carousseldelay = $this->config->carousseldelay ?? self::DEFAULT_CAROUSSEL_DELAY; - $this->maxentries = $this->config->numentries ?? self::DEFAULT_MAX_ENTRIES; - */ - $block = new block($this->get_carousseldelay()); if (!empty($this->config->rssid)) { From b8711f617d8dc289176f23d37b488aee70a79bc6 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Mon, 5 Dec 2022 09:47:13 +0100 Subject: [PATCH 05/10] Added an upgrade script to ensure keeping block configurations between versions - Fixes #8 --- block_rss_thumbnails.php | 5 ++- db/upgrade.php | 68 +++++++++++++++++++++++++++++----------- edit_form.php | 2 +- version.php | 3 +- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 1c9b4b2..03948bb 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -229,7 +229,10 @@ public function config_is_valid(): bool { if (empty($this->config)) { return false; } - if (!is_integer($this->config->carousseldelay) || !is_integer($this->config->numentries)) { + if (!is_integer($this->config->carousseldelay)) { + return false; + } + if (!is_integer($this->config->numentries)) { return false; } if (!$this->config->title) { diff --git a/db/upgrade.php b/db/upgrade.php index 796bb0a..1a45501 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -34,32 +34,39 @@ function xmldb_block_rss_thumbnails_upgrade($oldversion) { // Automatically generated Moodle v3.9.0 release upgrade line. // Put any upgrade step following this. $dbman = $DB->get_manager(); - if ($oldversion < 2022091502) { + if ($oldversion < 2022111004) { - // Define table block_rss_thumbnails to be created. - $table = new xmldb_table('block_rss_thumbnails'); - - // Adding fields to table block_rss_thumbnails. - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('title', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); - $table->add_field('preferredtitle', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null); - $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); - $table->add_field('shared', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); - $table->add_field('skiptime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('skipuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - - // Adding keys to table block_rss_thumbnails. - $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table = create_block_rss_thumbnails_table(); + $params = ['blockname' => 'rss_thumbnails']; // Conditionally launch create table for block_rss_thumbnails. if (!$dbman->table_exists($table)) { $dbman->create_table($table); } + if ($DB->record_exists('block_instances', $params)) { + if ($dbman->table_exists('block_rss_client')) { + $rssfeeds = $DB->get_records('block_rss_client'); + $DB->insert_records('block_rss_thumbnails', $rssfeeds); + } + $blockinstances = $DB->get_records('block_instances', ['blockname' => 'rss_thumbnails']); + foreach ($blockinstances as $blockinstance) { + // Access to block's configdata. + $blockconfig = unserialize(base64_decode($blockinstance->configdata)); + + // Update caroussel speed variable that changed into carousseldelay for semantical reasons. + $blockconfig->carousseldelay = $blockconfig->carousselspeed; + $blockconfig->numentries = $blockconfig->shownumentries; + + $newblockinstance = clone $blockinstance; + // Re-serialize block's configdata. + $newblockinstance->configdata = base64_encode(serialize($blockconfig)); + + $DB->update_record('block_instances', $newblockinstance); + } + } // Rss_thumbnails savepoint reached. - upgrade_block_savepoint(true, 2022091502, 'rss_thumbnails'); + upgrade_block_savepoint(true, 2022111004, 'rss_thumbnails'); } // Automatically generated Moodle v4.0.0 release upgrade line. @@ -67,3 +74,28 @@ function xmldb_block_rss_thumbnails_upgrade($oldversion) { return true; } + +/** + * Creates an empty block_rss_thumbnails table by adding to it all the fields it needs and setting up the right primary key. + * + * @return xmldb_table + */ +function create_block_rss_thumbnails_table(): xmldb_table { + // Define table block_rss_thumbnails to be created. + $table = new xmldb_table('block_rss_thumbnails'); + + // Adding fields to table block_rss_thumbnails. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('title', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('preferredtitle', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null); + $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('shared', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('skiptime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('skipuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + + // Adding keys to table block_rss_thumbnails. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + return $table; +} diff --git a/edit_form.php b/edit_form.php index 7da72d5..336657b 100644 --- a/edit_form.php +++ b/edit_form.php @@ -96,7 +96,7 @@ protected function specific_definition($mform) { get_string('choosefeedlabel', 'block_rss_thumbnails'), $rssfeeds ); - $select->setMultiple(false); + $select->setMultiple(true); } else { $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_thumbnails'), get_string('nofeeds', 'block_rss_thumbnails')); diff --git a/version.php b/version.php index 0168afd..81c3c8f 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,9 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022111004; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2022120700; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012112900; // Requires this Moodle version. $plugin->release = 1.0; +$plugin->maturity = MATURITY_STABLE; $plugin->component = 'block_rss_thumbnails'; // Full name of the plugin (used for diagnostics). $plugin->dependencies = []; From 0a7ef29320cc507abbf961c39daaca0aeb287985 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Fri, 9 Dec 2022 09:09:02 +0100 Subject: [PATCH 06/10] Fix caroussel delay & Footer: * Fixed caroussel delay always setting up to default value * Fixes #10 * Added a footer template * Fix multiple feed selection * Fixes #9 --- amd/src/glide.js | 2 +- block_rss_thumbnails.php | 27 +++++------------------- edit_form.php | 2 +- templates/block.mustache | 2 +- templates/footer.mustache | 43 +++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 6 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 templates/footer.mustache diff --git a/amd/src/glide.js b/amd/src/glide.js index 04e70dc..33b15de 100644 --- a/amd/src/glide.js +++ b/amd/src/glide.js @@ -13,4 +13,4 @@ define(['jquery', 'block_rss_thumbnails/config'], function($) { new Glide(locator, config).mount(); }); }; -}); \ No newline at end of file +}); diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 03948bb..4104db8 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -55,9 +55,6 @@ class block_rss_thumbnails extends block_base { /** @var bool Track whether any of the output feeds have recorded failures */ private $hasfailedfeeds = false; - /** @var int Defines the delay between two slides of the caroussel (ms) */ - private $carousseldelay = self::DEFAULT_CAROUSSEL_DELAY; - /** @var int Defines the number of maximum feeds in the thumbnail */ private $maxentries = self::DEFAULT_MAX_ENTRIES; @@ -68,11 +65,7 @@ class block_rss_thumbnails extends block_base { * @param int|null $carousseldelay * @throws coding_exception */ - public function init(?int $carousseldelay = null): void { - - if (!empty($carousseldelay)) { - $this->carousseldelay = $carousseldelay; - } + public function init(): void { $this->title = get_string('pluginname', 'block_rss_thumbnails'); @@ -114,8 +107,8 @@ public function get_content() { return $this->content; } - - $block = new block($this->get_carousseldelay()); + $carousseldelay = $this->config->carousseldelay ?? self::DEFAULT_CAROUSSEL_DELAY; + $block = new block($carousseldelay); if (!empty($this->config->rssid)) { list($rssidssql, $params) = $DB->get_in_or_equal($this->config->rssid); @@ -137,7 +130,7 @@ public function get_content() { $this->content = (object) [ 'text' => $renderer->render($block, $rssfeeds), - 'footer' => $footer ?? '' + 'footer' => $footer ? $renderer->render($footer) : '' ]; return $this->content; } @@ -154,8 +147,7 @@ protected function get_footer($feedrecords, $maxentries = self::DEFAULT_MAX_ENTR if (!empty($this->config->show_channel_link)) { $feedrecord = array_pop($feedrecords); - $feed = feed_creator::create_feed(file_get_contents($feedrecord->url), $maxentries); - $channellink = new moodle_url($feed->get_link()); + $channellink = new moodle_url($feedrecord->url); if (!empty($channellink)) { $footer = new footer($channellink); @@ -211,15 +203,6 @@ public function format_title($title, $max = 64): string { return (core_text::strlen($title) <= $max) ? $title : core_text::substr($title, 0, $max - 3) . '...'; } - /** - * Gets the caroussel delay between two slides - * - * @return int - */ - public function get_carousseldelay(): int { - return $this->carousseldelay; - } - /** * Checks wether the configuration of the block is valid or not. * diff --git a/edit_form.php b/edit_form.php index 336657b..7da72d5 100644 --- a/edit_form.php +++ b/edit_form.php @@ -96,7 +96,7 @@ protected function specific_definition($mform) { get_string('choosefeedlabel', 'block_rss_thumbnails'), $rssfeeds ); - $select->setMultiple(true); + $select->setMultiple(false); } else { $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_thumbnails'), get_string('nofeeds', 'block_rss_thumbnails')); diff --git a/templates/block.mustache b/templates/block.mustache index b2edc82..c7c4313 100644 --- a/templates/block.mustache +++ b/templates/block.mustache @@ -30,7 +30,7 @@ Example context (json): { - "carousseldelay" : 10, + "carousseldelay" : 100, "feeds": [ { "title": "News from around my living room", diff --git a/templates/footer.mustache b/templates/footer.mustache new file mode 100644 index 0000000..677d3a3 --- /dev/null +++ b/templates/footer.mustache @@ -0,0 +1,43 @@ +{{! + 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 . +}} +{{! + @template block_rss_thumbnails/block + + Template which defines an RSS Feeds block + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * feeds - array: An array of RSS feeds. + + Example context (json): + { + channellink: https://moodle.org, + hasfailedfeeds: true, + manageurl: https://example.com + } +}} + \ No newline at end of file diff --git a/version.php b/version.php index 81c3c8f..b0722d0 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022120700; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2022120900; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012112900; // Requires this Moodle version. $plugin->release = 1.0; $plugin->maturity = MATURITY_STABLE; From dacba3666b52b6fc1c8ded26e7f070d5d57145ea Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Fri, 9 Dec 2022 10:36:50 +0100 Subject: [PATCH 07/10] Validated moodle plugin ci --- amd/build/glide.min.js.map | 2 +- block_rss_thumbnails.php | 3 +-- templates/footer.mustache | 12 ++++++------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/amd/build/glide.min.js.map b/amd/build/glide.min.js.map index 62d40fb..8741714 100644 --- a/amd/build/glide.min.js.map +++ b/amd/build/glide.min.js.map @@ -1 +1 @@ -{"version":3,"file":"glide.min.js","sources":["../src/glide.js"],"sourcesContent":["/**\n * RSS Thumbnails block\n *\n * @package\n * @copyright 2020 - CALL Learning - Laurent David \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'block_rss_thumbnails/config'], function($) {\n return function(locator, config) {\n require(['glide'], function(Glide) {\n // Show the slider now we are initialised.\n $(locator).removeClass('d-none');\n new Glide(locator, config).mount();\n });\n };\n});"],"names":["define","$","locator","config","require","Glide","removeClass","mount"],"mappings":";;;;;;;AAOAA,oCAAO,CAAC,SAAU,gCAAgC,SAASC,UAChD,SAASC,QAASC,QACrBC,QAAQ,CAAC,UAAU,SAASC,OAExBJ,EAAEC,SAASI,YAAY,cACnBD,MAAMH,QAASC,QAAQI"} \ No newline at end of file +{"version":3,"file":"glide.min.js","sources":["../src/glide.js"],"sourcesContent":["/**\n * RSS Thumbnails block\n *\n * @package\n * @copyright 2020 - CALL Learning - Laurent David \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'block_rss_thumbnails/config'], function($) {\n return function(locator, config) {\n require(['glide'], function(Glide) {\n // Show the slider now we are initialised.\n $(locator).removeClass('d-none');\n new Glide(locator, config).mount();\n });\n };\n});\n"],"names":["define","$","locator","config","require","Glide","removeClass","mount"],"mappings":";;;;;;;AAOAA,oCAAO,CAAC,SAAU,gCAAgC,SAASC,UAChD,SAASC,QAASC,QACrBC,QAAQ,CAAC,UAAU,SAASC,OAExBJ,EAAEC,SAASI,YAAY,cACnBD,MAAMH,QAASC,QAAQI"} \ No newline at end of file diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 202bc07..fcbaa0c 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -62,8 +62,7 @@ class block_rss_thumbnails extends block_base { /** * Init function * - * @param int|null $carousseldelay - * @throws coding_exception +git * @throws coding_exception */ public function init(): void { diff --git a/templates/footer.mustache b/templates/footer.mustache index 677d3a3..78b7052 100644 --- a/templates/footer.mustache +++ b/templates/footer.mustache @@ -30,14 +30,14 @@ Example context (json): { - channellink: https://moodle.org, - hasfailedfeeds: true, - manageurl: https://example.com + "channellink": "https://moodle.org", + "hasfailedfeeds": true, + "manageurl": "https://example.com" } }} \ No newline at end of file From b87325f63b448927407ca577d91a15e8f4e7119b Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Fri, 9 Dec 2022 10:39:12 +0100 Subject: [PATCH 08/10] Fixed merge issues --- block_rss_thumbnails.php | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index fcbaa0c..8105b27 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -51,18 +51,16 @@ class block_rss_thumbnails extends block_base { /** @var int The maximum number of item entries for a feed by default */ const DEFAULT_MAX_ENTRIES = 5; - /** @var bool Track whether any of the output feeds have recorded failures */ private $hasfailedfeeds = false; /** @var int Defines the number of maximum feeds in the thumbnail */ private $maxentries = self::DEFAULT_MAX_ENTRIES; - /** * Init function * -git * @throws coding_exception + * git * @throws coding_exception */ public function init(): void { @@ -128,8 +126,8 @@ public function get_content() { $renderer = $this->page->get_renderer('block_rss_thumbnails'); $this->content = (object) [ - 'text' => $renderer->render($block, $rssfeeds), - 'footer' => $footer ? $renderer->render($footer) : '' + 'text' => $renderer->render($block, $rssfeeds), + 'footer' => $footer ? $renderer->render($footer) : '' ]; return $this->content; } @@ -161,7 +159,7 @@ protected function get_footer($feedrecords, $maxentries = self::DEFAULT_MAX_ENTR $footer = new footer(); } $manageurl = new moodle_url('/blocks/rss_thumbnails/managefeeds.php', - ['courseid' => $this->page->course->id]); + ['courseid' => $this->page->course->id]); $footer->set_failed($manageurl); } } @@ -222,25 +220,4 @@ public function config_is_valid(): bool { } return true; } - - /** - * Checks wether the configuration of the block is valid or not. - * - * @return bool true if the configuration of the block is valid, false if it's not. - */ - public function config_is_valid(): bool { - if (empty($this->config)) { - return false; - } - if (!is_integer($this->config->carousseldelay)) { - return false; - } - if (!is_integer($this->config->numentries)) { - return false; - } - if (!$this->config->title) { - return false; - } - return true; - } } From a693202aeefb6dde939499b988f22fcdabed9992 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Fri, 9 Dec 2022 10:45:07 +0100 Subject: [PATCH 09/10] Fix phpdoc --- block_rss_thumbnails.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/block_rss_thumbnails.php b/block_rss_thumbnails.php index 8105b27..9b15433 100644 --- a/block_rss_thumbnails.php +++ b/block_rss_thumbnails.php @@ -59,8 +59,6 @@ class block_rss_thumbnails extends block_base { /** * Init function - * - * git * @throws coding_exception */ public function init(): void { From 3c30d4bdccca26227f09f1cc07d5c71cf5f5aae7 Mon Sep 17 00:00:00 2001 From: martin-call-learning Date: Fri, 9 Dec 2022 10:48:17 +0100 Subject: [PATCH 10/10] updated release number --- version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.php b/version.php index b0722d0..0274dbc 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->version = 2022120900; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012112900; // Requires this Moodle version. -$plugin->release = 1.0; +$plugin->release = 1.1; $plugin->maturity = MATURITY_STABLE; $plugin->component = 'block_rss_thumbnails'; // Full name of the plugin (used for diagnostics). $plugin->dependencies = [];