private function calculate($state) { global $scratchDirectory, $maxKmFileUploads; $source = "we should never see this value!"; $str = ""; set_time_limit(300); $this->output("<H3>Calculating....</H3>"); date_default_timezone_set($this->preferences->getTimezone()); $start_text = "the beginning"; $end_text = "today"; $activities = []; $timestamp = time(); if (isset($_POST["start_date"]) && $_POST["start_date"] != "") { $start_text = $_POST["start_date"]; } if (isset($_POST["end_date"]) && $_POST["end_date"] != "") { $end_text = $_POST["end_date"]; } if ($state == "calculate_from_strava") { $this->processUploadedGpxFiles($this->strava->getUserId(), $scratchDirectory); $source = "Strava"; $activities = $this->strava->getRides($this->start_date, $this->end_date); if ($this->strava->getError()) { return "<br><span style=\"color:red;\">There was a problem getting data from {$source}.</span><br><em>" . $this->strava->getError() . "</em>"; } $overnight_rides = $this->strava->getOvernightActivities(); if ($this->preferences->getSplitRides() && $overnight_rides) { $str .= $this->askForStravaGpx($overnight_rides, $maxKmFileUploads, "calculate_from_strava", "recalculate your E-Number"); } } else { if ($state == "calculate_from_mcl") { $source = "MyCyclingLog"; $activities = $this->myCyclingLog->getRides($this->start_date, $this->end_date); } else { if ($state == "calculate_from_endo") { $source = "Endomondo"; $activities = $this->endomondo->getRides($this->start_date, $this->end_date); $error = $this->endomondo->getError(); if ($error) { return "There was a problem getting data from {$source}:<br>" . $error; } } else { if ($state == "calculate_from_rwgps") { $source = "RideWithGPS"; $activities = $this->rideWithGps->getRides($this->start_date, $this->end_date); $error = $this->rideWithGps->getError(); if ($error) { $str .= "<br>There was a problem getting data from {$source}:<br>" . $error . "<br>"; } } } } } if (!$this->start_date) { if (sizeof($activities) == 0) { $this->start_date = time(); } else { $days = array_keys($activities); sort($days); $this->start_date = strtotime($days[0]); } } if (!$this->end_date) { $this->end_date = time(); } $days = $this->sumActivities($activities); $str .= "<p>According to {$source}, for the period from {$start_text} to {$end_text}, " . round(($this->end_date - $this->start_date) / self::TWENTY_FOUR_HOURS) . " elapsed days</p>\n"; uasort($days, function ($a, $b) { if ($a == $b) { return 0; } else { return $a > $b ? -1 : 1; } }); $eddington_imperial = $this->calculateEddington($days, $result, self::METRE_TO_MILE); $table_imperial = '<table id="imperial" class="w3-table-all w3-right-align" style="width:60%"><tr><th>Count</th><th>Date </th><th class="w3-right-align">Distance</th></tr>'; for ($i = 1; $i <= $eddington_imperial; $i++) { $day = array_keys($result)[$i - 1]; $actual_distance = $result[$day]; $table_imperial .= "<tr><td> {$i} </td><td> {$day}</td><td class=\"w3-right-align\">{$actual_distance} miles</td></tr>"; } $table_imperial .= "</table>"; $str .= "<br><a href=\"#imperial\">Your imperial Eddington Number</a> is <strong>{$eddington_imperial}</strong>.<br>\n"; if ($end_text == "today") { $goals = $this->nextGoals($eddington_imperial); foreach ($goals as $goal) { $num = $this->numberOfDaysToGoal($goal, $days, self::METRE_TO_MILE); $str .= "You need to do {$num} ride(s) of at least {$goal} to increase it to {$goal}.<br>\n"; } } $eddington_metric = $this->calculateEddington($days, $result, self::METRE_TO_KM); $table_metric = '<table id="metric" class="w3-table-all w3-right-align" style="width:60%"><tr><th>Count</th><th>Date </th><th class="w3-right-align">Distance</th></tr>'; for ($i = 1; $i <= $eddington_metric; $i++) { $date = array_keys($result)[$i - 1]; $distance = $result[$date]; $table_metric .= "<tr><td> {$i} </td><td>{$date}</td><td class=\"w3-right-align\">{$distance} km</td></tr>"; } $table_metric .= "</table>"; $str .= "<br><a href=\"#metric\">Your metric Eddington Number</a> is <strong>{$eddington_metric}</strong><br>\n"; if ($end_text == "today") { $goals = $this->nextGoals($eddington_metric); foreach ($goals as $goal) { $num = $this->numberOfDaysToGoal($goal, $days, self::METRE_TO_KM); $str .= "You need to do {$num} ride(s) of at least {$goal} to increase it to {$goal}.<br>\n"; } } $str .= '<br><a href="#eddington_chart">See a chart of how your Eddington number has grown over the years.</a><br>'; $str .= "<p><em>Run time " . (time() - $timestamp) . " seconds.</em></p>\n"; $str .= $table_imperial; $str .= $table_metric; $imperial_history = $this->eddingtonHistory($days, self::METRE_TO_MILE); $metric_history = $this->eddingtonHistory($days, self::METRE_TO_KM); $str .= $this->buildChart($imperial_history, $metric_history); return $str; }