Move "Server's local time" to the top of the list?

Move "Server's local time" to the top of the list?

by Dustin Elliott -
Number of replies: 2
Hello,

In my Moodle it is very important for people to select their local time zone when setting up their account because times listed for Face-to-Face sessions are shown based on the students time zone set in their profile.

Because of that I have added the Timezone combo select box to the new account setup form. However that combo box is set to default to "Server's local time" which is at the bottom of the timezone list for the combo box.

Having the default selection at the bottom of the list seems to be very confusing for most people and is not the norm.

Is there a way to move the "Server's local time" option to the top of the Timezone list used to populate the Timezone combo box?

I have tried manipulating the code by moving the line $choices['99'] = get_string('serverlocaltime'); to come before $choices = get_list_of_timezones(); but that didn't seem to work. I'm not very proficient in PHP yet, sorry.

Original Code (I tried swapping lines 1 and 2):

$choices = get_list_of_timezones();
$choices['99'] = get_string('serverlocaltime');
if ($CFG->forcetimezone != 99) {
$mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
} else {
$mform->addElement('select', 'timezone', get_string('timezone'), $choices);
$mform->addRule('timezone', "Please set your local timezone", 'required', null, 'server');
$mform->setDefault('timezone', '99');
}
Average of ratings: -
In reply to Dustin Elliott

Re: Move "Server's local time" to the top of the list?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
$choices = get_list_of_timezones();
$choices['99'] = get_string('serverlocaltime');
Will assign output of get_list_of_timezones (an array) to $choices, then add get_string('serverlocaltime') to the array. However,

$choices['99'] = get_string('serverlocaltime');
$choices = get_list_of_timezones();
Will create an array containing get_string('serverlocaltime'), then overwrite it with the output of get_list_of_timezones();

Try something like
$choices = array();
$choices['99'] = get_string('serverlocaltime');
$choices = array_merge($choices, get_list_of_timezones());
That should create an array, add get_string('serverlocaltime') to it, then combine that array with the array output by get_list_of_timezones(), giving you an array containing all the timezones with (hopefully) yours at the top.
In reply to Mark Johnson

Re: Move "Server's local time" to the top of the list?

by Dustin Elliott -
Thanks Mark! Works perfectly and makes sense. big grin