Merge branch 'm21_MDL-28603' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / admin / timezoneimport.php
bloba09f17d5330f1f8b800992bc179f01f41c290a09
1 <?php
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 admin_externalpage_setup('timezoneimport');
12 $ok = optional_param('ok', 0, PARAM_BOOL);
15 /// Print headings
17 $strimporttimezones = get_string('importtimezones', 'admin');
19 echo $OUTPUT->header();
21 echo $OUTPUT->heading($strimporttimezones);
23 if (!$ok or !confirm_sesskey()) {
24 $message = '<br /><br />';
25 $message .= $CFG->dataroot.'/temp/olson.txt<br />';
26 $message .= $CFG->dataroot.'/temp/timezone.txt<br />';
27 $message .= '<a href="http://download.moodle.org/timezone/">http://download.moodle.org/timezone/</a><br />';
28 $message .= '<a href="'.$CFG->wwwroot.'/lib/timezone.txt">'.$CFG->dirroot.'/lib/timezone.txt</a><br />';
29 $message .= '<br />';
31 $message = get_string("configintrotimezones", 'admin', $message);
33 echo $OUTPUT->confirm($message, 'timezoneimport.php?ok=1', 'index.php');
35 echo $OUTPUT->footer();
36 exit;
40 /// Try to find a source of timezones to import from
42 $importdone = false;
44 /// First, look for an Olson file locally
46 $source = $CFG->dataroot.'/temp/olson.txt';
47 if (!$importdone and is_readable($source)) {
48 if ($timezones = olson_to_timezones($source)) {
49 update_timezone_records($timezones);
50 $importdone = $source;
54 /// Next, look for a CSV file locally
56 $source = $CFG->dataroot.'/temp/timezone.txt';
57 if (!$importdone and is_readable($source)) {
58 if ($timezones = get_records_csv($source, 'timezone')) {
59 update_timezone_records($timezones);
60 $importdone = $source;
64 /// Otherwise, let's try moodle.org's copy
65 $source = 'http://download.moodle.org/timezone/';
66 if (!$importdone && ($content=download_file_content($source))) {
67 if ($file = fopen($CFG->dataroot.'/temp/timezone.txt', 'w')) { // Make local copy
68 fwrite($file, $content);
69 fclose($file);
70 if ($timezones = get_records_csv($CFG->dataroot.'/temp/timezone.txt', 'timezone')) { // Parse it
71 update_timezone_records($timezones);
72 $importdone = $source;
74 unlink($CFG->dataroot.'/temp/timezone.txt');
79 /// Final resort, use the copy included in Moodle
80 $source = $CFG->dirroot.'/lib/timezone.txt';
81 if (!$importdone and is_readable($source)) { // Distribution file
82 if ($timezones = get_records_csv($source, 'timezone')) {
83 update_timezone_records($timezones);
84 $importdone = $source;
89 /// That's it!
91 if ($importdone) {
92 $a = null;
93 $a->count = count($timezones);
94 $a->source = $importdone;
95 echo $OUTPUT->heading(get_string('importtimezonescount', 'admin', $a), 3);
97 echo $OUTPUT->continue_button('index.php');
99 $timezonelist = array();
100 foreach ($timezones as $timezone) {
101 if (is_array($timezone)) {
102 $timezone = (object)$timezone;
104 if (isset($timezonelist[$timezone->name])) {
105 $timezonelist[$timezone->name]++;
106 } else {
107 $timezonelist[$timezone->name] = 1;
110 ksort($timezonelist);
112 echo "<br />";
113 echo $OUTPUT->box_start();
114 foreach ($timezonelist as $name => $count) {
115 echo "$name ($count)<br />";
117 echo $OUTPUT->box_end();
119 } else {
120 echo $OUTPUT->heading(get_string('importtimezonesfailed', 'admin'), 3);
121 echo $OUTPUT->continue_button('index.php');
124 echo $OUTPUT->footer();