forked from scottyallen/tutormatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
1624 lines (1408 loc) · 52.5 KB
/
functions.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?
//set_error_handler("error_handler");
function error_handler(){
print "<pre>";
var_dump(debug_backtrace());
print "</pre>";
}
include_once('config.php');
// Accepts array of available times to autofill
function availability_matrix($a=array()){ // TODO This needs to list period times as well
global $periods,$days,$periodNames,$dayNames;
?>
<table class=border cellspacing=0 cellpadding=0>
<tr>
<th>Period</th>
<?
foreach($periods as $period){
echo "<th>$periodNames[$period]</th>";
}
echo "</tr>";
foreach($days as $day){
echo "<tr><td>$dayNames[$day]</td>";
foreach($periods as $period){
if($a[$day][$period]){
$checked = "checked";
} else {
$checked = '';
}
echo "<td align='center'><input type='checkbox' name='avail-$day-$period' $checked></td>";
}
echo "</tr>";
}
echo "</table>";
}
// Accepts array of available times to display boxes for (in form of pairs of
// (period, day)) $selected is a day/period boolean matrix, which gives days
// and periods to mark as selected. If a day/period is not in $shown but in
// $selected, it is shown and checked.
function match_times_matrix($shown=array(),$selected=array()){
global $periods,$days,$periodNames,$dayNames;
$times=array();
foreach ($shown as $pair){
$times[$pair[0]][$pair[1]]=1;
}
?>
<table class=border>
<tr>
<th>Period</th>
<?
foreach($periods as $period){
echo "<th>$periodNames[$period]</th>";
}
echo "</tr>";
foreach($days as $day){
echo "<tr><td>$dayNames[$day]</td>";
foreach($periods as $period){
if($times[$day][$period] || $selected[$day][$period]){
if($selected[$day][$period]){
$checked = "checked";
} else {
$checked = '';
}
echo "<td align='center'><input type='checkbox' $checked name='time-$day-$period' $checked></td>";
} else {
echo "<td> </td>";
}
}
echo "</tr>";
}
echo "</table>";
}
function get_unmatched_students(){
$DB = new DB;
$result = $DB->query("select tutor_request.id from tutor_request left join student on student.id=tutor_request.student_id where student.id=tutor_request.student_id");
$request_ids=array();
while ($row = mysql_fetch_array($result)){
$request_id=$row[0];
// skip requests that are already matched
if (!request_matched($request_id)){
array_push($request_ids,$row[0]);
}
}
return $request_ids;
}
function request_matched($request_id){
$DB = new DB;
$result = $DB->query("select id from match_made where request_id=$request_id");
$row=mysql_fetch_row($result);
return $row[0];
}
// Returns a list of tutor ids that are potential matches for a given student
// id. This is the heart of the tutor matching algorithm.
function find_match($request_id){
$DB = new DB;
// get request details
$result=$DB->query("select * from tutor_request where id=$request_id");
$row=mysql_fetch_array($result);
$student_id=$row["student_id"];
$subject_id=$row["subject"];
$times_per_week=$row["times_per_week"];
$requested_categories = get_request_categories($request_id);
// select all tutors which have subject
$result=$DB->query("select tutor.* from tutor left join tutor2subject on tutor2subject.tutor_id =tutor.id where tutor2subject.subject_id=$subject_id");
// for each tutor returned
$matches_prefered_cat=array();
$matches_other=array();
while ($row = mysql_fetch_array($result)){
$tutor_id=$row[id];
// figure out if has time left
if (time_left($tutor_id) < 1){
continue; // if not, skip
}
// get tutor category
$tutor_category=tutor_category($row["id"]);
$category_matches = in_array($tutor_category, $requested_categories);
// get list of common available times between tutor and student
$common_times=get_common_times($tutor_id,$student_id);
$common_times_number=count($common_times);
// put data into datastructure
if ($category_matches){
array_push($matches_prefered_cat, array($tutor_id, $tutor_category, $common_times, $category_matches,$common_times_number));
} else {
array_push($matches_other, array($tutor_id, $tutor_category, $common_times, $category_matches,$common_times_number));
}
}
// sort matches on common hours
$matches_prefered_cat=msort($matches_prefered_cat,5);
$matches_other=msort($matches_other,5);
$matches=array_merge($matches_prefered_cat,$matches_other);
// return array
return $matches;
}
function msort($array, $column){
$i=0;
$sortarr=array();
for($i=0; $i<count($array); $i++){
$sortarr[]=$array[$i][$column];
}
array_multisort($sortarr, $array);
return($array);
}
// Returns and array of the requested tutor category ids for a particular request id
function get_request_categories($request_id){
$DB = new DB;
$categories=array();
$result = $DB->query("select category_id from tutor_request2category where request_id=$request_id");
while($row=mysql_fetch_row($result)){
array_push($categories,$row[0]);
}
return $categories;
}
//returns the number of time slots a tutor has left
function time_left($tutor_id){
// get number of slots that have been filled
$busy_count=count_times(get_tutor_busy($tutor_id));
// get number of slots left that haven't been filled
$avail_count=count_times(get_tutor_avail($tutor_id));
$avail_count -= $busy_count;
// get total amount of slots tutor wants
$tutor_info=tutor_info($tutor_id);
$total_hours_desired=$tutor_info["total_hours_desired"];
// subtract slots filled from total amount tutor wants, and return smaller between that and slots not yet filled
$time_left=$total_hours_desired - $busy_count;
if ($time_left < $avail_count){
return $time_left;
} else {
return $avail_count;
}
}
// returns an array of the available times in common between a student and a tutor
function get_common_times($tutor_id, $student_id){
global $days, $periods;
// get times for student
$student_times=get_student_avail($student_id);
$student_busy=get_student_busy($student_id);
// get times for tutor
$tutor_times=get_tutor_avail($tutor_id);
$tutor_busy=get_tutor_busy($tutor_id);
$result=array();
// compare
foreach($days as $day){
foreach($periods as $period){
if($student_times[$day][$period] &&
$tutor_times[$day][$period] &&
$tutor_busy[$day][$period] !=1 &&
$student_busy[$day][$period] !=1
){
array_push($result, array($day,$period));
}
}
}
// return common
return $result;
}
// returns the category id that a tutor is in for the specifed tutor id
function tutor_category($tutor_id){
$DB = new DB;
$result = $DB->query("select tutor_category from tutor where tutor.id=$tutor_id");
$row=mysql_fetch_row($result);
return $row[0];
}
function tutor_category_name($tutor_id){
$DB = new DB;
$result = $DB->query("select category_name from tutor,tutor_category where tutor.tutor_category=tutor_category.id and tutor.id=$tutor_id");
$row=mysql_fetch_row($result);
return $row[0];
}
function category_name($id){
$DB = new DB;
$result = $DB->query("select category_name from tutor_category where id=$id");
$row=mysql_fetch_row($result);
return $row[0];
}
function question_table($category, $answers=null){
$DB = new DB;
if ($category == 'student'){
$student_question=1;
}else {
$student_question=0;
}
$query = "SELECT * from question where student_question=$student_question order by id asc";
$result = $DB->query($query);
echo "<table>";
while ($row=mysql_fetch_array($result)){
$id=$row["id"];
$question=$row["question"];
?>
<tr>
<td><? echo $question ?></td>
</tr>
<tr>
<td><textarea rows=4 cols=80 name="question_<? echo $id ?>"><? echo $answers[$id] ?></textarea>
</td>
</tr>
<?
}
echo "</table>";
}
function get_answers($request_id=null,$tutor_id=null){
$DB = new DB;
$query="SELECT * from answer";
if ($request_id){
$query.=" WHERE tutor_request_id=$request_id";
} else if ($tutor_id){
$query.=" WHERE tutor_id=$tutor_id";
}
$result = $DB->query($query);
$answers=array();
while ($row=mysql_fetch_array($result)){
$answers[$row["question_id"]]=$row["answer"];
}
return $answers;
}
function tutor_info($tutor_id){
$DB = new DB;
$result = $DB->query("SELECT tutor.*,tutor_category.category_name from tutor,tutor_category where tutor.tutor_category=tutor_category.id and tutor.id=$tutor_id");
return mysql_fetch_array($result);
}
function student_info($student_id){
$DB = new DB;
if (!$student_id){
print "no student id";
print "<pre>";
print_r(debug_backtrace());
print "</pre>";
return;
}
$result = $DB->query("SELECT student.* from student where student.id=$student_id");
return mysql_fetch_array($result);
}
function request_info($request_id){
$DB = new DB;
$result = $DB->query("SELECT tutor_request.*,subject.subject_name from tutor_request, subject where tutor_request.subject=subject.id and tutor_request.id=$request_id");
return mysql_fetch_assoc($result);
}
function tutor_category_select($currcategory=NULL,$single=null){
$DB = new DB;
$result = $DB->query("SELECT * from tutor_category");
if ($single == 'multiple'){
echo "<select multiple name='tutor_category[]'>";
}else {
echo "<select name='tutor_category[]'>";
echo "<option></option>";
}
while ($row=mysql_fetch_array($result)){
$id=$row["id"];
$category=$row["category_name"];
if ($currcategory==$id){
$selected="selected";
} else if ( in_array($id,$currcategory) ){
$selected="selected";
} else {
$selected='';
}
echo "<option value=$id $selected>".$category."</option>";
}
echo "</select>";
}
// returns an array of subject ids that a tutor can tutor
function tutor_subjects($tutor_id){
$DB = new DB;
$result = $DB->query("SELECT subject_id from tutor2subject where tutor_id=$tutor_id");
$subjects=array();
while($row=mysql_fetch_row($result)){
array_push($subjects,$row[0]);
}
return $subjects;
}
function subjects_select($currsubjects=array(),$single=null){
$DB = new DB;
$result = $DB->query("SELECT * from subject order by subject_name");
if ($single == 'single'){
echo "<select name='subjects[]'>";
echo "<option></option>";
}else {
echo "<select multiple name='subjects[]'>";
}
while ($row=mysql_fetch_array($result)){
$id=$row["id"];
$subject=$row["subject_name"];
if (in_array($id,$currsubjects)){
$selected="selected";
} else {
$selected='';
}
echo "<option value=$id $selected>".$subject."</option>";
}
echo "</select>";
}
function grade_select($currgrade=0){
echo "<select name='grade'>";
echo "<option></option>";
foreach (array(9,10,11,12) as $grade){
if ($currgrade==$grade){
$selected="selected";
} else {
$selected='';
}
echo "<option value=$grade $selected>".$grade."th</option>";
}
echo "</select>";
}
function search_students(){
$search_params = array("name", "email", "grade", "username", "signed_contract", "notes");
$query = "SELECT * from student";
$count=0;
foreach ($search_params as $param){
$value = $_REQUEST[$param];
if ($value != ''){
$value=addslashes($value);
if ($count++ == 0){
$query .= " WHERE $param like '%$value%'";
} else {
$query .= " AND $param like '%$value%'";
}
}
}
$query .= " ORDER BY name";
$DB = new DB;
$result = $DB->query($query);
$results=array();
while ($row=mysql_fetch_assoc($result)){
array_push($results,$row);
}
return $results;
}
function search_tutors(){
$search_params = array("name", "email", "grade", "username", "signed_confidentiality", "notes");
$query = "SELECT tutor.id,tutor.name,tutor.address,tutor.phone,tutor.email,tutor.username,tutor.tutor_category,tutor.grade,tutor.total_hours_desired,tutor.signed_confidentiality,tutor.enabled,tutor.notes,tutor_category.category_name from tutor,tutor_category where tutor.tutor_category=tutor_category.id ";
$count=0;
foreach ($search_params as $param){
$value = $_REQUEST[$param];
if ($value != ''){
$value=addslashes($value);
$query .= " AND $param like '%$value%'";
}
}
if ($_REQUEST['tutor_category'][0]){
$value = addslashes($_REQUEST['tutor_category'][0]);
$query .= " AND tutor_category = $value";
}
$query.= " ORDER BY name";
$DB = new DB;
$result = $DB->query($query);
$results=array();
while ($row=mysql_fetch_assoc($result)){
array_push($results,$row);
}
return $results;
}
function search_requests(){
$query = "SELECT tutor_request.*,student.name,subject.subject_name,match_made.id as match_id from student,subject,tutor_request left join match_made on match_made.request_id=tutor_request.id where tutor_request.student_id=student.id and tutor_request.subject=subject.id";
if ( ($student_id = $_SESSION['student_id']) || ($student_id = addslashes($_REQUEST['student_id'])) ){
$query .= " AND student_id=$student_id";
}
$matched = addslashes($_REQUEST['matched']);
if ($matched != NULL){
if($matched){
$query .= " AND match_made.id is not null";
} else {
$query .= " AND match_made.id is null";
}
}
$DB = new DB;
$result = $DB->query($query);
$results=array();
while ($row=mysql_fetch_assoc($result)){
array_push($results,$row);
}
return $results;
}
function num_open_requests($student_id){
$DB = new DB;
$result=$DB->query("SELECT count(*) from tutor_request left join match_made on match_made.request_id=tutor_request.id where match_made.id is null and tutor_request.student_id=$student_id");
$row=mysql_fetch_row($result);
return $row[0];
}
function num_active_matches($student_id=null,$tutor_id=null){
$DB = new DB;
if ($student_id){
$result=$DB->query("SELECT count(*) from match_made,tutor_request where match_made.request_id=tutor_request.id and student_id=$student_id and active=1");
} else if ($tutor_id){
$result=$DB->query("SELECT count(*) from match_made,tutor_request where match_made.request_id=tutor_request.id and tutor_id=$tutor_id and active=1");
}
$row=mysql_fetch_row($result);
return $row[0];
}
function num_dead_matches($student_id=null,$tutor_id=null){
$DB = new DB;
if ($student_id){
$result=$DB->query("SELECT count(*) from match_made,tutor_request where match_made.request_id=tutor_request.id and student_id=$student_id and active=0");
} else if ($tutor_id){
$result=$DB->query("SELECT count(*) from match_made,tutor_request where match_made.request_id=tutor_request.id and tutor_id=$tutor_id and active=0");
}
$row=mysql_fetch_row($result);
return $row[0];
}
function create_match(){
$tutor_id=addslashes($_REQUEST["tutor_id"]);
$request_id=addslashes($_REQUEST["request_id"]);
$start_date=check_date($_REQUEST["start_date"]);
$notes=addslashes($_REQUEST["notes"]);
$times=get_times();
$DB = new DB;
$insert_query="INSERT INTO match_made SET
start_date='$start_date',
request_id=$request_id,
tutor_id=$tutor_id,
match_made=NOW(),
notes='$notes',
active=1
";
$DB->query($insert_query);
$match_id=mysql_insert_id();
add_tutoring_times($times,$match_id);
send_match_emails($match_id);
}
function create_student(){
$name=addslashes($_REQUEST["firstname"]." ".$_REQUEST["lastname"]);
$address=addslashes($_REQUEST["address"]);
$phone=addslashes(check_phone($_REQUEST["phone"]));
$email=addslashes(check_email($_REQUEST["email"]));
$grade=addslashes(check_grade($_REQUEST["grade"]));
$password=addslashes(md5(check_password($_REQUEST["password"],$_REQUEST["password2"])));
$avail=get_avail();
if(! count_times($avail) ){
show_error("You haven't selected any periods that you are available for tutoring. Please go back and fill in at least one period.");
}
$username=get_username($_REQUEST["firstname"],$_REQUEST["lastname"]);
$DB = new DB;
$insert_query="INSERT into student set
name='$name',
address='$address',
phone='$phone',
email='$email',
grade='$grade',
username='$username',
password='$password'
";
$DB->query($insert_query);
$student_id=mysql_insert_id();
$_SESSION["student_id"]=$student_id;
$_SESSION["logged_in"]=true;
add_availability($avail,$student_id);
return "You account has been created. Your username is $username. Make sure can remember your username and password, or write them down, so you can login to this system in the future. Click on 'Create a new tutor request' below to create your first request for tutoring.<p>Please remeber to click 'logout' at the top when you're done.";
}
// Creates a new tutor account from form input
function create_tutor(){
// validate form input
$name=addslashes($_REQUEST["firstname"]." ".$_REQUEST["lastname"]);
$address=addslashes($_REQUEST["address"]);
$phone=addslashes(check_phone($_REQUEST["phone"]));
$email=addslashes(check_email($_REQUEST["email"]));
$tutor_categories=check_tutor_category($_REQUEST["tutor_category"]);
$tutor_category=$tutor_categories[0];
$subjects=check_subjects($_REQUEST["subjects"]);
$grade=addslashes($_REQUEST["grade"]);
$password=addslashes(md5(check_password($_REQUEST["password"],$_REQUEST["password2"])));
$total_hours_desired=addslashes($_REQUEST["total_hours_desired"]);
$avail=get_avail();
if(! count_times($avail) ){
show_error("You haven't selected any periods that you are available for tutoring. Please go back and fill in at least one period.");
}
$username=get_username($_REQUEST["firstname"],$_REQUEST["lastname"]);
// insert data into tables
$DB = new DB;
$insert_query="INSERT into tutor set
name='$name',
address='$address',
phone='$phone',
email='$email',
tutor_category='$tutor_category',
grade='$grade',
username='$username',
password='$password',
total_hours_desired='$total_hours_desired',
enabled=1
";
$DB->query($insert_query);
$tutor_id=mysql_insert_id();
add_availability($avail,null,$tutor_id);
add_tutor_subjects($subjects,$tutor_id);
add_answers(null,$tutor_id);
$_SESSION["tutor_id"]=$tutor_id;
$_SESSION["logged_in"]=true;
return "Thank you for volunteering to tutor with the O-zone. Your username is $username. Please make sure you can remember your username and password or write them down so you can login to this system in the future.<p>We will notify you via email when we have found a student for you. If you don't have email, we will call you.<p>Please remember to click 'logout' at the top of the page when you're done.";
}
function create_tutor_request(){
// validate form input
$subjects=check_subjects($_REQUEST["subjects"]);
$subject=addslashes($subjects[0]);
$teacher=addslashes(check_teacher($_REQUEST['teacher']));
$tutor_categories=check_tutor_categories($_REQUEST["tutor_category"]);
$times_per_week=addslashes(check_times_per_week($_REQUEST["times_per_week"]));
$student_id=addslashes($_SESSION['student_id']);
$DB = new DB;
$insert_query="INSERT into tutor_request set
student_id='$student_id',
teacher='$teacher',
subject='$subject',
times_per_week='$times_per_week'
";
$DB->query($insert_query);
$tutor_request_id=mysql_insert_id();
if($tutor_categories){
add_request_tutor_categories($tutor_categories,$tutor_request_id);
}
add_answers($tutor_request_id);
return "Your request has been submitted, and we'll try and match you with a tutor as soon as we can. We will notify you of a match via email. Please check your email daily for the next week. If you don't have an email, we'll call you when we've found a tutor for you.";
}
function mysql_date2standard_date($mysql_date){
preg_match('/\d\d(\d\d)-(\d\d)-(\d\d)/',$mysql_date,$matches);
return $matches[2].'-'.$matches[3].'-'.$matches[1];
}
function add_answers($tutor_request_id=null,$tutor_id=null){
$DB = new DB;
foreach($_REQUEST as $key => $value){
$value=addslashes($value);
if(preg_match('/question_(\d+)/',$key,$res)){
$DB->query("INSERT into answer set
tutor_request_id='$tutor_request_id',
tutor_id='$tutor_id',
question_id='$res[1]',
answer='$value'");
}
}
}
function delete_answers($tutor_request_id=null,$tutor_id=null){
$DB = new DB;
if ($tutor_request_id){
$DB->query("DELETE from answer where
tutor_request_id='$tutor_request_id'");
} else if ($tutor_id){
$DB->query("DELETE from answer where
tutor_id='$tutor_id'");
}
}
function update_answers($tutor_request_id=null,$tutor_id=null){
delete_answers($tutor_request_id,$tutor_id);
add_answers($tutor_request_id,$tutor_id);
}
function add_request_tutor_categories($tutor_categories,$tutor_request_id){
$DB = new DB;
foreach($tutor_categories as $tutor_category){
$DB->query("INSERT into tutor_request2category set
request_id=$tutor_request_id,
category_id=$tutor_category");
}
}
function add_tutoring_times($times,$match_id){
global $days,$periods;
$DB = new DB;
foreach($days as $day){
foreach($periods as $period){
if($times[$day][$period]){
$DB->query("INSERT into match_time set
match_id=$match_id,
day=$day,
period=$period");
}
}
}
}
function delete_tutoring_times($match_id){
$DB = new DB;
$DB->query("DELETE from match_time where
match_id=$match_id");
}
function update_tutoring_times($times,$match_id){
delete_tutoring_times($match_id);
add_tutoring_times($times,$match_id);
}
function add_availability($avail,$student_id=Null,$tutor_id=NULL){
global $days,$periods;
$DB = new DB;
foreach($days as $day){
foreach($periods as $period){
if($avail[$day][$period]){
$DB->query("INSERT into available set
student_id='$student_id',
tutor_id='$tutor_id',
day='$day',
period='$period'");
}
}
}
}
function update_availability($avail,$student_id=Null,$tutor_id=NULL){
delete_availability($student_id,$tutor_id);
add_availability($avail,$student_id,$tutor_id);
}
function delete_availability($student_id=Null,$tutor_id=NULL){
$DB = new DB;
if ($student_id){
$DB->query("DELETE from available where
student_id='$student_id'");
} else if ($tutor_id){
$DB->query("DELETE from available where
tutor_id='$tutor_id'");
}
}
function add_tutor_subjects($subjects,$tutor_id){
$DB = new DB;
foreach($subjects as $subject){
$DB->query("INSERT into tutor2subject set
tutor_id=$tutor_id,
subject_id=$subject");
}
}
function update_tutor_subjects($subjects,$tutor_id){
delete_tutor_subjects($tutor_id);
add_tutor_subjects($subjects,$tutor_id);
}
function delete_tutor_subjects($tutor_id){
$DB = new DB;
$DB->query("DELETE from tutor2subject where
tutor_id='$tutor_id'");
}
function edit_student(){
$student_id=addslashes($_REQUEST["student_id"]);
$name=addslashes($_REQUEST["name"]);
$address=addslashes($_REQUEST["address"]);
$phone=addslashes(check_phone($_REQUEST["phone"]));
$email=addslashes(check_email($_REQUEST["email"]));
$grade=addslashes(check_grade($_REQUEST["grade"]));
$avail=get_avail();
if(! count_times($avail) ){
show_error("You haven't selected any periods that you are available for tutoring. Please go back and fill in at least one period.");
}
$username=addslashes($_REQUEST["username"]);
$notes=addslashes($_REQUEST["notes"]);
$signed_contract=$_REQUEST["signed_contract"]?1:0;
if($_REQUEST["password"]){
$password=addslashes(md5(check_password($_REQUEST["password"],$_REQUEST["password2"])));
}
$username_id=username_exists($username);
if ($username_id != $student_id && $username_id !=0 ){
show_error("That username already exists");
}
$DB = new DB;
$query = "UPDATE student SET\n";
if ($password){
$query.="password='$password',";
}
$query.="name='$name',
address='$address',
phone='$phone',
email='$email',
grade='$grade',
username='$username',
notes='$notes',
signed_contract=$signed_contract
WHERE id=$student_id";
$DB->query($query);
update_availability($avail,$student_id);
}
function edit_match(){
$match_id=addslashes($_REQUEST["match_id"]);
$start_date=check_date($_REQUEST["start_date"]);
$end_date=$_REQUEST["end_date"];
if($end_date != ''){
$end_date=check_date($_REQUEST["end_date"]);
}
$active=addslashes($_REQUEST["active"])?1:0;
$notes=addslashes($_REQUEST["notes"]);
$times=get_times();
$DB = new DB;
$DB->query("UPDATE match_made SET
start_date='$start_date',
end_date='$end_date',
notes='$notes',
active=$active
WHERE
id=$match_id
");
update_tutoring_times($times,$match_id);
}
function edit_tutor(){
// validate form input
$tutor_id=addslashes($_REQUEST["tutor_id"]);
$name=addslashes($_REQUEST["name"]);
$address=addslashes($_REQUEST["address"]);
$phone=addslashes(check_phone($_REQUEST["phone"]));
$email=addslashes(check_email($_REQUEST["email"]));
$tutor_categories=check_tutor_category($_REQUEST["tutor_category"]);
$tutor_category=$tutor_categories[0];
$subjects=check_subjects($_REQUEST["subjects"]);
$grade=addslashes($_REQUEST["grade"]);
$grade=$grade?$grade:0;
$total_hours_desired=addslashes($_REQUEST["total_hours_desired"]);
$avail=get_avail();
$signed_confidentiality=$_REQUEST["signed_confidentiality"] ? 1 : 0;
if(! count_times($avail) ){
show_error("You haven't selected any periods that you are available for tutoring. Please go back and fill in at least one period.");
}
$username=addslashes($_REQUEST["username"]);
if($_REQUEST["password"]){
$password=addslashes(md5(check_password($_REQUEST["password"],$_REQUEST["password2"])));
}
$username_id=username_exists($username);
if ($username_id != $tutor_id && $username_id !=0 ){
show_error("That username already exists");
}
$DB = new DB;
$query = "UPDATE tutor SET\n";
if ($password){
$query.="password='$password',";
}
$query.="name='$name',
address='$address',
phone='$phone',
email='$email',
grade=$grade,
tutor_category=$tutor_category,
total_hours_desired=$total_hours_desired,
username='$username',
signed_confidentiality=$signed_confidentiality
WHERE id=$tutor_id";
$DB->query($query);
update_availability($avail,null,$tutor_id);
update_tutor_subjects($subjects,$tutor_id);
update_answers(null,$tutor_id);
}
function edit_request(){
$request_id=addslashes($_REQUEST["request_id"]);
$teacher=addslashes($_REQUEST["teacher"]);
$subjects=$_REQUEST["subjects"];
$subject=$subjects[0];
$times_per_week=addslashes($_REQUEST["times_per_week"]);
$notes=addslashes($_REQUEST["notes"]);
$DB = new DB;
$query = "UPDATE tutor_request SET
teacher='$teacher',
subject=$subject,
times_per_week=$times_per_week,
notes='$notes'
WHERE id=$request_id";
$DB->query($query);
}
function cancel_request(){
$request_id=addslashes($_REQUEST["request_id"]);
$DB = new DB;
$query = "DELETE FROM tutor_request
WHERE id=$request_id";
$DB->query($query);
delete_answers($request_id);
}
function delete_student() {
$student_id=addslashes($_REQUEST['student_id']);
$DB = new DB;
$request_result=$DB->query("SELECT id from tutor_request where student_id=$student_id");
if(mysql_num_rows($request_result)>0){
while($row=mysql_fetch_array($request_result)){
$tutor_request_id=$row[0];
$DB->query("DELETE FROM answer WHERE tutor_request_id=$tutor_request_id");
$DB->query("DELETE FROM tutor_request2category WHERE request_id=$tutor_request_id");
$match_result=$DB->query("SELECT id from match_made where request_id=$tutor_request_id");
if(mysql_num_rows($match_result)>0){
while($row=mysql_fetch_array($match_result)){
$match_id=$row[0];
$DB->query("DELETE FROM match_time WHERE match_id=$match_id");
}
}
$DB->query("DELETE FROM match_made WHERE request_id=$tutor_request_id");
}
}
$DB->query("DELETE FROM tutor_request WHERE student_id=$student_id");
$DB->query("DELETE FROM available WHERE student_id=$student_id");
$DB->query("DELETE FROM student WHERE id=$student_id");
}
function delete_tutor() {
$tutor_id=addslashes($_REQUEST['tutor_id']);
$DB = new DB;
$match_result=$DB->query("SELECT id from match_made where tutor_id=$tutor_id");
if(mysql_num_rows($match_result)>0){
while($row=mysql_fetch_array($match_result)){
$match_id=$row[0];
$DB->query("DELETE FROM match_time WHERE match_id=$match_id");
}
}
$DB->query("DELETE FROM match_made WHERE tutor_id=$tutor_id");
$DB->query("DELETE FROM answer WHERE tutor_id=$tutor_id");
$DB->query("DELETE FROM available WHERE tutor_id=$tutor_id");
$DB->query("DELETE FROM tutor WHERE id=$tutor_id");
$DB->query("DELETE FROM tutor2subject WHERE tutor_id=$tutor_id");
}
function delete_request() {
$tutor_request_id=addslashes($_REQUEST['request_id']);
$DB = new DB;
$DB->query("DELETE FROM answer WHERE tutor_request_id=$tutor_request_id");
$DB->query("DELETE FROM tutor_request2category WHERE request_id=$tutor_request_id");
$match_result=$DB->query("SELECT id from match_made where request_id=$tutor_request_id");
if(mysql_num_rows($match_result)>0){
while($row=mysql_fetch_array($match_result)){
$match_id=$row[0];
$DB->query("DELETE FROM match_time WHERE match_id=$match_id");
}
}
$DB->query("DELETE FROM match_made WHERE request_id=$tutor_request_id");
$DB->query("DELETE FROM tutor_request WHERE id=$tutor_request_id");
}
function delete_match() {
$match_id=addslashes($_REQUEST['match_id']);
$DB->query("DELETE FROM match_time WHERE match_id=$match_id");
$DB->query("DELETE FROM match_made WHERE id=$match_id");
}
// finds a free username using the specifed first and last name.
function get_username($firstname,$lastname){
$firstname=strip_username_chars($firstname);
$lastname=strip_username_chars($lastname);
$username=strtolower("$firstname.$lastname");
if (username_exists($username)){
for($i=2;$i<50;$i++){
if(!username_exists($username.$i)){
$username=$username.$i;
break;
}
}
}
return $username;
}
// checks to see whether the specified username already exists in the system. if it does, returns the id of the username. Otherwise, returns 0.
function username_exists($username){
$DB = new DB;
$result=$DB->query("select id from student where username='$username'");
if(mysql_num_rows($result)>0){
$row=mysql_fetch_array($result);