Skip to content

Commit

Permalink
PM-45594 allow overriding kialo target url
Browse files Browse the repository at this point in the history
To simplify our test site management, this allows overriding the target kialo url on running test instances, so no redeployment is needed. This way different Kialo instances can be tested using the same Moodle instance.
  • Loading branch information
mk-kialo committed Oct 4, 2024
1 parent 468f68a commit a9d6a41
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
3 changes: 3 additions & 0 deletions classes/admin/kialo_configcheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class kialo_configcheckbox extends admin_setting_configcheckbox {
*/
public function force_readonly(bool $forcereadonly) {
$this->forcereadonly = $forcereadonly;

// If the checkbox is read-only, it should not be saved. It would be reset to its default otherwise when saving.
$this->nosave = $forcereadonly;
}

/**
Expand Down
21 changes: 18 additions & 3 deletions classes/kialo_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,27 @@ public static function get_release(): string {
* @return array|string
*/
public function get_tool_url() {
$targeturlfromconfig = get_config('mod_kialo', 'kialourl'); // See settings.php.
$targeturlfromenv = getenv('TARGET_KIALO_URL');
if (!empty($targeturlfromenv)) {
return $targeturlfromenv;

if (!empty($targeturlfromconfig)) {
$kialourl = $targeturlfromconfig;

// Is the URL valid?
if (!filter_var($kialourl, FILTER_VALIDATE_URL)) {
throw new \dml_exception("Invalid Kialo URL: $kialourl");
}
} else if (!empty($targeturlfromenv)) {
$kialourl = $targeturlfromenv;
} else {
return "https://www.kialo-edu.com";
$kialourl = "https://www.kialo-edu.com";
}

// Ensure $kialourl is a proper URL and does not have a trailing slash.
if (!preg_match('/^https?:\/\//', $kialourl)) {
$kialourl = "https://" . $kialourl;
}
return rtrim($kialourl, '/');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lang/en/kialo.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
$string['kialofieldset'] = 'Kialo Fieldset';
$string['kialoname'] = 'Activity Name';
$string['kialosettings'] = 'Settings';
$string['kialourl'] = 'Kialo URL';
$string['kialourl_desc'] = 'The URL of the Kialo instance to use.';
$string['modulename'] = 'Kialo Discussion';
$string['modulename_help'] = 'The Kialo Discussion activity allows you to include a Kialo discussion in your Moodle course. Students can participate in the discussion directly from Moodle, without having to manually create Kialo accounts. Kialo discussions are a great way to teach and train critical thinking, argumentation and to facilitate thoughtful classroom discussions.';
$string['modulename_link'] = 'https://support.kialo-edu.com/en/hc/moodle';
Expand Down
25 changes: 20 additions & 5 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,38 @@

// Terms and conditions need to have been accepted before the activity can be used.
if ($ADMIN->fulltree) {
$setting = new kialo_configcheckbox(
$termswerealreadyaccepted = get_config('mod_kialo', 'acceptterms') == 1;
$acceptterms = new kialo_configcheckbox(
'mod_kialo/acceptterms',
new lang_string('acceptterms', 'mod_kialo'),
new lang_string('acceptterms_desc', 'mod_kialo', [
"terms" => MOD_KIALO_TERMS_LINK,
"privacy" => MOD_KIALO_PRIVACY_LINK,
"data_security" => MOD_KIALO_DATA_SECURITY_LINK,
]),
0
$termswerealreadyaccepted ? 1 : 0,
);

// Once the terms have been accepted, they cannot be unaccepted.
$setting->force_readonly(get_config('mod_kialo', 'acceptterms'));
if ($termswerealreadyaccepted) {
$acceptterms->force_readonly(get_config('mod_kialo', 'acceptterms'));
}

// Enable the module once the terms have been accepted.
$setting->set_updatedcallback('kialo_update_visibility_depending_on_accepted_terms');
$acceptterms->set_updatedcallback('kialo_update_visibility_depending_on_accepted_terms');

/** @var admin_settingpage $settings */
$settings->add($setting);
$settings->add($acceptterms);

// For internal Kialo use only: Allow changing the Kialo target URL.
if (!empty(getenv('TARGET_KIALO_URL'))) {
$kialourl = new admin_setting_configtext(
'mod_kialo/kialourl',
new lang_string('kialourl', 'mod_kialo'),
new lang_string('kialourl_desc', 'mod_kialo'),
'https://www.kialo-edu.com',
);

$settings->add($kialourl);
}
}
27 changes: 22 additions & 5 deletions tests/classes/kialo_config_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,44 @@
* Tests the mod_kialo config.
*/
final class kialo_config_test extends \advanced_testcase {
public function setUp(): void {
parent::setUp();

$this->resetAfterTest();

// The variable TARGET_KIALO_URL is only set in Kialo test environments. By default it's not defined.
putenv("TARGET_KIALO_URL=");
set_config('kialourl', null, 'mod_kialo');
}

/**
* Tests the default tool URL.
* @covers \mod_kialo\kialo_config::get_instance::get_tool_url
*/
public function test_default_tool_url(): void {
// The variable TARGET_KIALO_URL is only set in Kialo test environments. By default it's not defined.
putenv("TARGET_KIALO_URL=");

// In production, kialo-edu.com is always the endpoint for our plugin.
$this->assertEquals("https://www.kialo-edu.com", kialo_config::get_instance()->get_tool_url());
}

/**
* Tests that the tool URL can be overridden.
* Tests that the tool URL can be overridden via environment variable.
* @covers \mod_kialo\kialo_config::get_instance::get_tool_url
*/
public function test_custom_tool_url(): void {
public function test_custom_tool_url_via_env(): void {
putenv("TARGET_KIALO_URL=http://localhost:5000");
$this->assertEquals("http://localhost:5000", kialo_config::get_instance()->get_tool_url());
}

/**
* Tests that the tool URL can be overridden via environment config, and it that is has precedence over the env var.
* @covers \mod_kialo\kialo_config::get_instance::get_tool_url
*/
public function test_custom_tool_url_via_config(): void {
set_config('kialourl', 'https://www.example.com', 'mod_kialo');
putenv("TARGET_KIALO_URL=http://localhost:5000");
$this->assertEquals("https://www.example.com", kialo_config::get_instance()->get_tool_url());
}

/**
* Tests private key generation.
* @covers \mod_kialo\kialo_config::get_instance::get_platform_keychain
Expand Down

0 comments on commit a9d6a41

Please sign in to comment.