Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mackensen committed Feb 10, 2025
1 parent 990bb05 commit 53524d2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
26 changes: 12 additions & 14 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace tool_rssfeeds;

defined('MOODLE_INTERNAL') || die();

/**
* Helper functions for tool_rssfeeds.
*
Expand All @@ -42,15 +40,15 @@ public static function delete_feed($feedid) {
global $DB;

$feed = self::get_feed($feedid);
$DB->delete_records('block_rss_client', array('id' => $feedid));
$DB->delete_records('block_rss_client', ['id' => $feedid]);

// Reprocess block configdata.
if (empty($feed[$feedid]->instances)) {
return;
}

foreach ($feed[$feedid]->instances as $instance) {
$block = $DB->get_record('block_instances', array('id' => $instance));
$block = $DB->get_record('block_instances', ['id' => $instance]);
$configdata = unserialize(base64_decode($block->configdata));
if (is_array($configdata->rssid) && ($key = array_search($feedid, $configdata->rssid)) !== false) {
unset($configdata->rssid[$key]);
Expand All @@ -70,7 +68,7 @@ public static function delete_feed($feedid) {
public static function get_feed($feedid) {
global $DB;

$rssfeed = $DB->get_records('block_rss_client', array('id' => $feedid));
$rssfeed = $DB->get_records('block_rss_client', ['id' => $feedid]);
return self::get_block_instances($rssfeed);
}

Expand Down Expand Up @@ -103,16 +101,16 @@ private static function get_block_instances($feeds) {

// Prep the items in the feeds array. We need to store course ids and block instance ids.
foreach ($feeds as $id => $feed) {
$feeds[$id]->courses = array();
$feeds[$id]->instances = array();
$feeds[$id]->courses = [];
$feeds[$id]->instances = [];
}

// Get all the block instances.
$blocksql = "SELECT bi.id, c.id as courseid, c.fullname, bi.configdata
FROM {course} c INNER JOIN {context} ctx ON c.id=ctx.instanceid INNER JOIN
{block_instances} bi ON ctx.id=bi.parentcontextid WHERE ctx.contextlevel=50
AND bi.blockname=?";
$blocks = $DB->get_records_sql($blocksql, array('rss_client'));
$blocks = $DB->get_records_sql($blocksql, ['rss_client']);

foreach ($blocks as $block) {
$configdata = unserialize(base64_decode($block->configdata));
Expand Down Expand Up @@ -142,21 +140,21 @@ public static function display($feeds) {
global $OUTPUT;

$table = new \html_table();
$table->head = array(
$table->head = [
get_string('feedurl', 'block_rss_client'),
get_string('feedowner', 'tool_rssfeeds'),
get_string('courses'),
get_string('actions')
);
get_string('actions'),
];
foreach ($feeds as $feed) {
$feedurl = \html_writer::link(
new \moodle_url($feed->url),
$feed->url
);
$courses = array();
$courses = [];
foreach ($feed->courses as $id => $fullname) {
$courses[] = \html_writer::link(
new \moodle_url('/course/view.php', array('id' => $id)),
new \moodle_url('/course/view.php', ['id' => $id]),
$fullname
);
}
Expand All @@ -170,7 +168,7 @@ public static function display($feeds) {
$deleteaction = $OUTPUT->action_icon(
$deleteurl, $deleteicon, new \confirm_action(get_string('deletefeedconfirm', 'tool_rssfeeds')));

$table->data[] = array($feedurl, \html_writer::link($userprofile, fullname($user)), $coursedisplay, $deleteaction);
$table->data[] = [$feedurl, \html_writer::link($userprofile, fullname($user)), $coursedisplay, $deleteaction];
}
return $table;
}
Expand Down
2 changes: 0 additions & 2 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace tool_rssfeeds\privacy;

defined('MOODLE_INTERNAL') || die();

/**
* Privacy subsystem for tool_rssfeeds implementing null_provider.
*
Expand Down
54 changes: 30 additions & 24 deletions tests/rss_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_rssfeeds;

use stdClass;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand All @@ -36,7 +40,7 @@
* @copyright 2018 Lafayette College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_rssfeeds_rss_manager_test extends advanced_testcase {
final class rss_manager_test extends \advanced_testcase {

/** @var int The id for feed1 */
private $guestfeedid;
Expand All @@ -53,55 +57,57 @@ class tool_rssfeeds_rss_manager_test extends advanced_testcase {
* General setup for PHPUnit testing
*/
protected function setUp(): void {
parent::setUp();

global $DB;

$this->resetAfterTest(true);

// Create a test user.
$this->enduser = $this->getDataGenerator()->create_user(array(
$this->enduser = $this->getDataGenerator()->create_user([
'username' => 'enduser',
'firstname' => 'End',
'lastname' => 'User',
'email' => 'enduser@example.com'
));
'email' => 'enduser@example.com',
]);

// Create an RSS feed for the guest user.
$record = (object) array(
$record = (object) [
'userid' => 1,
'title' => 'Feed 1',
'preferredtitle' => '',
'description' => 'Feed 1 belongs to the guest user.',
'shared' => 0,
'url' => 'http://example.com/feed',
);
];
$this->guestfeedid = $DB->insert_record('block_rss_client', $record);

// Create an RSS feed for enduser.
$record = (object) array(
$record = (object) [
'userid' => $this->enduser->id,
'title' => 'Enduser Feed',
'preferredtitle' => '',
'description' => 'End User owns this feed.',
'shared' => 0,
'url' => 'http://example.com/atom',
);
];
$this->enduserfeedid = $DB->insert_record('block_rss_client', $record);

// Create a couple courses.
$this->firstcourse = $this->getDataGenerator()->create_course(array(
$this->firstcourse = $this->getDataGenerator()->create_course([
'fullname' => "A first course",
'shortname' => "first",
));
$this->secondcourse = $this->getDataGenerator()->create_course(array(
]);
$this->secondcourse = $this->getDataGenerator()->create_course([
'fullname' => "A second course",
'shortname' => "second",
));
]);

// Add a couple RSS blocks.
$configdata = new stdClass;
$configdata->displaydescription = 0;
$configdata->shownumentries = 2;
$configdata->rssid = array($this->enduserfeedid);
$configdata->rssid = [$this->enduserfeedid];
$configdata->title = '';
$configdata->block_rss_client_show_channel_link = 0;
$configdata->block_rss_client_show_channel_image = 0;
Expand Down Expand Up @@ -137,9 +143,9 @@ protected function setUp(): void {
/**
* Test the get_feed helper function.
*/
public function test_get_feed() {
public function test_get_feed(): void {
// Confirm that we can get just the feed we want.
$feed = \tool_rssfeeds\helper::get_feed($this->guestfeedid);
$feed = helper::get_feed($this->guestfeedid);
$this->assertEquals(1, count($feed));
$this->assertEquals(1, $feed[$this->guestfeedid]->userid);
$this->assertEquals('Feed 1', $feed[$this->guestfeedid]->title);
Expand All @@ -149,19 +155,19 @@ public function test_get_feed() {
/**
* Test the get_feeds helper function.
*/
public function test_get_feeds() {
public function test_get_feeds(): void {
// Confirm that we can get all feeds.
$feeds = \tool_rssfeeds\helper::get_feeds();
$feeds = helper::get_feeds();
$this->assertEquals(2, count($feeds));
$this->assertEquals('http://example.com/atom', $feeds[$this->enduserfeedid]->url);
}

/**
* Test the display helper function.
*/
public function test_display() {
$feeds = \tool_rssfeeds\helper::get_feeds();
$table = \tool_rssfeeds\helper::display($feeds);
public function test_display(): void {
$feeds = helper::get_feeds();
$table = helper::display($feeds);
$this->assertInstanceOf('html_table', $table);
$this->assertEquals(2, count($table->data));
$this->assertEquals('Not used in any courses', $table->data[0][2]);
Expand All @@ -173,13 +179,13 @@ public function test_display() {
/**
* Test the delete_feed helper function.
*/
public function test_delete_feed() {
public function test_delete_feed(): void {
global $DB;

\tool_rssfeeds\helper::delete_feed($this->enduserfeedid);
helper::delete_feed($this->enduserfeedid);
$this->assertEquals(1, $DB->count_records('block_rss_client'));
$feeds = \tool_rssfeeds\helper::get_feeds();
$table = \tool_rssfeeds\helper::display($feeds);
$feeds = helper::get_feeds();
$table = helper::display($feeds);
$this->assertEquals(1, count($table->data));
}
}

0 comments on commit 53524d2

Please sign in to comment.