-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent.php
More file actions
174 lines (145 loc) · 4.75 KB
/
Copy pathevent.php
File metadata and controls
174 lines (145 loc) · 4.75 KB
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once('connect_db.php');
session_start();
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR)
));
if(!isset($_SESSION['faculty']))
$_SESSION['faculty'] = $_SESSION['uName'];
// echo $_SESSION['faculty'];
function getClient() {
$client = new Google_Client();
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
}
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
}
return $client;
}
function updateDB($id_list) {
global $db;
$query_string = "SELECT COUNT(*) AS count FROM events where fac_id = ?";
$query = $db->prepare($query_string);
$query->execute([$_SESSION['faculty']]);
while($count = $query->fetch()){
if($count['count']==='0'){
$query_string = "INSERT INTO events VALUES(?,?)";
$query = $db->prepare($query_string);
$query->execute([$_SESSION['faculty'],$id_list]);
}
else{
$query_string = "update events set eventID=? where fac_id=?";
$query = $db->prepare($query_string);
$query->execute([$id_list,$_SESSION['faculty']]);
}
}
header('Location: /');
}
function add_events($event_array, $service) {
foreach ($event_array as &$values){
$event = new Google_Service_Calendar_Event($values);
$event = $service->events->insert('primary', $event);
$id_list .= ',' . $event->getId();
}
// $id_list = mysql_real_escape_string($id_list);
//updating the events table with the latest event IDs added to the google calender
updateDB($id_list);
printf("added events \n");
}
function delete_events($fac_id, $service) {
global $db;
$query_string = "select * from events where fac_id=?";
$query = $db->prepare($query_string);
$query->execute([$_SESSION['faculty']]);
while($id_list = $query->fetch()){
$id_arr = explode(',', $id_list['event_id']);
foreach ($id_arr as &$values)
if ($values!='') {
try {
$service->events->delete('primary', $values);
} catch (Exception $e) {}
}
}
}
$client = getClient();
$service = new Google_Service_Calendar($client);
delete_events($_SESSION['faculty'], $service);
$query_string="select A.course_id, course_name, day, slot_num, room, table_name from courses as A join slot_allocs as B where A.course_id=B.course_id and A.fac_id=?";
$query = $db->prepare($query_string);
$query->execute([$_SESSION['faculty']]);
$retval = $query->fetch();
// semester starting date
$semStartDate = 1;
$semEndDate = 30;
$semStartMonth = intval(date('m'));
if($semStartMonth<=4){
$semStartMonth = 1;
$semEndMonth = 4;
}
else{
$semStartMonth = 8;
$semEndMonth = 11;
}
$year = date('Y');
// monday ==> 1, sunday==>0
$semStartDay = date('w', strtotime("".$year."-".$semStartMonth."-".$semStartDate));
$slot_mapping = array(
'09:00:00-09:55:00',
'10:00:00-10:55:00',
'11:00:00-11:55:00',
'12:00:00-12:55:00',
'13:00:00-14:25:00',
'14:30:00-15:55:00',
'16:00:00-17:25:00',
'17:30:00-19:00:00',
'19:00:00-20:30:00',
);
$event_array = array();
while($row = $query->fetch())
{
$dayOffset = $row['day']-$semStartDay;
if ($dayOffset<0)
$dayOffset += 7;
$firstclassDate = $semStartDate + ($dayOffset)%7;
$eventDate = date_format(date_create($year."-".$semStartMonth."-".$firstclassDate, timezone_open('Asia/Kolkata')), 'Y-m-d');
// get event's start and end time
$timestring = explode('-', $slot_mapping[$row['slot_num']-1]);
$eventStarttime = $timestring[0];
$eventEndtime = $timestring[1];
$untilDate = date_format(date_create($year."-".$semEndMonth."-".$semEndDate, timezone_open('Asia/Kolkata')), 'Ymd');
$event=array(
'summary' => $row['course_id'] . " : " . $row['course_name'],
'location' => $row['room'],
'start' => array(
'dateTime' => $eventDate."T".$eventStarttime."+05:30",
'timeZone' => 'Asia/Kolkata',
),
'end' => array(
'dateTime' => $eventDate."T".$eventEndtime."+05:30",
'timeZone' => 'Asia/Kolkata',
),
'recurrence' => array(
'RRULE:FREQ=WEEKLY;UNTIL='.$untilDate.'T235959Z'
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
);
// push the above event now to array
array_push($event_array,$event);
}
//add events to calendar
add_events($event_array, $service);
header('Location: /');
?>