weekly release 2.9dev
[moodle.git] / admin / tool / timezoneimport / index.php
CommitLineData
0df0df23 1<?php
9597e00b
PS
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Automatic update of Timezones from a new source
19 *
20 * @package tool
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
24 */
25
26 require_once('../../../config.php');
50999a0b 27 require_once($CFG->libdir.'/adminlib.php');
76458066 28 require_once($CFG->libdir.'/filelib.php');
29 require_once($CFG->libdir.'/olson.php');
20207b82 30
9597e00b 31 admin_externalpage_setup('tooltimezoneimport');
76458066 32
90a73bb3 33 $ok = optional_param('ok', 0, PARAM_BOOL);
76458066 34
76458066 35
a1e93da2 36/// Print headings
76458066 37
9597e00b 38 $strimporttimezones = get_string('importtimezones', 'tool_timezoneimport');
76458066 39
61ef8f9f 40 echo $OUTPUT->header();
76458066 41
2fff8846 42 echo $OUTPUT->heading($strimporttimezones);
76458066 43
a1e93da2 44 if (!$ok or !confirm_sesskey()) {
bad545e9 45 $message = '<br /><br />';
7aa06e6d
TL
46 $message .= $CFG->tempdir.'/olson.txt<br />';
47 $message .= $CFG->tempdir.'/timezone.txt<br />';
59abbd93 48 $message .= '<a href="https://download.moodle.org/timezone/">https://download.moodle.org/timezone/</a><br />';
4ae7df39 49 $message .= '<a href="'.$CFG->wwwroot.'/lib/timezone.txt">'.$CFG->dirroot.'/lib/timezone.txt</a><br />';
bad545e9 50 $message .= '<br />';
76458066 51
9597e00b 52 $message = get_string("configintrotimezones", 'tool_timezoneimport', $message);
76458066 53
9597e00b 54 echo $OUTPUT->confirm($message, 'index.php?ok=1', new moodle_url('/admin/index.php'));
76458066 55
73d6f52f 56 echo $OUTPUT->footer();
a1e93da2 57 exit;
58 }
76458066 59
76458066 60
a1e93da2 61/// Try to find a source of timezones to import from
76458066 62
a1e93da2 63 $importdone = false;
76458066 64
a1e93da2 65/// First, look for an Olson file locally
76458066 66
7aa06e6d 67 $source = $CFG->tempdir.'/olson.txt';
a1e93da2 68 if (!$importdone and is_readable($source)) {
69 if ($timezones = olson_to_timezones($source)) {
70 update_timezone_records($timezones);
71 $importdone = $source;
76458066 72 }
a1e93da2 73 }
76458066 74
a1e93da2 75/// Next, look for a CSV file locally
76458066 76
7aa06e6d 77 $source = $CFG->tempdir.'/timezone.txt';
a1e93da2 78 if (!$importdone and is_readable($source)) {
79 if ($timezones = get_records_csv($source, 'timezone')) {
80 update_timezone_records($timezones);
81 $importdone = $source;
82 }
83 }
76458066 84
a1e93da2 85/// Otherwise, let's try moodle.org's copy
59abbd93 86 $source = 'https://download.moodle.org/timezone/';
ae8c48f7 87 if (!$importdone && ($content=download_file_content($source))) {
7aa06e6d 88 if ($file = fopen($CFG->tempdir.'/timezone.txt', 'w')) { // Make local copy
ae8c48f7 89 fwrite($file, $content);
3e93ea86 90 fclose($file);
7aa06e6d 91 if ($timezones = get_records_csv($CFG->tempdir.'/timezone.txt', 'timezone')) { // Parse it
3e93ea86 92 update_timezone_records($timezones);
93 $importdone = $source;
76458066 94 }
7aa06e6d 95 unlink($CFG->tempdir.'/timezone.txt');
a1e93da2 96 }
97 }
76458066 98
76458066 99
a1e93da2 100/// Final resort, use the copy included in Moodle
4ae7df39 101 $source = $CFG->dirroot.'/lib/timezone.txt';
a1e93da2 102 if (!$importdone and is_readable($source)) { // Distribution file
a03d6134 103 if ($timezones = get_records_csv($source, 'timezone')) {
a1e93da2 104 update_timezone_records($timezones);
105 $importdone = $source;
76458066 106 }
a1e93da2 107 }
76458066 108
76458066 109
a1e93da2 110/// That's it!
76458066 111
a1e93da2 112 if ($importdone) {
b85b25eb 113 $a = new stdClass();
a1e93da2 114 $a->count = count($timezones);
115 $a->source = $importdone;
9597e00b 116 echo $OUTPUT->heading(get_string('importtimezonescount', 'tool_timezoneimport', $a), 3);
76458066 117
9597e00b 118 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php'));
76458066 119
a1e93da2 120 $timezonelist = array();
121 foreach ($timezones as $timezone) {
9156f58a 122 if (is_array($timezone)) {
123 $timezone = (object)$timezone;
124 }
a1e93da2 125 if (isset($timezonelist[$timezone->name])) {
126 $timezonelist[$timezone->name]++;
127 } else {
128 $timezonelist[$timezone->name] = 1;
76458066 129 }
a1e93da2 130 }
131 ksort($timezonelist);
76458066 132
a1e93da2 133 echo "<br />";
8fbce1c8 134 echo $OUTPUT->box_start();
a1e93da2 135 foreach ($timezonelist as $name => $count) {
136 echo "$name ($count)<br />";
76458066 137 }
8fbce1c8 138 echo $OUTPUT->box_end();
76458066 139
a1e93da2 140 } else {
9597e00b
PS
141 echo $OUTPUT->heading(get_string('importtimezonesfailed', 'tool_timezoneimport'), 3);
142 echo $OUTPUT->continue_button(new moodle_url('/admin/index.php'));
76458066 143 }
a1e93da2 144
73d6f52f 145 echo $OUTPUT->footer();
76458066 146
0df0df23 147