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
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\local\systemreports;
20
 
21
use context;
22
use lang_string;
23
use moodle_url;
24
use pix_icon;
25
use stdClass;
26
use core_reportbuilder\permission;
27
use core_reportbuilder\system_report;
28
use core_reportbuilder\local\entities\user;
29
use core_reportbuilder\local\filters\date;
30
use core_reportbuilder\local\filters\text;
31
use core_reportbuilder\local\helpers\format;
32
use core_reportbuilder\local\models\report;
33
use core_reportbuilder\local\models\schedule;
34
use core_reportbuilder\local\report\action;
35
use core_reportbuilder\local\report\column;
36
use core_reportbuilder\local\report\filter;
37
use core_reportbuilder\output\schedule_name_editable;
38
 
39
/**
40
 * Report schedules list
41
 *
42
 * @package     core_reportbuilder
43
 * @copyright   2021 Paul Holden <paulh@moodle.com>
44
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
46
class report_schedules extends system_report {
47
 
48
    /**
49
     * The name of our internal report entity
50
     *
51
     * @return string
52
     */
53
    private function get_schedule_entity_name(): string {
54
        return 'schedule';
55
    }
56
 
57
    /**
58
     * Initialise the report
59
     */
60
    protected function initialise(): void {
61
        $this->set_main_table(schedule::TABLE, 'sc');
62
        $this->add_join('JOIN {' . report::TABLE . '} rb ON rb.id = sc.reportid');
63
 
64
        $this->add_base_condition_simple('sc.reportid', $this->get_parameter('reportid', 0, PARAM_INT));
65
 
66
        // Select fields required for actions, permission checks, and row class callbacks.
67
        $this->add_base_fields('sc.id, sc.name, sc.enabled, rb.contextid');
68
 
69
        // Join user entity for "User modified" column.
70
        $entityuser = new user();
71
        $entityuseralias = $entityuser->get_table_alias('user');
72
 
73
        $this->add_entity($entityuser
74
            ->add_join("JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = sc.usermodified")
75
        );
76
 
77
        // Define our internal entity for schedule elements.
78
        $this->annotate_entity($this->get_schedule_entity_name(),
79
            new lang_string('schedules', 'core_reportbuilder'));
80
 
81
        $this->add_columns();
82
        $this->add_filters();
83
        $this->add_actions();
84
 
85
        $this->set_downloadable(false);
86
    }
87
 
88
    /**
89
     * Ensure we can view the report
90
     *
91
     * @return bool
92
     */
93
    protected function can_view(): bool {
1441 ariadna 94
        $reportid = $this->get_parameter('reportid', 0, PARAM_INT);
95
        $report = report::get_record(['id' => $reportid], MUST_EXIST);
96
 
97
        return permission::can_edit_report($report);
1 efrain 98
    }
99
 
100
    /**
101
     * Dim the table row for disabled schedules
102
     *
103
     * @param stdClass $row
104
     * @return string
105
     */
106
    public function get_row_class(stdClass $row): string {
107
        return $row->enabled ? '' : 'text-muted';
108
    }
109
 
110
    /**
111
     * Add columns to report
112
     */
113
    protected function add_columns(): void {
114
        $tablealias = $this->get_main_table_alias();
115
 
116
        // Enable toggle column.
117
        $this->add_column((new column(
118
            'enabled',
119
            null,
120
            $this->get_schedule_entity_name()
121
        ))
122
            ->set_type(column::TYPE_BOOLEAN)
123
            ->add_fields("{$tablealias}.enabled, {$tablealias}.id")
124
            ->set_is_sortable(false)
125
            ->set_callback(static function(bool $enabled, stdClass $row): string {
126
                global $PAGE;
127
 
128
                $renderer = $PAGE->get_renderer('core_reportbuilder');
129
                $attributes = [
130
                    ['name' => 'id', 'value' => $row->id],
131
                    ['name' => 'action', 'value' => 'schedule-toggle'],
132
                    ['name' => 'state', 'value' => $row->enabled],
133
                ];
134
                $label = $row->enabled ? get_string('disableschedule', 'core_reportbuilder')
135
                    : get_string('enableschedule', 'core_reportbuilder');
136
                return $renderer->render_from_template('core/toggle', [
137
                    'id' => 'schedule-toggle-' . $row->id,
138
                    'checked' => $row->enabled,
139
                    'dataattributes' => $attributes,
140
                    'label' => $label,
1441 ariadna 141
                    'labelclasses' => 'visually-hidden',
1 efrain 142
                ]);
143
            })
144
        );
145
 
146
        // Report name column.
147
        $this->add_column((new column(
148
            'name',
149
            new lang_string('name'),
150
            $this->get_schedule_entity_name()
151
        ))
152
            ->set_type(column::TYPE_TEXT)
153
            // We need enough fields to re-create the persistent and pass to the editable component.
154
            ->add_fields("{$tablealias}.id, {$tablealias}.name, {$tablealias}.reportid")
155
            ->set_is_sortable(true, ["{$tablealias}.name"])
156
            ->add_callback(function(string $value, stdClass $schedule): string {
157
                global $PAGE;
158
 
159
                $editable = new schedule_name_editable(0, new schedule(0, $schedule));
160
                return $editable->render($PAGE->get_renderer('core'));
161
            })
162
        );
163
 
164
        // Time last sent column.
165
        $this->add_column((new column(
166
            'timelastsent',
167
            new lang_string('timelastsent', 'core_reportbuilder'),
168
            $this->get_schedule_entity_name()
169
        ))
170
            ->set_type(column::TYPE_TIMESTAMP)
171
            ->add_fields("{$tablealias}.timelastsent")
172
            ->set_is_sortable(true)
173
            ->add_callback(static function(int $timelastsent, stdClass $row): string {
174
                if ($timelastsent === 0) {
175
                    return get_string('never');
176
                }
177
 
178
                return format::userdate($timelastsent, $row);
179
            })
180
        );
181
 
1441 ariadna 182
        // Time next send column.
183
        $this->add_column((new column(
184
            'timenextsend',
185
            new lang_string('timenextsend', 'core_reportbuilder'),
186
            $this->get_schedule_entity_name()
187
        ))
188
            ->set_type(column::TYPE_TIMESTAMP)
189
            ->add_fields("{$tablealias}.timenextsend")
190
            ->set_is_sortable(true)
191
            ->add_callback(static function(int $timenextsend, stdClass $row): string {
192
                if ($timenextsend < time()) {
193
                    return get_string('never');
194
                }
195
 
196
                return format::userdate($timenextsend, $row);
197
            })
198
        );
199
 
1 efrain 200
        // Format column.
201
        $this->add_column((new column(
202
            'format',
203
            new lang_string('format'),
204
            $this->get_schedule_entity_name()
205
        ))
206
            ->set_type(column::TYPE_TEXT)
207
            ->add_fields("{$tablealias}.format")
208
            ->set_is_sortable(true)
209
            ->add_callback(static function(string $format): string {
210
                if (get_string_manager()->string_exists('dataformat', 'dataformat_' . $format)) {
211
                    return get_string('dataformat', 'dataformat_' . $format);
212
                } else {
213
                    return $format;
214
                }
215
            })
216
        );
217
 
218
        // Time created column.
219
        $this->add_column((new column(
220
            'timecreated',
221
            new lang_string('timecreated', 'core_reportbuilder'),
222
            $this->get_schedule_entity_name()
223
        ))
224
            ->set_type(column::TYPE_TIMESTAMP)
225
            ->add_fields("{$tablealias}.timecreated")
226
            ->set_is_sortable(true)
227
            ->add_callback([format::class, 'userdate'])
228
        );
229
 
230
        // Time modified column.
231
        $this->add_column((new column(
232
            'timemodified',
233
            new lang_string('timemodified', 'core_reportbuilder'),
234
            $this->get_schedule_entity_name()
235
        ))
236
            ->set_type(column::TYPE_TIMESTAMP)
237
            ->add_fields("{$tablealias}.timemodified")
238
            ->set_is_sortable(true)
239
            ->add_callback([format::class, 'userdate'])
240
        );
241
 
242
        // The user who modified the schedule.
243
        $this->add_column_from_entity('user:fullname')
244
            ->set_title(new lang_string('usermodified', 'core_reportbuilder'));
245
 
246
        // Initial sorting.
247
        $this->set_initial_sort_column('schedule:timecreated', SORT_DESC);
248
    }
249
 
250
    /**
251
     * Add filters to report
252
     */
253
    protected function add_filters(): void {
254
        $tablealias = $this->get_main_table_alias();
255
 
256
        // Name filter.
257
        $this->add_filter((new filter(
258
            text::class,
259
            'name',
260
            new lang_string('name'),
261
            $this->get_schedule_entity_name(),
262
            "{$tablealias}.name"
263
        )));
264
 
1441 ariadna 265
        // Time last sent filter.
1 efrain 266
        $this->add_filter((new filter(
267
            date::class,
268
            'timelastsent',
269
            new lang_string('timelastsent', 'core_reportbuilder'),
270
            $this->get_schedule_entity_name(),
271
            "{$tablealias}.timelastsent"
272
        ))
273
            ->set_limited_operators([
274
                date::DATE_ANY,
275
                date::DATE_EMPTY,
1441 ariadna 276
                date::DATE_NOT_EMPTY,
1 efrain 277
                date::DATE_RANGE,
1441 ariadna 278
                date::DATE_BEFORE,
279
                date::DATE_LAST,
1 efrain 280
                date::DATE_CURRENT,
281
            ])
282
        );
283
 
1441 ariadna 284
        // Time next send filter.
285
        $this->add_filter((new filter(
286
            date::class,
287
            'timenextsend',
288
            new lang_string('timenextsend', 'core_reportbuilder'),
289
            $this->get_schedule_entity_name(),
290
            "{$tablealias}.timenextsend"
291
        ))
292
            ->set_limited_operators([
293
                date::DATE_ANY,
294
                date::DATE_RANGE,
295
                date::DATE_PAST,
296
                date::DATE_NEXT,
297
                date::DATE_AFTER,
298
                date::DATE_CURRENT,
299
            ])
300
        );
1 efrain 301
    }
302
 
303
    /**
304
     * Add actions to report
305
     */
306
    protected function add_actions(): void {
307
        // Edit action.
308
        $this->add_action(new action(
309
            new moodle_url('#'),
310
            new pix_icon('t/edit', ''),
311
            ['data-action' => 'schedule-edit', 'data-schedule-id' => ':id'],
312
            false,
313
            new lang_string('editscheduledetails', 'core_reportbuilder')
314
        ));
315
 
316
        // Send now action.
317
        $this->add_action((new action(
318
            new moodle_url('#'),
319
            new pix_icon('t/email', ''),
320
            ['data-action' => 'schedule-send', 'data-schedule-id' => ':id', 'data-schedule-name' => ':name'],
321
            false,
322
            new lang_string('sendschedule', 'core_reportbuilder')
323
        ))
324
            ->add_callback(function(stdClass $row): bool {
325
 
326
                // Ensure data name attribute is properly formatted.
327
                $row->name = (new schedule(0, $row))->get_formatted_name(
328
                    context::instance_by_id($row->contextid));
329
 
330
                return true;
331
            })
332
        );
333
 
334
        // Delete action.
335
        $this->add_action((new action(
336
            new moodle_url('#'),
337
            new pix_icon('t/delete', ''),
338
            [
339
                'data-action' => 'schedule-delete',
340
                'data-schedule-id' => ':id',
341
                'data-schedule-name' => ':name',
342
                'class' => 'text-danger',
343
            ],
344
            false,
345
            new lang_string('deleteschedule', 'core_reportbuilder')
346
        ))
347
            ->add_callback(function(stdClass $row): bool {
348
 
349
                // Ensure data name attribute is properly formatted.
350
                $row->name = (new schedule(0, $row))->get_formatted_name(
351
                    context::instance_by_id($row->contextid));
352
 
353
                return true;
354
            })
355
        );
356
    }
357
}