Skip to content

Commit 40d397a

Browse files
committed
MDL-78427 core_theme: Add theme usage report and icon
In addition to adding in theme usage reports, there is also the addition of an icon on the theme cards which takes you to the report. This icon only appears for that theme if it has been used in any overriding context.
1 parent f30110b commit 40d397a

File tree

24 files changed

+1157
-10
lines changed

24 files changed

+1157
-10
lines changed

admin/settings/appearance.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,27 @@
293293
new lang_string('configthemedesignermode', 'admin'), 0);
294294
$setting->set_updatedcallback('theme_reset_all_caches');
295295
$temp->add($setting);
296-
$temp->add(new admin_setting_configcheckbox('allowuserthemes', new lang_string('allowuserthemes', 'admin'),
297-
new lang_string('configallowuserthemes', 'admin'), 0));
298-
$temp->add(new admin_setting_configcheckbox('allowcoursethemes', new lang_string('allowcoursethemes', 'admin'),
299-
new lang_string('configallowcoursethemes', 'admin'), 0));
300-
$temp->add(new admin_setting_configcheckbox('allowcategorythemes', new lang_string('allowcategorythemes', 'admin'),
301-
new lang_string('configallowcategorythemes', 'admin'), 0));
302-
$temp->add(new admin_setting_configcheckbox('allowcohortthemes', new lang_string('allowcohortthemes', 'admin'),
303-
new lang_string('configallowcohortthemes', 'admin'), 0));
296+
297+
$setting = new admin_setting_configcheckbox('allowuserthemes', new lang_string('allowuserthemes', 'admin'),
298+
new lang_string('configallowuserthemes', 'admin'), 0);
299+
$setting->set_updatedcallback('theme_purge_used_in_context_caches');
300+
$temp->add($setting);
301+
302+
$setting = new admin_setting_configcheckbox('allowcoursethemes', new lang_string('allowcoursethemes', 'admin'),
303+
new lang_string('configallowcoursethemes', 'admin'), 0);
304+
$setting->set_updatedcallback('theme_purge_used_in_context_caches');
305+
$temp->add($setting);
306+
307+
$setting = new admin_setting_configcheckbox('allowcategorythemes', new lang_string('allowcategorythemes', 'admin'),
308+
new lang_string('configallowcategorythemes', 'admin'), 0);
309+
$setting->set_updatedcallback('theme_purge_used_in_context_caches');
310+
$temp->add($setting);
311+
312+
$setting = new admin_setting_configcheckbox('allowcohortthemes', new lang_string('allowcohortthemes', 'admin'),
313+
new lang_string('configallowcohortthemes', 'admin'), 0);
314+
$setting->set_updatedcallback('theme_purge_used_in_context_caches');
315+
$temp->add($setting);
316+
304317
$temp->add(new admin_setting_configcheckbox('allowthemechangeonurl', new lang_string('allowthemechangeonurl', 'admin'),
305318
new lang_string('configallowthemechangeonurl', 'admin'), 0));
306319
$temp->add(new admin_setting_configcheckbox('allowuserblockhiding', new lang_string('allowuserblockhiding', 'admin'),

admin/templates/themeselector/theme_card.mustache

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"current": true,
2828
"actionurl": "http://moodlesite/admin/themeselector.php",
2929
"sesskey": "123XYZ",
30-
"settingsurl": "http://moodlesite/admin/settings.php?section=themesettingboost"
30+
"settingsurl": "http://moodlesite/admin/settings.php?section=themesettingboost",
31+
"reporturl": "http://moodlesite/report/themeusage/index.php?themechoice=boost"
3132
}
3233
}}
3334
<div class="card dashboard-card" role="listitem" id="theme-card-{{choose}}" aria-labelledby="theme-name-{{choose}} {{#current}}current-theme-{{choose}}{{/current}}">
@@ -53,6 +54,16 @@
5354
<i class="icon fa fa-info-circle m-0" aria-hidden="true"></i>
5455
<span class="sr-only">{{#str}}previewthemename, moodle, {{name}}{{/str}}</span>
5556
</button>
57+
{{#reporturl}}
58+
<a
59+
href="{{reporturl}}"
60+
id="theme-usage-report-{{choose}}"
61+
class="btn btn-link p-0 ml-2"
62+
title="{{#str}}themeusagereportname, admin, {{name}}{{/str}}">
63+
<i class="icon fa fa-area-chart m-0" aria-hidden="true"></i>
64+
<span class="sr-only">{{#str}}themeusagereportname, admin, {{name}}{{/str}}</span>
65+
</a>
66+
{{/reporturl}}
5667
{{#settingsurl}}
5768
<a
5869
href="{{settingsurl}}"

admin/themeselector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@
127127
$themedata['settingsurl'] = $settingsurl;
128128
}
129129

130+
// Link to the theme usage report if override enabled and it is being used in at least one context.
131+
if (\core\output\theme_usage::is_theme_used_in_any_context($themename) === \core\output\theme_usage::THEME_IS_USED) {
132+
$reporturl = new moodle_url($CFG->wwwroot . '/report/themeusage/index.php');
133+
$reporturl->params(['themechoice' => $themename]);
134+
$themedata['reporturl'] = $reporturl->out(false);
135+
}
136+
130137
$data[$index] = $themedata;
131138
$index++;
132139
}

cohort/lib.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ function cohort_update_cohort($cohort) {
101101
if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) {
102102
unset($cohort->theme);
103103
}
104+
105+
// Delete theme usage cache if the theme has been changed.
106+
if (isset($cohort->theme)) {
107+
$oldcohort = $DB->get_record('cohort', ['id' => $cohort->id]);
108+
if ($cohort->theme != $oldcohort->theme) {
109+
theme_delete_used_in_context_cache($cohort->theme, $oldcohort->theme);
110+
}
111+
}
112+
104113
$cohort->timemodified = time();
105114

106115
// Update custom fields if there are any of them in the form.

course/classes/category.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,14 @@ public function update($data, $editoroptions = null) {
628628
fix_course_sortorder();
629629
}
630630

631+
// Delete theme usage cache if the theme has been changed.
632+
if (isset($data->theme)) {
633+
$oldcategory = $DB->get_record('course_categories', ['id' => $data->id]);
634+
if ($data->theme != $oldcategory->theme) {
635+
theme_delete_used_in_context_cache($data->theme, (string)$oldcategory->theme);
636+
}
637+
}
638+
631639
$newcategory->timemodified = time();
632640

633641
$categorycontext = $this->get_context();

course/edit_form.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ function definition_after_data() {
488488
// Tweak the form with values provided by custom fields in use.
489489
$handler = core_course\customfield\course_handler::create();
490490
$handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
491-
492491
}
493492

494493
/**

course/lib.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,11 @@ function update_course($data, $editoroptions = NULL) {
24852485
$DB->delete_records('course_format_options',
24862486
array('courseid' => $course->id, 'format' => $oldcourse->format));
24872487
}
2488+
2489+
// Delete theme usage cache if the theme has been changed.
2490+
if (isset($data->theme) && ($data->theme != $oldcourse->theme)) {
2491+
theme_delete_used_in_context_cache($data->theme, $oldcourse->theme);
2492+
}
24882493
}
24892494

24902495
/**

lang/en/admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,8 @@
14631463
$string['themeselector'] = 'Themes';
14641464
$string['themesettingsadvanced'] = 'Advanced theme settings';
14651465
$string['themeeditsettingsname'] = 'Edit theme settings \'{$a}\'';
1466+
$string['themesettingsname'] = 'Theme settings \'{$a}\'';
1467+
$string['themeusagereportname'] = 'Theme usage report \'{$a}\'';
14661468
$string['therewereerrors'] = 'There were errors in your data';
14671469
$string['thirdpartylibrary'] = 'Library';
14681470
$string['thirdpartylibrarylocation'] = 'Location';

lang/en/cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
$string['cachedef_string'] = 'Language string cache';
9797
$string['cachedef_tags'] = 'Tags collections and areas';
9898
$string['cachedef_temp_tables'] = 'Temporary tables cache';
99+
$string['cachedef_theme_usedincontext'] = 'A theme has been used in context to override the default theme';
99100
$string['cachedef_userselections'] = 'Data used to persist user selections throughout Moodle';
100101
$string['cachedef_user_favourite_course_content_items'] = 'User\'s starred items';
101102
$string['cachedef_user_group_groupings'] = 'User\'s groupings and groups per course';

lib/classes/output/theme_usage.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}

0 commit comments

Comments
 (0)