Commit | Line | Data |
---|---|---|
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; | |
8753653b | 116 | echo $OUTPUT->notification(get_string('importtimezonescount', 'tool_timezoneimport', $a), 'notifysuccess'); |
9597e00b | 117 | echo $OUTPUT->continue_button(new moodle_url('/admin/index.php')); |
76458066 | 118 | |
a1e93da2 | 119 | $timezonelist = array(); |
120 | foreach ($timezones as $timezone) { | |
9156f58a | 121 | if (is_array($timezone)) { |
122 | $timezone = (object)$timezone; | |
123 | } | |
a1e93da2 | 124 | if (isset($timezonelist[$timezone->name])) { |
125 | $timezonelist[$timezone->name]++; | |
126 | } else { | |
127 | $timezonelist[$timezone->name] = 1; | |
76458066 | 128 | } |
a1e93da2 | 129 | } |
130 | ksort($timezonelist); | |
76458066 | 131 | |
8753653b JC |
132 | $timezonetable = new html_table(); |
133 | $timezonetable->head = array( | |
134 | get_string('timezone', 'moodle'), | |
135 | get_string('entries', 'moodle') | |
136 | ); | |
137 | $rows = array(); | |
a1e93da2 | 138 | foreach ($timezonelist as $name => $count) { |
8753653b JC |
139 | $row = new html_table_row( |
140 | array( | |
141 | new html_table_cell($name), | |
142 | new html_table_cell($count) | |
143 | ) | |
144 | ); | |
145 | $rows[] = $row; | |
76458066 | 146 | } |
8753653b JC |
147 | $timezonetable->data = $rows; |
148 | echo html_writer::table($timezonetable); | |
76458066 | 149 | |
a1e93da2 | 150 | } else { |
8753653b | 151 | echo $OUTPUT->notification(get_string('importtimezonesfailed', 'tool_timezoneimport')); |
9597e00b | 152 | echo $OUTPUT->continue_button(new moodle_url('/admin/index.php')); |
76458066 | 153 | } |
a1e93da2 | 154 | |
73d6f52f | 155 | echo $OUTPUT->footer(); |
76458066 | 156 | |
0df0df23 | 157 |