Skip to content

Commit bb5b58a

Browse files
Danmarsden fixes (#7)
* fix invalid config, still have to implement the test * 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 * moodle-plugin-ci validation * Added an upgrade script to ensure keeping block configurations between versions - Fixes #8
1 parent dbcfae5 commit bb5b58a

File tree

10 files changed

+296
-194
lines changed

10 files changed

+296
-194
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cache:
1616
php:
1717
- 7.2
1818
- 7.4
19+
- 8.0
1920

2021
env:
2122
matrix:

block_rss_thumbnails.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,19 @@ public function init(?int $carousseldelay = null): void {
9191
public function get_content() {
9292
global $DB;
9393

94+
if ($this->content != null && !empty($this->content->text)) {
95+
return $this->content;
96+
}
97+
9498
$this->page->requires->css(
9599
new moodle_url('/blocks/rss_thumbnails/js/glide/dist/css/glide.core' .
96100
(debugging() ? '.min' : '') . '.css'));
97101

102+
if (!$this->config_is_valid()) {
103+
$this->content->text = get_string("invalidconfig", "block_rss_thumbnails");
104+
return $this->content;
105+
}
106+
98107
if (!isset($this->config)) {
99108
// The block has yet to be configured - just display configure message in
100109
// the block if user has permission to configure it.
@@ -106,11 +115,6 @@ public function get_content() {
106115
return $this->content;
107116
}
108117

109-
// We need this if an user deletes a field in the configuration of the block.
110-
$this->title = $this->config->title ?? self::DEFAULT_TITLE;
111-
$this->carousseldelay = $this->config->carousseldelay ?? self::DEFAULT_CAROUSSEL_DELAY;
112-
$this->maxentries = $this->config->numentries ?? self::DEFAULT_MAX_ENTRIES;
113-
114118
$block = new block($this->get_carousseldelay());
115119

116120
if (!empty($this->config->rssid)) {
@@ -215,4 +219,25 @@ public function format_title($title, $max = 64): string {
215219
public function get_carousseldelay(): int {
216220
return $this->carousseldelay;
217221
}
222+
223+
/**
224+
* Checks wether the configuration of the block is valid or not.
225+
*
226+
* @return bool true if the configuration of the block is valid, false if it's not.
227+
*/
228+
public function config_is_valid(): bool {
229+
if (empty($this->config)) {
230+
return false;
231+
}
232+
if (!is_integer($this->config->carousseldelay)) {
233+
return false;
234+
}
235+
if (!is_integer($this->config->numentries)) {
236+
return false;
237+
}
238+
if (!$this->config->title) {
239+
return false;
240+
}
241+
return true;
242+
}
218243
}

classes/form/feed_edit.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
namespace block_rss_thumbnails\form;
19+
20+
defined('MOODLE_INTERNAL') || die();
21+
22+
require_login();
23+
require_once($CFG->libdir . '/formslib.php');
24+
require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
25+
26+
use moodle_simplepie;
27+
use moodle_url;
28+
use moodleform;
29+
30+
/**
31+
* A class to be able to edit the feeds we import in the plugin.
32+
*
33+
* @package block_rss_thumbnails
34+
* @copyright 2022 - CALL Learning - Martin CORNU-MANSUY <[email protected]>
35+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36+
*/
37+
class feed_edit extends moodleform {
38+
39+
// TODO review this file.
40+
41+
/** @var bool $isadding checks whether the user is adding a new feed or not. */
42+
protected $isadding;
43+
44+
/** @var bool $caneditshared checks whether the user has the capability to edit a shared feed or not. */
45+
protected $caneditshared;
46+
47+
/** @var string $title The title */
48+
protected $title = '';
49+
50+
/** @var string $description The description */
51+
protected $description = '';
52+
53+
/**
54+
* Constructor.
55+
*
56+
* @param string $actionurl the url of the action.
57+
* @param bool $isadding to know if the user is adding a new feed or not.
58+
* @param bool $caneditshared to know if the user has the ability to edit a shared feed or not.
59+
*/
60+
public function __construct($actionurl, $isadding, $caneditshared) {
61+
$this->isadding = $isadding;
62+
$this->caneditshared = $caneditshared;
63+
parent::__construct($actionurl);
64+
}
65+
66+
/**
67+
* Defines the form allowing to edit a feed.
68+
*
69+
* @return void
70+
*/
71+
public function definition() {
72+
$mform =& $this->_form;
73+
74+
// Then show the fields about where this block appears.
75+
$mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_thumbnails'));
76+
77+
$mform->addElement('text', 'url', get_string('feedurl', 'block_rss_thumbnails'), array('size' => 60));
78+
$mform->setType('url', PARAM_URL);
79+
$mform->addRule('url', null, 'required');
80+
81+
$mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_thumbnails'));
82+
$mform->setDefault('autodiscovery', 1);
83+
$mform->setAdvanced('autodiscovery');
84+
$mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_thumbnails');
85+
86+
$mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_thumbnails'), array('size' => 60));
87+
$mform->setType('preferredtitle', PARAM_NOTAGS);
88+
89+
if ($this->caneditshared) {
90+
$mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_thumbnails'));
91+
$mform->setDefault('shared', 0);
92+
}
93+
94+
$submitlabal = null; // Default.
95+
if ($this->isadding) {
96+
$submitlabal = get_string('addnewfeed', 'block_rss_thumbnails');
97+
}
98+
$this->add_action_buttons(true, $submitlabal);
99+
}
100+
101+
/**
102+
* Defines the form after the discovery of data
103+
*
104+
* @return void
105+
*/
106+
public function definition_after_data() {
107+
$mform =& $this->_form;
108+
109+
if ($mform->getElementValue('autodiscovery')) {
110+
$mform->applyFilter('url', self::class . '::autodiscover_feed_url');
111+
}
112+
}
113+
114+
/**
115+
* Validates the edition form.
116+
*
117+
* @param array $data Datas of the form.
118+
* @param array $files Files of the form.
119+
* @return array
120+
*/
121+
public function validation($data, $files): array {
122+
$errors = parent::validation($data, $files);
123+
124+
$rss = new moodle_simplepie();
125+
// Set timeout for longer than normal to try and grab the feed.
126+
$rss->set_timeout();
127+
$rss->set_feed_url($data['url']);
128+
$rss->set_autodiscovery_cache_duration(0);
129+
$rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
130+
$rss->init();
131+
132+
if ($rss->error()) {
133+
$errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_thumbnails');
134+
} else {
135+
$this->title = $rss->get_title();
136+
$this->description = $rss->get_description();
137+
}
138+
139+
return $errors;
140+
}
141+
142+
/**
143+
* Gets data of the form.
144+
*
145+
* @return object|null
146+
*/
147+
public function get_data(): ?object {
148+
$data = parent::get_data();
149+
if ($data) {
150+
$data->title = '';
151+
$data->description = '';
152+
153+
if ($this->title) {
154+
$data->title = $this->title;
155+
}
156+
157+
if ($this->description) {
158+
$data->description = $this->description;
159+
}
160+
}
161+
return $data;
162+
}
163+
164+
/**
165+
* Autodiscovers a feed url from a given url, to be used by the formslibs
166+
* filter function
167+
*
168+
* Uses simplepie with autodiscovery set to maximum level to try and find
169+
* a feed to subscribe to.
170+
* See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level
171+
*
172+
* @param string $url URL to autodiscover a url
173+
* @return string URL of feed or original url if none found
174+
*/
175+
public static function autodiscover_feed_url($url): string {
176+
$rss = new moodle_simplepie();
177+
$rss->set_feed_url($url);
178+
$rss->set_autodiscovery_level();
179+
// When autodiscovering an RSS feed, simplepie will try lots of
180+
// rss links on a page, so set the timeout high.
181+
$rss->set_timeout(20);
182+
$rss->init();
183+
184+
if ($rss->error()) {
185+
return $url;
186+
}
187+
188+
// Return URL without quoting..
189+
$discoveredurl = new moodle_url($rss->subscribe_url());
190+
return $discoveredurl->out(false);
191+
}
192+
}

db/upgrade.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,68 @@ function xmldb_block_rss_thumbnails_upgrade($oldversion) {
3434
// Automatically generated Moodle v3.9.0 release upgrade line.
3535
// Put any upgrade step following this.
3636
$dbman = $DB->get_manager();
37-
if ($oldversion < 2022091502) {
37+
if ($oldversion < 2022111004) {
3838

39-
// Define table block_rss_thumbnails to be created.
40-
$table = new xmldb_table('block_rss_thumbnails');
41-
42-
// Adding fields to table block_rss_thumbnails.
43-
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
44-
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
45-
$table->add_field('title', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
46-
$table->add_field('preferredtitle', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
47-
$table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
48-
$table->add_field('shared', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
49-
$table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
50-
$table->add_field('skiptime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
51-
$table->add_field('skipuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
52-
53-
// Adding keys to table block_rss_thumbnails.
54-
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
39+
$table = create_block_rss_thumbnails_table();
40+
$params = ['blockname' => 'rss_thumbnails'];
5541

5642
// Conditionally launch create table for block_rss_thumbnails.
5743
if (!$dbman->table_exists($table)) {
5844
$dbman->create_table($table);
5945
}
46+
if ($DB->record_exists('block_instances', $params)) {
47+
if ($dbman->table_exists('block_rss_client')) {
48+
$rssfeeds = $DB->get_records('block_rss_client');
49+
$DB->insert_records('block_rss_thumbnails', $rssfeeds);
50+
}
51+
$blockinstances = $DB->get_records('block_instances', ['blockname' => 'rss_thumbnails']);
52+
foreach ($blockinstances as $blockinstance) {
53+
// Access to block's configdata.
54+
$blockconfig = unserialize(base64_decode($blockinstance->configdata));
55+
56+
// Update caroussel speed variable that changed into carousseldelay for semantical reasons.
57+
$blockconfig->carousseldelay = $blockconfig->carousselspeed;
58+
$blockconfig->numentries = $blockconfig->shownumentries;
59+
60+
$newblockinstance = clone $blockinstance;
61+
// Re-serialize block's configdata.
62+
$newblockinstance->configdata = base64_encode(serialize($blockconfig));
63+
64+
$DB->update_record('block_instances', $newblockinstance);
65+
}
66+
}
6067

6168
// Rss_thumbnails savepoint reached.
62-
upgrade_block_savepoint(true, 2022091502, 'rss_thumbnails');
69+
upgrade_block_savepoint(true, 2022111004, 'rss_thumbnails');
6370
}
6471

6572
// Automatically generated Moodle v4.0.0 release upgrade line.
6673
// Put any upgrade step following this.
6774

6875
return true;
6976
}
77+
78+
/**
79+
* Creates an empty block_rss_thumbnails table by adding to it all the fields it needs and setting up the right primary key.
80+
*
81+
* @return xmldb_table
82+
*/
83+
function create_block_rss_thumbnails_table(): xmldb_table {
84+
// Define table block_rss_thumbnails to be created.
85+
$table = new xmldb_table('block_rss_thumbnails');
86+
87+
// Adding fields to table block_rss_thumbnails.
88+
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
89+
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
90+
$table->add_field('title', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
91+
$table->add_field('preferredtitle', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
92+
$table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
93+
$table->add_field('shared', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
94+
$table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
95+
$table->add_field('skiptime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
96+
$table->add_field('skipuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
97+
98+
// Adding keys to table block_rss_thumbnails.
99+
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
100+
return $table;
101+
}

edit_form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class block_rss_thumbnails_edit_form extends block_edit_form {
4545
* @return void
4646
*/
4747
protected function specific_definition($mform) {
48-
global $CFG, $DB, $USER, $SESSION;
48+
global $CFG, $DB, $USER;
4949

5050
// Fields for editing block contents.
5151
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));

0 commit comments

Comments
 (0)