-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.php
133 lines (109 loc) · 2.65 KB
/
lib.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
<?php
require_once( $CFG->dirroot . '/local/jwc2ical/locallib.php');
function jwc2ical_update_array( $stus)
{
global $DB;
if ( debugging( '', DEBUG_DEVELOPER))
{
// This goes to command line output directly.
echo "Get ". count( $stus) . " students.\n";
}
$dtstart = get_config( PNAME, 'jwc_version');
$now = time();
$errors = 0;
$cnt = 0;
foreach ( $stus as $stu)
{
$flag = insert_events_single( $stu, $dtstart, $now);
++$cnt;
if ( $flag)
{
// This goes to command line output directly.
if ( debugging( '', DEBUG_DEVELOPER))
{
echo "No.$cnt: $stu->idnumber id $stu->id done\n";
}
}
else
{
error_log( "Processing No.$cnt student $stu->idnumber id $stu->id failed, class is $stu->department");
++$errors;
}
}
if ( $errors !== 0)
{
error_log( "$errors errors occured while inserting events!");
}
set_config( 'timestamp', $now, PNAME);
set_config( 'current_version', $dtstart, PNAME);
return $errors;
}
function select_student( $str)
{
global $DB;
return $DB->get_records_select( 'user', 'auth = \'cas\' AND address != \'1\' AND suspended = \'0\' AND deleted = \'0\'' . $str);
}
function jwc2ical_update_new()
{
$timestamp = get_config( PNAME, 'timestamp');
echo "Updating new user since " . date( "r", $timestamp) . "\n";
$stus = select_student( ' AND timecreated >= ' . $timestamp);
return jwc2ical_update_array( $stus);
}
function jwc2ical_insert_events()
{
echo "Updating\n";
$stus = select_student( "");
return jwc2ical_update_array( $stus);
}
function jwc2ical_delete_events()
{
echo "Rolling back\n";
global $DB;
$DB->delete_records( 'event', array( 'uuid' => PNAME));
clear_jwc_table();
set_config( 'current_version', '0-0-0', PNAME);
}
// From where are we corrupted? idnumber $stu_idnumber
function jwc2ical_fix_corrupt( $stu_idnumber)
{
echo "Fixing corrupts. If this script corrupts again, you can rerun it.\n";
$stus = select_student( " AND idnumber = '$stu_idnumber'");
if ( count( $stus) != 1)
{
echo "Are you sure $stu is the correct idnumber?\n";
return false;
}
else
{
reset( $stus);
$stu = current( $stus);
if ( !fix_corrupt_single( $stu))
{
echo "Failed to fix $stu->id.\n";
return false;
}
}
$stus = select_student( ' AND id >= ' . current( $stu)->id);
return jwc_update_array( $stus);
}
function refresh_date()
{
global $bin_path;
chdir( $bin_path);
$jwc_day = get_config( PNAME, 'jwc_version');
exec( "./date", $res, $ret);
$res = $res[0];
if ( $ret !== 0)
{
return false;
}
elseif ( $res !== $jwc_day)
{
$jwc_day = $res;
clear_jwc_table();
set_config( 'jwc_version', $jwc_day, PNAME);
echo "Refreshed date: $jwc_day\n";
}
return true;
}