Skip to content

Commit 1d9f6a3

Browse files
authored
Merge pull request #4 from learnweb/improvement/overall
Improvement/overall
2 parents 2e3cad3 + 4d5db86 commit 1d9f6a3

10 files changed

+256
-15
lines changed

.github/workflows/moodle-ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
5151
- name: Initialise moodle-plugin-ci
5252
run: |
53-
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
53+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
5454
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
5555
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
5656
sudo locale-gen en_AU.UTF-8
@@ -176,7 +176,7 @@ jobs:
176176
177177
- name: Initialise moodle-plugin-ci
178178
run: |
179-
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
179+
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
180180
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
181181
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
182182
sudo locale-gen en_AU.UTF-8

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
townsquareexpansion/*
2-
!townsquareexpansion/readme.md
32
!townsquareexpansion/README.md

classes/plugininfo/townsquareexpansion.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
1616

1717
/**
18-
* TODO: Add description.
18+
* File to manage subplugins from type townsquareexpansion.
1919
*
2020
* @package local_townsquaresupport
2121
* @copyright 2024 Tamaro Walter
@@ -36,7 +36,7 @@ class townsquareexpansion extends base {
3636
* @return bool
3737
*/
3838
public function is_uninstall_allowed(): bool {
39-
return true;
39+
return true; // A subplugin can be deleted without condition.
4040
}
4141

4242
/**

classes/townsquaresupportinterface.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@
3131
*/
3232
namespace local_townsquaresupport;
3333

34+
/**
35+
* Interface that need to be implemented
36+
*
37+
* @package local_townsquaresupport
38+
* @copyright 2024 Tamaro Walter
39+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40+
*/
3441
interface townsquaresupportinterface {
3542
/**
3643
* Function to gather the events
3744
* Every event must gain sufficient data so that townsquare can build a letter from it.
38-
* The array should contain following information:
45+
* The array must contain following information:
3946
* [courseid] => int Course ID from where the content comes from.
4047
* [modulename] => string Name of the activity module.
4148
* [instancename] => string Name of the instance that shows the notification.

db/subplugins.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424

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

27-
$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion'];
27+
$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion'];

lang/en/local_townsquaresupport.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525

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

28-
// Core strings for the installation.
29-
$string['pluginname'] = 'Townsquare support plugin';
3028
$string['pluginname'] = 'Townsquare support';
31-
32-
// Subplugin strings.
29+
$string['plugintitle'] = 'Townsquare support plugin';
30+
$string['subpluginerror'] = 'Error while retrieving events from an subplugin. There seems to be a coding error in the subplugin {$a->subpluginname}.';
3331
$string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion';
3432
$string['subplugintype_townsquareexpansion_plural'] = 'Townsquare event expansions';

lib.php

+27-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,28 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
namespace local_townsquaresupport;
18-
1917
/**
2018
* Function to get the events from every subplugin that extends the town square.
2119
*
2220
* As every subplugin from townsquaresupport follows the same structure and has the get_event method located in the same
2321
* place, this function can access it directly.
22+
* @package local_townsquaresupport
23+
* @copyright 2024 Tamaro Walter
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
27+
namespace local_townsquaresupport;
28+
29+
defined('MOODLE_INTERNAL') || die();
30+
31+
global $CFG;
32+
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
33+
34+
/**
35+
* Core function of the townsquaresupport plugin. Retrieves all events from the subplugins and makes them available
36+
* to the townsquare block.
37+
*
38+
* @return array
2439
*/
2540
function townsquaresupport_get_subplugin_events() {
2641

@@ -35,8 +50,16 @@ function townsquaresupport_get_subplugin_events() {
3550
$classstring = "\\townsquareexpansion_" . $expansionname . "\\" . $expansionname;
3651
$expansionclass = new $classstring();
3752

38-
// Get the events from the subplugin and add it to the events array.
39-
$events = array_merge($events, $expansionclass->get_events());
53+
// Get the events from the subplugin.
54+
$subpluginevents = $expansionclass->get_events();
55+
56+
// Check if the events meet the requirements of the interface.
57+
if (townsquaresupport_check_subplugin_events($subpluginevents)) {
58+
$events = array_merge($events, $subpluginevents);
59+
} else {
60+
// Throw an error as there is an error in the subplugin code.
61+
throw new \moodle_exception('subpluginerror', 'local_townsquaresupport', '', ['subpluginname' => $expansionname]);
62+
}
4063

4164
}
4265
return $events;

locallib.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
* Internal library of functions for the townsquaresupport plugin
19+
*
20+
* @package local_townsquaresupport
21+
* @copyright 2024 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace local_townsquaresupport;
26+
27+
/**
28+
* Helper function for townsquaresupport_get_subplugin_events, that checks if an amount
29+
* of events have all the required attributes from the townsquaresupport interface.
30+
*
31+
* @param array $subevents
32+
* @return bool
33+
*/
34+
function townsquaresupport_check_subplugin_events($subevents): bool {
35+
if (!gettype($subevents == 'array')) {
36+
return false;
37+
}
38+
39+
if ($subevents == []) {
40+
// If no events are available, then everything is okay.
41+
return true;
42+
} else {
43+
44+
// Check every event.
45+
foreach ($subevents as $event) {
46+
if (gettype($event) != 'object') {
47+
return false;
48+
}
49+
50+
// Check if all variables are set.
51+
$issetcheck = townsquaresupport_check_isset($event, 'courseid') &&
52+
townsquaresupport_check_isset($event, 'modulename') &&
53+
townsquaresupport_check_isset($event, 'instancename') &&
54+
townsquaresupport_check_isset($event, 'content') &&
55+
townsquaresupport_check_isset($event, 'timestart') &&
56+
townsquaresupport_check_isset($event, 'coursemoduleid') &&
57+
townsquaresupport_check_isset($event, 'eventtype');
58+
59+
if (!$issetcheck) {
60+
return false;
61+
}
62+
}
63+
}
64+
return true;
65+
}
66+
67+
/**
68+
* Helper function for check_subplugin_events function that proves if a variable is set in an array.
69+
*
70+
* @param array $event Event that is checked.
71+
* @param string $variablename Name of the variable
72+
* @return bool
73+
*/
74+
function townsquaresupport_check_isset($event, $variablename): bool {
75+
return isset($event->$variablename);
76+
}

tests/eventcheck_test.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
* Unit tests for the local_townsquaresupport.
19+
*
20+
* @package local_townsquaresupport
21+
* @copyright 2024 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
namespace local_townsquaresupport;
25+
26+
defined('MOODLE_INTERNAL') || die();
27+
28+
global $CFG;
29+
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
30+
31+
/**
32+
* PHPUnit tests for testing the logic of proving if subplugin events satisfy the requirements of the townsquaresupport interface.
33+
*
34+
* @package local_townsquaresupport
35+
* @copyright 2024 Tamaro Walter
36+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37+
*
38+
* @covers ::\local_townsquaresupport\townsquaresupport_check_subplugin_events()
39+
*/
40+
final class eventcheck_test extends \advanced_testcase {
41+
42+
// Attributes.
43+
44+
/** @var object The data that will be used for testing */
45+
private $testdata;
46+
47+
48+
// Construct functions.
49+
50+
public function setUp(): void {
51+
$this->testdata = new \stdClass();
52+
$this->resetAfterTest();
53+
$this->helper_test_set_up();
54+
}
55+
56+
public function tearDown(): void {
57+
$this->testdata = null;
58+
}
59+
60+
// Tests.
61+
62+
/**
63+
* Test, if the check_subplugin_events function works correctly.
64+
* @return void
65+
*/
66+
public function test_checkevents(): void {
67+
// Test the subevents.
68+
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents1));
69+
$this->assertEquals(true, townsquaresupport_check_subplugin_events($this->testdata->subevents2));
70+
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents3));
71+
}
72+
73+
// Helper functions.
74+
75+
/**
76+
* Helper function that sets up the testdata.
77+
* @return void
78+
*/
79+
private function helper_test_set_up(): void {
80+
// Build different arrays of events that are incorrect (and one correct array).
81+
82+
// First incorrect event: variable 'content' is missing.
83+
$incorrecteevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1',
84+
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
85+
86+
// Two completely correct events.
87+
$correctevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
88+
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
89+
90+
$correctevent2 = ['courseid' => 15, 'modulename' => 'pluginname', 'instancename' => 'instance2', 'content' => 'bye',
91+
'timestart' => 123456787, 'coursemoduleid' => 16, 'eventtype' => 'eventtypeone', ];
92+
93+
// Build different combinations of the events.
94+
$this->testdata->subevents1 = [(object)$incorrecteevent1, (object)$correctevent1];
95+
$this->testdata->subevents2 = [(object)$correctevent1, (object)$correctevent2];
96+
$this->testdata->subevents3 = ['arraykey' => 'incorrectsubevent'];
97+
}
98+
99+
}

tests/phpunit.xml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="lib/phpunit/phpunit.xsd"
5+
bootstrap="lib/phpunit/bootstrap.php"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
backupGlobals="false"
11+
backupStaticAttributes="false"
12+
cacheResult="false"
13+
stopOnError="false"
14+
stopOnFailure="false"
15+
stopOnIncomplete="false"
16+
stopOnSkipped="false"
17+
beStrictAboutTestsThatDoNotTestAnything="false"
18+
beStrictAboutOutputDuringTests="true"
19+
>
20+
21+
<php>
22+
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
23+
<const name="PHPUNIT_SEQUENCE_START" value="195000"/>
24+
25+
<!--Following constants instruct tests to fetch external test files from alternative location
26+
or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
27+
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/>
28+
uncomment and alter to fetch external test files from alternative location-->
29+
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/>
30+
uncomment and alter to fetch external test files from alternative location-->
31+
</php>
32+
33+
<testsuites>
34+
<testsuite name="local_townsquaresupport_testsuite">
35+
<directory suffix="_test.php">local/townsquaresupport/tests</directory>
36+
</testsuite>
37+
</testsuites>
38+
39+
</phpunit>

0 commit comments

Comments
 (0)