Skip to content

Commit

Permalink
Merge pull request #4 from learnweb/improvement/overall
Browse files Browse the repository at this point in the history
Improvement/overall
  • Loading branch information
TamaroWalter authored Sep 18, 2024
2 parents 2e3cad3 + 4d5db86 commit 1d9f6a3
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
Expand Down Expand Up @@ -176,7 +176,7 @@ jobs:
- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
townsquareexpansion/*
!townsquareexpansion/readme.md
!townsquareexpansion/README.md
4 changes: 2 additions & 2 deletions classes/plugininfo/townsquareexpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

/**
* TODO: Add description.
* File to manage subplugins from type townsquareexpansion.
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
Expand All @@ -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.
}

/**
Expand Down
9 changes: 8 additions & 1 deletion classes/townsquaresupportinterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@
*/
namespace local_townsquaresupport;

/**
* Interface that need to be implemented
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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.
Expand Down
2 changes: 1 addition & 1 deletion db/subplugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

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

$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion'];
$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion'];
6 changes: 2 additions & 4 deletions lang/en/local_townsquaresupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

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

// Core strings for the installation.
$string['pluginname'] = 'Townsquare support plugin';
$string['pluginname'] = 'Townsquare support';

// Subplugin strings.
$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';
31 changes: 27 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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
* @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.
*
* @return array
*/
function townsquaresupport_get_subplugin_events() {

Expand All @@ -35,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;
Expand Down
76 changes: 76 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
// 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 <http://www.gnu.org/licenses/>.

/**
* 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 bool
*/
function townsquaresupport_check_subplugin_events($subevents): bool {
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);
}
99 changes: 99 additions & 0 deletions tests/eventcheck_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
// 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 <http://www.gnu.org/licenses/>.

/**
* 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(true, townsquaresupport_check_subplugin_events($this->testdata->subevents2));
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents3));
}

// 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', ];

// 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 = [(object)$incorrecteevent1, (object)$correctevent1];
$this->testdata->subevents2 = [(object)$correctevent1, (object)$correctevent2];
$this->testdata->subevents3 = ['arraykey' => 'incorrectsubevent'];
}

}
39 changes: 39 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="lib/phpunit/phpunit.xsd"
bootstrap="lib/phpunit/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
backupGlobals="false"
backupStaticAttributes="false"
cacheResult="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
beStrictAboutTestsThatDoNotTestAnything="false"
beStrictAboutOutputDuringTests="true"
>

<php>
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
<const name="PHPUNIT_SEQUENCE_START" value="195000"/>

<!--Following constants instruct tests to fetch external test files from alternative location
or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/>
uncomment and alter to fetch external test files from alternative location-->
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/>
uncomment and alter to fetch external test files from alternative location-->
</php>

<testsuites>
<testsuite name="local_townsquaresupport_testsuite">
<directory suffix="_test.php">local/townsquaresupport/tests</directory>
</testsuite>
</testsuites>

</phpunit>

0 comments on commit 1d9f6a3

Please sign in to comment.