diff --git a/application/models/Dayswithqso_model.php b/application/models/Dayswithqso_model.php index 75c2ac1e..1f6b6aa6 100644 --- a/application/models/Dayswithqso_model.php +++ b/application/models/Dayswithqso_model.php @@ -9,6 +9,10 @@ class Dayswithqso_model extends CI_Model $CI->load->model('logbooks_model'); $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; $sql = "select year(COL_TIME_ON) Year, COUNT(DISTINCT TO_DAYS(COL_TIME_ON)) as Days from " @@ -25,35 +29,38 @@ class Dayswithqso_model extends CI_Model */ 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; + if ($dates) { + $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'] = date_create($streaks['endstreak'])->sub(new DateInterval('P'.($streak-1).'D'))->format('Y-m-d'); + } else { + break; + } + $dateprev = date_create($date->date); } - else if ($diff == 1 and !$firstrun) { // If diff = 1, means that we are on a streak - $streaks['highstreak'] = ++$streak; - $streaks['beginstreak'] = date_create($streaks['endstreak'])->sub(new DateInterval('P'.($streak-1).'D'))->format('Y-m-d'); + + if (isset($streaks) && is_array($streaks)) { + return $streaks; } else { - break; + return null; } - $dateprev = date_create($date->date); - } - - if (isset($streaks) && is_array($streaks)) { - return $streaks; - } else { - return null; } } @@ -62,36 +69,40 @@ class Dayswithqso_model extends CI_Model */ 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) { + if ($dates) { + $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['beginstreak'] = date_create($streaks['endstreak'])->sub(new DateInterval('P'.($streak-2).'D'))->format('Y-m-d'); - } else { - break; + $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'] = date_create($streaks['endstreak'])->sub(new DateInterval('P'.($streak-2).'D'))->format('Y-m-d'); + } else { + break; + } + $dateprev = date_create($date->date); + } + + if (isset($streaks) && is_array($streaks)) { + return $streaks; + } else { + return null; } - $dateprev = date_create($date->date); } - if (isset($streaks) && is_array($streaks)) { - return $streaks; - } else { - return null; - } } /* @@ -103,35 +114,39 @@ class Dayswithqso_model extends CI_Model $dateprev = date_create('1900-01-01'); // init variable with an old date $i = 0; - foreach($dates as $date) { // Loop through the result set - $datecurr = date_create($date->date); - if ($dateprev == date_create('1900-01-01')) { // If first run - $dateprev = $datecurr; - } - else { - $diff = $dateprev->diff($datecurr)->format("%a"); // Getting date difference between current date and previous date in array - if ($diff == 1) { // If diff = 1, means that we are on a streak - $streak++; - $endstreak = $datecurr; // As long as the streak continues, we update the end date - } else { - if ($streak > 1) { - $streaks[$i]['highstreak'] = $streak; - $streaks[$i]['endstreak'] = $endstreak->format('Y-m-d'); - $streaks[$i]['beginstreak'] = $endstreak->sub(new DateInterval('P'.($streak-1).'D'))->format('Y-m-d'); - $i++; - } - $streak = 1; + if ($dates) { + foreach($dates as $date) { // Loop through the result set + $datecurr = date_create($date->date); + if ($dateprev == date_create('1900-01-01')) { // If first run + $dateprev = $datecurr; + } + else { + $diff = $dateprev->diff($datecurr)->format("%a"); // Getting date difference between current date and previous date in array + if ($diff == 1) { // If diff = 1, means that we are on a streak + $streak++; + $endstreak = $datecurr; // As long as the streak continues, we update the end date + } else { + if ($streak > 1) { + $streaks[$i]['highstreak'] = $streak; + $streaks[$i]['endstreak'] = $endstreak->format('Y-m-d'); + $streaks[$i]['beginstreak'] = $endstreak->sub(new DateInterval('P'.($streak-1).'D'))->format('Y-m-d'); + $i++; + } + $streak = 1; + } + $dateprev = date_create($date->date); } - $dateprev = date_create($date->date); } - } + - if (isset($streaks) && is_array($streaks)) { - usort($streaks, array($this,'compareStreak')); // Sort array, highest streak first - $streaks_trimmed = array_slice($streaks, 0,10); // We only want top 10, so we trim the array - return $streaks_trimmed; - } else { - return null; + + if (isset($streaks) && is_array($streaks)) { + usort($streaks, array($this,'compareStreak')); // Sort array, highest streak first + $streaks_trimmed = array_slice($streaks, 0,10); // We only want top 10, so we trim the array + return $streaks_trimmed; + } else { + return null; + } } } @@ -150,6 +165,10 @@ class Dayswithqso_model extends CI_Model $CI->load->model('logbooks_model'); $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + if (!$logbooks_locations_array) { + return null; + } + $location_list = "'".implode("','",$logbooks_locations_array)."'"; $sql = "select distinct cast(col_time_on as date) as date from " diff --git a/assets/js/sections/dayswithqso.js b/assets/js/sections/dayswithqso.js index 23e0a819..27426356 100644 --- a/assets/js/sections/dayswithqso.js +++ b/assets/js/sections/dayswithqso.js @@ -1,46 +1,48 @@ $.ajax({ - url: base_url+'index.php/dayswithqso/get_days', - success: function(data) { - var labels = []; - var dataDxcc = []; - $.each(data, function(){ - labels.push(this.Year); - dataDxcc.push(this.Days); - }); - var ctx = document.getElementById("myChartDiff").getContext('2d'); - var color = ifDarkModeThemeReturn('white', 'grey'); - var myChart = new Chart(ctx, { - type: 'bar', - data: { - labels: labels, - datasets: [{ - label: 'Days with QSOs', - data: dataDxcc, - backgroundColor: 'rgba(54, 162, 235, 0.2)', - borderColor: 'rgba(54, 162, 235, 1)', - borderWidth: 2 - }] - }, - options: { - scales: { - yAxes: [{ - ticks: { - beginAtZero:true, - fontColor: color - } - }], - xAxes: [{ - ticks: { - fontColor: color - } + url: base_url + 'index.php/dayswithqso/get_days', + success: function (data) { + if ($.trim(data)) { + var labels = []; + var dataDxcc = []; + $.each(data, function () { + labels.push(this.Year); + dataDxcc.push(this.Days); + }); + var ctx = document.getElementById("myChartDiff").getContext('2d'); + var color = ifDarkModeThemeReturn('white', 'grey'); + var myChart = new Chart(ctx, { + type: 'bar', + data: { + labels: labels, + datasets: [{ + label: 'Days with QSOs', + data: dataDxcc, + backgroundColor: 'rgba(54, 162, 235, 0.2)', + borderColor: 'rgba(54, 162, 235, 1)', + borderWidth: 2 }] }, - legend: { - labels: { - fontColor: color + options: { + scales: { + yAxes: [{ + ticks: { + beginAtZero: true, + fontColor: color + } + }], + xAxes: [{ + ticks: { + fontColor: color + } + }] + }, + legend: { + labels: { + fontColor: color + } } } - } - }); + }); + } } });