1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace tool_admin_presets\reportbuilder\local\entities;
|
|
|
20 |
|
|
|
21 |
use lang_string;
|
|
|
22 |
use core\output\inplace_editable;
|
|
|
23 |
use core_reportbuilder\local\entities\base;
|
|
|
24 |
use core_reportbuilder\local\filters\text;
|
|
|
25 |
use core_reportbuilder\local\report\{column, filter};
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Admin preset entity
|
|
|
29 |
*
|
|
|
30 |
* @package tool_admin_presets
|
|
|
31 |
* @copyright 2024 David Carrillo <davidmc@moodle.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class admin_preset extends base {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Database tables that this entity uses
|
|
|
38 |
*
|
|
|
39 |
* @return string[]
|
|
|
40 |
*/
|
|
|
41 |
protected function get_default_tables(): array {
|
|
|
42 |
return [
|
|
|
43 |
'adminpresets',
|
|
|
44 |
];
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* The default title for this entity
|
|
|
49 |
*
|
|
|
50 |
* @return lang_string
|
|
|
51 |
*/
|
|
|
52 |
protected function get_default_entity_title(): lang_string {
|
|
|
53 |
return new lang_string('pluginname', 'tool_admin_presets');
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Initialise the entity
|
|
|
58 |
*
|
|
|
59 |
* @return base
|
|
|
60 |
*/
|
|
|
61 |
public function initialise(): base {
|
|
|
62 |
$columns = $this->get_all_columns();
|
|
|
63 |
foreach ($columns as $column) {
|
|
|
64 |
$this->add_column($column);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
68 |
$filters = $this->get_all_filters();
|
|
|
69 |
foreach ($filters as $filter) {
|
|
|
70 |
$this
|
|
|
71 |
->add_filter($filter)
|
|
|
72 |
->add_condition($filter);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
return $this;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Returns list of all available columns
|
|
|
80 |
*
|
|
|
81 |
* @return column[]
|
|
|
82 |
*/
|
|
|
83 |
protected function get_all_columns(): array {
|
|
|
84 |
$apalias = $this->get_table_alias('adminpresets');
|
|
|
85 |
|
|
|
86 |
// Name.
|
|
|
87 |
$columns[] = (new column(
|
|
|
88 |
'name',
|
|
|
89 |
new lang_string('name'),
|
|
|
90 |
$this->get_entity_name()
|
|
|
91 |
))
|
|
|
92 |
->add_joins($this->get_joins())
|
|
|
93 |
->set_type(column::TYPE_TEXT)
|
|
|
94 |
->add_fields("{$apalias}.id, {$apalias}.name, {$apalias}.iscore")
|
|
|
95 |
->set_is_sortable(true)
|
|
|
96 |
->set_callback(static function(?string $value, \stdClass $row): string {
|
|
|
97 |
global $OUTPUT;
|
|
|
98 |
$edithint = get_string('editadminpresetname', 'tool_admin_presets');
|
|
|
99 |
$displayvalue = format_string($row->name, true, ['context' => \context_system::instance(), 'escape' => false]);
|
|
|
100 |
$editlabel = get_string('newvaluefor', 'form', $displayvalue);
|
|
|
101 |
$canedit = (int)$row->iscore === \core_adminpresets\manager::NONCORE_PRESET;
|
|
|
102 |
$inlineeditable = new inplace_editable('tool_admin_presets', 'presetname', $row->id, $canedit,
|
|
|
103 |
$displayvalue, $row->name, $edithint, $editlabel);
|
|
|
104 |
return $OUTPUT->render($inlineeditable);
|
|
|
105 |
});
|
|
|
106 |
|
|
|
107 |
// Description.
|
|
|
108 |
$columns[] = (new column(
|
|
|
109 |
'description',
|
|
|
110 |
new lang_string('description'),
|
|
|
111 |
$this->get_entity_name()
|
|
|
112 |
))
|
|
|
113 |
->add_joins($this->get_joins())
|
|
|
114 |
->set_type(column::TYPE_LONGTEXT)
|
|
|
115 |
->add_field("{$apalias}.comments")
|
|
|
116 |
->set_is_sortable(true)
|
|
|
117 |
->set_callback(static function(?string $description): string {
|
|
|
118 |
return format_text($description, FORMAT_HTML, ['context' => \context_system::instance()]);
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
return $columns;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Return list of all available filters
|
|
|
126 |
*
|
|
|
127 |
* @return filter[]
|
|
|
128 |
*/
|
|
|
129 |
protected function get_all_filters(): array {
|
|
|
130 |
$apalias = $this->get_table_alias('adminpresets');
|
|
|
131 |
|
|
|
132 |
// Name.
|
|
|
133 |
$filters[] = (new filter(
|
|
|
134 |
text::class,
|
|
|
135 |
'name',
|
|
|
136 |
new lang_string('name'),
|
|
|
137 |
$this->get_entity_name(),
|
|
|
138 |
"{$apalias}.name"
|
|
|
139 |
))
|
|
|
140 |
->add_joins($this->get_joins());
|
|
|
141 |
|
|
|
142 |
return $filters;
|
|
|
143 |
}
|
|
|
144 |
}
|