diff --git a/classes/Charts.php b/classes/Charts.php index c30b63d..743528e 100644 --- a/classes/Charts.php +++ b/classes/Charts.php @@ -366,7 +366,7 @@ public static function altitudeChart ($user_id, $activity_id, $conn) { //$cfg['title'] = 'Altura'; $cfg['width'] = 800; $cfg['height'] = 300; - $cfg['average-line-visible'] = false; + $cfg['average-line-visible'] = true; $cfg['horizontal-divider-visible'] = true; $cfg['column-divider-visible'] = false; $cfg['round-value-range'] = true; @@ -376,6 +376,55 @@ public static function altitudeChart ($user_id, $activity_id, $conn) { //Create phpMyGraph instance $graph = new \phpMyGraph(); $graph->parseVerticalPolygonGraph($graph_data, $cfg, $altitude_offset); + #$graph->parseVerticalLineGraph($graph_data, $cfg, $altitude_offset); + + } + + public static function paceChart ($user_id, $activity_id, $conn) { + global $base_path; + + $current_act = new Activity(array('id' => $activity_id, 'user_id' => $user_id)); + $current_act->getActivity($conn); + $distancia = $current_act->distance/1000; + + $pace = array(); + + $cumulated_pace = array(); + foreach ($current_act->laps as $key => $value) { + $pace[] = round($value['pace'] * 60, 2); + $cumulated_pace[] = round((array_sum($pace)) / count($pace), 2); + } + + //Set config directives + //$cfg['title'] = 'Altura'; + $cfg['width'] = 800; + $cfg['height'] = 300; + $cfg['average-line-visible'] = false; + $cfg['horizontal-divider-visible'] = true; + $cfg['column-divider-visible'] = false; + $cfg['round-value-range'] = true; + $cfg['zero-line-visible'] = false; + $cfg['file-name'] = $base_path . "/users/" . $user_id . "/reports/alt_profile_" . $activity_id . ".png"; + $cfg['label'] = "Pace vs Cumulated Pace"; + + //Create phpMyGraph instance + #$graph = new \phpMyGraph(); + #$graph->parseVerticalLineGraph($pace, $cfg); + #$graph->parseVerticalLineGraph($pace, $cumulated_pace, $cfg); + +# $graph = new verticalLineGraph(); + + //Parse + $graph = \phpMyGraph::factory('verticalLineGraph', $pace, $cfg); + $graph->parseCompare($pace, $cumulated_pace, $cfg); + + +# $graph = new verticalLineGraph(); + //Parse +# $graph->parseCompare($data1, $data2, $cfg); + + # $graph->parseCompare($pace, $cumulated_pace, $cfg); + } } ?> diff --git a/classes/Goal.php b/classes/Goal.php index f22dd26..aab0ff2 100644 --- a/classes/Goal.php +++ b/classes/Goal.php @@ -166,9 +166,10 @@ function unlinkRecord ($record_id, $conn) { } } - function retrieveInfo ($field, $conn) { - $sql_query = "SELECT SUM(" . $field . ") FROM records WHERE id IN (SELECT record_id FROM goal_records WHERE goal_id = :goal_id)"; + function retrieveInfo ($sport_id, $field, $conn) { + $sql_query = "SELECT SUM(" . $field . ") FROM records WHERE sport_id = :sport_id AND id IN (SELECT record_id FROM goal_records WHERE goal_id = :goal_id) "; $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':sport_id', $sport_id); $stmt->bindParam(':goal_id', $this->id); $result = $stmt->execute(); if ($result) { @@ -176,17 +177,49 @@ function retrieveInfo ($field, $conn) { if ($row[0] == null or $row[0]==0) { return 0; } else { - return sprintf("%01.2f", $row[0]/1000000); # Result is in mm -> divide by 1000000 + if ($field == "distance") { + #return sprintf("%01.2f", $row[0]/1000000); # Result is in mm -> divide by 1000000 A + return $row[0]; + } else if ($field == "duration") { + #return Utils::formatMs($row[0]); + return $row[0]; + } } } else { throw new Exception("Error when retrieving " . $field . " data goal " . $this->id . ": " . json_encode($stmt->errorInfo()) . " | User: " . $this->user_id); } } - public function getKmDays($conn) { + + /** + * Gets sports for current Goal. + * + * @param resource $conn Database resource + * @returns Array of sport_id availables + */ + function getSports($conn) { + $sql_query = "SELECT * FROM sports WHERE id IN (SELECT sport_id FROM records WHERE id IN (SELECT record_id FROM goal_records WHERE goal_id = :goal_id)) ORDER BY id ASC"; + $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':goal_id', $this->id); + $result = $stmt->execute(); + $goal_sports = array(); + if ($result) { + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $sport_tmp = new Sport($row); + $goal_sports[] = $sport_tmp; + } + } else { + throw new Exception("Error when retrieving sports (num: " . $stmt->rowCount() . ") for goal " . $this->id . ". Error: " . json_encode($stmt->errorInfo()) . " | User " . $this->id); + } + return $goal_sports; + } + + + public function getKmDurationDays($sport_id, $conn) { # If only date is provided, mysql assumes time is 00h00:00 - $sql_query = "SELECT start_time, distance FROM records WHERE id IN (SELECT record_id FROM goal_records WHERE goal_id = :goal_id) ORDER BY start_time ASC"; + $sql_query = "SELECT sport_id, start_time, distance, duration FROM records WHERE sport_id = :sport_id AND id IN (SELECT record_id FROM goal_records WHERE goal_id = :goal_id) ORDER BY start_time ASC"; $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':sport_id', $sport_id); $stmt->bindParam(':goal_id', $this->id); $result = $stmt->execute(); if ($result) { @@ -195,12 +228,18 @@ public function getKmDays($conn) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { # Result is in mm -> divide by 1000000 $row['distance'] = sprintf("%01.2f", $row['distance']/1000000); + #$row['duration'] = Utils::formatMs($row['duration']); + $row['duration'] = sprintf("%01.2f", $row['duration'] / 3600000); $dateAndTime = Utils::getDateAndTimeFromDateTime($row['start_time']); $row['date'] = $dateAndTime['date']; if(array_key_exists($row['date'], $date_values)) { //merge different activities from same day $index = $date_values[$row['date']]; $old_value = $kms_goal[$index]['distance']; $kms_goal[$index]['distance'] += $row['distance']; + + $old_value = $kms_goal[$index]['duration']; + $kms_goal[$index]['distance'] += $row['duration']; + } else { $kms_goal[] = $row; $date_values[$row['date']] = count($kms_goal)-1; @@ -213,10 +252,36 @@ public function getKmDays($conn) { } public function imgGoalReport ($img_path, $ttf, $conn) { - $kms = $this->retrieveInfo('distance', $conn); + $kms_duration = ""; + // initial height + $banner_heigh = 30; + $goal_sports = $this->getSports($conn); + foreach ($goal_sports as $sport) { + // legent for this Sport, "Kms" and "Hours" + $met_type_label = Sport::$met_type_label[$sport->met]; + if ($sport->met == Sport::Duration) { + $dato = $this->retrieveInfo($sport->id, 'duration', $conn); + if ($dato <> 0) { + $dato_formateado = Utils::formatMs($dato); + // rpad the name and lpad the value + $kms_duration = $kms_duration . str_pad($sport->abrev . ":" , 10, " ", STR_PAD_RIGHT) . str_pad($dato_formateado, 12, " ", STR_PAD_LEFT) . " " . $met_type_label . "\r\n"; + // increase the banner_height + $banner_heigh = $banner_heigh + 18; + } + } else { + $dato = $this->retrieveInfo($sport->id, 'distance', $conn); + if ($dato <> 0) { + $dato_formateado = sprintf("%01.2f", $dato/1000000); + // rpad the name and lpad the value + $kms_duration = $kms_duration . str_pad ($sport->abrev . ":", 10, " ", STR_PAD_RIGHT) . str_pad($dato_formateado, 12, " ", STR_PAD_LEFT) . " " .$met_type_label . "\r\n"; + // increase the banner_height + $banner_heigh = $banner_heigh + 18; + } + } + } $num_days = Utils::daysToDate(date("Y-m-d"), date($this->goal_date)); - $summary = "#" . $this->name . ": " . $kms . " km (quedan " . $num_days . " días)"; - $im = @imagecreate(400, 30); + $summary = "#" . $this->name ." " . $this->goal_date . " (quedan " . $num_days . " días): \r\n" . $kms_duration; + $im = @imagecreate(400, $banner_heigh); if ($im) { $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); diff --git a/classes/Sport.php b/classes/Sport.php index a4ac530..e5c1bf1 100644 --- a/classes/Sport.php +++ b/classes/Sport.php @@ -1,15 +1,76 @@ "Kms", self::Duration => "Horas"); + + // Defining sport id - const Running = 0; - const Cycling = 1; - const Walking = 2; - const Swimming = 3; + const Running = 1; + const Cycling = 2; + const Walking = 3; + const Swimming = 4; + + public function __set($key,$value) { + $this->$key = $value; + + } + + public function __get($key) { + return $this->$key; + } + + function __construct($data) { + foreach ($data as $key => $value) { + $this->__set($key,$value); + } + } + // ToDo: better localization, move this out of here! - public static $display_es = array(0 => "Correr", 1 => "Ciclismo", 2 => "Caminar", 3 => "Natación"); + //public static $display_es = array(0 => "Correr", 1 => "Ciclismo", 2 => "Caminar", 3 => "Natación", 4=> "Eliptica"); + //public static $met_type = array(0 => self::Distance, 1 => self::Distance, 2 => self::Distance, 3 => self::Distance, 4=> self::Duration); + + + +/** + * + * +*/ + + public function insert ($conn) { + $sql_query = "INSERT INTO sports (user_id, name, abrev, extra_weight, met) VALUES (:user_id, :name, :abrev, :extra_weight, :met)"; + $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':user_id', $this->user_id); + $stmt->bindParam(':name', $this->name); + $stmt->bindParam(':abrev', $this->abrev); + $stmt->bindParam(':extra_weight', $this->extra_weight); + $stmt->bindParam(':met', $this->met); + $result = $stmt->execute(); + if ($result) { + $this->id = $conn->lastInsertId(); + } else { + throw new Exception("Error when inserting sport data: " . json_encode($stmt->errorInfo()) . " | Query: " . $sql_query . " | Sport: " . json_encode($this)); + } + } + /** * Check which sport corresponds to provided pace @@ -26,5 +87,52 @@ static public function check($pace, $distance = 0) { } return $result; } + + /** + * Retrieves all sports info found in database related to specified user + * + * @param string user_id + * object conn + * @return array + */ + public static function getUserSports ($user_id, $conn) { + $sql_query = "SELECT * FROM sports WHERE user_id = :user_id"; + $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':user_id', $user_id); + $result = $stmt->execute(); + if ($result) { + $sports = array(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // TODO: retrieve objects instead of mapped arrays! + $sports[]=$row; + } + return $sports; + } else { + throw new Exception("Error when retrieving user's sports data: " . json_encode($stmt->errorInfo()) . " | User " . $user_id); + } + } + + /** + * Retrieves all equipment data given unit's id + * + * @param object conn + * @return current object + */ + public function getSportData ($conn) { + $sql_query = "SELECT * FROM sports WHERE id = :sport_id"; + $stmt = $conn->prepare($sql_query); + $stmt->bindParam(':sport_id', $this->id); + $result = $stmt->execute(); + if ($result) { + if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + foreach ($row as $key => $value) { + $this->__set($key,$value); + } + } + return $this; // ? + } else { + throw new Exception("Error when executing db query: " . json_encode($stmt->errorInfo()) . " | Sport " . $this->id); + } + } + } ?> diff --git a/classes/User.php b/classes/User.php index e9eb5a5..b095e07 100644 --- a/classes/User.php +++ b/classes/User.php @@ -1,5 +1,6 @@ remote_ip = $remote_ip; $current_token->save($conn); $current_token->send_new_user($this->email); + + $sport = new Sport(array('user_id'=>$this->id, 'name' => 'Correr', 'abrev' => 'RUN', 'extra_weight' => 0, 'met' => 0)); + $sport->insert($conn); + + $sport = new Sport(array('user_id'=>$this->id, 'name' => 'Ciclismo', 'abrev' => 'BIK', 'extra_weight' => 0, 'met' => 0)); + $sport->insert($conn); + + $sport = new Sport(array('user_id'=>$this->id, 'name' => 'Andar', 'abrev' => 'WLK', 'extra_weight' => 0, 'met' => 0)); + $sport->insert($conn); + + $sport = new Sport(array('user_id'=>$this->id, 'name' => 'Natación', 'abrev' => 'SWI', 'extra_weight' => 0, 'met' => 0)); + $sport->insert($conn); + $success = TRUE; } catch (Exception $e) { throw new Exception($e->getMessage() . " | " . $e->getTraceAsString()); @@ -440,7 +455,7 @@ function getGoals($active_filter, $conn) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $goal_tmp = new Goal($row); $user_goals[] = $goal_tmp; - } + } } else { throw new Exception("Error when retrieving goals for user " . $this->id . ". Error: " . json_encode($stmt->errorInfo()) . " | User " . $this->id); } @@ -454,15 +469,16 @@ function getGoals($active_filter, $conn) { * @returns Array of sport_id availables */ function getSports($conn) { - $sql_query = "SELECT DISTINCT sport_id FROM records WHERE user_id = :user_id ORDER BY sport_id ASC"; + $sql_query = "SELECT * FROM sports WHERE user_id = :user_id ORDER BY id ASC"; $stmt = $conn->prepare($sql_query); $stmt->bindParam(':user_id', $this->id); $result = $stmt->execute(); $user_sports = array(); - if ($result and ($stmt->rowCount() > 0)) { - while ($row = $stmt->fetch(PDO::FETCH_NUM)) { - $user_sports[] = $row[0]; - } + if ($result) { + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $sport_tmp = new Sport($row); + $user_sports[] = $sport_tmp; + } } else { throw new Exception("Error when retrieving sports (num: " . $stmt->rowCount() . ") for user " . $this->id . ". Error: " . json_encode($stmt->errorInfo()) . " | User " . $this->id); } @@ -470,7 +486,7 @@ function getSports($conn) { } public function getDaySummary($date, $conn) { - $sql_query = "SELECT * FROM records WHERE user_id = :user_id AND start_time BETWEEN :date1 and DATE_ADD(:date2, INTERVAL 1 DAY)"; + $sql_query = "SELECT records.*, sports.abrev FROM records, sports WHERE records.sport_id = sports.id AND records.user_id = :user_id AND start_time BETWEEN :date1 and DATE_ADD(:date2, INTERVAL 1 DAY)"; $stmt = $conn->prepare($sql_query); $stmt->bindParam(':user_id', $this->id); $stmt->bindParam(':date1', $date); @@ -484,6 +500,7 @@ public function getDaySummary($date, $conn) { if (count($workouts) == 1) { $summary = $workouts[0]; } else { + $summary['abrev'] = $activity['abrev']; $summary['distance'] = 0; $summary['duration'] = 0; $summary['calories'] = 0; @@ -504,14 +521,15 @@ public function getDaySummary($date, $conn) { } $summary['pace'] = ($summary['duration']* 50) /($summary['distance'] * 3); } - + // Abrevation of the activity + $activity_abrev = $summary['abrev']; // Displaying pace depending on distance and speed: // Default: distance in km and pace in min/km // Pace faster than 3'/km (or faster than 3'30"/km and more than 15 km) => seems bike (km/h) // Pace slower than 10'/km => seems swimming, displaying elapsed time instead of speed/pace $speed_display = "@ " . Utils::formatPace($summary['pace']); if ($summary['pace'] < 3 or ($summary['pace'] < 3.5 and ($summary['distance']/1000000) > 15)) { - $speed_display = "@ " . sprintf("%01.2f", $summary['speed']) . " km/h"; + $speed_display = "@" . sprintf("%01.2f", $summary['speed']) . " km/h"; } else if ($summary['pace'] > 10) { $speed_display = "en " . Utils::formatMs($summary['duration'], true); } @@ -519,7 +537,7 @@ public function getDaySummary($date, $conn) { if ($summary['distance']/1000000 < 5) { $distance_display = sprintf("%4d", $summary['distance']/1000) . " m"; } - $txtSummary = $date . ": " . $distance_display . " " . $speed_display; + $txtSummary = $date . ": " . $activity_abrev . " " . $distance_display . " " . $speed_display; if ($summary['beats'] > 0) { $txtSummary .= " | FCmed: " . round($summary['beats']); } diff --git a/prueba_commit.txt b/prueba_commit.txt new file mode 100644 index 0000000..e69de29 diff --git a/public/goals.php b/public/goals.php index d9783da..b1277e2 100644 --- a/public/goals.php +++ b/public/goals.php @@ -2,6 +2,7 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/global_config.php'; require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/database.php'; require_once $base_path . '/check_access.php'; + ?> @@ -45,10 +46,11 @@ });