mirror of
https://github.com/moodle/moodle.git
synced 2025-06-05 15:47:46 +02:00
Improvements to timezone import interface
This commit is contained in:
parent
c9e55a2583
commit
a1e93da26c
@ -67,13 +67,7 @@
|
||||
</div>
|
||||
<?php } ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>managetimezones:</th>
|
||||
<td>
|
||||
<div><?php print_string('helpmanagetimezones', 'admin'); ?></div>
|
||||
<p><a href="timezoneimport.php"><?php print_string('updatetimezones', 'admin'); ?>...</a> </p>
|
||||
<p><a href="timezoneimport.php"><?php print_string('importtimezones', 'admin'); ?>...</a> </p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
@ -1,17 +1,13 @@
|
||||
<?php // $Id$
|
||||
|
||||
// Automatic update of DST presets
|
||||
// Automatic update of Timezones from a new source
|
||||
|
||||
require_once('../config.php');
|
||||
require_once($CFG->libdir.'/filelib.php');
|
||||
require_once($CFG->libdir.'/olson.php');
|
||||
|
||||
define('STEP_OLSON_TO_CSV', 1);
|
||||
define('STEP_DOWNLOAD_CSV', 2);
|
||||
define('STEP_IMPORT_CSV_LIB', 3);
|
||||
define('STEP_IMPORT_CSV_TEMP', 4);
|
||||
define('STEP_COMPLETED', 5);
|
||||
|
||||
$ok = optional_param('ok');
|
||||
|
||||
require_login();
|
||||
|
||||
if (!isadmin()) {
|
||||
@ -22,172 +18,122 @@
|
||||
error('Site isn\'t defined!');
|
||||
}
|
||||
|
||||
$ddd = olson_todst($CFG->dataroot.'/temp/olson.txt');
|
||||
/// Print headings
|
||||
|
||||
execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone');
|
||||
$stradministration = get_string('administration');
|
||||
$strconfiguration = get_string('configuration');
|
||||
$strcalendarsettings = get_string('calendarsettings', 'admin');
|
||||
$strimporttimezones = get_string('importtimezones', 'admin');
|
||||
|
||||
foreach($ddd as $rec) {
|
||||
print_object($rec);
|
||||
insert_record('timezone', $rec);
|
||||
}
|
||||
print_header("$site->shortname: $strcalendarsettings", "$site->fullname",
|
||||
"<a href=\"index.php\">$stradministration</a> -> ".
|
||||
"<a href=\"configure.php\">$strconfiguration</a> -> ".
|
||||
"<a href=\"calendar.php\">$strcalendarsettings</a> -> $strimporttimezones");
|
||||
|
||||
die();
|
||||
print_heading($strimporttimezones);
|
||||
|
||||
// These control what kind of operations import_dst_records will be allowed
|
||||
$insert = true;
|
||||
$update = true;
|
||||
if (!$ok or !confirm_sesskey()) {
|
||||
$message = '<p>';
|
||||
$message .= $CFG->dataroot.'/temp/olson.txt<br />';
|
||||
$message .= $CFG->dataroot.'/temp/timezones.txt<br />';
|
||||
$message .= '<a href="http://download.moodle.org/timezones/">http://download.moodle.org/timezones/</a><br />';
|
||||
$message .= '<a href="'.$CFG->wwwroot.'/lib/timezones.txt">'.$CFG->dirroot.'/lib/timezones.txt</a><br />';
|
||||
$message .= '</p>';
|
||||
|
||||
// Actions in REVERSE ORDER of execution
|
||||
$actions = array(STEP_IMPORT_CSV_LIB, STEP_DOWNLOAD_CSV, STEP_IMPORT_CSV_TEMP, STEP_OLSON_TO_CSV);
|
||||
$message = get_string("configintrotimezones", 'admin', $message);
|
||||
|
||||
while(!empty($actions)) {
|
||||
$action = array_pop($actions);
|
||||
switch($action) {
|
||||
notice_yesno($message, 'timezoneimport.php?ok=1&sesskey='.sesskey(), 'calendar.php');
|
||||
|
||||
case STEP_OLSON_TO_CSV:
|
||||
if(is_writable($CFG->dataroot.'/temp/olson.txt')) {
|
||||
$records = olson_simple_rule_parser($CFG->dataroot.'/temp/olson.txt');
|
||||
if(put_records_csv('dst.txt', $records, 'dst_preset')) {
|
||||
// Successful convert
|
||||
unlink($CFG->dataroot.'/temp/olson.txt');
|
||||
array_push($actions, STEP_IMPORT_CSV_TEMP);
|
||||
}
|
||||
else {
|
||||
// Error: put_records_csv complained
|
||||
error('44');
|
||||
}
|
||||
}
|
||||
break;
|
||||
print_footer();
|
||||
exit;
|
||||
}
|
||||
|
||||
case STEP_IMPORT_CSV_TEMP:
|
||||
if(is_writable($CFG->dataroot.'/temp/dst.txt')) {
|
||||
$records = get_records_csv($CFG->dataroot.'/temp/dst.txt', 'dst_preset');
|
||||
// Import and go to summary page
|
||||
$results = import_dst_records($records, $insert, $update);
|
||||
unlink($CFG->dataroot.'/temp/dst.txt');
|
||||
array_push($actions, STEP_COMPLETED);
|
||||
}
|
||||
break;
|
||||
|
||||
case STEP_DOWNLOAD_CSV:
|
||||
if(ini_get('allow_url_fopen')) {
|
||||
$contents = @file_get_contents('http://download.moodle.org/dst/');
|
||||
if(!empty($contents)) {
|
||||
// Got something
|
||||
if($fp = fopen($CFG->dataroot.'/temp/dst.txt', 'w')) {
|
||||
fwrite($fp, $contents);
|
||||
fclose($fp);
|
||||
array_push($actions, STEP_IMPORT_CSV_TEMP);
|
||||
}
|
||||
else {
|
||||
// Error: Couldn't open file correctly
|
||||
error('74');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Error: nothing from download.moodle.org
|
||||
error('73');
|
||||
}
|
||||
}
|
||||
break;
|
||||
/// Try to find a source of timezones to import from
|
||||
|
||||
case STEP_IMPORT_CSV_LIB:
|
||||
if(file_exists($CFG->dirroot.'/lib/dst.txt')) {
|
||||
$records = get_records_csv($CFG->dirroot.'/lib/dst.txt', 'dst_preset');
|
||||
$results = import_dst_records($records, $insert, $update);
|
||||
array_push($actions, STEP_COMPLETED);
|
||||
}
|
||||
break;
|
||||
$importdone = false;
|
||||
|
||||
case STEP_COMPLETED:
|
||||
echo get_string('updatedstpresetscompleted');
|
||||
print_object($results);
|
||||
die();
|
||||
break;
|
||||
/// First, look for an Olson file locally
|
||||
|
||||
$source = $CFG->dataroot.'/temp/olson.txt';
|
||||
if (!$importdone and is_readable($source)) {
|
||||
if ($timezones = olson_to_timezones($source)) {
|
||||
update_timezone_records($timezones);
|
||||
$importdone = $source;
|
||||
}
|
||||
}
|
||||
|
||||
function import_dst_records(&$records, $allowinsert = true, $allowupdate = true) {
|
||||
$results = array();
|
||||
$proto = array('insert' => 0, 'update' => 0, 'errors' => 0);
|
||||
/// Next, look for a CSV file locally
|
||||
|
||||
foreach($records as $record) {
|
||||
|
||||
if(!check_dst_preset($record)) {
|
||||
continue;
|
||||
$source = $CFG->dataroot.'/temp/timezones.txt';
|
||||
if (!$importdone and is_readable($source)) {
|
||||
if ($timezones = get_records_csv($source, 'timezone')) {
|
||||
update_timezone_records($timezones);
|
||||
$importdone = $source;
|
||||
}
|
||||
|
||||
$dbpreset = get_record('dst_preset', 'family', $record->family, 'year', $record->year);
|
||||
|
||||
if(empty($dbpreset)) {
|
||||
|
||||
if(!$allowinsert) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!isset($results[$record->family])) {
|
||||
$results[$record->family] = $proto;
|
||||
}
|
||||
|
||||
|
||||
unset($record->id);
|
||||
if(insert_record('dst_preset', $record)) {
|
||||
++$results[$record->family]['insert'];
|
||||
}
|
||||
else {
|
||||
++$results[$record->family]['errors'];
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
// Already exists
|
||||
|
||||
if(!$allowupdate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(hash_dst_preset($record) != hash_dst_preset($dbpreset)) {
|
||||
|
||||
// And is different
|
||||
|
||||
if(!isset($results[$record->family])) {
|
||||
$results[$record->family] = $proto;
|
||||
}
|
||||
|
||||
$record->id = $dbpreset->id;
|
||||
if(update_record('dst_preset', $record)) {
|
||||
++$results[$record->family]['update'];
|
||||
}
|
||||
else {
|
||||
++$results[$record->family]['update'];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
function hash_dst_preset($record) {
|
||||
return md5(implode('!', array(
|
||||
$record->family,
|
||||
$record->year,
|
||||
$record->apply_offset,
|
||||
$record->activate_index,
|
||||
$record->activate_day,
|
||||
$record->activate_month,
|
||||
$record->activate_time,
|
||||
$record->deactivate_index,
|
||||
$record->deactivate_day,
|
||||
$record->deactivate_month,
|
||||
$record->deactivate_time
|
||||
)));
|
||||
}
|
||||
/// Otherwise, let's try moodle.org's copy
|
||||
|
||||
function check_dst_preset($record) {
|
||||
// TODO: make this a real check
|
||||
return true;
|
||||
}
|
||||
$source = 'http://download.moodle.org/timezones/';
|
||||
if (!$importdone and ini_get('allow_url_fopen')) {
|
||||
if ($contents = file_get_contents($source)) { // Grab whole page
|
||||
if ($file = fopen($CFG->dataroot.'/temp/timezones.txt', 'w')) { // Make local copy
|
||||
fwrite($file, $contents);
|
||||
fclose($file);
|
||||
if ($timezones = get_records_csv($CFG->dataroot.'/temp/timezones.txt', 'timezone')) { // Parse it
|
||||
update_timezone_records($timezones);
|
||||
$importdone = $source;
|
||||
}
|
||||
unlink($CFG->dataroot.'/temp/timezones.txt');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Final resort, use the copy included in Moodle
|
||||
|
||||
$source = $CFG->dirroot.'/lib/timezones.txt';
|
||||
if (!$importdone and is_readable($source)) { // Distribution file
|
||||
if ($timezones = get_records_csv($source)) {
|
||||
update_timezone_records($timezones);
|
||||
$importdone = $source;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// That's it!
|
||||
|
||||
if ($importdone) {
|
||||
$a = null;
|
||||
$a->count = count($timezones);
|
||||
$a->source = $importdone;
|
||||
print_heading(get_string('importtimezonescount', 'admin', $a), '', 3);
|
||||
|
||||
print_continue('calendar.php');
|
||||
|
||||
$timezonelist = array();
|
||||
foreach ($timezones as $timezone) {
|
||||
if (isset($timezonelist[$timezone->name])) {
|
||||
$timezonelist[$timezone->name]++;
|
||||
} else {
|
||||
$timezonelist[$timezone->name] = 1;
|
||||
}
|
||||
}
|
||||
ksort($timezonelist);
|
||||
|
||||
echo "<br />";
|
||||
print_simple_box_start('center');
|
||||
foreach ($timezonelist as $name => $count) {
|
||||
echo "$name ($count)<br />";
|
||||
}
|
||||
print_simple_box_end();
|
||||
|
||||
} else {
|
||||
print_heading(get_string('importtimezonesfailed', 'admin'), '', 3);
|
||||
print_continue('calendar.php');
|
||||
}
|
||||
|
||||
print_footer();
|
||||
|
||||
?>
|
||||
|
@ -42,6 +42,7 @@ $string['configidnumber'] = 'This option specifies whether (a) Users are not be
|
||||
$string['configintro'] = 'On this page you can specify a number of configuration variables that help make Moodle work properly on your server. Don\'t worry too much about it - the defaults will usually work fine and you can always come back to this page later and change these settings.';
|
||||
$string['configintroadmin'] = 'On this page you should configure your main administrator account which will have complete control over the site. Make sure you give it a secure username and password as well as a valid email address. You can create more admin accounts later on.';
|
||||
$string['configintrosite'] = 'This page allows you to configure the front page and name of this new site. You can come back here later to change these settings any time using the \'Site Settings\' link on the home page.';
|
||||
$string['configintrotimezones'] = 'This page will search for new information about world timezones (including daylight savings time rules) and update your local database with this information. These locations will be checked, in order: $a This procedure is generally very safe and can not break normal installations. Do you wish to update your timezones now?';
|
||||
$string['configlang'] = 'Choose a default language for the whole site. Users can override this setting later.';
|
||||
$string['configlangdir'] = 'Most languages are printed left-to-right, but some, like Arabic and Hebrew, are printed right-to-left.';
|
||||
$string['configlanglist'] = 'Leave this blank to allow users to choose from any language you have in this installation of Moodle. However, you can shorten the language menu by entering a comma-separated list of language codes that you want. For example: en,es_es,fr,it';
|
||||
@ -94,12 +95,14 @@ $string['filteruploadedfiles'] = 'Filter uploaded files';
|
||||
$string['helpadminseesall'] = 'Do admins see all calendar events or just those that apply to themselves?';
|
||||
$string['helpcalendarsettings'] = 'Configure various calendar and date/time-related aspects of Moodle';
|
||||
$string['helpforcetimezone'] = 'You can allow users to individually select their timezone, or force a timezone for everyone.';
|
||||
$string['helpmanagetimezones'] = 'You can let Moodle automatically update and download new profiles from moodle.org.';
|
||||
$string['helpsitemaintenance'] = 'For upgrades and other work';
|
||||
$string['helpstartofweek'] = 'Which day starts the week in the calendar?';
|
||||
$string['helpupcominglookahead'] = 'How many days in the future does the calendar look for upcoming events by default?';
|
||||
$string['helpupcomingmaxevents'] = 'How many (maximum) upcoming events are shown to users by default?';
|
||||
$string['helpweekenddays'] = 'Which days of the week are treated as \"weekend\" and shown with a different colour?';
|
||||
$string['importtimezones'] = 'Update complete list of timezones';
|
||||
$string['importtimezonescount'] = '$a->count entries imported from $a->source';
|
||||
$string['importtimezonesfailed'] = 'No sources found! (Bad news)';
|
||||
$string['nodstpresetsexist'] = 'DST support is disabled for all users because there are no DST presets defined. You can define some presets using the button below.';
|
||||
$string['optionalmaintenancemessage'] = 'Optional maintenance messsage';
|
||||
$string['sitemaintenance'] = 'The site is undergoing maintenance and is currently not available';
|
||||
@ -112,7 +115,6 @@ $string['timezonenotforced'] = 'Users can choose their own timezone';
|
||||
$string['timezoneforced'] = 'This is forced by the site administrator';
|
||||
$string['timezoneisforcedto'] = 'Force all users to use';
|
||||
$string['therewereerrors'] = 'There were errors in your data';
|
||||
$string['updatetimezones'] = 'Automatically update profiles';
|
||||
$string['upgradelogs'] = 'For full functionality, your old logs need to be upgraded. <a href=\"$a\">More information</a>';
|
||||
$string['upgradelogsinfo'] = 'Some changes have recently been made in the way logs are stored. To be able to view all of your old logs on a per-activity basis, your old logs need to be upgraded. Depending on your site this can take a long time (eg several hours) and can be quite taxing on the database for large sites. Once you start this process you should let it finish (by keeping the browser window open). Don\'t worry - your site will work fine for other people while the logs are being upgraded.<br /><br />Do you want to upgrade your logs now?';
|
||||
$string['upgradesure'] = 'Your Moodle files have been changed, and you are about to automatically upgrade your server to this version:
|
||||
|
@ -208,7 +208,7 @@ function get_records_csv($file, $table) {
|
||||
function put_records_csv($file, $records, $table = NULL) {
|
||||
global $CFG, $db;
|
||||
|
||||
if(empty($records)) {
|
||||
if (empty($records)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -217,6 +217,8 @@ function put_records_csv($file, $records, $table = NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
echo "x";
|
||||
|
||||
if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) {
|
||||
error('put_records_csv failed to open '.$file);
|
||||
}
|
||||
@ -231,6 +233,7 @@ function put_records_csv($file, $records, $table = NULL) {
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
echo "x";
|
||||
|
||||
if(!empty($metacolumns)) {
|
||||
$fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php //$Id$
|
||||
|
||||
/***
|
||||
*** olson_todst ($filename)
|
||||
*** olson_to_timezones ($filename)
|
||||
***
|
||||
*** Parses the olson files for Zones and DST rules.
|
||||
*** It updates the Moodle database with the Zones/DST rules
|
||||
@ -9,7 +9,7 @@
|
||||
*** Returns true/false
|
||||
***
|
||||
*/
|
||||
function olson_todst ($filename) {
|
||||
function olson_to_timezones ($filename) {
|
||||
|
||||
$zones = olson_simple_zone_parser($filename);
|
||||
$rules = olson_simple_rule_parser($filename);
|
||||
@ -627,4 +627,4 @@ function olson_parse_at ($at, $set = 'set', $gmtoffset) {
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user