MDL-28701 Added $CFG->tempdir setting
[moodle.git] / admin / timezoneimport.php
CommitLineData
0df0df23 1<?php
76458066 2
a1e93da2 3 // Automatic update of Timezones from a new source
20207b82 4
76458066 5 require_once('../config.php');
50999a0b 6 require_once($CFG->libdir.'/adminlib.php');
76458066 7 require_once($CFG->libdir.'/filelib.php');
8 require_once($CFG->libdir.'/olson.php');
20207b82 9
1ae083e4 10 admin_externalpage_setup('timezoneimport');
76458066 11
90a73bb3 12 $ok = optional_param('ok', 0, PARAM_BOOL);
76458066 13
76458066 14
a1e93da2 15/// Print headings
76458066 16
a1e93da2 17 $strimporttimezones = get_string('importtimezones', 'admin');
76458066 18
61ef8f9f 19 echo $OUTPUT->header();
76458066 20
2fff8846 21 echo $OUTPUT->heading($strimporttimezones);
76458066 22
a1e93da2 23 if (!$ok or !confirm_sesskey()) {
bad545e9 24 $message = '<br /><br />';
a1e93da2 25 $message .= $CFG->dataroot.'/temp/olson.txt<br />';
4ae7df39 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 />';
bad545e9 29 $message .= '<br />';
76458066 30
a1e93da2 31 $message = get_string("configintrotimezones", 'admin', $message);
76458066 32
414a4a91 33 echo $OUTPUT->confirm($message, 'timezoneimport.php?ok=1', 'index.php');
76458066 34
73d6f52f 35 echo $OUTPUT->footer();
a1e93da2 36 exit;
37 }
76458066 38
76458066 39
a1e93da2 40/// Try to find a source of timezones to import from
76458066 41
a1e93da2 42 $importdone = false;
76458066 43
a1e93da2 44/// First, look for an Olson file locally
76458066 45
a1e93da2 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;
76458066 51 }
a1e93da2 52 }
76458066 53
a1e93da2 54/// Next, look for a CSV file locally
76458066 55
4ae7df39 56 $source = $CFG->dataroot.'/temp/timezone.txt';
a1e93da2 57 if (!$importdone and is_readable($source)) {
58 if ($timezones = get_records_csv($source, 'timezone')) {
59 update_timezone_records($timezones);
60 $importdone = $source;
61 }
62 }
76458066 63
a1e93da2 64/// Otherwise, let's try moodle.org's copy
4ae7df39 65 $source = 'http://download.moodle.org/timezone/';
ae8c48f7 66 if (!$importdone && ($content=download_file_content($source))) {
4ae7df39 67 if ($file = fopen($CFG->dataroot.'/temp/timezone.txt', 'w')) { // Make local copy
ae8c48f7 68 fwrite($file, $content);
3e93ea86 69 fclose($file);
4ae7df39 70 if ($timezones = get_records_csv($CFG->dataroot.'/temp/timezone.txt', 'timezone')) { // Parse it
3e93ea86 71 update_timezone_records($timezones);
72 $importdone = $source;
76458066 73 }
4ae7df39 74 unlink($CFG->dataroot.'/temp/timezone.txt');
a1e93da2 75 }
76 }
76458066 77
76458066 78
a1e93da2 79/// Final resort, use the copy included in Moodle
4ae7df39 80 $source = $CFG->dirroot.'/lib/timezone.txt';
a1e93da2 81 if (!$importdone and is_readable($source)) { // Distribution file
a03d6134 82 if ($timezones = get_records_csv($source, 'timezone')) {
a1e93da2 83 update_timezone_records($timezones);
84 $importdone = $source;
76458066 85 }
a1e93da2 86 }
76458066 87
76458066 88
a1e93da2 89/// That's it!
76458066 90
a1e93da2 91 if ($importdone) {
92 $a = null;
93 $a->count = count($timezones);
94 $a->source = $importdone;
2fff8846 95 echo $OUTPUT->heading(get_string('importtimezonescount', 'admin', $a), 3);
76458066 96
8fbce1c8 97 echo $OUTPUT->continue_button('index.php');
76458066 98
a1e93da2 99 $timezonelist = array();
100 foreach ($timezones as $timezone) {
9156f58a 101 if (is_array($timezone)) {
102 $timezone = (object)$timezone;
103 }
a1e93da2 104 if (isset($timezonelist[$timezone->name])) {
105 $timezonelist[$timezone->name]++;
106 } else {
107 $timezonelist[$timezone->name] = 1;
76458066 108 }
a1e93da2 109 }
110 ksort($timezonelist);
76458066 111
a1e93da2 112 echo "<br />";
8fbce1c8 113 echo $OUTPUT->box_start();
a1e93da2 114 foreach ($timezonelist as $name => $count) {
115 echo "$name ($count)<br />";
76458066 116 }
8fbce1c8 117 echo $OUTPUT->box_end();
76458066 118
a1e93da2 119 } else {
2fff8846 120 echo $OUTPUT->heading(get_string('importtimezonesfailed', 'admin'), 3);
8fbce1c8 121 echo $OUTPUT->continue_button('index.php');
76458066 122 }
a1e93da2 123
73d6f52f 124 echo $OUTPUT->footer();
76458066 125
0df0df23 126