-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
294 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_dynamic_cohorts\external; | ||
|
||
use context_system; | ||
use core_external\external_api; | ||
use core_external\external_function_parameters; | ||
use core_external\external_multiple_structure; | ||
use core_external\external_value; | ||
use core_external\external_single_structure; | ||
use Throwable; | ||
use tool_dynamic_cohorts\event\rule_updated; | ||
use tool_dynamic_cohorts\rule; | ||
use invalid_parameter_exception; | ||
use tool_dynamic_cohorts\rule_manager; | ||
|
||
/** | ||
* Rules external APIs. | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class rules extends external_api { | ||
|
||
/** | ||
* Describes the parameters for delete_rule webservice. | ||
* | ||
* @return external_function_parameters | ||
*/ | ||
public static function delete_rules_parameters(): external_function_parameters { | ||
return new external_function_parameters( | ||
[ | ||
'ruleids' => new external_multiple_structure(new external_value(PARAM_INT, 'Rule IDs'), | ||
'List of rule ids to delete.'), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Deletes provided rules. | ||
* | ||
* @param array $ruleids Rule IDs. | ||
* @return array | ||
*/ | ||
public static function delete_rules(array $ruleids): array { | ||
global $DB; | ||
|
||
self::validate_parameters(self::delete_rules_parameters(), ['ruleids' => $ruleids]); | ||
|
||
$context = context_system::instance(); | ||
self::validate_context($context); | ||
require_capability('tool/dynamic_cohorts:manage', $context); | ||
|
||
// We would like to treat deletion for multiple rules as one operation. | ||
// If one failed we would like to fail whole call and roll back. | ||
$transaction = $DB->start_delegated_transaction(); | ||
try { | ||
foreach ($ruleids as $ruleid) { | ||
$rule = rule::get_record(['id' => (int) $ruleid]); | ||
if (empty($rule)) { | ||
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid); | ||
} | ||
rule_manager::delete_rule($rule); | ||
//$transaction->allow_commit(); | ||
} | ||
} catch (throwable $ex) { | ||
$transaction->rollback($ex); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Returns description of method result value. | ||
* | ||
* @return external_single_structure | ||
*/ | ||
public static function delete_rules_returns(): external_single_structure { | ||
return new external_single_structure([]); | ||
} | ||
|
||
/** | ||
* Enable or disable rule. | ||
* | ||
* @return external_function_parameters | ||
*/ | ||
public static function toggle_status_parameters(): external_function_parameters { | ||
return new external_function_parameters([ | ||
'ruleid' => new external_value(PARAM_INT, 'The rule ID to toggle'), | ||
]); | ||
} | ||
|
||
/** | ||
* Toggle rule. | ||
* | ||
* @param int $ruleid Rule ID. | ||
* @return array | ||
*/ | ||
public static function toggle_status(int $ruleid): array { | ||
self::validate_parameters(self::delete_rules_parameters(), ['ruleid' => $ruleid]); | ||
|
||
self::validate_context(context_system::instance()); | ||
require_capability('tool/dynamic_cohorts:manage', context_system::instance()); | ||
|
||
$rule = rule::get_record(['id' => $ruleid]); | ||
if (empty($rule)) { | ||
throw new invalid_parameter_exception('Rule does not exist.'); | ||
} | ||
|
||
if ($rule->is_broken()) { | ||
// Disable broken rule | ||
$rule->set('enabled', 0); | ||
$rule->save(); | ||
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger(); | ||
|
||
throw new invalid_parameter_exception('A broken rule can\'t be enabled'); | ||
} | ||
|
||
$newvalue = (int) !$rule->is_enabled(); | ||
$rule->set('enabled', $newvalue); | ||
$rule->save(); | ||
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger(); | ||
|
||
return [ | ||
'ruleid' => $rule->get('id'), | ||
'enabled' => $newvalue, | ||
]; | ||
} | ||
|
||
/** | ||
* Returns description of method result value. | ||
* | ||
* @return external_single_structure | ||
*/ | ||
public static function toggle_status_returns(): external_single_structure { | ||
return new external_single_structure([ | ||
'ruleid' => new external_value(PARAM_INT, 'The rule ID'), | ||
'enabled' => new external_value(PARAM_INT, 'New status for the rule'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_dynamic_cohorts\external; | ||
|
||
use externallib_advanced_testcase; | ||
use tool_dynamic_cohorts\rule; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | ||
|
||
/** | ||
* Tests for matching users external APIs . | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \tool_dynamic_cohorts\external\rules | ||
*/ | ||
class rules_test extends externallib_advanced_testcase { | ||
|
||
/** | ||
* Test exception if rule is not exist. | ||
*/ | ||
public function test_delete_rules_exception_on_invalid_rule() { | ||
$this->resetAfterTest(); | ||
|
||
$this->setAdminUser(); | ||
$this->expectException(\invalid_parameter_exception::class); | ||
$this->expectExceptionMessage('Rule does not exist. ID: 777'); | ||
|
||
rules::delete_rules([777]); | ||
} | ||
|
||
/** | ||
* Test required permissions. | ||
*/ | ||
public function test_delete_rules_permissions() { | ||
$this->resetAfterTest(); | ||
$user = $this->getDataGenerator()->create_user(); | ||
$this->setUser($user); | ||
|
||
$this->expectException(\required_capability_exception::class); | ||
$this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Manage rules).'); | ||
|
||
rules::delete_rules([777]); | ||
} | ||
|
||
/** | ||
* Test can get total. | ||
*/ | ||
public function test_exception_delete_rules_when_one_is_invalid() { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
$rule = new rule(0, (object)['name' => 'Test rule 1']); | ||
$rule->save(); | ||
|
||
$this->expectException(\invalid_parameter_exception::class); | ||
$this->expectExceptionMessage('Rule does not exist. ID: 777'); | ||
|
||
rules::delete_rules([$rule->get('id'), 777]); | ||
} | ||
|
||
/** | ||
* Test can get total. | ||
*/ | ||
public function test_delete_rules_keep_rules_when_one_is_invalid() { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
$rule1 = new rule(0, (object)['name' => 'Test rule 1']); | ||
$rule1->save(); | ||
|
||
$rule2 = new rule(0, (object)['name' => 'Test rule 2']); | ||
$rule2->save(); | ||
|
||
$this->assertCount(2, rule::get_records()); | ||
|
||
$this->expectException(\required_capability_exception::class); | ||
$this->expectExceptionMessage('Rule does not exist. ID: 777'); | ||
|
||
try { | ||
rules::delete_rules([$rule1->get('id'), $rule2->get('id'), 777]); | ||
} catch (\invalid_parameter_exception $exception) { | ||
$this->assertSame( | ||
'Invalid parameter value detected (Rule does not exist. ID: 777)', | ||
$exception->getMessage() | ||
); | ||
$this->assertCount(2, rule::get_records()); | ||
} | ||
} | ||
} |