Skip to content

Commit 54d3117

Browse files
committed
issue #5: add CRUD for rules
1 parent 7fe2f88 commit 54d3117

15 files changed

+1442
-1
lines changed

classes/cohort_manager.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts;
18+
19+
use moodle_exception;
20+
21+
defined('MOODLE_INTERNAL') || die();
22+
require_once($CFG->dirroot.'/cohort/lib.php');
23+
24+
/**
25+
* Cohort manager class.
26+
*
27+
* @package tool_dynamic_cohorts
28+
* @copyright 2024 Catalyst IT
29+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30+
*/
31+
class cohort_manager {
32+
33+
/**
34+
* Cohort component.
35+
*/
36+
const COHORT_COMPONENT = 'tool_dynamic_cohorts';
37+
38+
/**
39+
* Get a list of all cohort names in the system keyed by cohort ID.
40+
*
41+
* @param bool $excludemanaged Exclude cohorts managed by us.
42+
* @return array
43+
*/
44+
public static function get_cohorts(bool $excludemanaged = false): array {
45+
$cohorts = [];
46+
foreach (\cohort_get_all_cohorts(0, 0)['cohorts'] as $cohort) {
47+
if (empty($cohort->component) || (!$excludemanaged && $cohort->component === self::COHORT_COMPONENT)) {
48+
$cohorts[$cohort->id] = $cohort;
49+
}
50+
}
51+
52+
return $cohorts;
53+
}
54+
55+
/**
56+
* Set cohort to be managed by tool_dynamic_cohorts.
57+
*
58+
* @param int $cohortid Cohort ID.
59+
*/
60+
public static function manage_cohort(int $cohortid): void {
61+
$cohorts = self::get_cohorts();
62+
if (!empty($cohorts[$cohortid])) {
63+
$cohort = $cohorts[$cohortid];
64+
65+
if ($cohort->component === self::COHORT_COMPONENT) {
66+
throw new moodle_exception('Cohort ' . $cohortid . ' is already managed by tool_dynamic_cohorts');
67+
}
68+
69+
$cohort->component = 'tool_dynamic_cohorts';
70+
cohort_update_cohort($cohort);
71+
}
72+
}
73+
74+
/**
75+
* Unset cohort from being managed by tool_dynamic_cohorts.
76+
*
77+
* @param int $cohortid Cohort ID.
78+
*/
79+
public static function unmanage_cohort(int $cohortid): void {
80+
$cohorts = self::get_cohorts();
81+
82+
if (!empty($cohorts[$cohortid])) {
83+
$cohort = $cohorts[$cohortid];
84+
$cohort->component = '';
85+
cohort_update_cohort($cohort);
86+
}
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\reportbuilder\local\entities;
18+
19+
use core_reportbuilder\local\entities\base;
20+
use core_reportbuilder\local\report\column;
21+
use lang_string;
22+
use tool_dynamic_cohorts\rule;
23+
24+
/**
25+
* Report builder entity for rules.
26+
*
27+
* @package tool_dynamic_cohorts
28+
* @copyright 2024 Catalyst IT
29+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30+
*/
31+
class rule_entity extends base {
32+
33+
/**
34+
* Returns the default table aliases.
35+
* @return array
36+
*/
37+
protected function get_default_table_aliases(): array {
38+
return [
39+
'tool_dynamic_cohorts' => 'tdc',
40+
];
41+
}
42+
43+
/**
44+
* Returns the default table name.
45+
* @return \lang_string
46+
*/
47+
protected function get_default_entity_title(): lang_string {
48+
return new lang_string('rule_entity', 'tool_dynamic_cohorts');
49+
}
50+
51+
/**
52+
* Initialises the entity.
53+
* @return \core_reportbuilder\local\entities\base
54+
*/
55+
public function initialise(): base {
56+
foreach ($this->get_all_columns() as $column) {
57+
$this->add_column($column);
58+
}
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* Returns list of available columns.
65+
*
66+
* @return column[]
67+
*/
68+
protected function get_all_columns(): array {
69+
$alias = $this->get_table_alias('tool_dynamic_cohorts');
70+
71+
$columns[] = (new column(
72+
'id',
73+
new lang_string('rule_entity.id', 'tool_dynamic_cohorts'),
74+
$this->get_entity_name()
75+
))
76+
->add_joins($this->get_joins())
77+
->set_type(column::TYPE_INTEGER)
78+
->add_field("{$alias}.id")
79+
->set_is_sortable(true);
80+
81+
$columns[] = (new column(
82+
'name',
83+
new lang_string('rule_entity.name', 'tool_dynamic_cohorts'),
84+
$this->get_entity_name()
85+
))
86+
->add_joins($this->get_joins())
87+
->set_type(column::TYPE_TEXT)
88+
->add_field("{$alias}.name")
89+
->add_field("{$alias}.id")
90+
->set_is_sortable(true)
91+
->add_callback(function ($value, $row) {
92+
return $value;
93+
});
94+
95+
$columns[] = (new column(
96+
'description',
97+
new lang_string('rule_entity.description', 'tool_dynamic_cohorts'),
98+
$this->get_entity_name()
99+
))
100+
->add_joins($this->get_joins())
101+
->set_type(column::TYPE_TEXT)
102+
->add_field("{$alias}.description")
103+
->set_is_sortable(false);
104+
105+
$columns[] = (new column(
106+
'bulkprocessing',
107+
new lang_string('rule_entity.bulkprocessing', 'tool_dynamic_cohorts'),
108+
$this->get_entity_name()
109+
))
110+
->add_joins($this->get_joins())
111+
->set_type(column::TYPE_TEXT)
112+
->add_field("{$alias}.bulkprocessing")
113+
->add_fields("{$alias}.id, {$alias}.name, {$alias}.bulkprocessing")
114+
->set_is_sortable(true)
115+
->add_callback(function ($value, $row) {
116+
$rule = new rule(0, $row);
117+
return !empty($rule->is_bulk_processing()) ? get_string('yes') : get_string('no');
118+
});
119+
120+
$columns[] = (new column(
121+
'status',
122+
new lang_string('rule_entity.status', 'tool_dynamic_cohorts'),
123+
$this->get_entity_name()
124+
))
125+
->add_joins($this->get_joins())
126+
->set_type(column::TYPE_TEXT)
127+
->add_field("{$alias}.broken")
128+
->add_fields("{$alias}.id, {$alias}.name, {$alias}.broken, {$alias}.enabled")
129+
->set_is_sortable(true)
130+
->add_callback(function ($value, $row) {
131+
global $OUTPUT;
132+
133+
$rule = new rule(0, $row);
134+
135+
if ($rule->is_enabled()) {
136+
$enabled = $OUTPUT->pix_icon('t/hide', get_string('enabled', 'tool_dynamic_cohorts'));
137+
} else {
138+
$enabled = $OUTPUT->pix_icon('t/show', get_string('disabled', 'tool_dynamic_cohorts'));
139+
}
140+
141+
if ($rule->is_broken()) {
142+
$broken = $OUTPUT->pix_icon('i/invalid', get_string('statuserror'));
143+
} else {
144+
$broken = $OUTPUT->pix_icon('i/valid', get_string('ok'));
145+
}
146+
147+
return $broken . $enabled;
148+
});
149+
150+
return $columns;
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace tool_dynamic_cohorts\reportbuilder\local\systemreports;
18+
19+
use context;
20+
use context_system;
21+
use core_cohort\reportbuilder\local\entities\cohort;
22+
use core_reportbuilder\local\report\action;
23+
use core_reportbuilder\system_report;
24+
use tool_dynamic_cohorts\reportbuilder\local\entities\rule_entity;
25+
use lang_string;
26+
use tool_dynamic_cohorts\rule;
27+
28+
/**
29+
* Rules admin table.
30+
*
31+
* @package tool_dynamic_cohorts
32+
* @copyright 2024 Catalyst IT
33+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34+
*/
35+
class rules extends system_report {
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function initialise(): void {
41+
$ruleentity = new rule_entity();
42+
$rulealias = $ruleentity->get_table_alias('tool_dynamic_cohorts');
43+
$this->set_main_table('tool_dynamic_cohorts', $rulealias);
44+
$this->add_entity($ruleentity);
45+
46+
// Any columns required by actions should be defined here to ensure they're always available.
47+
$this->add_base_fields("{$rulealias}.id, {$rulealias}.enabled");
48+
49+
$cohortentity = new cohort();
50+
$cohortalias = $cohortentity->get_table_alias('cohort');
51+
$this->add_entity($cohortentity
52+
->add_join("JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$rulealias}.cohortid"));
53+
54+
$this->add_columns();
55+
$this->add_actions();
56+
57+
$cohortentity->get_column('name')
58+
->set_title(new lang_string('cohort', 'tool_dynamic_cohorts'));
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function get_context(): context {
65+
return context_system::instance();
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
protected function can_view(): bool {
72+
return has_capability('tool/dynamic_cohorts:manage', $this->get_context());
73+
}
74+
75+
/**
76+
* Adds the columns we want to display in the report
77+
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
78+
* unique identifier
79+
*/
80+
protected function add_columns(): void {
81+
$columns = [
82+
'rule_entity:name',
83+
'rule_entity:description',
84+
'cohort:name',
85+
'rule_entity:bulkprocessing',
86+
'rule_entity:status',
87+
];
88+
89+
$this->add_columns_from_entities($columns);
90+
}
91+
92+
/**
93+
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
94+
*
95+
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
96+
*/
97+
protected function add_actions(): void {
98+
$this->add_action((new action(
99+
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
100+
new \pix_icon('t/hide', '', 'core'),
101+
[],
102+
false,
103+
new lang_string('enable')
104+
))->add_callback(function(\stdClass $row): bool {
105+
return empty($row->enabled);
106+
}));
107+
108+
$this->add_action((new action(
109+
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
110+
new \pix_icon('t/show', '', 'core'),
111+
[],
112+
false,
113+
new lang_string('disable')
114+
))->add_callback(function(\stdClass $row): bool {
115+
return !empty($row->enabled);
116+
}));
117+
118+
$this->add_action((new action(
119+
new \moodle_url('/admin/tool/dynamic_cohorts/edit.php', ['ruleid' => ':id']),
120+
new \pix_icon('t/edit', '', 'core'),
121+
[],
122+
false,
123+
new lang_string('edit')
124+
)));
125+
126+
$this->add_action((new action(
127+
new \moodle_url('/admin/tool/dynamic_cohorts/delete.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
128+
new \pix_icon('t/delete', '', 'core'),
129+
[],
130+
false,
131+
new lang_string('delete')
132+
)));
133+
}
134+
135+
/**
136+
* CSS class for the row
137+
*
138+
* @param \stdClass $row
139+
* @return string
140+
*/
141+
public function get_row_class(\stdClass $row): string {
142+
$rule = new rule(0, $row);
143+
return (!$rule->is_enabled()) ? 'text-muted' : '';
144+
}
145+
}

0 commit comments

Comments
 (0)