/
functions.php
executable file
·3146 lines (2939 loc) · 107 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
<?php
class Pref
{
public $section;
public $ta;
public $value;
}
function html_header($title)
{
print '<!DOCTYPE html>';
print '<html lang="en">';
print '<head>';
print '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
print '<title>';
print $title;
print '</title>';
print '<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">';
print '<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">';
print '<link rel="stylesheet" type="text/css" href="css/styles.css">';
print '<link rel="shortcut icon" href="icon.ico" >';
print '<style>';
print '.modal-footer { border-top: 20px; }';
print '.modal-content { margin-top: 100px; }';
print 'h1{';
print ' font-family: "Lucida Sans Unicode", "Arial", "sans-serif";';
print '}';
print '.btn-primary{';
print ' background-color: #002855;';
print ' border-color: #002855;';
print '}';
print 'a{';
print ' color: #002855;';
print '}';
print '</style>';
print '<!-- Site Header Content //-->';
print '</head>';
} //html_header
function staffheader($name, $filename, $year, $season_name, $season)
{
if((isset($_SESSION['scipfile_exists']) && $_SESSION['scipfile_exists'] != $season) ||
(isset($_SESSION['gsfile_exists']) && $_SESSION['gsfile_exists'] != $season )||
(isset($_SESSION['scipfile2_exists']) && $_SESSION['scipfile2_exists'] != $season))
{
unset($_SESSION['scipfile_exists']);
unset($_SESSION['gsfile_exists']);
unset($_SESSION['scipfile2_exists']);
}
print'
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="shortcut icon" href="icon.ico" >
</head>
<link rel="stylesheet" href="test_files/css3menu1/style.css" type="text/css" /><style type="text/css">._css3m{display:none}</style>
<link href="http://code.jquery.com/ui/1.9.2/themes/flick/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>
<IMG id="logo" style="height:100px; width:500px;" src="img/logo.png"/>
<a href="loginstaff.php" class="logout rounded blue effect-3">Logout</a>
';
if($filename=="ta.php" || $filename=="tacal.php" || $filename=="conflicts.php" || $filename == "scip.php" || $filename == "scipk.php" || $filename == "mancourse.php" || $filename == "mansection.php" || $filename == "quarterstaff.php" || $filename == "noresponse.php" || $filename == "coursepref.php")
{
print'<p class="quarterid"> '.$season_name.' '.$year.' </p>';
print'<p class="welcome">Welcome, '.$name.'</p>';
}
if($filename == "tapref.php")
{
print'<p class="quarterid" style="margin-top:2%"> '.$season_name.' '.$year.' </p>';
print'<p class="welcome">Welcome, '.$name.'</p><br><br>';
}
print'
<ul id="css3menu1" class="topmenu">
<li class="topfirst"><a href="quarterstaff.php" style="';
if($filename == "quarterstaff.php" || $filename == "passwordstaff.php") print 'color:gold;';
print 'height:30px;line-height:30px;"><span>Select Quarter</a></span>
<ul>
<li class="subfirst"><a ';
if($filename == "passwordstaff.php") print 'style="color:gold"';
print 'href="passwordstaff.php">Change Password</a></li>
</ul></li>
<li class="topmenu"><a href="ta.php" style="';
if($filename == "ta.php" || $filename == "tacal.php") print 'color:gold;';
print 'height:30px;line-height:30px;"><span>Manage TAs</a></span>
<ul>
<li class="subfirst"><a ';
if($filename == "tacal.php") print 'style="color:gold"';
print 'href="tacal.php">TA Schedules</a></li>
</ul></li>
<li class="topmenu"><a href="mancourse.php" style="';
if($filename == "mancourse.php") print 'color:gold;';
print 'height:30px;line-height:30px;">Manage Courses</a></li>
<li class="topmenu"><a href="coursepref.php" style="';
if($filename == "coursepref.php" || $filename == "tapref.php" || $filename == "noresponse.php") print 'color:gold;';
print 'height:30px;line-height:30px;"><span>TA Stats</span></a>
<ul>
<li class ="subfirst"><a ';
if($filename == "tapref.php") print 'style="color:gold"';
print 'href="tapref.php">TA Preferences</a></li>
<li><a ';
if($filename == "noresponse.php") print 'style="color:gold"';
print 'href="noresponse.php">TAs Not Responded</a></li>
</ul></li>
<li class="topmenu"><a href="conflicts.php" style="';
if($filename == "conflicts.php") print 'color:gold;';
print 'height:30px;line-height:30px;">Add Constraints</a></li>
<li class="toplast"><a href="scip.php" style="';
if($filename == "scip.php") print 'color:gold;';
print 'height:30px;line-height:30px;">View Results</a></li>
</ul><p class="_css3m"><a href="http://css3menu.com/">menu drop down</a> by Css3Menu.com</p>
<body>
';
}
function title($ta_name)
{
print '
<IMG id="logo" style="width: 505px; height:100px; margin: 10px;" src="img/logo.png"/>
<a href="login.php" class="logout rounded blue effect-3">Logout</a>
<h1 id="welcome"> <center> Welcome, '.$ta_name.' </center></h1>
';
}
function nav($filename)
{
print'
<ul id="css3menu1" class="topmenu">
<li class="topfirst"><a href="info.php" style="';
if($filename == "info.php" || $filename == "password.php") print 'color:gold;';
print ' height:30px;line-height:30px;"><span>Information</span></a>
<ul>
<li class="subfirst"><a ';
if($filename == "password.php") print 'style="color:gold"';
print 'href="password.php">Change Password</a></li>
</ul></li>
<li class="topmenu"><a href="calendar.php?dept='.$_SESSION['tadept'].'" style="';
if($filename == "calendar.php" || $filename == "quarter.php") print 'color:gold;';
print'"height:30px;line-height:30px;"><span>Calendar</span></a>';
/* <ul>
<li class="subfirst"><a ';
if($filename == "quarter.php") print 'style="color:gold;"';
print 'href="quarter.php">Select Quarter</a></li>
</ul></li>*/
print' <li class="toplast"><a href="MathCode.php" style="';
if($filename == "MathCode.php") print 'color:gold;';
print' height:30px;line-height:30px;">Preferences</a></li>
</ul><p class="_css3m"><a href="http://css3menu.com/"></a></p>
<hr>
';
}
function login_form($status,$username,$staff) {
print '<!--login modal-->';
print '<div id="loginModal" class="modal show" tabindex="-1" role="dialog" aria-hidden="true">';
print ' <div class="modal-dialog">';
print ' <div class="modal-content">';
print ' <div class="modal-header">';
if(!isset($staff))
print ' <img type="image" src="img/info.gif" id="tooltipbutton" value="Test" title="If this is your first time logging in, your username is the first letter of your first name and your entire last name (all lowercase) and your password is your UC Davis student ID.">';
print ' <h1 class="text-center">Login</h1>';
print ' </div>';
print ' <div class="modal-body">';
print ' <div id="error-message">';
print $status;
print ' </div>';
print ' <form class="form col-md-12 center-block" action="';
if(isset($staff)) print 'loginstaff.php'; else print 'login.php';
print'" method="post">';
print ' <div class="form-group">';
print ' <input type="text" class="form-control input-lg "';
if(empty($username))
{
print 'placeholder="Username" ';
}
else
{
print 'value="';
print $username;
print '" ';
}
print 'id="user" name="user">';
print ' </div>';
print ' <div class="form-group">';
print ' <input type="password" class="form-control input-lg" placeholder="Password" id="pass" name="pass">';
print ' </div>
';
print ' <div class="form-group">';
print ' <input type="submit" name="submit" id="submit" value="Submit" class="btn btn-primary btn-lg btn-block" />';
print ' </div>';
print ' </form>';
print ' </div>';
print ' <div class="modal-footer">';
print ' <div class="col-md-12">';
print ' </div>';
print ' </div>';
print ' </div>';
print '</div>';
} //login_form
function print_ranked($neutral, $like, $dislike, $type)
{
print '<p>Neutral '.$type.':</p><ul>';
foreach($neutral as $n)
{
if( $n != "[object HTMLOListElement]")
print '<li>'.$n.'</li>';
}
print '</ul><br />';
print '<p>Liked '.$type.':</p><ul>';
foreach($like as $l)
{
if( $l != "[object HTMLOListElement]")
print '<li>'.$l.'</li>';
}
print '</ul><br />';
print '<p>Disliked '.$type.':</p><ul>';
foreach($dislike as $d)
{
if( $d != "[object HTMLOListElement]")
print '<li>'.$d.'</li>';
}
print '</ul><br />';
} //print_ranked
function input_courses($ta, $list, $flag)
{
// This function inputs the course preferences into the database.
// $flag should be 0 if we are dealing with the neutral list, and -1 or 1 otherwise,
// depending on whether we are dealing with liked or disliked courses.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$count = count($list);
for($i=0; $i<$count; $i++)
{
//get course name
$words = explode(" ", $list[$i]);
$course = $words[1];
$dept = $words[0];
$supersection = '0';
if(count($words) > 2) $supersection = $words[2];
//print '<p>'.$sql.' '.$id.'</p><br />';
//update if value exists, insert otherwise
try {
$sql = "select count(*) from rcourse
where sid = $ta and course = '$course' AND department = '$dept'
AND supersection = '$supersection';";
$result = $db->query($sql)->fetch();
//print '<p>'.$sql.' '.$result[0].'</p><br />';
if( $result[0] > 0)
{
$exec = $db->prepare("UPDATE rcourse SET value = ? WHERE sid = ? and course = ? and department = ? AND supersection = ?;");
$exec->execute(array((($i+1)*$flag), $ta, $course, $dept, $supersection));
}
else
{
$exec = $db->prepare("INSERT into rcourse(sid, course, department, supersection, value) values (?,?,?, ?, ?)");
$exec->execute(array($ta, $course, $dept, $supersection, (($i+1)*$flag)));
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
function input_times($ta, $list, $flag)
{
// This function inputs the tod preferences into the database.
// $flag should be 0 if we are dealing with the neutral list, and -1 or 1 otherwise,
// depending on whether we are dealing with liked or disliked tods.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$count = count($list);
for($i=0; $i<$count; $i++)
{
$tod = $list[$i];
//print '<p>'.$sql.' '.$id.'</p><br />';
//update if value exists, insert otherwise
if($tod != "[object HTMLOListElement]")
try {
$sql = 'select count(*) from rtod where sid = '.$ta.' and tod = "'.$tod.'";';
$result = $db->query($sql)->fetch();
//print '<p>'.$sql.' '.$result[0].'</p><br />';
if( $result[0] > 0)
{
$exec = $db->prepare("UPDATE rtod SET value = ? WHERE sid = ? and tod = ?;");
$exec->execute(array((($i+1)*$flag), $ta, $tod));
}
else
{
$exec = $db->prepare("INSERT into rtod(sid, tod, value) values (?, ?, ?)");
$exec->execute(array($ta, $tod, (($i+1)*$flag)));
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
function parse_courses($raw, $ta)
{
$lists = explode( '|', $raw);
$neutral = explode( ',', $lists[0]);
$like = explode( ',', $lists[1]);
$dislike = explode( ',', $lists[2]);
//print_ranked($neutral, $like, $dislike, "Courses");
input_courses($ta, $neutral, "0");
input_courses($ta, $like, "1");
input_courses($ta, $dislike, "-1");
} //parse_courses
function parse_tods($raw, $ta)
{
$lists = explode( '|', $raw);
$neutral = explode( ',', $lists[2]);
$like = explode( ',', $lists[0]);
$dislike = explode( ',', $lists[1]);
// print_ranked($neutral, $like, $dislike, "Times");
input_times($ta, $neutral, "0");
input_times($ta, $like, "1");
input_times($ta, $dislike, "-1");
} //parse_courses
function puts($string) {
echo $string . "<br/>";
}
function try_again($str,$str2,$str3) {
puts(""); //reset the modal
login_form($str,$str2,$str3);
}
function print_movables($type,$sql)
{
//Queries the db with the SQL statement and prints out each corresponding course
//name as a movable list item.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$result = $db->query($sql);
foreach($result as $name)
{
if($type == "course")
{
print '<li class="'.$type.'"><i style="float:left" class="icon-move"></i><div style="text-align:center">'.$name['department'].' '.$name['name'];
$sql2 = "select count(*) from section
where department = '".$name['department']."'
AND course = '".$name['name']."' AND supersection = 'A';";
$result2 = $db->query($sql2)->fetch();
if($result2[0] > 0)
print ' '.$name['supersection'];
print '</div></li>';
}
else
print '<li class="'.$type.'"><i style="float:left" class="icon-move"></i><div style="text-align:center">'.$name['name'].'</div></li>';
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function populate_neutral_courses($ta, $dept, $season, $year)
{
//This function has two parts. First we must find all courses not yet ranked by this TA.
//Then we want all courses ranked 0 by this TA.
$sql = "select c.name as name, c.department as department, s.supersection as supersection
from course c, ta t, section s
where c.department = t.department
AND s.department = c.department AND s.course = c.name
AND t.sid = '$ta' and c.department = '$dept'
AND s.status != 'deleted' and c.status !='deleted'
AND s.quarter_season = '$season' AND s.quarter_year = '$year'
AND s.status != 'staff'
group by c.department, c.name, supersection
except
select course, department, supersection
from rcourse
where sid = '$ta' and department = '$dept' ;";
print_movables("course",$sql);
$sql = "select r.course as name, r.department, r.supersection
from rcourse r, course c
where r.sid = '$ta' and r.department = '$dept' and r.value = 0
and r.course = c.name and r.department = c.department
and c.status != 'deleted';";
print_movables("course",$sql);
} //populate neutral courses
function populate_liked_courses($ta, $dept, $season, $year)
{
$sql = "select course as name, department, supersection
from rcourse
where sid = '$ta' and department = '$dept' and value > 0
order by value asc;";
print_movables("course",$sql);
} //populate liked courses
function populate_disliked_courses($ta, $dept, $season, $year)
{
$sql = "select course as name, department, supersection
from rcourse
where sid = '$ta' and department = '$dept' and value < 0
order by value desc;";
print_movables("course",$sql);
} //populate disliked courses
function populate_neutral_times($ta)
{
//This function has two parts. First we must find all times not yet ranked by this TA.
//Then we want all courses ranked 0 by this TA.
$sql = "select name, start from tod except select t.name, t.start from tod t, rtod r where r.sid = '$ta' and r.tod = t.name order by start asc;";
print_movables("time",$sql);
$sql = "select t.name, t.start from tod t, rtod r where r.sid='$ta' and r.tod = t.name and r.value=0 order by t.start asc;";
print_movables("time",$sql);
} //populate neutral times
function populate_liked_times($ta)
{
$sql = "select t.name, r.value from tod t, rtod r where r.sid='$ta' and r.tod = t.name and r.value>0 order by r.value asc;";
print_movables("time",$sql);
} //populate liked courses
function populate_disliked_times($ta)
{
$sql = "select t.name from tod t, rtod r where r.sid='$ta' and r.tod = t.name and r.value<0 order by r.value desc;";
print_movables("time",$sql);
} //populate disliked courses
function set_pref_values($ta, $block, $btb, $sameday, $ct)
{
//We put the four integer preference values into the TA's record.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
$sql = "select count(*) from pref where sid = $ta;";
$result = $db->query($sql)->fetch();
if( $result[0] > 0)
{
$exec = $db->prepare("UPDATE pref SET block = ?, same_day = ?, back_to_back = ?, courses_vs_times = ? WHERE sid = ?;");
$exec->execute(array($block, $sameday, $btb, $ct, $ta));
}
else
{
$exec = $db->prepare("INSERT INTO pref(block, same_day, back_to_back, courses_vs_times, sid) VALUES(?,?,?,?,?);");
$exec->execute(array($block, $sameday, $btb, $ct, $ta));
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
} //set ta's preference values
function print_pref_value($field, $ta, $value)
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($field == 'back-to-back')
$value = 0;
$sql = 'SELECT count(*) FROM pref WHERE sid = "'.$ta.'" AND '.$field.' = "'.$value.'";';
$result = $db->query($sql)->fetch();
if( $result[0] != 0)
print 'selected="selected"';
}
function populate_professors()
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM professor ORDER BY name_full;';
$result = $db->query($sql);
print '<option value="0">None</option>';
foreach($result as $name)
{
print '<option value="'.$name['id'].'">'.$name['name_first'].' '.$name['name_last'].'</option>';
}
} //populate professors
function populate_quarters()
{
//first get current quarter and then go to next quarter.
$date = getdate();
$year = $date["year"];
$month = ($date["mon"]);
if($month < 3) $season = 1;
else if($month < 6) $season = 2;
else if($month < 8) $season = 3;
else if($month < 10) $season = 4;
else if($month <= 12) $season = 5;
$season++;
if($season == 3 || $season == 4)
$season = 5;
if($season > 5)
{
$season = 1;
$year++;
}
//next show current quarter and future/past quarters
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'select * from quarter WHERE (year*10+season_num) <= '.$year.$season.' ORDER BY year DESC,season_num DESC LIMIT 5;';
$result = $db->query($sql);
foreach($result as $quarter)
{
print '<option
value="'.$quarter['year'].'|'.$quarter['season_num'].'|'.$quarter['season'].'"';
if($quarter['year'] == $year && $quarter['season_num'] == $season)
print ' selected="selected" ';
print '>'.$quarter['season'].' '.$quarter['year'].'</option>';
}
}
function populate_depts()
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM department ORDER BY code ASC;';
try{
$result = $db->query($sql);
foreach($result as $dept)
{
if($dept['code'] == $_SESSION['dept'])
print '<option selected="selected" value="'.$dept['code'].'">'.$dept['code'].'</option>';
else if($dept['code'] != "Course")
print '<option value="http://tamatch.math.ucdavis.edu/MathWeb/calendar.php?dept='.$dept['code'].'">'.$dept['code'].'</option>';
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function populate_courses($dept, $url)
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT c.name FROM course c, event e WHERE c.department = \"$dept\" AND e.course = c.name AND e.department = c.department AND e.quarter_year = ".$_SESSION['year']." AND e.quarter_season = ".$_SESSION['season'];
if($url == "http://tamatch.math.ucdavis.edu/MathWeb/scip.php")
$sql.=" AND c.level < 2 AND e.type = 'discussion'
GROUP BY c.name ORDER BY c.level ASC;";
else if($url == "http://tamatch.math.ucdavis.edu/MathWeb/coursepref.php")
$sql.=" GROUP BY c.name ORDER BY 1 ASC;";
else
$sql.=" GROUP BY c.name ORDER BY 1 DESC;";
try{
$result = $db->query($sql);
print '<option value="'.$url.'?dept='.$dept.'&course=NULL">--Select a Course--</option>';
foreach($result as $course)
{
if($course['name'] == $_SESSION['course'])
print '<option selected = "selected" value="'.$course['name'].'">'.$course['name'].'</option>';
else
print '<option value="'.$url.'?dept='.$dept.'&course='.$course['name'].'">'.$course['name'].'</option>';
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function populate_sections($course, $dept, $url)
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT s.name
FROM section s, event e
WHERE e.course = s.course AND e.department = s.department
AND e.section = s.name AND e.course = "'.$course.'"
AND e.department ="'.$dept.'" AND e.quarter_year = '.$_SESSION['year'].'
AND e.quarter_season ='.$_SESSION['season'].'
AND s.status != "deleted"
GROUP BY s.name ORDER BY s.name ASC;';
try{
$result = $db->query($sql);
print '<option value="'.$url.'?dept='.$dept.'&course='.$course.'§ion=NULL">--Select a Section--</option>';
foreach($result as $section)
{
print '<option value="'.$url.'?dept='.$dept.'&course='.$course.'§ion='.$section['name'].'"';
if($section['name'] == $_SESSION['section']) print ' selected="selected"';
print '>'.$section['name'].'</option>';
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function add_event($input, $entry, $year, $season, $sid)
{
$section="";
$course="";
$dept = "";
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
//if we came in with section name, we also have dept and course.
if($entry == "section")
{
$section = $input;
$course = $_SESSION['course'];
$dept = $_SESSION['dept'];
}
else if($entry == "crn")
{
//if we came in with crn, need to get section, course, dept from crn.
$crnerror=1;
$sql = "SELECT * FROM section WHERE crn = $input AND quarter_year = $year AND quarter_season = $season;";
$result = $db->query($sql);
foreach($result as $s)
{
//find events, check for conflicts, add to calendar.
$section = $s['name'];
$course = $s['course'];
$dept = $s['department'];
$crnerror--;
}
if($crnerror == 1) return("CRN $crn does not exist.");
}
//Now we have the section data. Find all corresponding events.
$sql = "SELECT * FROM event WHERE section = \"$section\" AND course = \"$course\" AND department = \"$dept\" AND quarter_year = $year AND quarter_season = $season;";
$events = $db->query($sql);
$i=0;
foreach($events as $event)
{
$i++;
//first get all user events of this quarter.
$sql2 = "SELECT * FROM calendar WHERE sid = $sid AND quarter_year = $year AND quarter_season = $season;";
$uevents = $db->query($sql2);
foreach($uevents as $uevent)
{
//check time conflict between $event and $uevent.
if($event['day'] == $uevent['day'] &&
(($event['end']>$uevent['start'] && $uevent['start']>=$event['start']) ||
($uevent['end']>$event['start'] && $event['start']>=$uevent['start'])))
{
$error = "This section conflicts with your current schedule. ";
return($error);
}
//check duplicate section in a course.
if($uevent['course'] == $event['course'] &&
$uevent['department'] == $event['department'])
{
$error = "You are already signed up for a section in this course. ";
return($error);
}
} //foreach uevents as uevent
} //for each event
//if there are no conflicts, now add event to db
$sql3 = "SELECT * FROM event WHERE section = \"$section\" AND course = \"$course\" AND department = \"$dept\" AND quarter_year = $year AND quarter_season = $season;";
$events2 = $db->query($sql3);
foreach($events2 as $event2)
{
//insert into calendar all these events
$exec = $db->prepare("INSERT into calendar(sid, section, course, department, day, start, end, type, quarter_year, quarter_season) values (?,?,?,?,?,?,?,?,?,?)");
$exec->execute(array($sid, $event2['section'], $event2['course'], $event2['department'], $event2['day'], $event2['start'], $event2['end'], $event2['type'], $event2['quarter_year'], $event2['quarter_season']));
}
// return('Reached the End!'.$i);
return("");
} //try
catch(PDOException $e)
{ echo $e->getMessage();}
}
function populate_assigned($sid, $year, $season)
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM calendar WHERE sid = $sid AND quarter_year = $year AND quarter_season = $season GROUP BY department, course, section;";
try
{
$sections = $db->query($sql);
foreach($sections as $section)
{
print '<option value="'.$section['department'].'|'.$section['course'].'|'.$section['section'].'">'.$section['department'].' '.$section['course'].' '.$section['section'].'</option>';
}
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function drop_section($section, $course, $dept, $year, $season, $sid)
{
//got section id, now go and drop all related cal events.
if(!isset($db))
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
try
{
//drop the calendar events of this section
$exec = $db->prepare("DELETE FROM calendar WHERE sid = ? AND section = ? AND course = ? AND department = ? AND quarter_year = ? AND quarter_season = ?;");
$exec->execute(array($sid, $section, $course, $dept, $year, $season));
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
function populate_calendar_body($sid, $year, $season)
{
print '<tbody>';
$ten = new DateInterval("PT10M");
$time = new DateTime('0730');
for($i=0; $i<87; $i+=1)
{
print '<tr>';
if($i%3 == 0)
{
print '<td rowspan="3" style="text-align:center;"><p2 class="shadow40" style="text-align:center;width:100%;">'.$time->format('h:i A').'</p2></td>';
// print '<td rowspan="3">'.($time->format('H')*100+$time->format('i')).'</td>';
}
for($day=1; $day<=5; $day++)
{
//If there is an event starting now, print it with proper rowspan.
//If there is an event going on now, print nothing.
//Otherwise, print empty cell.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
//First gather all sections in an array, for implicit numbering.
$sql = "SELECT course, department
FROM calendar
WHERE quarter_year = $year AND quarter_season = $season AND sid = $sid
GROUP BY department, course;";
$result = $db->query($sql);
$courses = array();
foreach($result as $r)
array_push($courses, $r['department'].' '.$r['course']);
//Get all events for this TA on this day.
if($day==1) $d = "M";
if($day==2) $d = "T";
if($day==3) $d = "W";
if($day==4) $d = "R";
if($day==5) $d = "F";
$sql = "SELECT * FROM calendar WHERE sid = $sid AND quarter_year = $year AND quarter_season = $season AND day = \"$d\";";
$events = $db->query($sql);
$found = 0;
foreach($events as $event)
{
//check for starting now.
$now = ($time->format('H')*100+$time->format('i'));
if($now == $event['start'])
{
//start a cell that spans as long as it needs to.
$a = (string)$event['start'];
$b = (string)$event['end'];
if(strlen($a)==3) $a = '0'.$a;
if(strlen($b)==3) $b = '0'.$b;
$ad = new DateTime($a);
$bd = new DateTime($b);
$interval = $ad->diff($bd);
$span = ($interval->format("%H")*60+$interval->format("%i"))/10;
$title = $event['department'].' '.$event['course'].' '.$event['section'];
print '<td id="nonempty" class="color';
//now get course # mod 6 and print it
for($j=0; $j< count($courses); $j++)
{
if($courses[$j] == $event['department'].' '.$event['course'])
print $j;
}
print '" rowspan="'.$span.'">'.$title.'</td>';
$found++;
}
//check for overlapping now.
if($now > $event['start'] && $now < $event['end'])
{
//do nothing. Literally.
//except say we found a match.
$found++;
}
}
//if we haven't found a time match, nothing is here. print normal cell.
if(!$found) print '<td id="empty'.($i%3).'"></td>';
}
catch(PDOException $e)
{ echo $e->getMessage();}
}
print '</tr>';
$time->add($ten);
}
print '</tbody>';
}
function quote($string)
{
return '"'.$string.'"';
}
function insert_ta($data, $i, $year, $season, $dept)
{
$units = $data->sheets[0]['cells'][$i][4];
$sid = $data->sheets[0]['cells'][$i][5];
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
//from here we know what form the data takes- namely that the headers are
//Name, Rank, Units, SID, Department, Status
//So we can insert a ta based on the data in this row.
//assume name takes the form of "FirstName LastName"
//ONLY INSERT IF sid is unique!!
if($sid != NULL)
{
$sql = "SELECT count(*) FROM ta WHERE sid = $sid and department = '$dept';";
$result = $db->query($sql)->fetch();
if($result[0] == 0)
{
$exec = $db->prepare("DELETE FROM ta where sid = ?;");
$exec->execute(array($sid));
$exec = $db->prepare("DELETE FROM units where sid = ?;");
$exec->execute(array($sid));
$exec = $db->prepare("INSERT into
ta(rank, name_first, name_last, status, sid, department)
values (?, ?, ?, ?, ?, ?)");
$exec->execute(array(
$data->sheets[0]['cells'][$i][3],$data->sheets[0]['cells'][$i][1],
$data->sheets[0]['cells'][$i][2],$data->sheets[0]['cells'][$i][6],
$data->sheets[0]['cells'][$i][5],"$dept"
));
$exec = $db->prepare("INSERT into units(sid, year, season, value)
values (?, ?, ?, ?)");
$exec->execute(array($sid, $year,$season,$units));
} //no blank SID
} //sid unique
} //try
catch(PDOException $e)
{echo $e->getMessage();}
}
function drop_table($table, $dept, $year, $season)
{
//remember that $season is season_num.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
if($table == "section")
{
//delete all sections, events of this quarter and dept.
$exec = $db->prepare('DELETE FROM event WHERE quarter_year = '.$year.' AND quarter_season = '.$season.' AND department = "'.$dept.'";');
$exec->execute();
$exec = $db->prepare('DELETE FROM section WHERE quarter_year = '.$year.' AND quarter_season = '.$season.' AND department = "'.$dept.'";');
$exec->execute();
}
else if($table == "ta")
{
$sql = "SELECT t.sid FROM ta t, units u WHERE t.sid = u.sid
AND u.year = $year AND u.season = $season AND t.department = \"$dept\";";
$result = $db->query($sql);
foreach($result as $r)
{
$exec = $db->prepare("DELETE FROM units WHERE sid = ?;");
$exec->execute(array($r['sid']));
}
$exec = $db->prepare("DELETE FROM ta WHERE department = \"$dept\";");
$exec->execute();
}
}
catch(PDOException $e)
{echo $e->getMessage();}
} //drop_table
function parse_times($times)
{
//Assume Times is in form "DDD T-T, D T-T T-T,..." with D days of week and T times.
//first we must break up the comma separated chunks.
$chunks = explode(",",$times);
$ccount = count($chunks);
$events = array();
for($i=0; $i<$ccount; $i++)
{
$blocks = explode(" ",$chunks[$i]);
$j=0;
if($blocks[$j]=="")
$j++;
$bcount = count($blocks);
//first block should be a string of days.
$days = $blocks[$j];
$j++;
for($j; $j < $bcount; $j++)
{
//each following block should be take the form of T-T, T times.
$t = explode("-",$blocks[$j]);
//make an event for this time period coupled with each day.
for($k=0; $k < strlen($days); $k++)
{
array_push($events, $days[$k], $t[0]*100, $t[1]*100);
}
}
}
return($events);
}
function insert_section($data, $i, $year, $season)
{
//season is int.
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
//from here we know what form the data takes- namely that the headers are
//CRN, Course, Dept, Name, Times
//So we can insert a section based on the data in this row.
$exec = $db->prepare("INSERT into section(course, department, professor_name, professor_dept,quarter_year,quarter_season, crn, name, times) values (?, ?, NULL,NULL, ?, ?, ?, ?,?);");
$exec->execute(array($data->sheets[0]['cells'][$i][2],$data->sheets[0]['cells'][$i][3],
$year, $season,$data->sheets[0]['cells'][$i][1],$data->sheets[0]['cells'][$i][4],
$data->sheets[0]['cells'][$i][5]));
//We have inserted the raw Times data but we need to make all the corresponding events.
$events = parse_times($data->sheets[0]['cells'][$i][5]);
//For each event i:i%3==0; i is the day, i+1 is the start time, i+2 the end time.
for($j=0; $j<count($events); $j+=3)
{
//HACK: Put all events in as lectures and then change the T/R ones to discussion.
$exec = $db->prepare("INSERT into event(section, course, department, type, day, start, end, quarter_year, quarter_season) values (?, ?, ?, ?, ?, ?, ?, ?,?);");
$exec->execute(array($data->sheets[0]['cells'][$i][4],$data->sheets[0]['cells'][$i][2],
$data->sheets[0]['cells'][$i][3],"lecture",$events[$j],$events[$j+1],
$events[$j+2], $year, $season));
$exec = $db->prepare("UPDATE event SET type = 'discussion' WHERE day = 'T';");
$exec->execute();
$exec = $db->prepare("UPDATE event SET type = 'discussion' WHERE day = 'R';");
$exec->execute();
//HACK: Change 22AL to a lecture as opposed to discussion so that later when we input section times 22AL will show up in the spreadsheet
$exec = $db->prepare("UPDATE event SET type = 'lecture' WHERE course = '022AL' and department = 'MAT';");
$exec->execute();
}
}
catch(PDOException $e)
{echo $e->getMessage();}
}
function get_prefs($ta, $year, $season, $dept, $model)
{
//Given a TA sid, we find all their course/tod prefs and compute their section prefs.
//Returns array of section pref objects for all sections this quarter.
//Note that a pref object has members value, ta (sid), and section (course and name).
//First we need this TA's course vs time value.
if(!isset($db))
{
$db = new PDO(DB_PATH, DB_LOGIN, DB_PW);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
try
{
$sql = "select courses_vs_times from pref where sid = $ta;";
$result = $db->query($sql);
$cvt = 0;
$t = 1;
$c = 1;
foreach($result as $r)
$cvt = $r['courses_vs_times'];
if($cvt == 2)
$t = 0;
if($cvt == 1)
$c = 2;
if($cvt == -1)
$t = 2;
if($cvt == -2)
$c = 0;
$num_courses = 0;
$num_tods = 0;
$sql = "select count(*) from rcourse where sid = $ta;";
$result = $db->query($sql)->fetch();
$num_courses = $result[0];
$sql = "select count(*) from rtod where sid = $ta;";
$result = $db->query($sql)->fetch();
$num_tods = $result[0];
//for every section, we must compute prefs.
$sql = sql_section($dept, $year, $season);
$result = $db->query($sql);
$prefs = array();
foreach($result as $r)
{