From b2a6ae60e57f611fcc64d39ff49c0a99ef5eb6b5 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Tue, 17 Sep 2024 20:47:32 +1000 Subject: [PATCH] WIP --- classes/helper.php | 45 ++++++ classes/install_helper.php | 45 ------ classes/preset.php | 62 ++++++++ .../local/entities/preset_entity.php | 149 ++++++++++++++++++ cli/set_up.php | 22 +-- db/install.xml | 24 +++ db/upgrade.php | 65 ++++++++ index.php | 53 +++++++ lang/en/tool_enrolprofile.php | 6 + version.php | 2 +- 10 files changed, 416 insertions(+), 57 deletions(-) create mode 100644 classes/preset.php create mode 100644 classes/reportbuilder/local/entities/preset_entity.php create mode 100644 db/install.xml create mode 100644 db/upgrade.php create mode 100644 index.php diff --git a/classes/helper.php b/classes/helper.php index ce6e929..dac38fc 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -520,4 +520,49 @@ public static function validate_task_custom_data(stdClass $data, array $fields = } } } + + /** + * Gets a list of tags related to courses. + * + * @param int $courseid Optional to filter by course ID. + * @return array + */ + public static function get_course_tags(int $courseid = 0): array { + global $DB; + + $params = []; + $where = ''; + if (!empty($courseid)) { + $where = " AND ti.itemid = ?"; + $params[] = $courseid; + } + + $sql = "SELECT DISTINCT t.id, t.rawname + FROM {tag} t + JOIN {tag_instance} ti ON t.id = ti.tagid + WHERE ti.itemtype = 'course' $where ORDER BY t.id, t.rawname"; + return $DB->get_records_sql($sql, $params); + } + + /** + * Returns a list of courses. + * + * @return array + */ + public static function get_courses(): array { + global $DB; + + return $DB->get_records('course', ['visible' => 1], 'fullname'); + } + + /** + * Get categories. + * + * @return array + */ + public static function get_categories(): array { + global $DB; + + return $DB->get_records('course_categories', ['visible' => 1], 'name'); + } } diff --git a/classes/install_helper.php b/classes/install_helper.php index 246d4b7..3dd05f4 100644 --- a/classes/install_helper.php +++ b/classes/install_helper.php @@ -155,51 +155,6 @@ public static function add_cohort_custom_field(int $categoryid, string $shortnam $handler->save_field_configuration($field, $record); } - /** - * Gets a list of tags related to courses. - * - * @param int $courseid Optional to filter by course ID. - * @return array - */ - public static function get_course_tags(int $courseid = 0): array { - global $DB; - - $params = []; - $where = ''; - if (!empty($courseid)) { - $where = " AND ti.itemid = ?"; - $params[] = $courseid; - } - - $sql = "SELECT DISTINCT t.id, t.rawname - FROM {tag} t - JOIN {tag_instance} ti ON t.id = ti.tagid - WHERE ti.itemtype = 'course' $where ORDER BY t.id, t.rawname"; - return $DB->get_records_sql($sql, $params); - } - - /** - * Returns a list of courses. - * - * @return array - */ - public static function get_courses(): array { - global $DB; - - return $DB->get_records('course', ['visible' => 1], 'fullname'); - } - - /** - * Get categories. - * - * @return array - */ - public static function get_categories(): array { - global $DB; - - return $DB->get_records('course_categories', ['visible' => 1], 'name'); - } - /** * Clean up cohort enrolments. */ diff --git a/classes/preset.php b/classes/preset.php new file mode 100644 index 0000000..3b90007 --- /dev/null +++ b/classes/preset.php @@ -0,0 +1,62 @@ +. + +namespace tool_enrolprofile; + +use core\persistent; + +/** + * Preset persistent. + * + * @package tool_enrolprofile + * @copyright 2024 Dmitrii Metelkin + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class preset extends persistent { + + /** + * Table. + */ + const TABLE = 'tool_enrolprofile_presets'; + + /** + * Return the definition of the properties of this model. + * + * @return array + */ + protected static function define_properties() { + return [ + 'name' => [ + 'type' => PARAM_TEXT, + ], + 'categories' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'courses' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'tags' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + ]; + } +} diff --git a/classes/reportbuilder/local/entities/preset_entity.php b/classes/reportbuilder/local/entities/preset_entity.php new file mode 100644 index 0000000..bb07b32 --- /dev/null +++ b/classes/reportbuilder/local/entities/preset_entity.php @@ -0,0 +1,149 @@ +. + +namespace tool_enrolprofile\reportbuilder\local\entities; + +use core_reportbuilder\local\entities\base; +use core_reportbuilder\local\report\column; +use lang_string; +use tool_dynamic_cohorts\rule; +use tool_enrolprofile\helper; + +/** + * Report builder entity for presets. + * + * @package tool_enrolprofile + * @copyright 2024 Dmitrii Metelkin + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class preset_entity extends base { + + /** + * Returns the default table aliases. + * @return array + */ + protected function get_default_tables(): array { + return [ + 'tool_enrolprofile_presets', + ]; + } + + /** + * Returns the default table name. + * @return \lang_string + */ + protected function get_default_entity_title(): lang_string { + return new lang_string('preset_entity', 'tool_enrolprofile_presets'); + } + + /** + * 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 { + global $DB; + + $alias = $this->get_table_alias('tool_enrolprofile_presets'); + $categories = helper::get_categories(); + $courses = helper::get_courses(); + $tags = helper::get_course_tags(); + + $columns[] = (new column( + 'id', + new lang_string('preset_entity.id', 'tool_enrolprofile_presets'), + $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('preset_entity.name', 'tool_enrolprofile_presets'), + $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( + 'categories', + new lang_string('preset_entity.categories', 'tool_enrolprofile_presets'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_field("{$alias}.categories") + ->set_is_sortable(false) + ->add_callback(function ($value, $row) use ($categories) { + $catnames = []; + $categoryids = explode(',', $value); + foreach ($categoryids as $categoryid) { + $catnames[] = $categories[$categoryid]->name; + } + + return implode(', ', $catnames); + }); + + $columns[] = (new column( + 'courses', + new lang_string('preset_entity.courses', 'tool_enrolprofile_presets'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_field("{$alias}.courses") + ->set_is_sortable(false) + ->add_callback(function ($value, $row) { + return $value; + }); + + $columns[] = (new column( + 'tags', + new lang_string('preset_entity.tags', 'tool_enrolprofile_presets'), + $this->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_TEXT) + ->add_field("{$alias}.tags") + ->set_is_sortable(false) + ->add_callback(function ($value, $row) { + return $value; + }); + + + return $columns; + } +} diff --git a/cli/set_up.php b/cli/set_up.php index 452a827..073f699 100644 --- a/cli/set_up.php +++ b/cli/set_up.php @@ -170,7 +170,7 @@ if ($options['run']) { // Fill the field options with a list of courses. $param1 = []; - foreach (install_helper::get_courses() as $course) { + foreach (helper::get_courses() as $course) { if ($course->id == SITEID) { continue; } @@ -189,7 +189,7 @@ } else { if ($options['run']) { $param1 = []; - foreach (install_helper::get_courses() as $course) { + foreach (helper::get_courses() as $course) { if ($course->id == SITEID) { continue; } @@ -209,7 +209,7 @@ if ($options['run']) { // Fill the field options with a list of categories. $param1 = []; - foreach (install_helper::get_categories() as $category) { + foreach (helper::get_categories() as $category) { $param1[$category->name] = $category->name; } $extras = []; @@ -225,7 +225,7 @@ } else { if ($options['run']) { $param1 = []; - foreach (install_helper::get_categories() as $category) { + foreach (helper::get_categories() as $category) { $param1[$category->name] = $category->name; } $categoryfield->param1 = implode("\n", $param1); @@ -260,7 +260,7 @@ if ($options['run']) { // Fill the field options with a list of course related tags. $param1 = []; - foreach (install_helper::get_course_tags() as $tag) { + foreach (helper::get_course_tags() as $tag) { $param1[$tag->rawname] = $tag->rawname; } $extras = []; @@ -276,7 +276,7 @@ } else { if ($options['run']) { $param1 = []; - foreach (install_helper::get_course_tags() as $tag) { + foreach (helper::get_course_tags() as $tag) { $param1[$tag->rawname] = $tag->rawname; } $tagfield->param1 = implode("\n", $param1); @@ -288,7 +288,7 @@ } // Go through all tags and create cohort for each tag. - $tags = install_helper::get_course_tags(); + $tags = helper::get_course_tags(); foreach ($tags as $tag) { $cohort = new stdClass(); $cohort->contextid = context_system::instance()->id; @@ -303,7 +303,7 @@ } // Go through all categories and for each category create a cohort. - foreach (install_helper::get_categories() as $category) { + foreach (helper::get_categories() as $category) { $cohort = new stdClass(); $cohort->contextid = context_system::instance()->id; $cohort->name = $category->name; @@ -317,7 +317,7 @@ } // Go through each course from each category and create cohort. - foreach (install_helper::get_courses() as $course) { + foreach (helper::get_courses() as $course) { if ($course->id == SITEID) { continue; } @@ -339,7 +339,7 @@ // - get related course cohort and add an enrolment method for that cohort. // - get related category cohort and add an enrolment method for that cohort. // - get all tags for the course and for each tag create an enrolment method. - foreach (install_helper::get_courses() as $course) { + foreach (helper::get_courses() as $course) { if ($course->id == SITEID) { continue; @@ -365,7 +365,7 @@ } // Tags cohorts. - foreach (install_helper::get_course_tags($course->id) as $tag) { + foreach (helper::get_course_tags($course->id) as $tag) { if ($cohort = $DB->get_record('cohort', ['name' => $tag->rawname])) { install_helper::add_enrolment_method($course, $cohort, $options); } else { diff --git a/db/install.xml b/db/install.xml new file mode 100644 index 0000000..240d292 --- /dev/null +++ b/db/install.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + +
+
+
diff --git a/db/upgrade.php b/db/upgrade.php new file mode 100644 index 0000000..a9f82fb --- /dev/null +++ b/db/upgrade.php @@ -0,0 +1,65 @@ +. + +/** + * Upgrade hook. + * + * @package tool_enrolprofile + * @copyright 2024 Dmitrii Metelkin + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Upgrade the plugin. + * + * @param int $oldversion The old version of the plugin + * @return bool + */ +function xmldb_tool_enrolprofile_upgrade($oldversion): bool { + global $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2024091701) { + + // Define table tool_enrolprofile_presets to be created. + $table = new xmldb_table('tool_enrolprofile_presets'); + + // Adding fields to table tool_enrolprofile_presets. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('categories', XMLDB_TYPE_TEXT, null, null, null, null, null); + $table->add_field('courses', XMLDB_TYPE_TEXT, null, null, null, null, null); + $table->add_field('tags', XMLDB_TYPE_TEXT, null, null, null, null, null); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + + // Adding keys to table tool_enrolprofile_presets. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']); + + // Conditionally launch create table for tool_enrolprofile_presets. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Enrolprofile savepoint reached. + upgrade_plugin_savepoint(true, 2024091701, 'tool', 'enrolprofile'); + } + + return true; +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..d61cf17 --- /dev/null +++ b/index.php @@ -0,0 +1,53 @@ +. + +/** + * Presets page. + * + * @package tool_enrolprofile + * @copyright 2024 Dmitrii Metelkin + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use core\notification; +use core_reportbuilder\system_report_factory; +use tool_dynamic_cohorts\rule; +use tool_dynamic_cohorts\reportbuilder\local\systemreports\rules; + +require_once(__DIR__ . '/../../../config.php'); +require_once($CFG->libdir . '/adminlib.php'); + +admin_externalpage_setup('tool_enrolprofile_presets'); + +$manageurl = new moodle_url('/admin/tool/dynamic_cohorts/index.php'); +$editurl = new moodle_url('/admin/tool/dynamic_cohorts/edit.php'); + +$report = system_report_factory::create(rules::class, context_system::instance(), 'tool_enrolprofile'); + +//$PAGE->requires->js_call_amd('tool_dynamic_cohorts/manage_rules', 'init'); + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('managepresets', 'tool_dynamic_cohorts')); + +// TODO: add preset button + +echo $OUTPUT->render_from_template('tool_dynamic_cohorts/button', [ + 'url' => $editurl->out(), + 'text' => get_string('addpreset', 'tool_dynamic_cohorts'), +]); + +echo $report->output(); +echo $OUTPUT->footer(); diff --git a/lang/en/tool_enrolprofile.php b/lang/en/tool_enrolprofile.php index c240e5d..a16a0c4 100644 --- a/lang/en/tool_enrolprofile.php +++ b/lang/en/tool_enrolprofile.php @@ -27,3 +27,9 @@ $string['pluginname'] = 'Profile enrolment'; $string['privacy:metadata'] = 'Profile enrolment does not store any personal data.'; +$string['preset_entity'] = 'Preset'; +$string['preset_entity.id'] = 'ID'; +$string['preset_entity.name'] = 'Name'; +$string['preset_entity.categories'] = 'Categories'; +$string['preset_entity.courses'] = 'Courses'; +$string['preset_entity.tags'] = 'Tags'; diff --git a/version.php b/version.php index a9f5dc3..24d5713 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'tool_enrolprofile'; $plugin->release = '0.1.0'; -$plugin->version = 2024091700; +$plugin->version = 2024091701; $plugin->requires = 2022112800; $plugin->maturity = MATURITY_ALPHA; $plugin->supported = [404, 404];