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\form;
18
 
19
use moodleform;
20
use core\output\theme_usage;
21
 
22
/**
23
 * Defines the form for generating theme usage report data.
24
 *
25
 * @package    report_themeusage
26
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL Juv3 or later
28
 */
29
class theme_usage_form extends moodleform {
30
 
31
    /**
32
     * Build the form definition.
33
     */
34
    protected function definition() {
35
        $mform = $this->_form;
36
 
37
        // Theme choices (e.g. boost, classic).
38
        $themechoice = $this->_customdata['themechoice'];
39
        $themechoices = array_merge(['' => get_string('select') . '...'], self::get_theme_choices());
40
        $mform->addElement('select', 'themechoice', get_string('themename', 'report_themeusage'), $themechoices);
41
        $mform->setType('themechoice', PARAM_TEXT);
42
        $mform->addRule('themechoice', get_string('required'), 'required', null, 'client');
43
        if (!empty($themechoice)) {
44
            $mform->setDefault('themechoice', $themechoice);
45
        }
46
 
47
        // Theme usage types (e.g. user, course, cohort, category).
48
        $typechoices = self::get_type_choices();
49
        $mform->addElement('select', 'typechoice', get_string('usagetype', 'report_themeusage'), $typechoices);
50
        $mform->setType('typechoice', PARAM_TEXT);
51
        $mform->addRule('typechoice', get_string('required'), 'required', null, 'client');
52
        $mform->setDefault(theme_usage::THEME_USAGE_TYPE_ALL, $themechoice);
53
 
54
        // Submit button.
55
        $mform->addElement('submit', 'submit', get_string('getreport', 'report_themeusage'));
56
    }
57
 
58
    /**
59
     * Get a list of available theme usage types.
60
     *
61
     * @return array
62
     */
63
    public static function get_type_choices(): array {
64
        return [
65
            theme_usage::THEME_USAGE_TYPE_ALL => get_string('all'),
66
            theme_usage::THEME_USAGE_TYPE_USER => get_string('user'),
67
            theme_usage::THEME_USAGE_TYPE_COURSE => get_string('course'),
68
            theme_usage::THEME_USAGE_TYPE_COHORT => get_string('cohort', 'cohort'),
69
            theme_usage::THEME_USAGE_TYPE_CATEGORY => get_string('category'),
70
        ];
71
    }
72
 
73
    /**
74
     * Get a list of available themes.
75
     *
76
     * @return array
77
     */
78
    public static function get_theme_choices(): array {
79
        $themes = \core_component::get_plugin_list('theme');
80
        foreach ($themes as $themename => $themedir) {
81
            $themechoices[$themename] = get_string('pluginname', 'theme_'.$themename);
82
        }
83
        return $themechoices;
84
    }
85
 
86
    /**
87
     * Check the requested theme is in a list of available themes.
88
     *
89
     * @param string $themechoice The theme name.
90
     * @return bool
91
     */
92
    public static function validate_theme_choice_param(string $themechoice): bool {
93
        if (!empty($themechoice) && !array_key_exists($themechoice, self::get_theme_choices())) {
94
            return false;
95
        }
96
        return true;
97
    }
98
}