Skip to content

Commit

Permalink
Bug fixes after dev
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyforth committed Feb 8, 2011
1 parent cd09a44 commit 7194ef0
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 65 deletions.
2 changes: 1 addition & 1 deletion lang/en/admin.php
Expand Up @@ -450,7 +450,7 @@
$string['denyemailaddresses'] = 'Denied email domains';
$string['development'] = 'Development';
$string['devicedetectregex'] = 'Device detection regular expressions';
$string['devicedetectregex_desc'] = 'Additional regular expressions for detecting individual devices or browsers or types of devices or browsers. Only applied if enabledevicedetection is set to yes. Returns value if expression is met. Moodle returns the values \'mobile\', \'tablet\', \'legacy\' and \'default\'. These should not be used.';
$string['devicedetectregex_desc'] = 'Additional regular expressions for detecting types of devices or browsers. Only applied if enabledevicedetection is set to yes. Returns value if expression is met. Moodle returns the values \'mobile\', \'tablet\', \'legacy\' and \'default\'. These should not be used.';
$string['devicedetectregexexpression'] = 'Regular expression';
$string['devicedetectregexvalue'] = 'Return value';
$string['devicetype'] = 'Device type';
Expand Down
14 changes: 7 additions & 7 deletions lib/adminlib.php
Expand Up @@ -7324,7 +7324,7 @@ class admin_setting_devicedetectregex extends admin_setting {
public function __construct() {
global $CFG;

parent::__construct('devicedetectregex', get_string('devicedetectregex', 'admin'), get_string('devicedetectregex_desc', 'admin'));
parent::__construct('devicedetectregex', get_string('devicedetectregex', 'admin'), get_string('devicedetectregex_desc', 'admin'), '');
}

/**
Expand Down Expand Up @@ -7446,10 +7446,10 @@ protected function prepare_form_data($regexes) {

foreach($regexes as $regex) {
$expressionname = 'expression'.$i;
$valuename = 'value'.$i;
$valuename = 'value'.$i;

$form[$expressionname] = $regex->$expressionname;
$form[$valuename] = $regex->$valuename;
$form[$expressionname] = $regex->expression;
$form[$valuename] = $regex->value;
$i++;
}

Expand Down Expand Up @@ -7484,9 +7484,9 @@ protected function process_form_data(array $form) {
continue;
}

$regex = new stdClass();
$regex->$expressionname = $expression;
$regex->$valuename = $value;
$regex = new stdClass();
$regex->expression = $expression;
$regex->value = $value;

$regexes[] = $regex;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/db/upgrade.php
Expand Up @@ -6010,7 +6010,11 @@ function xmldb_main_upgrade($oldversion) {
}

//remove the old theme and themelegacy fields and add any stored settings to the new themes setting.
if ($oldversion < 2011020400) {
if ($oldversion < 2011020700) {
$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();
Expand All @@ -6028,17 +6032,15 @@ function xmldb_main_upgrade($oldversion) {
$config_themes[] = $theme_obj;

$table = new xmldb_table('config');

$table = new xmldb_table('message');
$field = new xmldb_field('theme');

if (!$dbman->field_exists($table, $field)) {
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

$field = new xmldb_field('themelegacy');

if (!$dbman->field_exists($table, $field)) {
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}

Expand All @@ -6047,6 +6049,7 @@ function xmldb_main_upgrade($oldversion) {
$dbman->add_field($table, $field);

set_config('themes', json_encode($config_themes));
set_config('enabledevicedetection', 1);
}

return true;
Expand Down
93 changes: 53 additions & 40 deletions lib/moodlelib.php
Expand Up @@ -7556,40 +7556,29 @@ function get_device_type(){

$useragent = $_SERVER['HTTP_USER_AGENT'];

if(!empty($CFG->devicedetectionregex)){
$regexes = json_decode($CFG->devicedetectionregex);

$i = 0;

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

foreach($regexes as $regex){
$expressionname = 'expression'.$i;
$valuename = 'value'.$i;

if(preg_match($regex->$expressionname,$useragent)){
return $regex->$valuename;
if(preg_match($regex->expression,$useragent)){
return $regex->value;
}
}
}

if(check_browser_version('MSIE',6.0)) {
return 'legacy';
}


$tablet_regex = '/Tablet browser|iPad|iProd|GT-P1000|GT-I9000|SHW-M180S|SGH-T849|SCH-I800|Build/ERE27|sholest/i';


$tablet_regex = '/Tablet browser|iPad|iProd|GT-P1000|GT-I9000|SHW-M180S|SGH-T849|SCH-I800|Build\/ERE27|sholest/i';

if(preg_match($tablet_regex,$useragent)){
return 'tablet';
}

//mobile detection regex from detectmobilebrowser.com
$mobile_regexs[0] = '/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i';
$mobile_regexs[1] = '/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i';

foreach($mobile_regexs as $mobile_regex){
if(preg_match($mobile_regex,substr($useragent,0,4))){
return 'mobile';
}
//mobile detection PHP direct copy from open source detectmobilebrowser.com
if(preg_match('/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i',substr($useragent,0,4))){
return 'mobile';
}

return 'default';
Expand All @@ -7606,17 +7595,11 @@ function get_device_type_list($inc_user_types = true){

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

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

$i = 0;

foreach($regexes as $regex){
$device_name = 'value'.$i;

$types[] = $regex->$device_name;

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

foreach($regexes as $regex){
$types[] = $regex->value;
}
}

Expand All @@ -7626,27 +7609,57 @@ function get_device_type_list($inc_user_types = true){

/**
* Returns the theme selected for a particular device or false if none selected.
* @param string $device_type
* @param string $themes
* @param string $device_type
* @return string $theme or boolean false
*/
function get_theme_for_device_type($themes, $device_type = null){
function get_selected_theme_for_device_type($themes, $device_type = null){

if(empty($device_type)){
$device_type = get_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(get_user_switched_theme($device_type)){
$device_type = 'default';
}

$themes = json_decode($themes);

foreach($themes as $theme){
if($theme->device == $device_type){
return $theme->themename;
if(!empty($themes)){
foreach($themes as $theme){
if($theme->device == $device_type){
return $theme->themename;
}
}
}

return false;
}

/**
* Returns true or false if the user has switched theme to default for a device type
* @param string $device_type
*/
function get_user_switched_theme($device_type = 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)){
return true;
}
}

return false;
}


/**
* 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
2 changes: 1 addition & 1 deletion lib/outputrenderers.php
Expand Up @@ -2602,7 +2602,7 @@ protected function theme_switch_links(){

global $USER, $PAGE;

$switched = (!empty($USER->themeswitch));
$switched = get_user_switched_theme();

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

Expand Down
4 changes: 2 additions & 2 deletions lib/pagelib.php
Expand Up @@ -1273,7 +1273,7 @@ protected function resolve_theme() {
} else {
$device_type = get_device_type();
}

$theme = '';

foreach ($themeorder as $themetype) {
Expand Down Expand Up @@ -1316,7 +1316,7 @@ protected function resolve_theme() {
$this->_legacythemeinuse = true;
}

return get_theme_for_device_type($CFG->themes);
return get_selected_theme_for_device_type($CFG->themes);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions theme/index.php
Expand Up @@ -49,7 +49,7 @@

foreach($config_themes as $config_theme){
if($config_theme->device == $device){
unset($config_themes[$i]);
array_splice($config_themes,$i,1);
break;
}
$i++;
Expand Down Expand Up @@ -108,7 +108,11 @@

$row[] = $device;

$themename = get_theme_for_device_type($CFG->themes, $device);
$themename = get_selected_theme_for_device_type($CFG->themes, $device);

if(!$themename && $device == 'default'){
$themename = theme_config::DEFAULT_THEME;
}

if($themename){
$strthemename = get_string('pluginname', 'theme_'.$themename);
Expand Down Expand Up @@ -171,7 +175,7 @@
$rowclasses = array();

// Set up bools whether this theme is chosen either main or legacy
$ischosentheme = ($themename == get_theme_for_device_type($CFG->themes, $device));
$ischosentheme = ($themename == get_selected_theme_for_device_type($CFG->themes, $device));

if ($ischosentheme) {
// Is the chosen main theme
Expand Down
38 changes: 33 additions & 5 deletions theme/switch.php
Expand Up @@ -3,13 +3,41 @@

$url = required_param('url', PARAM_TEXT);

if(!empty($USER->switchtheme)){
$pref = 0;
$current_prefs = get_user_preferences('switchthemes');

$current_prefs = json_decode($current_prefs, true);
$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 {
$pref = 1;
$current_prefs = array();
}

set_user_preference('switchtheme',$pref,$USER->id);

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);

redirect($url);
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2011020400; // YYYYMMDD = date of the last version bump
$version = 2011020700; // YYYYMMDD = date of the last version bump
// XX = daily increments

$release = '2.0.1+ (Build: 20110112)'; // Human-friendly version name

0 comments on commit 7194ef0

Please sign in to comment.