-
Notifications
You must be signed in to change notification settings - Fork 17
/
import.php
249 lines (228 loc) · 11.6 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
// This file is part of the MRBS block for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file imports bookings via CSV flatfile; it is intended to allow mrbs to be
* populated with bookings from any other timetable sytem. It is intended to run
* regularly, it will replace any non-edited imported bookings with a new copy but
* not any that have been edited.
*
* It is included by the blocks cron() function each time it runs
*/
//TODO:maybe set it up like tutorlink etc so that it can take uploaded files directly?
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
global $DB;
//record time for time taken stat
$script_start_time = time();
$cfg_mrbs = get_config('block/mrbs'); //get Moodle config settings for the MRBS block
if (!isset($cfg_mrbs->periods) or empty($cfg_mrbs->periods)) {
$cfg_mrbs->periods = array();
$cfg_mrbs->periods[] = "Period 1";
$cfg_mrbs->periods[] = "Period 2";
$cfg_mrbs->periods[] = "Period 3";
$cfg_mrbs->periods[] = "Period 4";
$cfg_mrbs->periods[] = "Period 5";
$cfg_mrbs->periods[] = "Period 6";
$cfg_mrbs->periods[] = "Period 7";
$cfg_mrbs->periods[] = "Period 8";
$cfg_mrbs->periods[] = "Period 9";
$cfg_mrbs->periods[] = "Period 10";
$cfg_mrbs->periods[] = "Period 11";
$cfg_mrbs->periods[] = "Period 12";
} else {
$pds = explode("\n", $cfg_mrbs->periods);
$cfg_mrbs->periods = array();
foreach ($pds as $pd) {
$pd = trim($pd);
$cfg_mrbs->periods[] = $pd;
}
}
$output = '';
if (!empty($cfg_mrbs->cronfile) && file_exists($cfg_mrbs->cronfile)) {
if ($mrbs_sessions = fopen($cfg_mrbs->cronfile, 'r')) {
$output .= get_string('startedimport', 'block_mrbs')."\n";
$now = time();
$DB->set_field_select('block_mrbs_entry', 'type', 'M', 'type=\'K\' and start_time > ?', array($now)); // Change old imported (type K) records to temporary type M
while ($array = fgetcsv($mrbs_sessions)) { //import timetable into mrbs
$csvrow = new stdClass();
$csvrow->start_time = clean_param($array[0], PARAM_TEXT);
$csvrow->end_time = clean_param($array[1], PARAM_TEXT);
$csvrow->first_date = clean_param($array[2], PARAM_TEXT);
$csvrow->weekpattern = clean_param($array[3], PARAM_TEXT);
$csvrow->room_name = clean_param($array[4], PARAM_TEXT);
$csvrow->username = clean_param($array[5], PARAM_TEXT);
$csvrow->name = clean_param($array[6], PARAM_TEXT);
$csvrow->description = clean_param($array[7], PARAM_TEXT);
list($year, $month, $day) = explode('/', $csvrow->first_date);
$date = mktime(00, 00, 00, $month, $day, $year);
$room = room_id_lookup($csvrow->room_name);
$weeks = str_split($csvrow->weekpattern);
foreach ($weeks as $week) {
if (($week == 1) and ($date > $now)) {
$start_time = time_to_datetime($date, $csvrow->start_time);
$end_time = time_to_datetime($date, $csvrow->end_time);
if (!is_timetabled($csvrow->name, $start_time)) { ////only timetable class if it isn't already timetabled elsewhere (class been moved)
$entry = new stdClass();
$entry->start_time = $start_time;
$entry->end_time = $end_time;
$entry->room_id = $room;
$entry->timestamp = $now;
$entry->create_by = $csvrow->username;
$entry->name = $csvrow->name;
$entry->type = 'K';
$entry->description = $csvrow->description;
$newentryid = $DB->insert_record('block_mrbs_entry', $entry);
//If there is another non-imported booking there, send emails. It is assumed that simultanious imported classes are intentional
$sql = "SELECT *
FROM {block_mrbs_entry} AS e
WHERE
((e.start_time < ? AND e.end_time > ?)
OR (e.start_time < ? AND e.end_time > ?)
OR (e.start_time >= ? AND e.end_time <= ? ))
AND e.room_id = ? AND type <>'K'";
//limit to 1 to keep this simpler- if there is a 3-way clash it will be noticed by one of the 2 teachers notified
if ($existingclass = $DB->get_record_sql($sql, array(
$start_time, $start_time, $end_time,
$end_time, $start_time, $end_time, $room
))
) {
$hr_start_time = date("j F, Y", $start_time).", ".to_hr_time($start_time);
$a = new stdClass();
$a->oldbooking = $existingclass->description.'('.$existingclass->id.')';
$a->newbooking = $csvrow->description.'('.$newentryid.')';
$a->time = $hr_start_time;
$a->room = $csvrow->room_name;
$a->admin = $cfg_mrbs->admin.' ('.$cfg_mrbs->admin_email.')';
$output .= get_string('clash', 'block_mrbs', $a);
$existingteacher = $DB->get_record('user', array('username' => $existingclass->create_by));
$newteacher = $DB->get_record('user', array('username' => $csvrow->username));
$body = get_string('clashemailbody', 'block_mrbs', $a);
if (email_to_user($existingteacher, $newteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) {
$output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$existingteacher->firstname.' '.$existingteacher->lastname.'<'.$existingteacher->email.'>';
} else {
$output .= get_string('clashemailnotsent', 'block_mrbs').$existingclass->description.'('.$existingclass->id.')';
}
if (email_to_user($newteacher, $existingteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) {
$output .= ', '.get_string('clashemailsent', 'block_mrbs').' '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>';
} else {
$output .= get_string('clashemailnotsent', 'block_mrbs').$csvrow->description.'('.$newentryid.')';
}
$output .= "\n";
}
}
}
$date += 604800;
//checks for being an hour out due to BST/GMT change and corrects
if (date('G', $date) == 01) {
$date = $date + 3600;
}
if (date('G', $date) == 23) {
$date = $date - 3600;
}
}
}
// any remaining type M records are no longer in the import file, so delete
$DB->delete_records_select('block_mrbs_entry', 'type=\'M\'');
//move the processed file to prevent wasted time re-processing TODO: option for how long to keep these- I've found them useful for debugging but obviously can't keep them for ever
$date = date('Ymd');
if (rename($cfg_mrbs->cronfile, $cfg_mrbs->cronfile.'.'.$date)) {
$output .= $cfg_mrbs->cronfile.get_string('movedto', 'block_mrbs').$cfg_mrbs->cronfile.'.'.$date."\n";
}
$script_time_taken = time() - $script_start_time;
$output .= get_string('finishedimport', 'block_mrbs', $script_time_taken);
echo $output; //will only show up if being run via apache
//email output to admin
if ($mrbsadmin = $DB->get_record('user', array('email' => $cfg_mrbs->admin_email))) {
email_to_user($mrbsadmin, $mrbsadmin, get_string('importlog', 'block_mrbs'), $output);
}
}
}
//==========================================FUNCTIONS==============================================================
//looks up the room id from the name
function room_id_lookup($name) {
global $DB;
if (!$room = $DB->get_record('block_mrbs_room', array('room_name' => $name))) {
$error = "ERROR: failed to return id from database (room $name probably doesn't exist)";
echo $error."\n";
return 'error';
} else {
return $room->id;
}
}
/**
* Checks if a class already has a timetable entry. If a previous imported entry exists,
* and was edited, leave it. If it wasn't edited (flagged by type M), change it's type back to
* K (to show it's an imported record), and return true. If there's no record for the class, or
* updating the type back to K fails, return false.
*
* @param $name string name of the booking
* @param $time int start time of the booking in unix timestamp format
* @return bool does a previous booking exist?
*/
function is_timetabled($name, $time) {
global $DB;
if ($DB->get_record('block_mrbs_entry', array('name' => $name, 'start_time' => $time, 'type' => 'L'))) {
return true;
} else if ($record = $DB->get_record('block_mrbs_entry', array(
'name' => $name, 'start_time' => $time, 'type' => 'M'
))
) {
$upd = new stdClass;
$upd->id = $record->id;
$upd->type = 'K';
if ($DB->update_record('block_mrbs_entry', $upd)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Adds together a date (unixtime) and a time (hh:mm)
*
* @param $date integer date in seconds since epoch
* @param $time string time in hh:mm format
* @return integer date/time in seconds since epoch
*/
function time_to_datetime($date, $time) {
global $cfg_mrbs;
list($hours, $mins) = explode(':', $time);
$hours = intval($hours);
$mins = intval($mins);
if ($cfg_mrbs->enable_periods && $hours == 0 && $mins < count($cfg_mrbs->periods)) {
$hours = 12; // Periods are imported as P1 - 00:00, P2 - 00:01, P3 - 00:02, etc.
// but stored internally as P1 - 12:00, P2 - 12:01, P3 - 12:02, etc.
}
return $date + 60 * $mins + 3600 * $hours;
}
/**
* Returns a human readable mrbs time from a unix timestamp.
* If periods are enabled then gives the name of the period starting at this time
* Will probably break is some idiot has more than 59 periods per day (seems very unlikely though)
*
* @param $time integer unix timestamp
* @return string either the time formatted as hh:mm or the name of the period starting at this time
*/
function to_hr_time($time) {
$cfg_mrbs = get_config('block/mrbs');
if ($cfg_mrbs->enable_periods) {
$period = intval(date('i', $time));
return $cfg_mrbs->periods[$period];
} else {
return date('G:i', $time);
}
}