MDL-14624:
[moodle-linuxchix.git] / admin / timezoneimport.php
blob89a4459750cf8e4b39124db708f082a40e4cc786
1 <?php // $Id$
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 admin_externalpage_print_header();
21 print_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 notice_yesno($message, 'timezoneimport.php?ok=1&amp;sesskey='.sesskey(), 'index.php');
35 admin_externalpage_print_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 print_heading(get_string('importtimezonescount', 'admin', $a), '', 3);
97 print_continue('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 print_simple_box_start('center');
114 foreach ($timezonelist as $name => $count) {
115 echo "$name ($count)<br />";
117 print_simple_box_end();
119 } else {
120 print_heading(get_string('importtimezonesfailed', 'admin'), '', 3);
121 print_continue('index.php');
124 admin_externalpage_print_footer();