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