Skip to content

Commit 5b489e1

Browse files
authored
Merge pull request #20 from catalyst/issue5
issue #5: add CRUD for rules
2 parents 7fe2f88 + 85ce3a4 commit 5b489e1

15 files changed

+1448
-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,151 @@
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+
* Initialise the report.
39+
*
40+
* @return void
41+
*/
42+
protected function initialise(): void {
43+
$ruleentity = new rule_entity();
44+
$rulealias = $ruleentity->get_table_alias('tool_dynamic_cohorts');
45+
$this->set_main_table('tool_dynamic_cohorts', $rulealias);
46+
$this->add_entity($ruleentity);
47+
48+
// Any columns required by actions should be defined here to ensure they're always available.
49+
$this->add_base_fields("{$rulealias}.id, {$rulealias}.enabled");
50+
51+
$cohortentity = new cohort();
52+
$cohortalias = $cohortentity->get_table_alias('cohort');
53+
$this->add_entity($cohortentity
54+
->add_join("JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$rulealias}.cohortid"));
55+
56+
$this->add_columns();
57+
$this->add_actions();
58+
59+
$cohortentity->get_column('name')
60+
->set_title(new lang_string('cohort', 'tool_dynamic_cohorts'));
61+
}
62+
63+
/**
64+
* Returns report context.
65+
*
66+
* @return \context
67+
*/
68+
public function get_context(): context {
69+
return context_system::instance();
70+
}
71+
72+
/**
73+
* Check if can view this system report.
74+
*
75+
* @return bool
76+
*/
77+
protected function can_view(): bool {
78+
return has_capability('tool/dynamic_cohorts:manage', $this->get_context());
79+
}
80+
81+
/**
82+
* Adds the columns we want to display in the report
83+
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
84+
* unique identifier
85+
*/
86+
protected function add_columns(): void {
87+
$columns = [
88+
'rule_entity:name',
89+
'rule_entity:description',
90+
'cohort:name',
91+
'rule_entity:bulkprocessing',
92+
'rule_entity:status',
93+
];
94+
95+
$this->add_columns_from_entities($columns);
96+
}
97+
98+
/**
99+
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
100+
*
101+
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
102+
*/
103+
protected function add_actions(): void {
104+
$this->add_action((new action(
105+
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
106+
new \pix_icon('t/hide', '', 'core'),
107+
[],
108+
false,
109+
new lang_string('enable')
110+
))->add_callback(function(\stdClass $row): bool {
111+
return empty($row->enabled);
112+
}));
113+
114+
$this->add_action((new action(
115+
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
116+
new \pix_icon('t/show', '', 'core'),
117+
[],
118+
false,
119+
new lang_string('disable')
120+
))->add_callback(function(\stdClass $row): bool {
121+
return !empty($row->enabled);
122+
}));
123+
124+
$this->add_action((new action(
125+
new \moodle_url('/admin/tool/dynamic_cohorts/edit.php', ['ruleid' => ':id']),
126+
new \pix_icon('t/edit', '', 'core'),
127+
[],
128+
false,
129+
new lang_string('edit')
130+
)));
131+
132+
$this->add_action((new action(
133+
new \moodle_url('/admin/tool/dynamic_cohorts/delete.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
134+
new \pix_icon('t/delete', '', 'core'),
135+
[],
136+
false,
137+
new lang_string('delete')
138+
)));
139+
}
140+
141+
/**
142+
* CSS class for the row
143+
*
144+
* @param \stdClass $row
145+
* @return string
146+
*/
147+
public function get_row_class(\stdClass $row): string {
148+
$rule = new rule(0, $row);
149+
return (!$rule->is_enabled()) ? 'text-muted' : '';
150+
}
151+
}

0 commit comments

Comments
 (0)