Skip to content

Commit

Permalink
MDL-76889 competency: implement datasource for custom reporting.
Browse files Browse the repository at this point in the history
Create entity definitions for competencies, frameworks and users
for new report source to provide data for report editor.

AMOS BEGIN
 CPY [competencyframework,tool_lp],[competencyframework,core_competency]
 CPY [proficient,tool_lp],[proficient,core_competency]
 CPY [rating,tool_lp],[rating,core_competency]
 CPY [taxonomy_competency,core_competency],[competency,core_competency]
AMOS END
  • Loading branch information
paulholden committed Aug 15, 2024
1 parent a75365f commit fd18c50
Show file tree
Hide file tree
Showing 6 changed files with 1,211 additions and 0 deletions.
155 changes: 155 additions & 0 deletions competency/classes/reportbuilder/datasource/competencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_competency\reportbuilder\datasource;

use core\reportbuilder\local\entities\context;
use core_competency\reportbuilder\local\entities\{competency, framework, usercompetency};
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\filters\boolean_select;

/**
* Competencies datasource
*
* @package core_competency
* @copyright 2024 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competencies extends datasource {

/**
* Return user friendly name of the report source
*
* @return string
*/
public static function get_name(): string {
return get_string('competencies', 'core_competency');
}

/**
* Initialise report
*/
protected function initialise(): void {
$frameworkentity = new framework();
$frameworkalias = $frameworkentity->get_table_alias('competency_framework');

$contextentity = new context();
$contextalias = $contextentity->get_table_alias('context');

$this->set_main_table('competency_framework', $frameworkalias);

// Join context entity (unconditionally, as table also used by both framework/competency entities).
$this->add_join("LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$frameworkalias}.contextid");
$this->add_entity($contextentity);

$this->add_entity($frameworkentity
->set_table_join_alias('context', $contextalias)
);

// Join competency entity.
$competencyentity = new competency();
$competencyalias = $competencyentity->get_table_alias('competency');
$this->add_entity($competencyentity
->set_table_join_alias('context', $contextalias)
->add_join("LEFT JOIN {competency} {$competencyalias}
ON {$competencyalias}.competencyframeworkid = {$frameworkalias}.id")
);

// Join user competency entity.
$usercompetencyentity = new usercompetency();
$usercompetencyalias = $usercompetencyentity->get_table_alias('competency_usercomp');
$this->add_entity($usercompetencyentity
->add_joins($competencyentity->get_joins())
->add_join("LEFT JOIN {competency_usercomp} {$usercompetencyalias}
ON {$usercompetencyalias}.competencyid = {$competencyalias}.id")
);

// Join user entity.
$userentity = new user();
$useralias = $userentity->get_table_alias('user');
$this->add_entity($userentity
->add_joins($usercompetencyentity->get_joins())
->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$usercompetencyalias}.userid")
);

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entities();
}

/**
* Return the columns that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_columns(): array {
return [
'framework:name',
'competency:name',
'user:fullname',
'usercompetency:proficient',
];
}

/**
* Return the column sorting that will be added to the report upon creation
*
* @return int[]
*/
public function get_default_column_sorting(): array {
return [
'framework:name' => SORT_ASC,
'competency:name' => SORT_ASC,
'user:fullname' => SORT_ASC,
];
}

/**
* Return the filters that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_filters(): array {
return [
'competency:name',
'user:fullname',
];
}

/**
* Return the conditions that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_conditions(): array {
return [
'framework:visible',
];
}

/**
* Return the condition values that will be added to the report upon creation
*
* @return array
*/
public function get_default_condition_values(): array {
return [
'framework:visible_operator' => boolean_select::CHECKED,
];
}
}
238 changes: 238 additions & 0 deletions competency/classes/reportbuilder/local/entities/competency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_competency\reportbuilder\local\entities;

use core\{context, context_helper};
use core\lang_string;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\{date, text};
use core_reportbuilder\local\helpers\{database, format};
use core_reportbuilder\local\report\{column, filter};
use stdClass;

/**
* Competency entity
*
* @package core_competency
* @copyright 2024 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competency extends base {

/**
* Database tables that this entity uses
*
* @return string[]
*/
protected function get_default_tables(): array {
return [
'competency',
'context',
];
}

/**
* The default title for this entity
*
* @return lang_string
*/
protected function get_default_entity_title(): lang_string {
return new lang_string('competency', 'core_competency');
}

/**
* Initialise the entity
*
* @return base
*/
public function initialise(): base {
$columns = $this->get_all_columns();
foreach ($columns as $column) {
$this->add_column($column);
}

// All the filters defined by the entity can also be used as conditions.
$filters = $this->get_all_filters();
foreach ($filters as $filter) {
$this
->add_filter($filter)
->add_condition($filter);
}

return $this;
}

/**
* Returns list of all available columns
*
* @return column[]
*/
protected function get_all_columns(): array {
global $DB;

$contextalias = $this->get_table_alias('context');
$competencyalias = $this->get_table_alias('competency');

// Name.
$columns[] = (new column(
'name',
new lang_string('name'),
$this->get_entity_name(),
))
->add_joins($this->get_joins())
->add_field("{$competencyalias}.shortname")
->set_is_sortable(true);

// Description.
$descriptionfieldsql = "{$competencyalias}.description";
if ($DB->get_dbfamily() === 'oracle') {
$descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
}
$columns[] = (new column(
'description',
new lang_string('description'),
$this->get_entity_name(),
))
->add_joins($this->get_joins())
->add_joins($this->get_context_joins())
->set_type(column::TYPE_LONGTEXT)
->add_field($descriptionfieldsql, 'description')
->add_field("{$competencyalias}.descriptionformat")
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
->add_callback(static function(?string $description, stdClass $competency): string {
if ($description === null) {
return '';
}

context_helper::preload_from_record(clone $competency);
$context = context::instance_by_id($competency->ctxid);

return format_text($description, $competency->descriptionformat, ['context' => $context->id]);
});

// ID number.
$columns[] = (new column(
'idnumber',
new lang_string('idnumber'),
$this->get_entity_name(),
))
->add_joins($this->get_joins())
->add_field("{$competencyalias}.idnumber")
->set_is_sortable(true);

// Time created.
$columns[] = (new column(
'timecreated',
new lang_string('timecreated', 'core_reportbuilder'),
$this->get_entity_name(),
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TIMESTAMP)
->add_field("{$competencyalias}.timecreated")
->set_is_sortable(true)
->add_callback([format::class, 'userdate']);

// Time modified.
$columns[] = (new column(
'timemodified',
new lang_string('timemodified', 'core_reportbuilder'),
$this->get_entity_name(),
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TIMESTAMP)
->add_field("{$competencyalias}.timemodified")
->set_is_sortable(true)
->add_callback([format::class, 'userdate']);

return $columns;
}

/**
* Return list of all available filters
*
* @return filter[]
*/
protected function get_all_filters(): array {
$competencyalias = $this->get_table_alias('competency');

// Name.
$filters[] = (new filter(
text::class,
'name',
new lang_string('name'),
$this->get_entity_name(),
"{$competencyalias}.shortname",
))
->add_joins($this->get_joins());

// ID number.
$filters[] = (new filter(
text::class,
'idnumber',
new lang_string('idnumber'),
$this->get_entity_name(),
"{$competencyalias}.idnumber",
))
->add_joins($this->get_joins());

// Time created.
$filters[] = (new filter(
date::class,
'timecreated',
new lang_string('timecreated', 'core_reportbuilder'),
$this->get_entity_name(),
"{$competencyalias}.timecreated",
))
->add_joins($this->get_joins());

// Time modified.
$filters[] = (new filter(
date::class,
'timemodified',
new lang_string('timemodified', 'core_reportbuilder'),
$this->get_entity_name(),
"{$competencyalias}.timemodified",
))
->add_joins($this->get_joins());

return $filters;
}

/**
* Return context joins
*
* @return string[]
*/
private function get_context_joins(): array {

// If the context table is already joined, we don't need to do that again.
if ($this->has_table_join_alias('context')) {
return [];
}

$frameworkalias = database::generate_alias();
$competencyalias = $this->get_table_alias('competency');
$contextalias = $this->get_table_alias('context');

return [
"LEFT JOIN {competency_framework} {$frameworkalias} ON {$frameworkalias}.id = {$competencyalias}.competencyframeworkid",
"LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$frameworkalias}.contextid",
];
}
}
Loading

0 comments on commit fd18c50

Please sign in to comment.