forked from ditsing/moodle-local_jwc2ical
-
Notifications
You must be signed in to change notification settings - Fork 2
/
locallib.php
241 lines (216 loc) · 5.26 KB
/
locallib.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
<?php
require_once( $CFG->dirroot . '/calendar/lib.php');
$bin_path = $CFG->dirroot . '/local/jwc2ical';
define( "PNAME", 'local_jwc2ical');
function split_date( $day)
{
$cur = explode( "-", $day);
$cur[1] = $cur[1] < 6 ? "春" : "秋";
return "$cur[0]年$cur[1]";
}
/*
* Make sure your code and the get_record return
* data in the same structrue.
*/
function fetch_new_class( $class, $version, &$ret)
{
if ( debugging( '', DEBUG_DEVELOPER))
{
echo "Fetching class $class\n";
}
global $bin_path;
chdir( $bin_path);
exec( "./class $class", $res, $ret);
if ( $ret !== 0)
{
return false;
}
$all_class = array();
$all_exam = array();
$all = array();
$class_parts = array();
$exam_parts = array();
$class_flag = false;
$exam_flag = false;
foreach ( $res as $line)
{
if ( strstr( $line, 'Class'))
{
$name = 'class';
}
else
{
$name = 'exam';
}
if ( !${$name.'_flag'})
{
${$name.'_parts'} = explode( ' ', $line);
${$name.'_flag'} = true;
}
else
{
$parts = explode( '`', $line);
$$name = new stdClass();
for ( $i = 1; $i < count( ${$name.'_parts'}); $i++)
{
${$name}->${$name.'_parts'}[$i] = $parts[$i];
}
$$name->version = $version;
${'all_' . $name}[] = $$name;
$all[] = $$name;
}
}
$ret = $all;
return true;
}
function fetch_class( $class, $dtstart, &$ret)
{
global $DB;
if ( debugging( '', DEBUG_DEVELOPER))
{
echo "Looking for class $class.\n";
}
$ret = $DB->get_records( 'jwc_schedule', array( 'version' => $dtstart, 'class' => $class));
if ( count( $ret) > 0)
{
if ( reset( $ret)->name == 'failed')
{
return false;
}
}
else
{
$ret = array();
$exist = fetch_new_class( $class, $dtstart, $virgin);
if ( $exist)
{
foreach( $virgin as $data)
{
$days = $data->week * 7 - 7 + $data->date;
try
{
$date = new DateTime( $dtstart);
$s_time = explode( ":", $data->s_time);
++$s_time[1]; --$s_time[1]; // To prevent 00 appears.
$date->add( new DateInterval( "P${days}DT$s_time[0]H$s_time[1]M"));
$end = new DateTime( $dtstart);
$t_time = explode( ":", $data->t_time);
++$t_time[1]; --$t_time[1];
$end->add( new DateInterval( "P${days}DT$t_time[0]H$t_time[1]M"));
$is_exam = isset( $data->teacher) && $data->teacher !== '' ? false : true;
$record = array(
'version' => $data->version,
'class' => $class,
'name' => $data->name,
'teacher' => $is_exam ? "" : $data->teacher,
'location' => $data->location,
'repeats' => ( !$is_exam && $data->repeats ? $data->repeats : 1),
'inter' => $is_exam ? 1 : $data->interval,
'time' => $date->getTimestamp(),
'length' => $is_exam ? 120*60 : 105*60
// I do not found any php method to calculate that.
);
$DB->insert_record( 'jwc_schedule', $record);
$ret[] = ( object) $record;
} catch ( Exception $e)
{
error_log( "Aoh, something happened: " . $e->getMessage() . "\n");
return false;
}
}
}
else
{
$record = array(
'version' => $dtstart,
'class' => $class,
'name' => "failed",
'teacher' => NULL,
'location' => 0,
'repeats' => 0,
'inter' => 0,
'time' => 0,
'length' => 0
);
$DB->insert_record( 'jwc_schedule', $record);
return false;
}
}
return true;
}
function correct_entry( $repeatid, $inter)
{
global $DB;
$records = $DB->get_records( 'event', array( 'repeatid' => $repeatid));
$list = array();
$times = array();
foreach( $records as $id => $value)
{
$time = $value->timestart;
$list[$time] = $id;
$times[] = $time;
}
sort( $times);
$cnt = 0;
--$inter;
foreach( $times as $time)
{
$DB->set_field( 'event', 'timestart', $time + WEEKSECS * $cnt,
array( 'id' => $list[$time]));
$cnt += $inter;
}
}
function insert_events_single( $stu, $dtstart, $now)
{
$class = $stu->department;
$flag = fetch_class( $class, $dtstart, $events);
if ( $flag)
{
foreach ( $events as $event)
{
$entry = array (
"eventtype" => 'user', #fixed value
"id" => 0, #fixed value
"courseid" => 0, #fixed value
"modulename" => '', #fixed value
"instance" => 0, #fixed value
"action" => 0, #fixed value
"duration" => 2, #fixed value
"repeat" => 1, #fixed value
"timemodified" => $now,
"userid" => $stu->id, # Get from user information
"name" => $event->name,
"description" => "$event->location $event->teacher",
"timestart" => $event->time,
"repeats" => $event->repeats ? $event->repeats : 1, #repeat times
"timeduration" => $event->length, #in seconds
"uuid" => PNAME # Stamp
);
$cal = new calendar_event();
$flag = $flag && $cal->update( $entry, false);
if ( $flag && $event->inter >= 2)
{
correct_entry( $cal->repeatid, $event->inter);
}
}
return $flag;
}
return false;
}
// Not tested yet!!!
function fix_corrupt_single( $stu)
{
global $DB;
// Remove corrupted entries.
$DB->delete_records( 'jwc_schedule', array( 'class' => $stu->department));
$DB->delete_records( 'event', array( 'userid' => $stu->id, 'uuid' => PNAME));
$dtstart = get_config( PNAME, 'jwc_version');
$now = time();
return insert_events_single( $stu, $dtstart, $now);
}
function clear_jwc_table()
{
global $DB;
//Delete all thing from db.
$DB->delete_records( 'jwc_schedule', array());
}