file includeteachersgrade.html was added on branch MOODLE_15_STABLE on 2005-07-07...
[moodle.git] / admin / timezoneimport.php
blob34c5347b34151db9cca59b7fb0f3b3acb4d5808c
1 <?php // $Id$
3 // Automatic update of Timezones from a new source
5 require_once('../config.php');
6 require_once($CFG->libdir.'/filelib.php');
7 require_once($CFG->libdir.'/olson.php');
9 $ok = optional_param('ok');
11 require_login();
13 if (!isadmin()) {
14 error('Only administrators can use this page!');
17 if (!$site = get_site()) {
18 error('Site isn\'t defined!');
21 /// Print headings
23 $stradministration = get_string('administration');
24 $strconfiguration = get_string('configuration');
25 $strcalendarsettings = get_string('calendarsettings', 'admin');
26 $strimporttimezones = get_string('importtimezones', 'admin');
28 print_header("$site->shortname: $strcalendarsettings", "$site->fullname",
29 "<a href=\"index.php\">$stradministration</a> -> ".
30 "<a href=\"configure.php\">$strconfiguration</a> -> ".
31 "<a href=\"calendar.php\">$strcalendarsettings</a> -> $strimporttimezones");
33 print_heading($strimporttimezones);
35 if (!$ok or !confirm_sesskey()) {
36 $message = '<p>';
37 $message .= $CFG->dataroot.'/temp/olson.txt<br />';
38 $message .= $CFG->dataroot.'/temp/timezones.txt<br />';
39 $message .= '<a href="http://download.moodle.org/timezones/">http://download.moodle.org/timezones/</a><br />';
40 $message .= '<a href="'.$CFG->wwwroot.'/lib/timezones.txt">'.$CFG->dirroot.'/lib/timezones.txt</a><br />';
41 $message .= '</p>';
43 $message = get_string("configintrotimezones", 'admin', $message);
45 notice_yesno($message, 'timezoneimport.php?ok=1&sesskey='.sesskey(), 'calendar.php');
47 print_footer();
48 exit;
52 /// Try to find a source of timezones to import from
54 $importdone = false;
56 /// First, look for an Olson file locally
58 $source = $CFG->dataroot.'/temp/olson.txt';
59 if (!$importdone and is_readable($source)) {
60 if ($timezones = olson_to_timezones($source)) {
61 update_timezone_records($timezones);
62 $importdone = $source;
66 /// Next, look for a CSV file locally
68 $source = $CFG->dataroot.'/temp/timezones.txt';
69 if (!$importdone and is_readable($source)) {
70 if ($timezones = get_records_csv($source, 'timezone')) {
71 update_timezone_records($timezones);
72 $importdone = $source;
76 /// Otherwise, let's try moodle.org's copy
78 $source = 'http://download.moodle.org/timezones/';
79 if (!$importdone and ini_get('allow_url_fopen')) {
80 if (is_readable($source) && $contents = file_get_contents($source)) { // Grab whole page
81 if ($file = fopen($CFG->dataroot.'/temp/timezones.txt', 'w')) { // Make local copy
82 fwrite($file, $contents);
83 fclose($file);
84 if ($timezones = get_records_csv($CFG->dataroot.'/temp/timezones.txt', 'timezone')) { // Parse it
85 update_timezone_records($timezones);
86 $importdone = $source;
88 unlink($CFG->dataroot.'/temp/timezones.txt');
94 /// Final resort, use the copy included in Moodle
96 $source = $CFG->dirroot.'/lib/timezones.txt';
97 if (!$importdone and is_readable($source)) { // Distribution file
98 if ($timezones = get_records_csv($source, 'timezone')) {
99 update_timezone_records($timezones);
100 $importdone = $source;
105 /// That's it!
107 if ($importdone) {
108 $a = null;
109 $a->count = count($timezones);
110 $a->source = $importdone;
111 print_heading(get_string('importtimezonescount', 'admin', $a), '', 3);
113 print_continue('calendar.php');
115 $timezonelist = array();
116 foreach ($timezones as $timezone) {
117 if (isset($timezonelist[$timezone->name])) {
118 $timezonelist[$timezone->name]++;
119 } else {
120 $timezonelist[$timezone->name] = 1;
123 ksort($timezonelist);
125 echo "<br />";
126 print_simple_box_start('center');
127 foreach ($timezonelist as $name => $count) {
128 echo "$name ($count)<br />";
130 print_simple_box_end();
132 } else {
133 print_heading(get_string('importtimezonesfailed', 'admin'), '', 3);
134 print_continue('calendar.php');
137 print_footer();