PageRenderTime 26ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/back_ups/patrick/test/data_access.php

https://github.com/jroglesby/Test-Reservation-System
PHP | 322 lines | 296 code | 23 blank | 3 comment | 9 complexity | 45afc35bdb47f9001e405efc8c78c87a MD5 | raw file
  1. <?php
  2. $localhost = "localhost:3306";
  3. $dbusername = "testres";
  4. $dbpassword = "gaitros";
  5. $dbname = "test_reservation_system";
  6. function redirect()
  7. {
  8. if( !array_key_exists('fsuid', $_SESSION) )
  9. {
  10. $_SESSION['message'] = "Please sign in.";
  11. header( 'Location: http://troyprog.dyndns.tv/~testres/testres.php' ) ;
  12. }
  13. }
  14. function updatePassword($fsuid, $newpassword)
  15. {
  16. global $localhost, $dbusername, $dbpassword, $dbname;
  17. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  18. if(!$con)
  19. {
  20. die("Couldn't connect to SQL host");
  21. }
  22. mysql_select_db($dbname, $con);
  23. mysql_query("UPDATE user set password='$newpassword' where fsuid='$fsuid'");
  24. }
  25. function findLogin($username, $password)
  26. {
  27. global $localhost, $dbusername, $dbpassword, $dbname;
  28. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  29. if(!$con)
  30. {
  31. die("Couldn't connect to SQL host");
  32. }
  33. mysql_select_db($dbname, $con);
  34. $result = mysql_query("SELECT * from user WHERE fsuid='$username' AND password='$password'");
  35. $arr=array();
  36. while($row = mysql_fetch_array($result))
  37. {
  38. $arr[] = $row;
  39. }
  40. return $arr;
  41. }
  42. function findSessions($testname, $classname, $year, $month, $dayofmonth, $dayarray, $timearray)
  43. {
  44. global $localhost, $dbusername, $dbpassword, $dbname;
  45. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  46. if(!$con)
  47. {
  48. die("Couldn't connect to SQL host");
  49. }
  50. mysql_select_db($dbname, $con);
  51. $dateflag=false;
  52. $query = "select t1.seshid, DATE_FORMAT(t1.day, '%c/%d/%Y'), DAYNAME(t1.day), TIME_FORMAT(t1.session_time,'%h:%i%p'),
  53. l1.name, t1.seats_avail FROM test_session t1, location l1, test t2 WHERE t1.locid=l1.locid and t1.testid = t2.testid
  54. and t2.testname='".$testname."' and t2.coursenum='".$classname."' ";
  55. if( $month != NULL)
  56. {
  57. if( $dayofmonth != NULL)
  58. {
  59. if( $year != NULL)
  60. {
  61. $dateflag = true;
  62. $query = $query . "AND (t1.day = '".$year."-".$month."-".$dayofmonth."' ";
  63. }
  64. }
  65. }
  66. if ($dayarray != NULL)
  67. {
  68. if($dateflag == true)
  69. {
  70. $query = $query . "OR (";
  71. }
  72. else
  73. {
  74. $query = $query . "AND (";
  75. }
  76. for($i=0;$i<(count($dayarray)-1);$i++)
  77. {
  78. $query = $query . "DAYNAME(t1.day) = '".$dayarray[$i]."' OR ";
  79. }
  80. $query = $query . "DAYNAME(t1.day) = '".$dayarray[count($dayarray)-1]."')";
  81. }
  82. if ($timearray != NULL)
  83. {
  84. $query = $query . "AND (";
  85. for($i=0;$i<(count($timearray)-1);$i++)
  86. {
  87. $query = $query . "t1.session_time = '".$timearray[$i]."' OR ";
  88. }
  89. $query = $query . "t1.session_time = '".$timearray[count($timearray)-1]."')";
  90. }
  91. if($dateflag == true)
  92. {
  93. $query = $query.")";
  94. }
  95. $result = mysql_query($query);
  96. $arr=array();
  97. while($row = mysql_fetch_array($result))
  98. {
  99. $arr[] = $row;
  100. }
  101. return $arr;
  102. }
  103. function findAllSessions($testname, $classname)
  104. {
  105. global $localhost, $dbusername, $dbpassword, $dbname;
  106. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  107. if(!$con)
  108. {
  109. die("Couldn't connect to SQL host");
  110. }
  111. mysql_select_db($dbname, $con);
  112. $query = "select t1.seshid, DATE_FORMAT(t1.day, '%c/%d/%Y'), DAYNAME(t1.day), TIME_FORMAT(t1.session_time,'%h:%i%p'),
  113. l1.name, t1.seats_avail FROM test_session t1, location l1, test t2 WHERE t1.locid=l1.locid and t1.testid = t2.testid
  114. and t2.testname='".$testname."' and t2.coursenum='".$classname."' ";
  115. $result = mysql_query($query);
  116. $arr=array();
  117. while($row = mysql_fetch_array($result))
  118. {
  119. $arr[] = $row;
  120. }
  121. return $arr;
  122. }
  123. function findReservedSessions($fsuid)
  124. {
  125. global $localhost, $dbusername, $dbpassword, $dbname;
  126. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  127. if(!$con)
  128. {
  129. die("Couldn't connect to SQL host");
  130. }
  131. mysql_select_db($dbname, $con);
  132. $result = mysql_query("SELECT t2.coursenum, t2.testname, DATE_FORMAT(t1.day, '%a, %c/%e'), l1.name, TIME_FORMAT(t1.session_time,'%h:%i%p'), t1.seshid, r1.isMakeup
  133. FROM reservation r1, test_session t1, test t2, location l1
  134. WHERE r1.fsuid='$fsuid' AND r1.tsid = t1.seshid AND t1.testid = t2.testid AND t1.locid=l1.locid");
  135. $arr=array();
  136. while($row = mysql_fetch_array($result))
  137. {
  138. $arr[] = $row;
  139. }
  140. return $arr;
  141. }
  142. function deleteReservation($fsuid, $seshid)
  143. {
  144. global $localhost, $dbusername, $dbpassword, $dbname;
  145. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  146. if(!$con)
  147. {
  148. die("Couldn't connect to SQL host");
  149. }
  150. mysql_select_db($dbname, $con);
  151. $query = "delete from reservation where fsuid='$fsuid' and tsid='$seshid'";
  152. mysql_query($query);
  153. $result = mysql_query("SELECT seats_avail from test_session where seshid = '$seshid'");
  154. $row = mysql_fetch_array($result);
  155. $seatcount = $row['seats_avail'];
  156. $seatcount = $seatcount +1;
  157. mysql_query("UPDATE test_session SET seats_avail='$seatcount' where seshid='$seshid'");
  158. $logentry = "$fsuid deleted their reservation for tsid $seshid\n";
  159. $outFileName = "system_log.txt";
  160. $outFileHandle = fopen($outFileName, 'a') or die("File cannot be opened. Ensure file is available for writing.");
  161. fwrite($outFileHandle, $logentry);
  162. }
  163. function findEnrolledClasses($fsuid)
  164. {
  165. global $localhost, $dbusername, $dbpassword, $dbname;
  166. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  167. if(!$con)
  168. {
  169. die("Couldn't connect to SQL host");
  170. }
  171. mysql_select_db($dbname, $con);
  172. $result = mysql_query("SELECT * from enrollment WHERE fsuid='$fsuid'");
  173. $arr=array();
  174. while($row = mysql_fetch_array($result))
  175. {
  176. $arr[] = $row;
  177. }
  178. return $arr;
  179. }
  180. function findAvailableTests($fsuid, $classname)
  181. {
  182. global $localhost, $dbusername, $dbpassword, $dbname;
  183. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  184. if(!$con)
  185. {
  186. die("Couldn't connect to SQL host");
  187. }
  188. mysql_select_db($dbname, $con);
  189. $section_result = mysql_query("SELECT * from enrollment WHERE fsuid='$fsuid' and coursenum='$classname'");
  190. $section_row = mysql_fetch_array($section_result);
  191. $section = $section_row['section'];
  192. $date = date("Y-m-d");
  193. /* TROY SAYS:
  194. Made <= && >= and initiated the arr for ya :)
  195. */
  196. $result = mysql_query("SELECT * from test WHERE coursenum='$classname' and section = '$section' and reg_win_open <= '$date'");
  197. $arr = array();
  198. while($row = mysql_fetch_array($result))
  199. {
  200. $arr[] = $row;
  201. }
  202. return $arr;
  203. }
  204. function insertReservation($fsuid, $seshid, $testname, $makeupbool)
  205. {
  206. global $localhost, $dbusername, $dbpassword, $dbname;
  207. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  208. if(!$con)
  209. {
  210. die("Couldn't connect to SQL host");
  211. }
  212. mysql_select_db($dbname, $con);
  213. $query = " select r1.tsid, t.seats_avail from reservation r1, test_session t where fsuid='$fsuid' AND r1.tsid=t.seshid
  214. AND r1.tsid IN ( select seshid from test_session t1, test t2 where t2.testname = '$testname' and t1.testid=t2.testid)";
  215. echo $query;
  216. $result = mysql_query($query);
  217. $row = mysql_fetch_array($result);
  218. $oldid = $row['tsid'];
  219. $oldseat = $row['seats_avail'];
  220. if($makeupbool == false)
  221. {
  222. $makeup='0';
  223. }
  224. else if ($makeupbool == true)
  225. {
  226. $makeup='1';
  227. }
  228. echo "Old tsid = ";echo $oldid;
  229. echo "Old seatnum = ";echo $oldseat;
  230. echo "New tsid = ";echo $seshid;
  231. if(mysql_num_rows($result))
  232. {
  233. $query = "UPDATE reservation SET tsid='$seshid' where fsuid='$fsuid' and tsid='$oldid'";
  234. mysql_query($query);
  235. mysql_query("UPDATE reservation SET isMakeup='$makeup' where fsuid='$fsuid' and tsid='$seshid'");
  236. $oldseat=$oldseat+1;
  237. mysql_query("UPDATE test_session SET seats_avail='$oldseat' where seshid='$oldid'");
  238. $logentry = "$fsuid changed their reservation for tsid $oldid to tsid $seshid\n";
  239. $outFileName = "system_log.txt";
  240. $outFileHandle = fopen($outFileName, 'a') or die("File cannot be opened. Ensure file is available for writing.");
  241. fwrite($outFileHandle, $logentry);
  242. }
  243. else
  244. {
  245. mysql_query("INSERT INTO reservation VALUES('$fsuid', '$seshid', '$makeup')");
  246. $logentry = "$fsuid made a reservation for tsid $seshid\n";
  247. $outFileName = "system_log.txt";
  248. $outFileHandle = fopen($outFileName, 'a') or die("File cannot be opened. Ensure file is available for writing.");
  249. fwrite($outFileHandle, $logentry);
  250. }
  251. $result2 = mysql_query("SELECT seats_avail from test_session where seshid = '$seshid'");
  252. $row = mysql_fetch_array($result2);
  253. $seatcount = $row['seats_avail'];
  254. $seatcount = $seatcount-1;
  255. mysql_query("UPDATE test_session SET seats_avail='$seatcount' where seshid='$seshid'");
  256. }
  257. function checkIfMakeup($coursenum, $testname)
  258. {
  259. global $localhost, $dbusername, $dbpassword, $dbname;
  260. $con = mysql_connect($localhost, $dbusername, $dbpassword);
  261. if(!$con)
  262. {
  263. die("Couldn't connect to SQL host");
  264. }
  265. mysql_select_db($dbname, $con);
  266. $query = "select reg_win_close from test where testname='$testname' and coursenum='$coursenum'";
  267. $date = date("Y-m-d");
  268. $result = mysql_query($query);
  269. $row = mysql_fetch_array($result);
  270. if($date <= $row["reg_win_close"])
  271. {
  272. return false;
  273. }
  274. else
  275. {
  276. return true;
  277. }
  278. }