/purpool/visualizationcalculation.php

https://github.com/purpool/purpool · PHP · 268 lines · 153 code · 66 blank · 49 comment · 15 complexity · d3762fb9c3477ecd3f14877ea91c54b2 MD5 · raw file

  1. <?php
  2. ini_set('display_errors', 1);
  3. #################################################################
  4. # Name: dashobard.php #
  5. # Author: John Kuiphoff #
  6. # Description: Allows users to view all purpool related info #
  7. #################################################################
  8. // Include configuration file
  9. include_once('config_path.php'); include_once($config_path.'config.php');
  10. // Include common utility library
  11. include_once($DIR['inc'] . 'Utils.class.php');
  12. // Include database package
  13. include_once($DIR['pear'] . 'MDB2.php');
  14. // Include SMARTY templating engine
  15. include_once($DIR['smarty'] . 'Smarty.class.php');
  16. // Initialize database connection
  17. $dbh = Utils::initDB();
  18. // Initialize templating engine
  19. $tpl = Utils::initTPL();$tpl->assign('site_url', $MISC['site_url']);
  20. // Start new session
  21. session_start();
  22. // Validate user
  23. if(!$_SESSION['username'])
  24. {
  25. header("Location: index.php");
  26. }
  27. // Switch state
  28. switch($_GET['state'])
  29. {
  30. # TOTAL USER SAVINGS
  31. case "totalusersavings":
  32. // Default values for my savings
  33. $gassavings = 0;
  34. $emissionssavings = 0;
  35. $carsoffroad = 0;
  36. // Get users workplace
  37. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  38. $row = $dbh->queryRow($sql);
  39. $workplace = $row['workplace'];
  40. // Get users workplace information
  41. $sql = "SELECT state FROM workplaces WHERE workplace_id = '$workplace'";
  42. $row = $dbh->queryRow($sql);
  43. $state = $row['state'];
  44. // Get the users car and location information
  45. $sql = "SELECT vehiclempg, vehicleco2 FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  46. $row = $dbh->queryRow($sql);
  47. if($row)
  48. {
  49. $mpg = $row['vehiclempg'];
  50. $co2 = $row['vehicleco2'];
  51. } else {
  52. $mpg = 0;
  53. $co2 = 0;
  54. }
  55. // Get number of confirmed rides
  56. $sql = "SELECT pool_id, DATE_FORMAT(rdate, '%v') AS week, DATE_FORMAT(rdate, '%Y') AS year, rdate FROM poolpassengers WHERE user_id = '{$_SESSION['user_id']}' AND confirm = 'accept'";
  57. $result = $dbh->query($sql);
  58. while($row = $result->fetchRow())
  59. {
  60. $pool_id = $row['pool_id'];
  61. $rdate = $row['rdate'];
  62. $week = $row['week'];
  63. $year = $row['year'];
  64. // Get the pool itinerary information
  65. $sql2 = "SELECT driver, route FROM poolitineraries WHERE pool_id = '$pool_id' AND rdate = '$rdate'";
  66. $row2 = $dbh->queryRow($sql2);
  67. $driver = $row2['driver'];
  68. $route = $row2['route'];
  69. // Get route distance
  70. $sql3 = "SELECT distance FROM poolroutes WHERE route_id = '{$row2['route']}'";
  71. $row3 = $dbh->queryRow($sql3);
  72. // Clean distance (1 meter = 0.000621371192 miles)
  73. $distance = round($row3['distance'] * 0.000621371192);
  74. // Get week number and year that rdate occurs on
  75. $gasdate = $week . '|' . $year;
  76. // Get gas prices for the week
  77. $sql4 = "SELECT $state AS gasprices FROM gasprices WHERE uploaddate = '$gasdate'";
  78. $row4 = $dbh->queryRow($sql4);
  79. // Clean gas data (get 'regular' price)
  80. $gasprices = explode(",", $row4['gasprices']);
  81. $gasprice = $gasprices[0];
  82. // Determine if user has driven
  83. if($driver != $_SESSION['user_id'])
  84. {
  85. // Calculate savings in gas and emissions
  86. if($mpg == 0)
  87. {
  88. $gassavings = $gassavings + 0;
  89. $emissionssavings = $emissionssavings + 0;
  90. } else {
  91. $gassavings = $gassavings + ($distance * ($gasprice / $mpg));
  92. $emissionssavings = $emissonssavings + ($distance * $co2);
  93. }
  94. // Calculate miles not driven
  95. $milesnotdriven = $milesnotdriven + $distance;
  96. $carsoffroad++;
  97. }
  98. }
  99. // Format gas savings
  100. $gassavings = '$' . number_format($gassavings, 2);
  101. $tpl->assign('mygassavings', $gassavings);
  102. $tpl->assign('mymilesnotdriven', $milesnotdriven);
  103. $tpl->assign('mycarsoffroad', $carsoffroad);
  104. $tpl->assign('myemissionssavings', $emissionssavings);
  105. echo 'Gas savings: ' . $gassavings . "<br />";
  106. echo 'Miles not driven: ' . $milesnotdriven . "<br />";
  107. echo 'Cars off road: ' . $carsoffroad . "<br />";
  108. echo 'Emissions: ' . $emissionssavings . "<br />";
  109. break;
  110. # TOTAL WORKPLACE SAVINGS
  111. case "totalworkplacesavings":
  112. // Default values for my savings
  113. $gassavings = 0;
  114. $emissionssavings = 0;
  115. $carsoffroad = 0;
  116. // Get users workplace
  117. $sql = "SELECT workplace FROM users WHERE user_id = '{$_SESSION['user_id']}'";
  118. $row = $dbh->queryRow($sql);
  119. $workplace = $row['workplace'];
  120. // Get users workplace information
  121. $sql = "SELECT state FROM workplaces WHERE workplace_id = '$workplace'";
  122. $row = $dbh->queryRow($sql);
  123. $state = $row['state'];
  124. // Get all users that belong to the workplace
  125. $sql9 = "SELECT user_id FROM users WHERE workplace = '$workplace'";
  126. $result9 = $dbh->query($sql9);
  127. while($row9 = $result9->fetchRow())
  128. {
  129. // Current user_id
  130. $user_id = $row9['user_id'];
  131. // Get the users car and location information
  132. $sql = "SELECT vehiclempg, vehicleco2 FROM users WHERE user_id = '$user_id'";
  133. $row = $dbh->queryRow($sql);
  134. if($row)
  135. {
  136. $mpg = $row['vehiclempg'];
  137. $co2 = $row['vehicleco2'];
  138. } else {
  139. $mpg = 0;
  140. $co2 = 0;
  141. }
  142. // Get number of confirmed rides
  143. $sql = "SELECT pool_id, DATE_FORMAT(rdate, '%v') AS week, DATE_FORMAT(rdate, '%Y') AS year, rdate FROM poolpassengers WHERE user_id = '$user_id' AND confirm = 'accept'";
  144. $result = $dbh->query($sql);
  145. while($row = $result->fetchRow())
  146. {
  147. $pool_id = $row['pool_id'];
  148. $rdate = $row['rdate'];
  149. $week = $row['week'];
  150. $year = $row['year'];
  151. // Get the pool itinerary information
  152. $sql2 = "SELECT driver, route FROM poolitineraries WHERE pool_id = '$pool_id' AND rdate = '$rdate'";
  153. $row2 = $dbh->queryRow($sql2);
  154. $driver = $row2['driver'];
  155. $route = $row2['route'];
  156. // Get route distance
  157. $sql3 = "SELECT distance FROM poolroutes WHERE route_id = '{$row2['route']}'";
  158. $row3 = $dbh->queryRow($sql3);
  159. // Clean distance (1 meter = 0.000621371192 miles)
  160. $distance = round($row3['distance'] * 0.000621371192);
  161. // Get week number and year that rdate occurs on
  162. $gasdate = $week . '|' . $year;
  163. // Get gas prices for the week
  164. $sql4 = "SELECT $state AS gasprices FROM gasprices WHERE uploaddate = '$gasdate'";
  165. $row4 = $dbh->queryRow($sql4);
  166. // Clean gas data (get 'regular' price)
  167. $gasprices = explode(",", $row4['gasprices']);
  168. $gasprice = $gasprices[0];
  169. // Determine if user has driven
  170. if($driver != $user_id)
  171. {
  172. // Calculate savings in gas and emissions
  173. if($mpg == 0)
  174. {
  175. $gassavings = $gassavings + 0;
  176. $emissionssavings = $emissionssavings + 0;
  177. } else {
  178. $gassavings = $gassavings + ($distance * ($gasprice / $mpg));
  179. $emissionssavings = $emissonssavings + ($distance * $co2);
  180. }
  181. // Calculate miles not driven
  182. $milesnotdriven = $milesnotdriven + $distance;
  183. $carsoffroad++;
  184. }
  185. }
  186. }
  187. // Format gas savings
  188. $gassavings = '$' . number_format($gassavings, 2);
  189. $tpl->assign('mygassavings', $gassavings);
  190. $tpl->assign('mymilesnotdriven', $milesnotdriven);
  191. $tpl->assign('mycarsoffroad', $carsoffroad);
  192. $tpl->assign('myemissionssavings', $emissionssavings);
  193. echo 'Gas savings: ' . $gassavings . "<br />";
  194. echo 'Miles not driven: ' . $milesnotdriven . "<br />";
  195. echo 'Cars off road: ' . $carsoffroad . "<br />";
  196. echo 'Emissions: ' . $emissionssavings . "<br />";
  197. break;
  198. # DISPLAY DASHBOARD
  199. default:
  200. break;
  201. }
  202. ?>