Skip to content

Commit 0f69f6a

Browse files
committed
issue #3: add persistent classes
1 parent 0ed236b commit 0f69f6a

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

classes/condition.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
namespace tool_dynamic_cohorts;
18+
19+
use core\persistent;
20+
21+
/**
22+
* Conditions persistent
23+
*
24+
* @package tool_dynamic_cohorts
25+
* @copyright 2024 Catalyst IT
26+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
class condition extends persistent {
29+
30+
/**
31+
* Table.
32+
*/
33+
const TABLE = 'tool_dynamic_cohorts_c';
34+
35+
/**
36+
* Return the definition of the properties of this model.
37+
*
38+
* @return array
39+
*/
40+
protected static function define_properties() {
41+
return [
42+
'ruleid' => [
43+
'type' => PARAM_INT,
44+
],
45+
'classname' => [
46+
'type' => PARAM_TEXT,
47+
],
48+
'configdata' => [
49+
'type' => PARAM_RAW,
50+
'default' => '{}',
51+
],
52+
'sortorder' => [
53+
'type' => PARAM_INT,
54+
],
55+
];
56+
}
57+
}

classes/rule.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
namespace tool_dynamic_cohorts;
18+
19+
use core\persistent;
20+
21+
/**
22+
* Rules persistent
23+
*
24+
* @package tool_dynamic_cohorts
25+
* @copyright 2024 Catalyst IT
26+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
class rule extends persistent {
29+
30+
/**
31+
* Table.
32+
*/
33+
const TABLE = 'tool_dynamic_cohorts';
34+
35+
/**
36+
* Return the definition of the properties of this model.
37+
*
38+
* @return array
39+
*/
40+
protected static function define_properties() {
41+
return [
42+
'name' => [
43+
'type' => PARAM_TEXT,
44+
],
45+
'description' => [
46+
'type' => PARAM_TEXT,
47+
'default' => null,
48+
'null' => NULL_ALLOWED,
49+
],
50+
'cohortid' => [
51+
'type' => PARAM_INT,
52+
'default' => 0,
53+
],
54+
'enabled' => [
55+
'type' => PARAM_INT,
56+
'default' => 0,
57+
],
58+
'bulkprocessing' => [
59+
'type' => PARAM_INT,
60+
'default' => 0,
61+
],
62+
'broken' => [
63+
'type' => PARAM_INT,
64+
'default' => 0,
65+
],
66+
];
67+
}
68+
69+
/**
70+
* Get a list of condition records for that rule.
71+
*
72+
* @return condition[]
73+
*/
74+
public function get_condition_records(): array {
75+
$conditions = [];
76+
foreach (condition::get_records(['ruleid' => $this->get('id')], 'sortorder') as $condition) {
77+
$conditions[$condition->get('id')] = $condition;
78+
}
79+
80+
return $conditions;
81+
}
82+
83+
/**
84+
* Return if the rule is enabled.
85+
*
86+
* @return bool
87+
*/
88+
public function is_enabled() : bool {
89+
return (bool) $this->get('enabled');
90+
}
91+
92+
/**
93+
* Check if this rule should process in bulk.
94+
* @return bool
95+
*/
96+
public function is_bulk_processing(): bool {
97+
return (bool) $this->get('bulkprocessing');
98+
}
99+
100+
/**
101+
* Return if the rule is broken.
102+
*
103+
* @return bool
104+
*/
105+
public function is_broken() : bool {
106+
return (bool) $this->get('broken');
107+
}
108+
}

tests/rule_test.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
namespace tool_dynamic_cohorts;
18+
19+
/**
20+
* Tests for rule class.
21+
*
22+
* @package tool_dynamic_cohorts
23+
* @copyright 2024 Catalyst IT
24+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*
26+
* @covers \tool_dynamic_cohorts\rule
27+
28+
*/
29+
class rule_test extends \advanced_testcase {
30+
31+
/**
32+
* Test is_enabled.
33+
*/
34+
public function test_is_enabled() {
35+
$this->resetAfterTest();
36+
37+
$rule = new rule(0, (object)['name' => 'Test rule disabled']);
38+
$this->assertFalse($rule->is_enabled());
39+
40+
$rule = new rule(0, (object)['name' => 'Test rule enabled', 'enabled' => 1]);
41+
$this->assertTrue($rule->is_enabled());
42+
}
43+
44+
/**
45+
* Test is_bulk_processing.
46+
*/
47+
public function is_bulk_processing() {
48+
$this->resetAfterTest();
49+
50+
$rule = new rule(0, (object)['name' => 'Test rule bulk processing', 'bulkprocessing' => 1]);
51+
$this->assertTrue($rule->is_bulk_processing());
52+
53+
$rule = new rule(0, (object)['name' => 'Test rule normal']);
54+
$this->assertFalse($rule->is_bulk_processing());
55+
}
56+
57+
/**
58+
* Test getting a list of related condition records.
59+
*/
60+
public function test_get_condition_records() {
61+
$this->resetAfterTest();
62+
63+
$rule = new rule(0, (object)['name' => 'Test rule with conditions']);
64+
$rule->save();
65+
66+
$this->assertEmpty($rule->get_condition_records());
67+
68+
$condition1 = new condition(0, (object)['ruleid' => $rule->get('id'), 'classname' => 'test', 'sortorder' => 0]);
69+
$condition1->save();
70+
$condition2 = new condition(0, (object)['ruleid' => $rule->get('id'), 'classname' => 'test', 'sortorder' => 1]);
71+
$condition2->save();
72+
73+
$actual = $rule->get_condition_records();
74+
$this->assertCount(2, $actual);
75+
76+
$this->assertEquals($actual[$condition1->get('id')]->to_record(), $condition1->to_record());
77+
$this->assertEquals($actual[$condition2->get('id')]->to_record(), $condition2->to_record());
78+
}
79+
80+
/**
81+
* Test is_broken.
82+
*/
83+
public function test_is_broken() {
84+
$this->resetAfterTest();
85+
86+
$rule = new rule(0, (object)['name' => 'Test rule broken', 'broken' => 1]);
87+
$this->assertTrue($rule->is_broken());
88+
89+
$rule = new rule(0, (object)['name' => 'Test not broken']);
90+
$this->assertFalse($rule->is_broken());
91+
}
92+
}

0 commit comments

Comments
 (0)