-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschedule.php
356 lines (300 loc) · 9.96 KB
/
schedule.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
session_start();
include 'common.php';
/*
2014-10-30: display only 200 raws to preventload on server
change /etc/mysql/mysql.cnf
*=========================*
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
event-scheduler=On
restart mysql service (service mysql restart)
CREATE TABLE `schedule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`every` int(11) NOT NULL,
`unit` varchar(30) NOT NULL,
`starts` datetime NOT NULL,
`description` varchar(500) NOT NULL,
`authority` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`id`)
)
CREATE TABLE `reminder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`schedule_id` int(11) NOT NULL,
`time` datetime NOT NULL,
`checked_by` varchar(30) NOT NULL,
`remark` varchar(200) NOT NULL,
`checked_at` datetime NOT NULL,
`complated` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=840 DEFAULT CHARSET=latin1
*
give authority in schedule
update schedule
create new reminder table
*/
function delete_cron($id)
{
$link=start_nchsls();
$sql='delete from reminder where id=\''.$id.'\'';
//echo $sql;
if(!$result=mysql_query($sql,$link)){echo mysql_error();return FALSE;}
}
function delete_schedule($id)
{
$link=start_nchsls();
$sql='delete from schedule where id=\''.$id.'\'';
//echo $sql;
if(!$result=mysql_query($sql,$link)){echo mysql_error();return FALSE;}
}
function get_schedule_info($id)
{
$link=start_nchsls();
$sql='select * from schedule where id=\''.$id.'\'';
$result=mysql_query($sql,$link);
return mysql_fetch_assoc($result);
}
function view_pending_cron_unitwise($unit)
{
$link=start_nchsls();
echo '<table bgcolor=lightblue>';
if(isset($_POST['checkbox']))
{
$sql='select * from reminder order by id desc';
}
else
{
$sql='select * from reminder where complated=0 order by id desc';
}
$style='style="background-color:lightgray;"';
echo '<tr '.$style.'><th colspan=10>'.$unit.'</th>';
$style='style="background-color:lightpink;"';
echo '<tr '.$style.'>';
// echo '<th>schedule id</th>';
echo '<th>description</th><th>remark</th><th>reminder id</th><th>authority</th>
<th>checked_by</th><th>time of reminder</th><th>checked at</th><th>complated</th></tr>';
$result=mysql_query($sql,$link);
$count=0; //prevent excess of data display
while($ed=mysql_fetch_assoc($result))
{
$schedule_info=get_schedule_info($ed['schedule_id']);
if($schedule_info['unit']==$unit)
{
echo '<form method=post>';
echo '<tr>';
echo '<input type=hidden name=id value=\''.$ed['id'].'\'>'; //id of reminder
echo '<input type=hidden name=unit value=\''.$unit.'\'>'; //id of reminder
//echo '<td>'.$ed['schedule_id'].'</td>';
echo '<td>'.$schedule_info['description'].'</td>';
echo '<td><input type=text name=remark value=\''.$ed['remark'].'\'></td>';
echo '<td><button type=submit name=action value=save_cron>'.$ed['id'].'</td>';
echo '<td>'.$schedule_info['authority'].'</td>';
echo '<td>'.$ed['checked_by'].'</td>';
if($ed['complated']==0)
{
echo '<td><input type=checkbox name=complated></td>';
}
else if($ed['complated']==1)
{
echo '<td><input type=checkbox checked name=complated></td>';
}
echo '<td>'.$ed['time'].'</td>';
echo '<td>'.$ed['checked_at'].'</td>';
echo '</tr>';
echo '</form>';
$count++;
}
if($count>200){echo '<H4>Too much of data!!! Displaying only first 200 raws.</H4>';break;}
}
echo '</table>';
}
function save_cron($post)
{
if(isset($_POST['complated'])){$complated=1;}else{$complated=0;}
$sql='update reminder set
checked_by=\''.$_SESSION['login'].'\' ,
remark=\''.$post['remark'].'\' ,
checked_at=now(),
complated=\''.$complated.'\'
where id=\''.$post['id'].'\'';
//echo $sql;
$link=start_nchsls();
$result=mysql_query($sql,$link);
echo mysql_error();
}
function update_schedule()
{
$link=start_nchsls();
$sql_all_schedule='show events';
if(!$result_all_schedule=mysql_query($sql_all_schedule,$link)){echo 'update_schedule() error in listing all schedules'.mysql_error(); return;}
while($array_all_schedule=mysql_fetch_assoc($result_all_schedule))
{
$sql_delete_schedule='drop event if exists `'.$array_all_schedule['Name'].'`';
if(!$result_delete_schedule=mysql_query($sql_delete_schedule,$link)){echo 'update_schedule() error in deleting schedule '.mysql_error(); return;}
}
$sql='select * from schedule';
if(!$result=mysql_query($sql,$link)){echo 'update_schedule() error'.mysql_error(); return;}
while($array=mysql_fetch_assoc($result))
{
$drop_event_sql='drop event if exists `'.$array['id'].'`';
$create_event_sql='CREATE EVENT `'.$array['id'].'`
ON SCHEDULE
EVERY '.$array['every'].' '.$array['unit'].'
STARTS \''.$array['starts'].'\'
DO
insert into reminder values
(\'\', \''.$array['id'].'\',now(),\'\',\'\',\'\',0)';
if(!$drop_event_result=mysql_query($drop_event_sql,$link)){echo 'update_schedule() drop event error'.mysql_error();}
if(!$create_event_result=mysql_query($create_event_sql,$link)){echo 'update_schedule() create event error'.mysql_error();}
//echo $create_event_sql.'<br>';
}
}
function show_schedule()
{
$link=start_nchsls();
$sql='select * from schedule';
if(!$result=mysql_query($sql,$link)){echo 'show_schedule() error'.mysql_error(); return;}
echo '<table border=1>';
echo '<tr><td>Action</td><td>ID</td><td>Every</td><td>Unit</td><td>Starts</td><td>Description</td><td>authority</td></tr>';
while($array=mysql_fetch_assoc($result))
{
echo '<form method=post>';
echo '<tr>';
echo '<td>
<button type=submit name=action value=delete_schedule>X</button>
</td>';
echo '<input type=hidden name=id value=\''.$array['id'].'\'>';
echo '<td>'.$array['id'].'</td><td>'.$array['every'].'</td><td>'.$array['unit'].'</td><td>'
.$array['starts'].'</td><td>'.$array['description'].'</td><td>'
.$array['authority'].'</td>';
echo '</tr>';
echo '</form>';
}
echo '</table>';
}
function new_schedule()
{
echo '<table border=1> <form method=post>';
echo '<tr>';
echo '<tr><th colspan=5>New Schedule</th></tr>';
echo '<tr><td>Every</td><td><input type=text name=every size=2></td><td>Write a number</td></tr>';
echo '<tr><td>Period</td><td>
<select name=unit>
<option>minute</option>
<option>hour</option>
<option>day</option>
<option>week</option>
<option>month</option>
<option>year</option>
</select></td><td>Select period</td></tr>';
echo '<tr><td>Start from</td><td><input type=text name=starts></td><td>Write starting time Example Format: 2013-11-29 09:10:00</td></tr>';
echo '<tr><td>What to remind</td><td><input type=text name=description></td><td>Describe what to remind for</td></tr>';
echo '<tr><td>Authority</td><td>';
$authority_array=array('1','2','3','4','5');
mk_select_from_array_return_value('authority',$authority_array,'','');
echo '</td><td>Select authority who will verify. R1=1 R2=2 R3=3 Teaching staff=4 HOD=5</td></tr>';
echo '<tr><td><button type=submit name=action value=save_new_schedule>save</button></td></tr>';
echo '</form></table>';
}
function save_new_schedule($array)
{
$link=start_nchsls();
$sql='insert into schedule values(
\'\',
\''.$array['every'].'\',
\''.$array['unit'].'\',
\''.$array['starts'].'\',
\''.$array['description'].'\',
\''.$array['authority'].'\'
)';
//echo $sql;
if(!$result=mysql_query($sql,$link))
{
echo 'save_schedule()'.mysql_error();
}
}
if(!login_varify())
{
exit();
}
main_menu();
echo '<form method=post>';
echo '<table><tr>';
echo '<td><input type=checkbox name=checkbox></td>';
echo '<td><button type=submit name=action value=hour>Hourly Reminders</button></td>';
echo '<td><button type=submit name=action value=day>Daily </button></td>';
echo '<td><button type=submit name=action value=week>Weekly Reminders</button></td>';
echo '<td><button type=submit name=action value=month>Monthly Reminders</button></td>';
echo '<td><button type=submit name=action value=year>Yearly Reminders</button></td>';
echo '<td><button type=submit name=action value=show_schedule>Show Schedule</button></td>';
echo '<td><button type=submit name=action value=new_schedule>New Schedule</button></td>';
echo '<td><button type=submit name=action value=update_schedule>Update Schedule</button></td>';
echo '</tr></table>';
echo '</form>';
if(isset($_POST['action']))
{
if($_SESSION['login']!='root' && substr($_SESSION['login'],0,3)!='Dr.')
{
echo '<h2><font color=red>Please instruct appropriate person for pending work. Thank you</font></h2>';
}
else
{
if($_POST['action']=='delete_reminder')
{
delete_cron($_POST['id']);
view_pending_cron();
}
if($_POST['action']=='show_schedule')
{
show_schedule();
}
if($_POST['action']=='delete_schedule')
{
delete_schedule($_POST['id']);
update_schedule();
show_schedule();
}
if($_POST['action']=='update_schedule')
{
update_schedule();
show_schedule();
}
if($_POST['action']=='new_schedule')
{
new_schedule();
}
if($_POST['action']=='save_new_schedule')
{
save_new_schedule($_POST);
update_schedule();
}
if($_POST['action']=='day'||$_POST['action']=='hour'||$_POST['action']=='week'||$_POST['action']=='month'||$_POST['action']=='year')
{
view_pending_cron_unitwise($_POST['action']);
}
if($_POST['action']=='save_cron')
{
save_cron($_POST);
view_pending_cron_unitwise($_POST['unit']);
}
}
}
//update_schedule();
/*
echo '<pre>';
print_r($GLOBALS);
echo '</pre>';
*/
?>