MDL-51739 theme: allow config to lock theme selector
authorRyan Wyllie <ryan@moodle.com>
Thu, 15 Oct 2015 07:29:02 +0000 (07:29 +0000)
committerRyan Wyllie <ryan@moodle.com>
Fri, 23 Oct 2015 05:06:21 +0000 (05:06 +0000)
If any of the devices have their theme set in config.php
it will lock the theme selector UI from being able to change
the theme.

lib/outputlib.php
theme/index.php

index 87e5c73..e1ada8a 100644 (file)
@@ -96,6 +96,32 @@ function theme_get_revision() {
     }
 }
 
+/**
+ * Checks if the given device has a theme defined in config.php.
+ *
+ * @return bool
+ */
+function theme_is_device_locked($device) {
+    global $CFG;
+    $themeconfigname = core_useragent::get_device_type_cfg_var_name($device);
+    return isset($CFG->config_php_settings[$themeconfigname]);
+}
+
+/**
+ * Returns the theme named defined in config.php for the given device.
+ *
+ * @return string or null
+ */
+function theme_get_locked_theme_for_device($device) {
+    global $CFG;
+
+    if (!theme_is_device_locked($device)) {
+        return null;
+    }
+
+    $themeconfigname = core_useragent::get_device_type_cfg_var_name($device);
+    return $CFG->config_php_settings[$themeconfigname];
+}
 
 /**
  * This class represents the configuration variables of a Moodle theme.
index 1dbeded..788e3e8 100644 (file)
@@ -45,9 +45,10 @@ unset($SESSION->theme);
 if ($reset and confirm_sesskey()) {
     theme_reset_all_caches();
 
-} else if ($choose && $device && !$unsettheme && confirm_sesskey()) {
+} else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
     // Load the theme to make sure it is valid.
     $theme = theme_config::load($choose);
+
     // Get the config argument for the chosen device.
     $themename = core_useragent::get_device_type_cfg_var_name($device);
     set_config($themename, $theme->name);
@@ -73,7 +74,7 @@ if ($reset and confirm_sesskey()) {
     echo $output->continue_button($CFG->wwwroot . '/theme/index.php');
     echo $output->footer();
     exit;
-} else if ($device && $unsettheme && confirm_sesskey() && ($device != 'default')) {
+} else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
     // Unset the theme and continue.
     unset_config(core_useragent::get_device_type_cfg_var_name($device));
     $device = '';
@@ -104,6 +105,7 @@ if (!empty($CFG->enabledevicedetection) && empty($device)) {
         if (!$themename && $thedevice == 'default') {
             $themename = theme_config::DEFAULT_THEME;
         }
+        $themelocked = theme_is_device_locked($thedevice);
 
         $screenshotcell = $strthemenotselected;
         $unsetthemebutton = '';
@@ -125,7 +127,7 @@ if (!empty($CFG->enabledevicedetection) && empty($device)) {
                 $headingthemename = $OUTPUT->heading($strthemename, 3);
             }
             // If not default device then show option to unset theme.
-            if ($thedevice != 'default') {
+            if ($thedevice != 'default' && !$themelocked) {
                 $unsetthemestr = get_string('unsettheme', 'admin');
                 $unsetthemeurl = new moodle_url('/theme/index.php',
                     array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
@@ -135,12 +137,21 @@ if (!empty($CFG->enabledevicedetection) && empty($device)) {
         }
 
         $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
-        $select = new single_button($deviceurl, $strthemeselect, 'get');
+
+        $select = '';
+        if (!$themelocked) {
+            $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
+        }
+
+        $lockwarning = '';
+        if ($themelocked) {
+            $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
+        }
 
         $table->data[] = array(
             $OUTPUT->heading(ucfirst($thedevice), 3),
             $screenshotcell,
-            $headingthemename . $OUTPUT->render($select) . $unsetthemebutton
+            $headingthemename . $lockwarning . $select . $unsetthemebutton
         );
     }
 } else {
@@ -152,10 +163,19 @@ if (!empty($CFG->enabledevicedetection) && empty($device)) {
         $device = core_useragent::get_device_type();
     }
 
+    $themelocked = theme_is_device_locked($device);
     $table->id = 'adminthemeselector';
     $table->head = array(get_string('theme'), get_string('info'));
 
-    $themes = core_component::get_plugin_list('theme');
+    $themes = array();
+    if ($themelocked) {
+        $heading = get_string('currenttheme', 'admin');
+        $themename = theme_get_locked_theme_for_device($device);
+        $themedirectory = core_component::get_plugin_directory('theme', $themename);
+        $themes[$themename] = $themedirectory;
+    } else {
+        $themes = core_component::get_plugin_list('theme');
+    }
 
     foreach ($themes as $themename => $themedir) {
 
@@ -199,19 +219,25 @@ if (!empty($CFG->enabledevicedetection) && empty($device)) {
         // Contents of the second cell.
         $infocell = $OUTPUT->heading($strthemename, 3);
 
+        if ($themelocked) {
+            $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
+        }
+
         // Button to choose this as the main theme or unset this theme for devices other then default.
-        if (($ischosentheme) && ($device != 'default')) {
-            $unsetthemestr = get_string('unsettheme', 'admin');
-            $unsetthemeurl = new moodle_url('/theme/index.php',
-                array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
-            $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
-            $infocell .= $OUTPUT->render($unsetbutton);
-        } else if ((!$ischosentheme)) {
-            $setthemestr = get_string('usetheme');
-            $setthemeurl = new moodle_url('/theme/index.php',
-                array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
-            $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
-            $infocell .= $OUTPUT->render($setthemebutton);
+        if (!$themelocked) {
+            if (($ischosentheme) && ($device != 'default')) {
+                $unsetthemestr = get_string('unsettheme', 'admin');
+                $unsetthemeurl = new moodle_url('/theme/index.php',
+                    array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
+                $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
+                $infocell .= $OUTPUT->render($unsetbutton);
+            } else if ((!$ischosentheme)) {
+                $setthemestr = get_string('usetheme');
+                $setthemeurl = new moodle_url('/theme/index.php',
+                    array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
+                $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
+                $infocell .= $OUTPUT->render($setthemebutton);
+            }
         }
 
         $row[] = $infocell;