Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_cohort\reportbuilder\local\systemreports;
18
 
19
use context;
20
use context_coursecat;
21
use context_system;
1441 ariadna 22
use core_cohort\reportbuilder\local\entities\{cohort, cohort_member};
1 efrain 23
use core_reportbuilder\local\helpers\database;
24
use core_reportbuilder\local\report\action;
25
use core_reportbuilder\local\report\column;
26
use html_writer;
27
use lang_string;
28
use moodle_url;
29
use pix_icon;
30
use core_reportbuilder\system_report;
31
use stdClass;
32
 
33
/**
34
 * Cohorts system report class implementation
35
 *
36
 * @package    core_cohort
37
 * @copyright  2021 David Matamoros <davidmc@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class cohorts extends system_report {
41
 
42
    /**
43
     * Initialise report, we need to set the main table, load our entities and set columns/filters
44
     */
45
    protected function initialise(): void {
46
        // Our main entity, it contains all of the column definitions that we need.
47
        $cohortentity = new cohort();
48
        $entitymainalias = $cohortentity->get_table_alias('cohort');
49
 
50
        $this->set_main_table('cohort', $entitymainalias);
51
        $this->add_entity($cohortentity);
52
 
1441 ariadna 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")
58
        );
59
 
1 efrain 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, " .
1441 ariadna 62
            "{$entitymainalias}.component, {$entitymainalias}.name");
1 efrain 63
 
1441 ariadna 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),
72
            ];
73
        });
74
 
1 efrain 75
        // Check if report needs to show a specific category.
1441 ariadna 76
        if (!$this->get_context() instanceof context_system || !$this->get_parameter('showall', false, PARAM_BOOL)) {
1 efrain 77
            $paramcontextid = database::generate_param_name();
1441 ariadna 78
            $this->add_base_condition_sql("{$entitymainalias}.contextid = :{$paramcontextid}", [
79
                $paramcontextid => $this->get_context()->id,
80
            ]);
1 efrain 81
        }
82
 
83
        // Now we can call our helper methods to add the content we want to include in the report.
1441 ariadna 84
        $this->add_columns();
1 efrain 85
        $this->add_filters();
86
        $this->add_actions();
87
 
88
        // Set if report can be downloaded.
89
        $this->set_downloadable(false);
90
    }
91
 
92
    /**
93
     * Validates access to view this report
94
     *
95
     * @return bool
96
     */
97
    protected function can_view(): bool {
1441 ariadna 98
        return has_any_capability(['moodle/cohort:manage', 'moodle/cohort:view'], $this->get_context());
1 efrain 99
    }
100
 
101
    /**
102
     * Adds the columns we want to display in the report
103
     *
104
     * They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
105
     * unique identifier. If custom columns are needed just for this report, they can be defined here.
106
     */
1441 ariadna 107
    protected function add_columns(): void {
108
        $cohortentity = $this->get_entity('cohort');
1 efrain 109
        $entitymainalias = $cohortentity->get_table_alias('cohort');
110
 
111
        // Category column. An extra callback is appended in order to extend the current column formatting.
1441 ariadna 112
        if ($this->get_context() instanceof context_system && $this->get_parameter('showall', false, PARAM_BOOL)) {
1 efrain 113
            $this->add_column_from_entity('cohort:context')
114
                ->add_callback(static function(string $value, stdClass $cohort): string {
115
                    $context = context::instance_by_id($cohort->contextid);
116
                    if ($context instanceof context_coursecat) {
117
                        return html_writer::link(new moodle_url('/cohort/index.php',
118
                            ['contextid' => $cohort->contextid]), $value);
119
                    }
120
 
121
                    return $value;
122
                });
123
        }
124
 
125
        // Name column using the inplace editable component.
126
        $this->add_column(new column(
127
            'editablename',
128
            new lang_string('name', 'core_cohort'),
129
            $cohortentity->get_entity_name()
130
        ))
131
            ->set_type(column::TYPE_TEXT)
132
            ->set_is_sortable(true)
133
            ->add_fields("{$entitymainalias}.name, {$entitymainalias}.id, {$entitymainalias}.contextid")
134
            ->add_callback(static function(string $name, stdClass $cohort): string {
135
                global $OUTPUT, $PAGE;
136
                $renderer = $PAGE->get_renderer('core');
137
 
138
                $template = new \core_cohort\output\cohortname($cohort);
139
                return $renderer->render_from_template('core/inplace_editable', $template->export_for_template($OUTPUT));
140
            });
141
 
142
        // ID Number column using the inplace editable component.
143
        $this->add_column(new column(
144
            'editableidnumber',
145
            new lang_string('idnumber', 'core_cohort'),
146
            $cohortentity->get_entity_name()
147
        ))
148
            ->set_type(column::TYPE_TEXT)
149
            ->set_is_sortable(true)
150
            ->add_fields("{$entitymainalias}.idnumber, {$entitymainalias}.id, {$entitymainalias}.contextid")
151
            ->add_callback(static function(?string $idnumber, stdClass $cohort): string {
152
                global $OUTPUT, $PAGE;
153
                $renderer = $PAGE->get_renderer('core');
154
 
155
                $template = new \core_cohort\output\cohortidnumber($cohort);
156
                return $renderer->render_from_template('core/inplace_editable', $template->export_for_template($OUTPUT));
157
            });
158
 
159
        // Description column.
160
        $this->add_column_from_entity('cohort:description');
161
 
1441 ariadna 162
        // Member count.
163
        $this->add_column_from_entity('cohort_member:timeadded')
164
            ->set_title(new lang_string('memberscount', 'cohort'))
165
            ->set_aggregation('count');
1 efrain 166
 
167
        // Component column. Override the display name of a column.
168
        $this->add_column_from_entity('cohort:component')
169
            ->set_title(new lang_string('source', 'core_plugin'));
170
 
171
        // It's possible to set a default initial sort direction for one column.
172
        $this->set_initial_sort_column('cohort:editablename', SORT_ASC);
173
    }
174
 
175
    /**
176
     * Adds the filters we want to display in the report
177
     *
178
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
179
     * unique identifier
180
     */
181
    protected function add_filters(): void {
1441 ariadna 182
        $this->add_filters_from_entity('cohort', ['name', 'idnumber', 'description', 'customfield*']);
1 efrain 183
    }
184
 
185
    /**
186
     * Add the system report actions. An extra column will be appended to each row, containing all actions added here
187
     *
188
     * Note the use of ":id" placeholder which will be substituted according to actual values in the row
189
     */
190
    protected function add_actions(): void {
191
 
1441 ariadna 192
        $returnurl = (new moodle_url('/cohort/index.php', [
193
            'id' => ':id',
194
            'contextid' => $this->get_context()->id,
195
            'showall' => $this->get_parameter('showall', false, PARAM_BOOL),
196
        ]))->out(false);
1 efrain 197
 
198
        // Hide action. It will be only shown if the property 'visible' is true and user has 'moodle/cohort:manage' capabillity.
199
        $this->add_action((new action(
200
            new moodle_url('/cohort/edit.php', ['id' => ':id', 'sesskey' => sesskey(), 'hide' => 1, 'returnurl' => $returnurl]),
201
            new pix_icon('t/show', '', 'core'),
202
            [],
203
            false,
204
            new lang_string('hide')
205
        ))->add_callback(function(stdClass $row): bool {
206
            return empty($row->component) && $row->visible
207
                && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
208
        }));
209
 
210
        // Show action. It will be only shown if the property 'visible' is false and user has 'moodle/cohort:manage' capabillity.
211
        $this->add_action((new action(
212
            new moodle_url('/cohort/edit.php', ['id' => ':id', 'sesskey' => sesskey(), 'show' => 1, 'returnurl' => $returnurl]),
213
            new pix_icon('t/hide', '', 'core'),
214
            [],
215
            false,
216
            new lang_string('show')
217
        ))->add_callback(function(stdClass $row): bool {
218
            return empty($row->component) && !$row->visible
219
                && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
220
        }));
221
 
222
        // Edit action. It will be only shown if user has 'moodle/cohort:manage' capabillity.
223
        $this->add_action((new action(
224
            new moodle_url('/cohort/edit.php', ['id' => ':id', 'returnurl' => $returnurl]),
225
            new pix_icon('t/edit', '', 'core'),
226
            [],
227
            false,
228
            new lang_string('edit')
229
        ))->add_callback(function(stdClass $row): bool {
230
            return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
231
        }));
232
 
233
        // Delete action. It will be only shown if user has 'moodle/cohort:manage' capabillity.
234
        $this->add_action((new action(
1441 ariadna 235
            new moodle_url('#'),
1 efrain 236
            new pix_icon('t/delete', '', 'core'),
1441 ariadna 237
            ['class' => 'text-danger', 'data-action' => 'cohort-delete', 'data-cohort-id' => ':id', 'data-cohort-name' => ':name'],
1 efrain 238
            false,
239
            new lang_string('delete')
240
        ))->add_callback(function(stdClass $row): bool {
241
            return empty($row->component) && has_capability('moodle/cohort:manage', context::instance_by_id($row->contextid));
242
        }));
243
 
244
        // Assign members to cohort action. It will be only shown if user has 'moodle/cohort:assign' capabillity.
245
        $this->add_action((new action(
246
            new moodle_url('/cohort/assign.php', ['id' => ':id', 'returnurl' => $returnurl]),
247
            new pix_icon('i/users', '', 'core'),
248
            [],
249
            false,
250
            new lang_string('assign', 'core_cohort')
251
        ))->add_callback(function(stdClass $row): bool {
252
            return empty($row->component) && has_capability('moodle/cohort:assign', context::instance_by_id($row->contextid));
253
        }));
254
    }
255
 
256
    /**
257
     * CSS class for the row
258
     *
259
     * @param stdClass $row
260
     * @return string
261
     */
262
    public function get_row_class(stdClass $row): string {
263
        return (!$row->visible) ? 'text-muted' : '';
264
    }
265
}