Skip to content

Commit

Permalink
bug fixes and comments from the community and OU
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyforth committed Mar 11, 2011
1 parent 6b6a72f commit b458f94
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 101 deletions.
16 changes: 10 additions & 6 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7394,7 +7394,11 @@ public function get_setting() {
* @return bool
*/
public function write_setting($data) {
if ($this->config_write($this->name, $this->process_form_data($data))) {
if (empty($data)) {
$data = array();
}

if ($this->config_write($this->name, $this->process_form_data($data))) {
return ''; // success
} else {
return get_string('errorsetting', 'admin') . $this->visiblename . html_writer::empty_tag('br');
Expand Down Expand Up @@ -7461,13 +7465,13 @@ public function output_html($data, $query='') {
)
), array('class' => 'c'.$i)
);

$out .= html_writer::end_tag('tr');
}

$out .= html_writer::end_tag('tbody');
$out .= html_writer::end_tag('table');

return format_admin_setting($this, $this->visiblename, $out, $this->description, false, '', NULL, $query);
}

Expand All @@ -7485,7 +7489,7 @@ protected function prepare_form_data($regexes) {
$form = array();

$i = 0;

foreach($regexes as $regex) {
$expressionname = 'expression'.$i;
$valuename = 'value'.$i;
Expand Down Expand Up @@ -7530,7 +7534,7 @@ protected function process_form_data(array $form) {
$regex->expression = $expression;
$regex->value = $value;

$regexes[] = $regex;
$regexes[] = $regex;
}

$regexes = json_encode($regexes);
Expand Down
47 changes: 16 additions & 31 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6063,44 +6063,29 @@ function xmldb_main_upgrade($oldversion) {

//remove the old theme and themelegacy fields and add any stored settings to the new themes setting.
if ($oldversion < 2011022100.01) {
set_config('enabledevicedetection',1);

$table = new xmldb_table('config');
$field = new xmldb_field('themes');
$dbman->drop_field($table, $field);

$theme = $DB->get_record('config', array('name'=>'theme'));

$theme_obj = new stdClass();
$theme_obj->device = 'default';
$theme_obj->themename = $theme->value;

$config_themes[] = $theme_obj;

$themelegacy = $DB->get_record('config', array('name'=>'themelegacy'));

$theme_obj = new stdClass();
$theme_obj->device = 'legacy';
$theme_obj->themename = $themelegacy->value;
if (!empty($CFG->themes)) {
unset_config('themes');
}

$config_themes[] = $theme_obj;
if (!empty($CFG->theme)) {
$theme_obj = new stdClass();
$theme_obj->device = 'default';
$theme_obj->themename = $CFG->theme;

$table = new xmldb_table('config');
$field = new xmldb_field('theme');
$config_themes[] = $theme_obj;

if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
unset_config('theme');
}

$field = new xmldb_field('themelegacy');
if (!empty($CFG->themelegacy)) {
$theme_obj = new stdClass();
$theme_obj->device = 'legacy';
$theme_obj->themename = $CFG->themelegacy;

if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
$config_themes[] = $theme_obj;

$field = new xmldb_field('themes');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null);
$dbman->add_field($table, $field);
unset_config('themelegacy');
}

set_config('themes', json_encode($config_themes));
set_config('enabledevicedetection', 1);
Expand Down
78 changes: 69 additions & 9 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7595,10 +7595,10 @@ function get_device_type_list($inc_user_types = true){

$types = array('default','legacy','mobile','tablet');

if($inc_user_types && !empty($CFG->devicedetectregex)){
if($inc_user_types && !empty($CFG->devicedetectregex)){
$regexes = json_decode($CFG->devicedetectregex);

foreach($regexes as $regex){
foreach($regexes as $regex){
$types[] = $regex->value;
}
}
Expand All @@ -7614,15 +7614,13 @@ function get_device_type_list($inc_user_types = true){
* @return string $theme or boolean false
*/
function get_selected_theme_for_device_type($themes, $device_type = null){

if(empty($device_type)){
$device_type = get_device_type();
}

//If a non-default device type is being used, and the user has switched theme, changr $device_type to default and we'll get the theme for that.

//If a non-default device type is being used, and the user has switched theme, change $device_type to default and we'll get the theme for that.
if(get_user_switched_theme($device_type)){
$device_type = 'default';
$device_type = 'default';
}

$themes = json_decode($themes);
Expand All @@ -7644,16 +7642,25 @@ function get_selected_theme_for_device_type($themes, $device_type = null){
* @param string $device_type
*/
function get_user_switched_theme($device_type = null){
global $CFG;

if(empty($device_type)){
if (empty($CFG->themes)) {
return null;
}

if (empty($device_type)) {
$device_type = get_device_type();
}

$switchthemes = get_user_preferences('switchthemes');
$switchthemes = json_decode($switchthemes);

foreach($switchthemes as $switch){
if($switch->device == $device_type && !empty($switch->switched)){
if (!is_array($switchthemes)) {
return false;
}

foreach ($switchthemes as $switch) {
if ($switch->device == $device_type && !empty($switch->switched)) {
return true;
}
}
Expand All @@ -7662,6 +7669,59 @@ function get_user_switched_theme($device_type = null){
}


function switch_theme($device_type = null) {
global $USER;

$current_prefs = get_user_preferences('switchthemes');
$current_prefs = json_decode($current_prefs, true);

if (is_null($device_type)) {
$device_type = get_device_type();
}

if(!empty($current_prefs)){
$i = 0;

foreach($current_prefs as $current){
if($current['device'] == $device_type){
$switched = $current['switched'];
array_splice($current_prefs,$i,1);
break;
}

$i++;
}
} else {
$current_prefs = array();
}

if(!empty($switched)){
$switched = 0;
} else {
$switched = 1;
}

$device_pref = array();
$device_pref['device'] = $device_type;
$device_pref['switched'] = $switched;

$current_prefs[] = $device_pref;

set_user_preference('switchthemes',json_encode($current_prefs),$USER->id);

//this is an OU customisation to support the OU mobile cookie - at present users cannot switch from the desktop view.
if ($device_type == 'mobile' && $switched == 1) {
ou_unset_mobile_cookie();
ou_set_fullsize_cookie();
}

if ($device_type == 'mobile' && $switched == 0) {
ou_set_mobile_cookie();
ou_unset_fullsize_cookie();
}
}


/**
* Returns one or several CSS class names that match the user's browser. These can be put
* in the body tag of the page to apply browser-specific rules without relying on CSS hacks
Expand Down
15 changes: 9 additions & 6 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2595,30 +2595,33 @@ protected function render_custom_menu_item(custom_menu_item $menunode) {
* Renders theme links for switching between default and other themes.
*/
protected function theme_switch_links(){

if($this->switchlinkdisplayed){
return '';
}

global $USER, $PAGE;

$switched = get_user_switched_theme();
$type = get_device_type();

$content = html_writer::start_tag('div', array('id'=>'theme_switch_link'));
$this->switchlinkdisplayed = true;

if($switched){
if (is_null($switched)) {
return '';
} else if(!$switched && $type == 'default') {
return '';
} else if ($switched) {
$link_text = get_string('switchdevicerecommended');
} else {
$link_text = get_string('switchdevicedefault');
$link_text = get_string('switchdevicedefault');
}

$content = html_writer::start_tag('div', array('id'=>'theme_switch_link'));
$link_url = new moodle_url('/theme/switch.php', array('url' => $PAGE->url));

$content .= html_writer::link($link_url, $link_text);
$content .= html_writer::end_tag('div');

$this->switchlinkdisplayed = true;

return $content;
}
}
Expand Down
75 changes: 68 additions & 7 deletions lib/pagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,12 +1274,17 @@ protected function resolve_theme() {
}
}

if(!empty($USER->themeswitch) || !empty($SESSION->themeswitch)){
if(!empty($USER->themeswitch) || !empty($SESSION->themeswitch)){
$device_type = 'default';
} else {
$device_type = get_device_type();
}

//set up of the new themes variable hasn't happened so use the old theme support.
if (empty($CFG->themes)) {
return $this->legacy_theme_support($themeorder, $mnetpeertheme);
}

$theme = '';

foreach ($themeorder as $themetype) {
Expand Down Expand Up @@ -1309,25 +1314,81 @@ protected function resolve_theme() {
if ($mnetpeertheme) {
return $mnetpeertheme;
} else {
return $USER->theme;
}
return $USER->theme;
}
}

case 'site':
if ($mnetpeertheme) {
return $mnetpeertheme;
}
}

if($device_type == 'legacy'){
$this->_legacythemeinuse = true;
$this->_legacythemeinuse = true;
}

return get_selected_theme_for_device_type($CFG->themes);
return get_selected_theme_for_device_type($CFG->themes);
}
}
}


/**
* Work out the theme this page should use if an upgrade to the new theme selection has not taken place.
*
* @param array $themeorder
* @param string $mnetpeertheme
* @return string the name of the theme that should be used on this page.
*/
protected function legacy_theme_support($themeorder, $mnetpeertheme) {
global $CFG;

$theme = '';

foreach ($themeorder as $themetype) {
switch ($themetype) {
case 'course':
if (!empty($CFG->allowcoursethemes) and !empty($this->course->theme)) {
return $this->course->theme;
}

case 'category':
if (!empty($CFG->allowcategorythemes)) {
$categories = $this->categories;
foreach ($categories as $category) {
if (!empty($category->theme)) {
return $category->theme;
}
}
}

case 'session':
if (!empty($SESSION->theme)) {
return $SESSION->theme;
}

case 'user':
if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) {
if ($mnetpeertheme) {
return $mnetpeertheme;
} else {
return $USER->theme;
}
}

case 'site':
if ($mnetpeertheme) {
return $mnetpeertheme;
} else if(!empty($CFG->themelegacy) && $this->browser_is_outdated()) {
$this->_legacythemeinuse = true;
return $CFG->themelegacy;
} else {
return $CFG->theme;
}
}
}
}

/**
* Sets ->pagetype from the script name. For example, if the script that was
* run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
Expand Down
8 changes: 5 additions & 3 deletions lib/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,10 @@ function stripslashes_deep($value) {
unset($urlthemename);

// Ensure a valid theme is set.
if (!isset($CFG->theme)) {
$CFG->theme = 'standardwhite';
if (!isset($CFG->themes)) {
$theme = 'standardwhite';
$current_prefs[] = array('default',$theme);
$CFG->themes = json_encode($current_prefs);
}

// Set language/locale of printed times. If user has chosen a language that
Expand Down Expand Up @@ -729,7 +731,7 @@ function stripslashes_deep($value) {
moodle_setlocale();

if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) {
if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation
if ($theme == 'standardwhite') { // Temporary measure to help with XHTML validation
if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest)
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
(strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
Expand Down
Loading

0 comments on commit b458f94

Please sign in to comment.