2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Automatic update of Timezones from a new source
21 * @subpackage timezoneimport
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../../../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
28 require_once($CFG->libdir.'/filelib.php');
29 require_once($CFG->libdir.'/olson.php');
31 admin_externalpage_setup('tooltimezoneimport');
33 $ok = optional_param('ok', 0, PARAM_BOOL);
38 $strimporttimezones = get_string('importtimezones', 'tool_timezoneimport');
40 echo $OUTPUT->header();
42 echo $OUTPUT->heading($strimporttimezones);
44 if (!$ok or !confirm_sesskey()) {
45 $message = '<br /><br />';
46 $message .= $CFG->tempdir.'/olson.txt<br />';
47 $message .= $CFG->tempdir.'/timezone.txt<br />';
48 $message .= '<a href="https://download.moodle.org/timezone/">https://download.moodle.org/timezone/</a><br />';
49 $message .= '<a href="'.$CFG->wwwroot.'/lib/timezone.txt">'.$CFG->dirroot.'/lib/timezone.txt</a><br />';
52 $message = get_string("configintrotimezones", 'tool_timezoneimport', $message);
54 echo $OUTPUT->confirm($message, 'index.php?ok=1', new moodle_url('/admin/index.php'));
56 echo $OUTPUT->footer();
61 /// Try to find a source of timezones to import from
65 /// First, look for an Olson file locally
67 $source = $CFG->tempdir.'/olson.txt';
68 if (!$importdone and is_readable($source)) {
69 if ($timezones = olson_to_timezones($source)) {
70 update_timezone_records($timezones);
71 $importdone = $source;
75 /// Next, look for a CSV file locally
77 $source = $CFG->tempdir.'/timezone.txt';
78 if (!$importdone and is_readable($source)) {
79 if ($timezones = get_records_csv($source, 'timezone')) {
80 update_timezone_records($timezones);
81 $importdone = $source;
85 /// Otherwise, let's try moodle.org's copy
86 $source = 'https://download.moodle.org/timezone/';
87 if (!$importdone && ($content=download_file_content($source))) {
88 if ($file = fopen($CFG->tempdir.'/timezone.txt', 'w')) { // Make local copy
89 fwrite($file, $content);
91 if ($timezones = get_records_csv($CFG->tempdir.'/timezone.txt', 'timezone')) { // Parse it
92 update_timezone_records($timezones);
93 $importdone = $source;
95 unlink($CFG->tempdir.'/timezone.txt');
100 /// Final resort, use the copy included in Moodle
101 $source = $CFG->dirroot.'/lib/timezone.txt';
102 if (!$importdone and is_readable($source)) { // Distribution file
103 if ($timezones = get_records_csv($source, 'timezone')) {
104 update_timezone_records($timezones);
105 $importdone = $source;
114 $a->count = count($timezones);
115 $a->source = $importdone;
116 echo $OUTPUT->heading(get_string('importtimezonescount', 'tool_timezoneimport', $a), 3);
118 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php'));
120 $timezonelist = array();
121 foreach ($timezones as $timezone) {
122 if (is_array($timezone)) {
123 $timezone = (object)$timezone;
125 if (isset($timezonelist[$timezone->name])) {
126 $timezonelist[$timezone->name]++;
128 $timezonelist[$timezone->name] = 1;
131 ksort($timezonelist);
134 echo $OUTPUT->box_start();
135 foreach ($timezonelist as $name => $count) {
136 echo "$name ($count)<br />";
138 echo $OUTPUT->box_end();
141 echo $OUTPUT->heading(get_string('importtimezonesfailed', 'tool_timezoneimport'), 3);
142 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php'));
145 echo $OUTPUT->footer();