3 // Automatic update of Timezones from a new source
5 require_once('../config.php');
6 require_once($CFG->libdir
.'/adminlib.php');
7 require_once($CFG->libdir
.'/filelib.php');
8 require_once($CFG->libdir
.'/olson.php');
10 $adminroot = admin_get_root();
11 admin_externalpage_setup('timezoneimport', $adminroot);
13 $ok = optional_param('ok', 0, PARAM_BOOL
);
18 $strimporttimezones = get_string('importtimezones', 'admin');
20 admin_externalpage_print_header($adminroot);
22 print_heading($strimporttimezones);
24 if (!$ok or !confirm_sesskey()) {
25 $message = '<br /><br />';
26 $message .= $CFG->dataroot
.'/temp/olson.txt<br />';
27 $message .= $CFG->dataroot
.'/temp/timezones.txt<br />';
28 $message .= '<a href="http://download.moodle.org/timezones/">http://download.moodle.org/timezones/</a><br />';
29 $message .= '<a href="'.$CFG->wwwroot
.'/lib/timezones.txt">'.$CFG->dirroot
.'/lib/timezones.txt</a><br />';
32 $message = get_string("configintrotimezones", 'admin', $message);
34 notice_yesno($message, 'timezoneimport.php?ok=1&sesskey='.sesskey(), 'index.php');
36 admin_externalpage_print_footer($adminroot);
41 /// Try to find a source of timezones to import from
45 /// First, look for an Olson file locally
47 $source = $CFG->dataroot
.'/temp/olson.txt';
48 if (!$importdone and is_readable($source)) {
49 if ($timezones = olson_to_timezones($source)) {
50 update_timezone_records($timezones);
51 $importdone = $source;
55 /// Next, look for a CSV file locally
57 $source = $CFG->dataroot
.'/temp/timezones.txt';
58 if (!$importdone and is_readable($source)) {
59 if ($timezones = get_records_csv($source, 'timezone')) {
60 update_timezone_records($timezones);
61 $importdone = $source;
65 /// Otherwise, let's try moodle.org's copy
67 $source = 'http://download.moodle.org/timezones/';
68 if (!$importdone and ini_get('allow_url_fopen')) {
69 if (is_readable($source) && $contents = file_get_contents($source)) { // Grab whole page
70 if ($file = fopen($CFG->dataroot
.'/temp/timezones.txt', 'w')) { // Make local copy
71 fwrite($file, $contents);
73 if ($timezones = get_records_csv($CFG->dataroot
.'/temp/timezones.txt', 'timezone')) { // Parse it
74 update_timezone_records($timezones);
75 $importdone = $source;
77 unlink($CFG->dataroot
.'/temp/timezones.txt');
83 /// Final resort, use the copy included in Moodle
85 $source = $CFG->dirroot
.'/lib/timezones.txt';
86 if (!$importdone and is_readable($source)) { // Distribution file
87 if ($timezones = get_records_csv($source, 'timezone')) {
88 update_timezone_records($timezones);
89 $importdone = $source;
98 $a->count
= count($timezones);
99 $a->source
= $importdone;
100 print_heading(get_string('importtimezonescount', 'admin', $a), '', 3);
102 print_continue('index.php');
104 $timezonelist = array();
105 foreach ($timezones as $timezone) {
106 if (isset($timezonelist[$timezone->name
])) {
107 $timezonelist[$timezone->name
]++
;
109 $timezonelist[$timezone->name
] = 1;
112 ksort($timezonelist);
115 print_simple_box_start('center');
116 foreach ($timezonelist as $name => $count) {
117 echo "$name ($count)<br />";
119 print_simple_box_end();
122 print_heading(get_string('importtimezonesfailed', 'admin'), '', 3);
123 print_continue('index.php');
126 admin_externalpage_print_footer($adminroot);