Proyectos de Subversion Moodle

Rev

| 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 report_themeusage\reportbuilder\local\entities;
18
 
19
use core_reportbuilder\local\entities\base;
20
use core_reportbuilder\local\report\column;
21
use lang_string;
22
 
23
/**
24
 * Theme entity.
25
 *
26
 * Defines all the columns and filters that can be added to reports that use this entity.
27
 *
28
 * @package    report_themeusage
29
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class theme extends base {
33
 
34
    /**
35
     * Database tables that this entity uses.
36
     *
37
     * @return array
38
     */
39
    protected function get_default_tables(): array {
40
        return [
41
            'config_plugins',
42
        ];
43
    }
44
 
45
    /**
46
     * The default title for this entity.
47
     *
48
     * @return lang_string
49
     */
50
    protected function get_default_entity_title(): lang_string {
51
        return new lang_string('theme');
52
    }
53
 
54
    /**
55
     * Initialize the entity.
56
     *
57
     * @return base
58
     */
59
    public function initialise(): base {
60
        $columns = $this->get_all_columns();
61
        foreach ($columns as $column) {
62
            $this->add_column($column);
63
        }
64
 
65
        return $this;
66
    }
67
 
68
    /**
69
     * Returns list of all available columns.
70
     *
71
     * @return column[]
72
     */
73
    protected function get_all_columns(): array {
74
        global $DB;
75
        $themealias = $this->get_table_alias('config_plugins');
76
        $sqlsubstring = $DB->sql_substr("{$themealias}.plugin", 7);
77
 
78
        $courselabel = get_string('course');
79
        $cohortlabel = get_string('cohort', 'cohort');
80
        $userlabel = get_string('user');
81
        $categorylabel = get_string('category');
82
 
83
        // Force theme column.
84
        $columns[] = (new column(
85
            'forcetheme',
86
            new lang_string('forcetheme'),
87
            $this->get_entity_name()
88
        ))
89
            ->add_joins($this->get_joins())
90
            ->set_type(column::TYPE_TEXT)
91
            ->add_fields("{$themealias}.plugin")
92
            ->add_callback(static function(?string $theme): string {
93
                $theme = get_string('pluginname', $theme);
94
                return format_text($theme, FORMAT_PLAIN);
95
            });
96
 
97
        // Usage type column.
98
        $columns[] = (new column(
99
            'usagetype',
100
            new lang_string('usagetype', 'report_themeusage'),
101
            $this->get_entity_name()
102
        ))
103
            ->add_joins($this->get_joins())
104
            ->add_join("LEFT JOIN (
105
                           SELECT '{$courselabel}' AS usagetype, theme, COUNT(theme) AS themecount
106
                             FROM {course}
107
                            WHERE " . $DB->sql_isnotempty('course', 'theme', false, false) . "
108
                         GROUP BY theme
109
                            UNION
110
                           SELECT '{$userlabel}' AS usagetype, theme, COUNT(theme) AS themecount
111
                             FROM {user}
112
                            WHERE " . $DB->sql_isnotempty('user', 'theme', false, false) . "
113
                         GROUP BY theme
114
                            UNION
115
                           SELECT '{$cohortlabel}' AS usagetype, theme, COUNT(theme) AS themecount
116
                             FROM {cohort}
117
                            WHERE " . $DB->sql_isnotempty('cohort', 'theme', false, false) . "
118
                         GROUP BY theme
119
                            UNION
120
                           SELECT '{$categorylabel}' AS usagetype, theme, COUNT(theme) AS themecount
121
                             FROM {course_categories}
122
                            WHERE " . $DB->sql_isnotempty('course_categories', 'theme', false, false) . "
123
                         GROUP BY theme
124
                        ) tuse ON tuse.theme={$sqlsubstring}")
125
            ->set_type(column::TYPE_TEXT)
126
            ->add_fields("tuse.usagetype, tuse.themecount")
127
            ->add_callback(static function(?string $usagetype, \stdClass $row): string {
128
                $count = $row->themecount ?? 0;
129
                return format_text($usagetype . ' ('. $count . ')', FORMAT_PLAIN);
130
            });
131
 
132
        return $columns;
133
    }
134
}