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_competency\reportbuilder\local\entities;
20
 
21
use core\lang_string;
22
use core_competency\{competency, user_competency};
23
use core_reportbuilder\local\entities\base;
24
use core_reportbuilder\local\filters\{boolean_select, select};
25
use core_reportbuilder\local\helpers\format;
26
use core_reportbuilder\local\report\{column, filter};
27
use stdClass;
28
 
29
/**
30
 * User competency entity
31
 *
32
 * @package     core_competency
33
 * @copyright   2024 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class usercompetency extends base {
37
 
38
    /**
39
     * Database tables that this entity uses
40
     *
41
     * @return string[]
42
     */
43
    protected function get_default_tables(): array {
44
        return [
45
            'competency_usercomp',
46
        ];
47
    }
48
 
49
    /**
50
     * The default title for this entity
51
     *
52
     * @return lang_string
53
     */
54
    protected function get_default_entity_title(): lang_string {
55
        return new lang_string('usercompetency', 'core_competency');
56
    }
57
 
58
    /**
59
     * Initialise the entity
60
     *
61
     * @return base
62
     */
63
    public function initialise(): base {
64
        $columns = $this->get_all_columns();
65
        foreach ($columns as $column) {
66
            $this->add_column($column);
67
        }
68
 
69
        // All the filters defined by the entity can also be used as conditions.
70
        $filters = $this->get_all_filters();
71
        foreach ($filters as $filter) {
72
            $this
73
                ->add_filter($filter)
74
                ->add_condition($filter);
75
        }
76
 
77
        return $this;
78
    }
79
 
80
    /**
81
     * Returns list of all available columns
82
     *
83
     * @return column[]
84
     */
85
    protected function get_all_columns(): array {
86
        $usercompetencyalias = $this->get_table_alias('competency_usercomp');
87
 
88
        // Status.
89
        $columns[] = (new column(
90
            'status',
91
            new lang_string('status'),
92
            $this->get_entity_name(),
93
        ))
94
            ->add_joins($this->get_joins())
95
            ->set_type(column::TYPE_TEXT)
96
            ->add_field("{$usercompetencyalias}.status")
97
            ->set_is_sortable(true)
98
            ->add_callback(static function(?string $status): string {
99
                if ($status === null) {
100
                    return '';
101
                }
102
 
103
                return (string) user_competency::get_status_name((int) $status);
104
            });
105
 
106
        // Proficient.
107
        $columns[] = (new column(
108
            'proficient',
109
            new lang_string('proficient', 'core_competency'),
110
            $this->get_entity_name(),
111
        ))
112
            ->add_joins($this->get_joins())
113
            ->set_type(column::TYPE_BOOLEAN)
114
            ->add_field("{$usercompetencyalias}.proficiency")
115
            ->set_is_sortable(true)
116
            ->add_callback([format::class, 'boolean_as_text']);
117
 
118
        // Rating.
119
        $columns[] = (new column(
120
            'rating',
121
            new lang_string('rating', 'core_competency'),
122
            $this->get_entity_name(),
123
        ))
124
            ->add_joins($this->get_joins())
125
            ->add_fields("{$usercompetencyalias}.grade, {$usercompetencyalias}.competencyid")
126
            ->add_callback(static function(?string $grade, stdClass $row): string {
127
                if ($grade === null) {
128
                    return '';
129
                }
130
 
131
                $competency = new competency($row->competencyid);
132
                $scale = $competency->get_scale()->scale_items;
133
 
134
                return (string) ($scale[(int) $grade - 1] ?? $grade);
135
            });
136
 
137
        return $columns;
138
    }
139
 
140
    /**
141
     * Return list of all available filters
142
     *
143
     * @return filter[]
144
     */
145
    protected function get_all_filters(): array {
146
        $usercompetencyalias = $this->get_table_alias('competency_usercomp');
147
 
148
        // Status.
149
        $filters[] = (new filter(
150
            select::class,
151
            'status',
152
            new lang_string('status'),
153
            $this->get_entity_name(),
154
            "{$usercompetencyalias}.status",
155
        ))
156
            ->add_joins($this->get_joins())
157
            ->set_options_callback([user_competency::class, 'get_status_list']);
158
 
159
        // Proficient.
160
        $filters[] = (new filter(
161
            boolean_select::class,
162
            'proficient',
163
            new lang_string('proficient', 'core_competency'),
164
            $this->get_entity_name(),
165
            "{$usercompetencyalias}.proficiency",
166
        ))
167
            ->add_joins($this->get_joins());
168
 
169
        return $filters;
170
    }
171
}