Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 17, 2024
1 parent acef4c8 commit b2a6ae6
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 57 deletions.
45 changes: 45 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
45 changes: 0 additions & 45 deletions classes/install_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
62 changes: 62 additions & 0 deletions classes/preset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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_enrolprofile;

use core\persistent;

/**
* Preset persistent.
*
* @package tool_enrolprofile
* @copyright 2024 Dmitrii Metelkin <[email protected]>
* @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,
],
];
}
}
149 changes: 149 additions & 0 deletions classes/reportbuilder/local/entities/preset_entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?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_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 <[email protected]>
* @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;
}
}
Loading

0 comments on commit b2a6ae6

Please sign in to comment.