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
namespace core\output;
18
 
19
/**
20
 * This class houses methods for checking theme usage in a given context.
21
 *
22
 * @package    core
23
 * @category   output
24
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class theme_usage {
28
    /** @var string The theme usage type for users. */
29
    public const THEME_USAGE_TYPE_USER = 'user';
30
 
31
    /** @var string The theme usage type for courses. */
32
    public const THEME_USAGE_TYPE_COURSE = 'course';
33
 
34
    /** @var string The theme usage type for cohorts. */
35
    public const THEME_USAGE_TYPE_COHORT = 'cohort';
36
 
37
    /** @var string The theme usage type for categories. */
38
    public const THEME_USAGE_TYPE_CATEGORY = 'category';
39
 
40
    /** @var string The theme usage type for all. */
41
    public const THEME_USAGE_TYPE_ALL = 'all';
42
 
43
    /** @var int The theme is used in context. */
44
    public const THEME_IS_USED = 1;
45
 
46
    /** @var int The theme is not used in context. */
47
    public const THEME_IS_NOT_USED = 0;
48
 
49
    /**
50
     * Check if the theme is used in any context (e.g. user, course, cohort, category).
51
     *
52
     * This query is cached.
53
     *
54
     * @param string $themename The theme to check.
55
     * @return int Return 1 if at least one record was found, 0 if none.
56
     */
57
    public static function is_theme_used_in_any_context(string $themename): int {
58
        global $DB;
59
        $cache = \cache::make('core', 'theme_usedincontext');
60
        $isused = $cache->get($themename);
61
 
62
        if ($isused === false) {
63
            $sqlunions = [];
64
 
65
            // For each context, check if the config is enabled and there is at least one use.
66
            if (get_config('core', 'allowuserthemes')) {
67
                $sqlunions[self::THEME_USAGE_TYPE_USER] = "
68
                        SELECT u.id
69
                            FROM {user} u
70
                            WHERE u.theme = :usertheme
71
                            ";
72
            }
73
 
74
            if (get_config('core', 'allowcoursethemes')) {
75
                $sqlunions[self::THEME_USAGE_TYPE_COURSE] = "
76
                        SELECT c.id
77
                            FROM {course} c
78
                            WHERE c.theme = :coursetheme
79
                            ";
80
            }
81
 
82
            if (get_config('core', 'allowcohortthemes')) {
83
                $sqlunions[self::THEME_USAGE_TYPE_COHORT] = "
84
                        SELECT co.id
85
                            FROM {cohort} co
86
                            WHERE co.theme = :cohorttheme
87
                            ";
88
            }
89
 
90
            if (get_config('core', 'allowcategorythemes')) {
91
                $sqlunions[self::THEME_USAGE_TYPE_CATEGORY] = "
92
                        SELECT cat.id
93
                            FROM {course_categories} cat
94
                            WHERE cat.theme = :categorytheme
95
                            ";
96
            }
97
 
98
            // Union the sql statements from the different tables.
99
            if (!empty($sqlunions)) {
100
                $sql = implode(' UNION ', $sqlunions);
101
 
102
                // Prepare params.
103
                $params = [];
104
                foreach ($sqlunions as $type => $val) {
105
                    $params[$type . 'theme'] = $themename;
106
                }
107
 
108
                $result = $DB->record_exists_sql($sql, $params);
109
            }
110
 
111
            if (!empty($result)) {
112
                $isused = self::THEME_IS_USED;
113
            } else {
114
                $isused = self::THEME_IS_NOT_USED;
115
            }
116
 
117
            // Cache the result so we don't have to keep checking for this theme.
118
            $cache->set($themename, $isused);
119
            return $isused;
120
        } else {
121
            return $isused;
122
        }
123
    }
124
}