diff --git a/classes/plugininfo/townsquareexpansion.php b/classes/plugininfo/townsquareexpansion.php
index 497d699..51045b8 100644
--- a/classes/plugininfo/townsquareexpansion.php
+++ b/classes/plugininfo/townsquareexpansion.php
@@ -15,7 +15,7 @@
// along with Moodle. If not, see .
/**
- * TODO: Add description.
+ * File to manage subplugins from type townsquareexpansion.
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
@@ -36,7 +36,7 @@ class townsquareexpansion extends base {
* @return bool
*/
public function is_uninstall_allowed(): bool {
- return true;
+ return true; // A subplugin can be deleted without condition.
}
/**
diff --git a/classes/townsquaresupportinterface.php b/classes/townsquaresupportinterface.php
index 86ee091..8caaba9 100644
--- a/classes/townsquaresupportinterface.php
+++ b/classes/townsquaresupportinterface.php
@@ -42,7 +42,7 @@ interface townsquaresupportinterface {
/**
* Function to gather the events
* Every event must gain sufficient data so that townsquare can build a letter from it.
- * The array should contain following information:
+ * The array must contain following information:
* [courseid] => int Course ID from where the content comes from.
* [modulename] => string Name of the activity module.
* [instancename] => string Name of the instance that shows the notification.
diff --git a/db/subplugins.php b/db/subplugins.php
index 46f5980..0f918bd 100644
--- a/db/subplugins.php
+++ b/db/subplugins.php
@@ -24,4 +24,4 @@
defined('MOODLE_INTERNAL') || die();
-$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion'];
+$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion'];
diff --git a/lang/en/local_townsquaresupport.php b/lang/en/local_townsquaresupport.php
index 402f634..f258506 100644
--- a/lang/en/local_townsquaresupport.php
+++ b/lang/en/local_townsquaresupport.php
@@ -27,5 +27,6 @@
$string['pluginname'] = 'Townsquare support';
$string['plugintitle'] = 'Townsquare support plugin';
+$string['subpluginerror'] = 'Error while retrieving events from an subplugin. There seems to be a coding error in the subplugin {$a->subpluginname}.';
$string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion';
$string['subplugintype_townsquareexpansion_plural'] = 'Townsquare event expansions';
diff --git a/lib.php b/lib.php
index a2b3176..ebcd582 100644
--- a/lib.php
+++ b/lib.php
@@ -14,18 +14,23 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see .
-namespace local_townsquaresupport;
-
/**
* Function to get the events from every subplugin that extends the town square.
*
* As every subplugin from townsquaresupport follows the same structure and has the get_event method located in the same
* place, this function can access it directly.
- * @package local_townsquaresupport
+ * @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
+namespace local_townsquaresupport;
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
+
/**
* Core function of the townsquaresupport plugin. Retrieves all events from the subplugins and makes them available
* to the townsquare block.
@@ -45,8 +50,16 @@ function townsquaresupport_get_subplugin_events() {
$classstring = "\\townsquareexpansion_" . $expansionname . "\\" . $expansionname;
$expansionclass = new $classstring();
- // Get the events from the subplugin and add it to the events array.
- $events = array_merge($events, $expansionclass->get_events());
+ // Get the events from the subplugin.
+ $subpluginevents = $expansionclass->get_events();
+
+ // Check if the events meet the requirements of the interface.
+ if (townsquaresupport_check_subplugin_events($subpluginevents)) {
+ $events = array_merge($events, $subpluginevents);
+ } else {
+ // Throw an error as there is an error in the subplugin code.
+ throw new \moodle_exception('subpluginerror', 'local_townsquaresupport', '', ['subpluginname' => $expansionname]);
+ }
}
return $events;
diff --git a/locallib.php b/locallib.php
new file mode 100644
index 0000000..f55d071
--- /dev/null
+++ b/locallib.php
@@ -0,0 +1,76 @@
+.
+
+/**
+ * Internal library of functions for the townsquaresupport plugin
+ *
+ * @package local_townsquaresupport
+ * @copyright 2024 Tamaro Walter
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace local_townsquaresupport;
+
+/**
+ * Helper function for townsquaresupport_get_subplugin_events, that checks if an amount
+ * of events have all the required attributes from the townsquaresupport interface.
+ *
+ * @param array $subevents
+ * @return int
+ */
+function townsquaresupport_check_subplugin_events($subevents): int {
+ if (!gettype($subevents == 'array')) {
+ return false;
+ }
+
+ if ($subevents == []) {
+ // If no events are available, then everything is okay.
+ return true;
+ } else {
+
+ // Check every event.
+ foreach ($subevents as $event) {
+ if (gettype($event) != 'object') {
+ return false;
+ }
+
+ // Check if all variables are set.
+ $issetcheck = townsquaresupport_check_isset($event, 'courseid') &&
+ townsquaresupport_check_isset($event, 'modulename') &&
+ townsquaresupport_check_isset($event, 'instancename') &&
+ townsquaresupport_check_isset($event, 'content') &&
+ townsquaresupport_check_isset($event, 'timestart') &&
+ townsquaresupport_check_isset($event, 'coursemoduleid') &&
+ townsquaresupport_check_isset($event, 'eventtype');
+
+ if (!$issetcheck) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+/**
+ * Helper function for check_subplugin_events function that proves if a variable is set in an array.
+ *
+ * @param array $event Event that is checked.
+ * @param string $variablename Name of the variable
+ * @return bool
+ */
+function townsquaresupport_check_isset($event, $variablename): bool {
+ return isset($event->$variablename);
+}
diff --git a/tests/eventcheck_test.php b/tests/eventcheck_test.php
new file mode 100644
index 0000000..ec3c1ab
--- /dev/null
+++ b/tests/eventcheck_test.php
@@ -0,0 +1,105 @@
+.
+
+/**
+ * Unit tests for the local_townsquaresupport.
+ *
+ * @package local_townsquaresupport
+ * @copyright 2024 Tamaro Walter
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace local_townsquaresupport;
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
+
+/**
+ * PHPUnit tests for testing the logic of proving if subplugin events satisfy the requirements of the townsquaresupport interface.
+ *
+ * @package local_townsquaresupport
+ * @copyright 2024 Tamaro Walter
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ *
+ * @covers ::\local_townsquaresupport\townsquaresupport_check_subplugin_events()
+ */
+final class eventcheck_test extends \advanced_testcase {
+
+ // Attributes.
+
+ /** @var object The data that will be used for testing */
+ private $testdata;
+
+
+ // Construct functions.
+
+ public function setUp(): void {
+ $this->testdata = new \stdClass();
+ $this->resetAfterTest();
+ $this->helper_test_set_up();
+ }
+
+ public function tearDown(): void {
+ $this->testdata = null;
+ }
+
+ // Tests.
+
+ /**
+ * Test, if the check_subplugin_events function works correctly.
+ * @return void
+ */
+ public function test_checkevents(): void {
+ // Test the subevents.
+ $this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents1));
+ $this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents2));
+ $this->assertEquals(true, townsquaresupport_check_subplugin_events($this->testdata->subevents3));
+ $this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents4));
+ }
+
+ // Helper functions.
+
+ /**
+ * Helper function that sets up the testdata.
+ * @return void
+ */
+ private function helper_test_set_up(): void {
+ // Build different arrays of events that are incorrect (and one correct array).
+
+ // First incorrect event: variable 'content' is missing.
+ $incorrecteevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1',
+ 'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
+
+ // Second incorrect event: variable 'courseid' is not a integer.
+ $incorrecteevent2 = ['courseid' => '6', 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
+ 'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
+
+ // Two completely correct events.
+ $correctevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
+ 'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
+
+ $correctevent2 = ['courseid' => 15, 'modulename' => 'pluginname', 'instancename' => 'instance2', 'content' => 'bye',
+ 'timestart' => 123456787, 'coursemoduleid' => 16, 'eventtype' => 'eventtypeone', ];
+
+ // Build different combinations of the events.
+ $this->testdata->subevents1 = [$incorrecteevent1, $correctevent1];
+ $this->testdata->subevents2 = [$incorrecteevent2, $correctevent2];
+ $this->testdata->subevents3 = [$correctevent1, $correctevent2];
+ $this->testdata->subevents4 = ['arraykey' => 'incorrectsubevent'];
+ }
+
+}
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
new file mode 100644
index 0000000..e1b0f34
--- /dev/null
+++ b/tests/phpunit.xml
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ local/townsquaresupport/tests
+
+
+
+