Correctly identify direction of each trip by it's final stop
Correctly identify direction of each trip by it's final stop

--- a/include/db/trip-dao.inc.php
+++ b/include/db/trip-dao.inc.php
@@ -120,6 +120,23 @@
     return $r['arrival_time'];
 }
 
+function getTripDestination($tripID) {
+    global $conn;
+    $query = "SELECT stops.stop_id, stops.stop_name, stops.stop_desc 
+        from stop_times inner join stops on stop_times.stop_id =  stops.stop_id
+	WHERE trip_id = :tripID order by stop_sequence desc limit 1";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":tripID", $tripID);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    $r = $query->fetch(PDO :: FETCH_ASSOC);
+    return $r;
+}
+
 function getActiveTrips($time) {
     global $conn;
     if ($time == "")

--- a/labs/stopBrowser.kml.php
+++ b/labs/stopBrowser.kml.php
@@ -41,7 +41,8 @@
     if ($trips) {
         foreach ($trips as $key => $row) {
             if ($key < 3) {
-                $description .= $row['route_short_name'] . ' ' . $row['route_long_name'] . ' @ ' . $row['arrival_time'] . "<br>";
+                $destination = getTripDestination($row['trip_id']);
+                $description .= $row['route_short_name'] . ' ' . $destination['stop_name'] . ' @ ' . $row['arrival_time'] . "<br>";
             }
         }
     } else {

--- a/layar_api.php
+++ b/layar_api.php
@@ -45,7 +45,8 @@
         $trips = getStopTripsWithTimes($stop['stop_id'], "", "", "", 3);
         foreach ($trips as $key => $row) {
             if ($key < 3) {
-                $hotspot['line' . strval($key + 2)] = $row['route_short_name'] . ' ' . $row['route_long_name'] . ' @ ' . $row['arrival_time'];
+                $destination = getTripDestination($row['trip_id']);
+                $hotspot['line' . strval($key + 2)] = $row['route_short_name'] . ' ' . $destination['stop_name'] . ' @ ' . $row['arrival_time'];
             }
         }
         if (sizeof($trips) == 0)

file:a/stop.php -> file:b/stop.php
--- a/stop.php
+++ b/stop.php
@@ -133,7 +133,9 @@
 } else {
     foreach ($trips as $trip) {
         echo '<li>';
-        echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " " . $trip['route_long_name'] . "</h3><p>";
+        
+                $destination = getTripDestination($trip['trip_id']);
+        echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " " . $destination['stop_name'] . "</h3><p>";
         $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']);
         if ($viaPoints != "")
             echo '<br><span class="viaPoints">Via: ' . $viaPoints . '</span>';

file:a/trip.php -> file:b/trip.php
--- a/trip.php
+++ b/trip.php
@@ -24,8 +24,10 @@
     $trip = getTrip($tripid);
     $routeid = $trip["route_id"];
 }
-include_header("Stops on " . $trip['route_short_name'] . ' ' . $trip['route_long_name'], "trip");
-trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $trip['route_long_name'], $routeid);
+
+                $destination = getTripDestination($trip['trip_id']);
+include_header("Stops on " . $trip['route_short_name'] . ' ' . $destination['stop_name'], "trip");
+trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $destination['stop_name'], $routeid);
 echo '<span class="content-secondary">';
 echo '<a href="' . $trip['route_url'] . '">View Original Timetable/Map</a>';
 echo '<h2>Via:</h2> <small>' . viaPointNames($tripid) . '</small>';
@@ -66,7 +68,7 @@
 echo '  <ul data-role="listview"  data-inset="true">';
 $stopsGrouped = Array();
 $tripStopTimes = getTripStopTimes($tripid);
-echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' ' . $trip['route_long_name'] . ' (' . ucwords($tripStopTimes[0]['service_id']) . ')</li>';
+echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' ' . $destination['stop_name'] . ' (' . ucwords($tripStopTimes[0]['service_id']) . ')</li>';
 foreach ($tripStopTimes as $key => $tripStopTime) {
     if ($key + 1 > sizeof($tripStopTimes) || stopCompare($tripStopTimes[$key]["stop_name"]) != stopCompare($tripStopTimes[$key + 1]["stop_name"])) {
         echo '<li>';