-
Notifications
You must be signed in to change notification settings - Fork 1
/
appointment.js
199 lines (151 loc) · 5.78 KB
/
appointment.js
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
function loaded() {
gapi.hangout.render('hangout-button', {
'render': 'createhangout',
'invites': [{'id' : '[email protected]', 'invite_type' : 'EMAIL'}],
'initial_apps': [{'app_id' : '211383333638', 'start_data' : 'dQw4w9WgXcQ', 'app_type' : 'ROOM_APP' }],
'widget_size': 175
});
submitData(false);
refreshTime();
}
var obj;
var seq = 0;
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var dow = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
function refreshTime() {
var d = new Date();
var hour = d.getUTCHours().toString().length == 1 ? '0'+d.getUTCHours():d.getUTCHours();
var minute = d.getUTCMinutes().toString().length == 1 ? '0'+d.getUTCMinutes():d.getUTCMinutes();
var second = d.getUTCSeconds().toString().length == 1 ? '0'+d.getUTCSeconds():d.getUTCSeconds();
var lhour = d.getHours().toString().length == 1 ? '0'+d.getHours():d.getHours();
var lminute = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes():d.getMinutes();
var lsecond = d.getSeconds().toString().length == 1 ? '0'+d.getSeconds():d.getSeconds();
var myTime = 'The time now is:<br/><br/><b id="now">'+hour+':'+minute+':'+second + ' UTC, ' + dow[d.getUTCDay()] + ', ' + monthNames[d.getUTCMonth()] + ' ' + d.getUTCDate() + ', '+d.getUTCFullYear()+'</b><br/><i>('+lhour+':'+lminute+':'+lsecond+', ' + dow[d.getDay()] + ', ' + monthNames[d.getMonth()] + ' ' + d.getDate() + ', '+d.getFullYear()+' your time)</i>';
$('#live').html(myTime);
if(!G_static) {
if(meetingMember && meetingMember != '') {
$('#meeting').show();
$('#meeting-user').html('Currently scheduled: <a class="link bold" target="_blank" href="profile.php?user='+meetingMember+'">'+meetingMember+'</a>.');
if(meetingRoom != '') {
$('#meeting-room').attr('href','meeting.php?room='+meetingRoom);
}
else {
$('#meeting').hide();
}
}
else
$('#meeting').hide();
if(++seq % 10 == 0) {
submitData('');
}
}
window.setTimeout(function() {refreshTime() },1000);
}
function changeAppointment(day,time,username) {
submitData('form_id=appoint&day='+day+'&time='+time+'&username='+username);
}
var me = [-1,'0000'];
var meetingRoom = '';
var meetingMember = '';
function submitData(serializedData) {
// variable to hold request
var request;
// abort any pending request
if (request) {
request.abort();
}
// fire off the request to /commitdb.php
request = $.ajax({
url: "/appointmentdb.php",
type: "post",
data: serializedData,
dataType: "json"
});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// log a message to the console
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
// callback handler that will be called on success
request.success(function (result){
var output = ' <tr><td></td><td class="th">Sunday</td><td class="th">Monday</td><td class="th">Tuesday</td><td class="th">Wednesday</td><td class="th">Thursday</td><td class="th">Friday</td><td class="th">Saturday</td></tr>';
var obj = result.appointments;
var reserves = [];
for(var atime in obj) {
for(var i = 0; i < 7; i++) {
var a = obj[atime][i];
if(a && a['username'])
reserves[a['username']] = true;
}
}
var im = false;
var d = new Date();
for(var atime in obj) {
var mmins = parseInt(atime.substring(0,2))*60 + parseInt(atime.substring(2,4));
var mins = d.getUTCHours()*60 + d.getUTCMinutes();
var myTimeM = mmins - d.getTimezoneOffset();
var myHours = Math.floor(myTimeM/60);
var myMins = myTimeM % 60;
var myTime = (myHours < 10 ? '0' : '') + myHours + ':' + (myMins < 10 ? '0' : '') +myMins;
output += '<tr><td class="th pointer" title="'+myTime+' your time."><b>'+atime+'h UTC</b><br/>(<em>'+myTime+'</em>)</td>';
for(var i = 0; i < 7; i++) {
if(obj[atime][i]) {
// meeting room hash
if(obj[atime][i]['room'] && i == d.getUTCDay() && mmins <= mins && mins - mmins < 30) {
meetingRoom = obj[atime][i]['room'];
meetingMember = obj[atime][i]['username'];
}
var a = obj[atime][i];
var func;
if(logged_user && !(a['username']) && !reserves[logged_user])
func = ' onclick="changeAppointment(\''+i+'\',\''+atime+'\',\''+logged_user+'\')"';
else if(logged_user && a['username'] == logged_user || isAdmin)
func = ' onclick="changeAppointment(\''+i+'\',\''+atime+'\',\''+a['username']+'\')"';
else
func = '';
var style;
if(a['username'] && a['username'] == logged_user) {// me
style = 'me';
me = [i,atime];
im = true;
}
else if(a['username']) // taken
style = 'taken';
else if(reserves[logged_user]) // avail, but we are taken
style = 'open';
else
style = 'avail';
var userlink;
if(a['username'] && a['username'] != logged_user)
userlink = '<a href="profile.php?user='+a['username'] +'" target="_blank">'+a['username'] +'</a>';
else if(a['username'])
userlink = a['username'];
else
userlink = '<i>available</i>';
output += '<td'+func+' class="appoint-'+style+'"><div>'+userlink+'</div></td>';
}
else
output += '<td class="appoint-empty"></td>';
}
output += '</tr>';
}
if(im)
$('#register').hide();
else {
me = [-1,'0000'];
$('#register').show();
}
$('#appointment-schedule').html(output);
});
// callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
});
}