diff --git a/application/controllers/Dayswithqso.php b/application/controllers/Dayswithqso.php index 2f9e9acd..b6b88a33 100644 --- a/application/controllers/Dayswithqso.php +++ b/application/controllers/Dayswithqso.php @@ -19,6 +19,8 @@ class Dayswithqso extends CI_Controller { $data['result'] = $this->dayswithqso_model->getDaysWithQso(); $data['streaks'] = $this->dayswithqso_model->getLongestStreak(); + $data['currentstreak'] = $this->dayswithqso_model->getCurrentStreak(); + $data['almostcurrentstreak'] = $this->dayswithqso_model->getAlmostCurrentStreak(); $this->load->view('interface_assets/header', $data); $this->load->view('dayswithqso/index'); diff --git a/application/models/Dayswithqso_model.php b/application/models/Dayswithqso_model.php index 4ee0eae3..790f84be 100644 --- a/application/models/Dayswithqso_model.php +++ b/application/models/Dayswithqso_model.php @@ -24,6 +24,82 @@ class Dayswithqso_model extends CI_Model return $query->result(); } + /* + * Function returns current streak + */ + function getCurrentStreak() { + $dates = $this->getDates(); + $dates = array_reverse($dates); + $streak = 1; + $firstrun = true; + + $dateprev = date_create(date('Y-m-d')); + + foreach($dates as $date) { // Loop through the result set + $datecurr = date_create($date->date); + $diff = $dateprev->diff($datecurr)->format("%a"); // Getting date difference between current date and previous date in array + + if ($diff == 0) { + $streaks['highstreak'] = $streak; + $streaks['endstreak'] = $datecurr->format('Y-m-d'); + $streaks['beginstreak'] = $datecurr->format('Y-m-d'); + $firstrun = false; + } + else if ($diff == 1 and !$firstrun) { // If diff = 1, means that we are on a streak + $streaks['highstreak'] = ++$streak; + $streaks['beginstreak'] = $datecurr->sub(new DateInterval('P'.($streak-1).'D'))->format('Y-m-d'); + } else { + break; + } + $dateprev = date_create($date->date); + } + + if (isset($streaks) && is_array($streaks)) { + return $streaks; + } else { + return null; + } + } + + /* + * Function returns streak that ended yesterday, but can be continued if a qso is made today + */ + function getAlmostCurrentStreak() { + $dates = $this->getDates(); + $dates = array_reverse($dates); + $streak = 1; + $firstrun = true; + + $dateprev = date_create(date('Y-m-d')); + + foreach($dates as $date) { // Loop through the result set + $datecurr = date_create($date->date); + $diff = $dateprev->diff($datecurr)->format("%a"); // Getting date difference between current date and previous date in array + + if ($diff == 1 && $firstrun == true) { + $streaks['highstreak'] = $streak++; + $streaks['endstreak'] = $datecurr->format('Y-m-d'); + $streaks['beginstreak'] = $datecurr->format('Y-m-d'); + $firstrun = false; + } + else if ($diff == 1 && $firstrun == false) { + $streaks['highstreak'] = $streak++; + $streaks['beginstreak'] = $datecurr->format('Y-m-d'); + } else { + //$streaks['highstreak'] = $streak; + //$streaks['beginstreak'] = $datecurr->format('Y-m-d'); + break; + } + $dateprev = date_create($date->date); + } + + if (isset($streaks) && is_array($streaks)) { + return $streaks; + } else { + return null; + } + } + /* * Function returns the 10 longest streaks of QSOs based on all QSO dates in the log on active station profile */ diff --git a/application/views/dayswithqso/index.php b/application/views/dayswithqso/index.php index 13c8e9e3..c6da8721 100644 --- a/application/views/dayswithqso/index.php +++ b/application/views/dayswithqso/index.php @@ -70,4 +70,52 @@ echo ''; } ?> + +

Current streak with QSOs in the log

+ '; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + $beginstreak_newdate = strtotime($currentstreak['beginstreak']); + echo ''; + $endstreak_newdate = strtotime($currentstreak['endstreak']); + echo ''; + echo ''; + + echo '
Current Streak (Continues days with QSOs)Begin dateEnd date
' . $currentstreak['highstreak'] . '' . date($custom_date_format, $beginstreak_newdate) . '' . date($custom_date_format, $endstreak_newdate) . '
'; + } + elseif (is_array($almostcurrentstreak)) { + ?> +

If you make a QSO today, you can continue to extend your streak, else your current streak will be broken!

+ '; + + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + + echo ''; + echo ''; + $beginstreak_newdate = strtotime($almostcurrentstreak['beginstreak']); + echo ''; + $endstreak_newdate = strtotime($almostcurrentstreak['endstreak']); + echo ''; + echo ''; + + echo '
Current Streak (Continues days with QSOs)Begin dateEnd date
' . $almostcurrentstreak['highstreak'] . '' . date($custom_date_format, $beginstreak_newdate) . '' . date($custom_date_format, $endstreak_newdate) . '
'; + } + else { + echo ''; + } + ?>