Skip to content

Commit

Permalink
issue #5: add CRUD for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Mar 5, 2024
1 parent 7fe2f88 commit 85ce3a4
Show file tree
Hide file tree
Showing 15 changed files with 1,448 additions and 1 deletion.
88 changes: 88 additions & 0 deletions classes/cohort_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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;

use moodle_exception;

defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/cohort/lib.php');

/**
* Cohort manager class.
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cohort_manager {

/**
* Cohort component.
*/
const COHORT_COMPONENT = 'tool_dynamic_cohorts';

/**
* Get a list of all cohort names in the system keyed by cohort ID.
*
* @param bool $excludemanaged Exclude cohorts managed by us.
* @return array
*/
public static function get_cohorts(bool $excludemanaged = false): array {
$cohorts = [];
foreach (\cohort_get_all_cohorts(0, 0)['cohorts'] as $cohort) {
if (empty($cohort->component) || (!$excludemanaged && $cohort->component === self::COHORT_COMPONENT)) {
$cohorts[$cohort->id] = $cohort;
}
}

return $cohorts;
}

/**
* Set cohort to be managed by tool_dynamic_cohorts.
*
* @param int $cohortid Cohort ID.
*/
public static function manage_cohort(int $cohortid): void {
$cohorts = self::get_cohorts();
if (!empty($cohorts[$cohortid])) {
$cohort = $cohorts[$cohortid];

if ($cohort->component === self::COHORT_COMPONENT) {
throw new moodle_exception('Cohort ' . $cohortid . ' is already managed by tool_dynamic_cohorts');
}

$cohort->component = 'tool_dynamic_cohorts';
cohort_update_cohort($cohort);
}
}

/**
* Unset cohort from being managed by tool_dynamic_cohorts.
*
* @param int $cohortid Cohort ID.
*/
public static function unmanage_cohort(int $cohortid): void {
$cohorts = self::get_cohorts();

if (!empty($cohorts[$cohortid])) {
$cohort = $cohorts[$cohortid];
$cohort->component = '';
cohort_update_cohort($cohort);
}
}
}
152 changes: 152 additions & 0 deletions classes/reportbuilder/local/entities/rule_entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?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\reportbuilder\local\entities;

use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\report\column;
use lang_string;
use tool_dynamic_cohorts\rule;

/**
* Report builder entity for rules.
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rule_entity extends base {

/**
* Returns the default table aliases.
* @return array
*/
protected function get_default_table_aliases(): array {
return [
'tool_dynamic_cohorts' => 'tdc',
];
}

/**
* Returns the default table name.
* @return \lang_string
*/
protected function get_default_entity_title(): lang_string {
return new lang_string('rule_entity', 'tool_dynamic_cohorts');
}

/**
* Initialises the entity.
* @return \core_reportbuilder\local\entities\base
*/
public function initialise(): base {
foreach ($this->get_all_columns() as $column) {
$this->add_column($column);
}

return $this;
}

/**
* Returns list of available columns.
*
* @return column[]
*/
protected function get_all_columns(): array {
$alias = $this->get_table_alias('tool_dynamic_cohorts');

$columns[] = (new column(
'id',
new lang_string('rule_entity.id', 'tool_dynamic_cohorts'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_INTEGER)
->add_field("{$alias}.id")
->set_is_sortable(true);

$columns[] = (new column(
'name',
new lang_string('rule_entity.name', 'tool_dynamic_cohorts'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.name")
->add_field("{$alias}.id")
->set_is_sortable(true)
->add_callback(function ($value, $row) {
return $value;
});

$columns[] = (new column(
'description',
new lang_string('rule_entity.description', 'tool_dynamic_cohorts'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.description")
->set_is_sortable(false);

$columns[] = (new column(
'bulkprocessing',
new lang_string('rule_entity.bulkprocessing', 'tool_dynamic_cohorts'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.bulkprocessing")
->add_fields("{$alias}.id, {$alias}.name, {$alias}.bulkprocessing")
->set_is_sortable(true)
->add_callback(function ($value, $row) {
$rule = new rule(0, $row);
return !empty($rule->is_bulk_processing()) ? get_string('yes') : get_string('no');
});

$columns[] = (new column(
'status',
new lang_string('rule_entity.status', 'tool_dynamic_cohorts'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$alias}.broken")
->add_fields("{$alias}.id, {$alias}.name, {$alias}.broken, {$alias}.enabled")
->set_is_sortable(true)
->add_callback(function ($value, $row) {
global $OUTPUT;

$rule = new rule(0, $row);

if ($rule->is_enabled()) {
$enabled = $OUTPUT->pix_icon('t/hide', get_string('enabled', 'tool_dynamic_cohorts'));
} else {
$enabled = $OUTPUT->pix_icon('t/show', get_string('disabled', 'tool_dynamic_cohorts'));
}

if ($rule->is_broken()) {
$broken = $OUTPUT->pix_icon('i/invalid', get_string('statuserror'));
} else {
$broken = $OUTPUT->pix_icon('i/valid', get_string('ok'));
}

return $broken . $enabled;
});

return $columns;
}
}
151 changes: 151 additions & 0 deletions classes/reportbuilder/local/systemreports/rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?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\reportbuilder\local\systemreports;

use context;
use context_system;
use core_cohort\reportbuilder\local\entities\cohort;
use core_reportbuilder\local\report\action;
use core_reportbuilder\system_report;
use tool_dynamic_cohorts\reportbuilder\local\entities\rule_entity;
use lang_string;
use tool_dynamic_cohorts\rule;

/**
* Rules admin table.
*
* @package tool_dynamic_cohorts
* @copyright 2024 Catalyst IT
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rules extends system_report {

/**
* Initialise the report.
*
* @return void
*/
protected function initialise(): void {
$ruleentity = new rule_entity();
$rulealias = $ruleentity->get_table_alias('tool_dynamic_cohorts');
$this->set_main_table('tool_dynamic_cohorts', $rulealias);
$this->add_entity($ruleentity);

// Any columns required by actions should be defined here to ensure they're always available.
$this->add_base_fields("{$rulealias}.id, {$rulealias}.enabled");

$cohortentity = new cohort();
$cohortalias = $cohortentity->get_table_alias('cohort');
$this->add_entity($cohortentity
->add_join("JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$rulealias}.cohortid"));

$this->add_columns();
$this->add_actions();

$cohortentity->get_column('name')
->set_title(new lang_string('cohort', 'tool_dynamic_cohorts'));
}

/**
* Returns report context.
*
* @return \context
*/
public function get_context(): context {
return context_system::instance();
}

/**
* Check if can view this system report.
*
* @return bool
*/
protected function can_view(): bool {
return has_capability('tool/dynamic_cohorts:manage', $this->get_context());
}

/**
* Adds the columns we want to display in the report
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
* unique identifier
*/
protected function add_columns(): void {
$columns = [
'rule_entity:name',
'rule_entity:description',
'cohort:name',
'rule_entity:bulkprocessing',
'rule_entity:status',
];

$this->add_columns_from_entities($columns);
}

/**
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
*
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
*/
protected function add_actions(): void {
$this->add_action((new action(
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
new \pix_icon('t/hide', '', 'core'),
[],
false,
new lang_string('enable')
))->add_callback(function(\stdClass $row): bool {
return empty($row->enabled);
}));

$this->add_action((new action(
new \moodle_url('/admin/tool/dynamic_cohorts/toggle.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
new \pix_icon('t/show', '', 'core'),
[],
false,
new lang_string('disable')
))->add_callback(function(\stdClass $row): bool {
return !empty($row->enabled);
}));

$this->add_action((new action(
new \moodle_url('/admin/tool/dynamic_cohorts/edit.php', ['ruleid' => ':id']),
new \pix_icon('t/edit', '', 'core'),
[],
false,
new lang_string('edit')
)));

$this->add_action((new action(
new \moodle_url('/admin/tool/dynamic_cohorts/delete.php', ['ruleid' => ':id', 'sesskey' => sesskey()]),
new \pix_icon('t/delete', '', 'core'),
[],
false,
new lang_string('delete')
)));
}

/**
* CSS class for the row
*
* @param \stdClass $row
* @return string
*/
public function get_row_class(\stdClass $row): string {
$rule = new rule(0, $row);
return (!$rule->is_enabled()) ? 'text-muted' : '';
}
}
Loading

0 comments on commit 85ce3a4

Please sign in to comment.