| Línea 17... |
Línea 17... |
| 17 |
namespace core_cohort\reportbuilder\local\systemreports;
|
17 |
namespace core_cohort\reportbuilder\local\systemreports;
|
| Línea 18... |
Línea 18... |
| 18 |
|
18 |
|
| 19 |
use context;
|
19 |
use context;
|
| 20 |
use context_coursecat;
|
20 |
use context_coursecat;
|
| 21 |
use context_system;
|
21 |
use context_system;
|
| 22 |
use core_cohort\reportbuilder\local\entities\cohort;
|
22 |
use core_cohort\reportbuilder\local\entities\{cohort, cohort_member};
|
| 23 |
use core_reportbuilder\local\helpers\database;
|
23 |
use core_reportbuilder\local\helpers\database;
|
| 24 |
use core_reportbuilder\local\report\action;
|
24 |
use core_reportbuilder\local\report\action;
|
| 25 |
use core_reportbuilder\local\report\column;
|
25 |
use core_reportbuilder\local\report\column;
|
| 26 |
use html_writer;
|
26 |
use html_writer;
|
| Línea 48... |
Línea 48... |
| 48 |
$entitymainalias = $cohortentity->get_table_alias('cohort');
|
48 |
$entitymainalias = $cohortentity->get_table_alias('cohort');
|
| Línea 49... |
Línea 49... |
| 49 |
|
49 |
|
| 50 |
$this->set_main_table('cohort', $entitymainalias);
|
50 |
$this->set_main_table('cohort', $entitymainalias);
|
| Línea -... |
Línea 51... |
| - |
|
51 |
$this->add_entity($cohortentity);
|
| - |
|
52 |
|
| - |
|
53 |
// Join cohort member entity.
|
| - |
|
54 |
$cohortmemberentity = new cohort_member();
|
| - |
|
55 |
$cohortmemberalias = $cohortmemberentity->get_table_alias('cohort_members');
|
| - |
|
56 |
$this->add_entity($cohortmemberentity
|
| - |
|
57 |
->add_join("LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.cohortid = {$entitymainalias}.id")
|
| 51 |
$this->add_entity($cohortentity);
|
58 |
);
|
| 52 |
|
59 |
|
| 53 |
// Any columns required by actions should be defined here to ensure they're always available.
|
60 |
// Any columns required by actions should be defined here to ensure they're always available.
|
| - |
|
61 |
$this->add_base_fields("{$entitymainalias}.id, {$entitymainalias}.contextid, {$entitymainalias}.visible, " .
|
| - |
|
62 |
"{$entitymainalias}.component, {$entitymainalias}.name");
|
| - |
|
63 |
|
| - |
|
64 |
$this->set_checkbox_toggleall(static function(stdClass $cohort): ?array {
|
| - |
|
65 |
if (!has_capability('moodle/cohort:manage', context::instance_by_id($cohort->contextid))) {
|
| - |
|
66 |
return null;
|
| - |
|
67 |
}
|
| - |
|
68 |
|
| - |
|
69 |
return [
|
| - |
|
70 |
$cohort->id,
|
| - |
|
71 |
get_string('selectitem', 'moodle', $cohort->name),
|
| Línea 54... |
Línea 72... |
| 54 |
$this->add_base_fields("{$entitymainalias}.id, {$entitymainalias}.contextid, {$entitymainalias}.visible, " .
|
72 |
];
|
| 55 |
"{$entitymainalias}.component");
|
- |
|
| 56 |
|
73 |
});
|
| 57 |
// Check if report needs to show a specific category.
|
- |
|
| 58 |
$contextid = $this->get_parameter('contextid', 0, PARAM_INT);
|
74 |
|
| 59 |
$showall = $this->get_parameter('showall', true, PARAM_BOOL);
|
75 |
// Check if report needs to show a specific category.
|
| - |
|
76 |
if (!$this->get_context() instanceof context_system || !$this->get_parameter('showall', false, PARAM_BOOL)) {
|
| - |
|
77 |
$paramcontextid = database::generate_param_name();
|
| 60 |
if (!$showall) {
|
78 |
$this->add_base_condition_sql("{$entitymainalias}.contextid = :{$paramcontextid}", [
|
| Línea 61... |
Línea 79... |
| 61 |
$paramcontextid = database::generate_param_name();
|
79 |
$paramcontextid => $this->get_context()->id,
|
| 62 |
$this->add_base_condition_sql("{$entitymainalias}.contextid = :$paramcontextid", [$paramcontextid => $contextid]);
|
80 |
]);
|
| 63 |
}
|
81 |
}
|
| 64 |
|
82 |
|
| Línea 65... |
Línea 83... |
| 65 |
// Now we can call our helper methods to add the content we want to include in the report.
|
83 |
// Now we can call our helper methods to add the content we want to include in the report.
|
| 66 |
$this->add_columns($cohortentity);
|
84 |
$this->add_columns();
|
| Línea 75... |
Línea 93... |
| 75 |
* Validates access to view this report
|
93 |
* Validates access to view this report
|
| 76 |
*
|
94 |
*
|
| 77 |
* @return bool
|
95 |
* @return bool
|
| 78 |
*/
|
96 |
*/
|
| 79 |
protected function can_view(): bool {
|
97 |
protected function can_view(): bool {
|
| 80 |
$contextid = $this->get_parameter('contextid', 0, PARAM_INT);
|
- |
|
| 81 |
if ($contextid) {
|
- |
|
| 82 |
$context = context::instance_by_id($contextid, MUST_EXIST);
|
- |
|
| 83 |
} else {
|
- |
|
| 84 |
$context = context_system::instance();
|
- |
|
| 85 |
}
|
- |
|
| 86 |
|
- |
|
| 87 |
return has_any_capability(['moodle/cohort:manage', 'moodle/cohort:view'], $context);
|
98 |
return has_any_capability(['moodle/cohort:manage', 'moodle/cohort:view'], $this->get_context());
|
| 88 |
}
|
99 |
}
|
| Línea 89... |
Línea 100... |
| 89 |
|
100 |
|
| 90 |
/**
|
101 |
/**
|
| 91 |
* Adds the columns we want to display in the report
|
102 |
* Adds the columns we want to display in the report
|
| 92 |
*
|
103 |
*
|
| 93 |
* They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
104 |
* They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
| 94 |
* unique identifier. If custom columns are needed just for this report, they can be defined here.
|
- |
|
| 95 |
*
|
- |
|
| 96 |
* @param cohort $cohortentity
|
105 |
* unique identifier. If custom columns are needed just for this report, they can be defined here.
|
| 97 |
*/
|
106 |
*/
|
| 98 |
public function add_columns(cohort $cohortentity): void {
|
- |
|
| - |
|
107 |
protected function add_columns(): void {
|
| 99 |
|
108 |
$cohortentity = $this->get_entity('cohort');
|
| 100 |
$entitymainalias = $cohortentity->get_table_alias('cohort');
|
- |
|
| Línea 101... |
Línea 109... |
| 101 |
$showall = $this->get_parameter('showall', false, PARAM_BOOL);
|
109 |
$entitymainalias = $cohortentity->get_table_alias('cohort');
|
| 102 |
|
110 |
|
| 103 |
// Category column. An extra callback is appended in order to extend the current column formatting.
|
111 |
// Category column. An extra callback is appended in order to extend the current column formatting.
|
| 104 |
if ($showall) {
|
112 |
if ($this->get_context() instanceof context_system && $this->get_parameter('showall', false, PARAM_BOOL)) {
|
| 105 |
$this->add_column_from_entity('cohort:context')
|
113 |
$this->add_column_from_entity('cohort:context')
|
| 106 |
->add_callback(static function(string $value, stdClass $cohort): string {
|
114 |
->add_callback(static function(string $value, stdClass $cohort): string {
|
| 107 |
$context = context::instance_by_id($cohort->contextid);
|
115 |
$context = context::instance_by_id($cohort->contextid);
|
| Línea 149... |
Línea 157... |
| 149 |
});
|
157 |
});
|
| Línea 150... |
Línea 158... |
| 150 |
|
158 |
|
| 151 |
// Description column.
|
159 |
// Description column.
|
| Línea 152... |
Línea -... |
| 152 |
$this->add_column_from_entity('cohort:description');
|
- |
|
| 153 |
|
- |
|
| 154 |
// Cohort size column using a custom SQL query to count cohort members.
|
- |
|
| 155 |
$cm = database::generate_param_name();
|
160 |
$this->add_column_from_entity('cohort:description');
|
| 156 |
$sql = "(SELECT count($cm.id) as memberscount
|
- |
|
| 157 |
FROM {cohort_members} $cm
|
161 |
|
| 158 |
WHERE $cm.cohortid = {$entitymainalias}.id)";
|
- |
|
| 159 |
$this->add_column(new column(
|
162 |
// Member count.
|
| 160 |
'memberscount',
|
- |
|
| 161 |
new lang_string('memberscount', 'cohort'),
|
- |
|
| 162 |
$cohortentity->get_entity_name()
|
- |
|
| 163 |
))
|
163 |
$this->add_column_from_entity('cohort_member:timeadded')
|
| 164 |
->set_type(column::TYPE_INTEGER)
|
- |
|
| Línea 165... |
Línea 164... |
| 165 |
->set_is_sortable(true)
|
164 |
->set_title(new lang_string('memberscount', 'cohort'))
|
| 166 |
->add_field($sql, 'memberscount');
|
165 |
->set_aggregation('count');
|
| 167 |
|
166 |
|
| Línea 178... |
Línea 177... |
| 178 |
*
|
177 |
*
|
| 179 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
178 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
| 180 |
* unique identifier
|
179 |
* unique identifier
|
| 181 |
*/
|
180 |
*/
|
| 182 |
protected function add_filters(): void {
|
181 |
protected function add_filters(): void {
|
| 183 |
$filters = [
|
- |
|
| 184 |
'cohort:name',
|
- |
|
| 185 |
'cohort:idnumber',
|
- |
|
| 186 |
'cohort:description',
|
- |
|
| 187 |
];
|
- |
|
| 188 |
$this->add_filters_from_entities($filters);
|
182 |
$this->add_filters_from_entity('cohort', ['name', 'idnumber', 'description', 'customfield*']);
|
| 189 |
}
|
183 |
}
|
| Línea 190... |
Línea 184... |
| 190 |
|
184 |
|
| 191 |
/**
|
185 |
/**
|
| 192 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
186 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
| 193 |
*
|
187 |
*
|
| 194 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
188 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
| 195 |
*/
|
189 |
*/
|
| Línea 196... |
Línea 190... |
| 196 |
protected function add_actions(): void {
|
190 |
protected function add_actions(): void {
|
| 197 |
|
191 |
|
| 198 |
$contextid = $this->get_parameter('contextid', 0, PARAM_INT);
|
192 |
$returnurl = (new moodle_url('/cohort/index.php', [
|
| 199 |
$showall = $this->get_parameter('showall', true, PARAM_BOOL);
|
193 |
'id' => ':id',
|
| - |
|
194 |
'contextid' => $this->get_context()->id,
|
| Línea 200... |
Línea 195... |
| 200 |
$returnurl = (new moodle_url('/cohort/index.php',
|
195 |
'showall' => $this->get_parameter('showall', false, PARAM_BOOL),
|
| 201 |
['id' => ':id', 'contextid' => $contextid, 'showall' => $showall]))->out(false);
|
196 |
]))->out(false);
|
| 202 |
|
197 |
|
| 203 |
// Hide action. It will be only shown if the property 'visible' is true and user has 'moodle/cohort:manage' capabillity.
|
198 |
// Hide action. It will be only shown if the property 'visible' is true and user has 'moodle/cohort:manage' capabillity.
|
| Línea 235... |
Línea 230... |
| 235 |
return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
|
230 |
return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
|
| 236 |
}));
|
231 |
}));
|
| Línea 237... |
Línea 232... |
| 237 |
|
232 |
|
| 238 |
// Delete action. It will be only shown if user has 'moodle/cohort:manage' capabillity.
|
233 |
// Delete action. It will be only shown if user has 'moodle/cohort:manage' capabillity.
|
| 239 |
$this->add_action((new action(
|
234 |
$this->add_action((new action(
|
| 240 |
new moodle_url('/cohort/edit.php', ['id' => ':id', 'delete' => 1, 'returnurl' => $returnurl]),
|
235 |
new moodle_url('#'),
|
| 241 |
new pix_icon('t/delete', '', 'core'),
|
236 |
new pix_icon('t/delete', '', 'core'),
|
| 242 |
['class' => 'text-danger'],
|
237 |
['class' => 'text-danger', 'data-action' => 'cohort-delete', 'data-cohort-id' => ':id', 'data-cohort-name' => ':name'],
|
| 243 |
false,
|
238 |
false,
|
| 244 |
new lang_string('delete')
|
239 |
new lang_string('delete')
|
| 245 |
))->add_callback(function(stdClass $row): bool {
|
240 |
))->add_callback(function(stdClass $row): bool {
|
| 246 |
return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
|
241 |
return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
|