Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 core_webservice\reportbuilder\local\entities;
20
 
21
use core_collator;
22
use lang_string;
23
use core_reportbuilder\local\entities\base;
24
use core_reportbuilder\local\filters\autocomplete;
25
use core_reportbuilder\local\report\column;
26
use core_reportbuilder\local\report\filter;
27
 
28
/**
29
 * External service report builder entity
30
 *
31
 * @package    core_webservice
32
 * @copyright  2023 Mikel Martín <mikel@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class service extends base {
36
 
37
    /**
38
     * Database tables that this entity uses and their default aliases
39
     *
40
     * @return array
41
     */
42
    protected function get_default_tables(): array {
43
        return [
44
            'external_services',
45
        ];
46
    }
47
 
48
    /**
49
     * The default title for this entity in the list of columns/conditions/filters in the report builder
50
     *
51
     * @return lang_string
52
     */
53
    protected function get_default_entity_title(): lang_string {
54
        return new lang_string('service', 'core_webservice');
55
    }
56
 
57
    /**
58
     * Initialise the entity
59
     *
60
     * @return base
61
     */
62
    public function initialise(): base {
63
        $columns = $this->get_all_columns();
64
        foreach ($columns as $column) {
65
            $this->add_column($column);
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
        $tokenalias = $this->get_table_alias('external_services');
85
 
86
        // Service name column.
87
        $columnns[] = (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("{$tokenalias}.name, {$tokenalias}.shortname")
95
            ->set_is_sortable(true)
96
            ->add_callback(static function(?string $value, \stdClass $row): string {
97
                $output = $value;
98
                $output .= \html_writer::tag('div', format_text($row->shortname), [
99
                    'class' => 'small text-muted',
100
                ]);
101
                return $output;
102
            });
103
 
104
        return $columnns;
105
    }
106
 
107
    /**
108
     * Return list of all available filters
109
     *
110
     * @return filter[]
111
     */
112
    protected function get_all_filters(): array {
113
        global $DB;
114
 
115
        $tablealias = $this->get_table_alias('external_services');
116
 
117
        // Service Name filter.
118
        $filters[] = (new filter(
119
            autocomplete::class,
120
            'name',
121
            new lang_string('name'),
122
            $this->get_entity_name(),
123
            "{$tablealias}.name"
124
        ))
125
            ->add_joins($this->get_joins())
126
            ->set_options_callback(static function(): array {
127
                global $DB;
128
                $names = $DB->get_fieldset_sql('SELECT DISTINCT name FROM {external_services} ORDER BY name ASC');
129
 
130
                $options = [];
131
                foreach ($names as $name) {
132
                    $options[$name] = $name;
133
                }
134
 
135
                core_collator::asort($options);
136
                return $options;
137
            });
138
 
139
        return $filters;
140
    }
141
}