From fb69db009ffad66bfc27250c219e7b92f19ab807 Mon Sep 17 00:00:00 2001
From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com>
Date: Thu, 21 Nov 2024 09:44:10 +0100
Subject: [PATCH] [Days with QSOs] Added stats for weekdays
---
application/controllers/Dayswithqso.php | 20 +-
application/models/Dayswithqso_model.php | 50 +++--
application/views/dayswithqso/index.php | 225 +++++++++++++----------
assets/js/sections/dayswithqso.js | 149 ++++++++++-----
4 files changed, 282 insertions(+), 162 deletions(-)
diff --git a/application/controllers/Dayswithqso.php b/application/controllers/Dayswithqso.php
index c82567b55..5d3262cc0 100644
--- a/application/controllers/Dayswithqso.php
+++ b/application/controllers/Dayswithqso.php
@@ -11,16 +11,16 @@ class Dayswithqso extends CI_Controller {
if(!$this->user_model->authorize(2)) { $this->session->set_flashdata('error', __("You're not allowed to do that!")); redirect('dashboard'); }
}
- public function index()
- {
- $this->load->model('dayswithqso_model');
+ public function index() {
+ $this->load->model('dayswithqso_model');
// Render Page
- $data['page_title'] = __("Number of days with QSOs each year");
+ $data['page_title'] = __("Days with QSOs");
$data['result'] = $this->dayswithqso_model->getDaysWithQso();
$data['streaks'] = $this->dayswithqso_model->getLongestStreak();
$data['currentstreak'] = $this->dayswithqso_model->getCurrentStreak();
$data['almostcurrentstreak'] = $this->dayswithqso_model->getAlmostCurrentStreak();
+ $data['daysofweek'] = $this->dayswithqso_model->getDaysOfWeek();
$this->load->view('interface_assets/header', $data);
$this->load->view('dayswithqso/index');
@@ -38,4 +38,14 @@ class Dayswithqso extends CI_Controller {
echo json_encode($data);
}
-}
\ No newline at end of file
+ public function get_weekdays() {
+ //load model
+ $this->load->model('dayswithqso_model');
+
+ // get data
+ $data = $this->dayswithqso_model->getDaysOfWeek();
+ header('Content-Type: application/json');
+ echo json_encode($data);
+ }
+
+}
diff --git a/application/models/Dayswithqso_model.php b/application/models/Dayswithqso_model.php
index cd5850086..2a895d4a6 100644
--- a/application/models/Dayswithqso_model.php
+++ b/application/models/Dayswithqso_model.php
@@ -5,9 +5,8 @@ class Dayswithqso_model extends CI_Model
{
function getDaysWithQso()
{
- $CI =& get_instance();
- $CI->load->model('logbooks_model');
- $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
+ $this->load->model('logbooks_model');
+ $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
if (!$logbooks_locations_array) {
return null;
@@ -34,13 +33,13 @@ class Dayswithqso_model extends CI_Model
$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?? '1970-01-01 00:00:00');
$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');
@@ -55,7 +54,7 @@ class Dayswithqso_model extends CI_Model
}
$dateprev = date_create($date->date ?? '1970-01-01 00:00:00');
}
-
+
if (isset($streaks) && is_array($streaks)) {
return $streaks;
} else {
@@ -74,13 +73,13 @@ class Dayswithqso_model extends CI_Model
$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 ?? '1970-01-01 00:00:00');
$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');
@@ -95,7 +94,7 @@ class Dayswithqso_model extends CI_Model
}
$dateprev = date_create($date->date);
}
-
+
if (isset($streaks) && is_array($streaks)) {
return $streaks;
} else {
@@ -137,7 +136,7 @@ class Dayswithqso_model extends CI_Model
$dateprev = date_create($date->date);
}
}
-
+
if (isset($streaks) && is_array($streaks)) {
@@ -161,9 +160,8 @@ class Dayswithqso_model extends CI_Model
* Returns all distinct dates from db on active profile
*/
function getDates() {
- $CI =& get_instance();
- $CI->load->model('logbooks_model');
- $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
+ $this->load->model('logbooks_model');
+ $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
if (!$logbooks_locations_array) {
return null;
@@ -180,4 +178,26 @@ class Dayswithqso_model extends CI_Model
return $query->result();
}
+ /*
+ * Returns the total number of QSOs made for each day of the week (Monday to Sunday)
+ */
+ function getDaysOfWeek() {
+ $this->load->model('logbooks_model');
+ $logbooks_locations_array = $this->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 DAYNAME(col_time_off) AS weekday, COUNT(*) AS qsos FROM " . $this->config->item('table_name')
+ . " WHERE WEEKDAY(col_time_off) BETWEEN 0 AND 6 AND station_id in (" . $location_list . ")"
+ . " GROUP BY weekday ORDER BY FIELD(weekday, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')";
+
+ $query = $this->db->query($sql);
+
+ return $query->result();
+ }
+
}
diff --git a/application/views/dayswithqso/index.php b/application/views/dayswithqso/index.php
index 0d3ef585c..015457889 100644
--- a/application/views/dayswithqso/index.php
+++ b/application/views/dayswithqso/index.php
@@ -1,125 +1,156 @@
-
-
-
';
+
+
- echo '';
- echo '| ' . __("Year") . ' | ';
+
+
+
+
+
+
';
- foreach ($result as $master) {
- echo '| ' . $master->Year . ' | ';
- }
+ echo '';
+ echo '| ' . __("Year") . ' | ';
- echo '
';
+ foreach ($result as $master) {
+ echo '' . $master->Year . ' | ';
+ }
- echo '';
- echo '| ' . __("Days") . ' | ';
+ echo '
';
- foreach ($result as $master) {
- echo '' . $master->Days . ' | ';
- }
+ echo '';
+ echo '| ' . __("Days") . ' | ';
- echo '
';
+ foreach ($result as $master) {
+ echo '' . $master->Days . ' | ';
+ }
- echo '
';
- }
- ?>
-
-
= __("Longest streak with QSOs in the log"); ?>
-
= __("A maximum of the 10 longest streaks are shown!"); ?>
+ echo '
';
- session->userdata('user_date_format')) {
- // If Logged in and session exists
- $custom_date_format = $this->session->userdata('user_date_format');
- } else {
- // Get Default date format from /config/wavelog.php
- $custom_date_format = $this->config->item('qso_date_format');
- }
- ?>
+ echo '
';
+ }
+ ?>
+
+
- ';
+
+
+
+
+
- echo '';
- echo '| ' . __("Streak (continuous days with QSOs)") . ' | ';
+
+
+
= __("Longest streak with QSOs in the log"); ?>
+
+
+ session->userdata('user_date_format')) {
+ // If Logged in and session exists
+ $custom_date_format = $this->session->userdata('user_date_format');
+ } else {
+ // Get Default date format from /config/wavelog.php
+ $custom_date_format = $this->config->item('qso_date_format');
+ }
+ ?>
+
+
';
+
+ echo '';
+ echo '| ' . __("Streak (continuous days with QSOs)") . ' | ';
+ echo '' . __("Start Date") . ' | ';
+ echo '' . __("End Date") . ' | ';
+ echo '
';
+
+ foreach ($streaks as $streak) {
+ echo '';
+ echo '| ' . $streak['highstreak'] . ' | ';
+ $beginstreak_newdate = strtotime($streak['beginstreak']);
+ echo '' . date($custom_date_format, $beginstreak_newdate) . ' | ';
+ $endstreak_newdate = strtotime($streak['endstreak']);
+ echo '' . date($custom_date_format, $endstreak_newdate) . ' | ';
+ echo '
';
+ }
+
+ echo '
';
+ }
+ else {
+ echo '' . _pgettext("Days with QSOs", "No streak found!") . '
';
+ }
+ ?>
+
+ = __("Current streak with QSOs in the log"); ?>
+ ';
+
+ echo '';
+ echo '| ' . __("Current streak (continuous days with QSOs)") . ' | ';
echo '' . __("Start Date") . ' | ';
echo '' . __("End Date") . ' | ';
echo '
';
- foreach ($streaks as $streak) {
+ echo '';
+ echo '| ' . $currentstreak['highstreak'] . ' | ';
+ $beginstreak_newdate = strtotime($currentstreak['beginstreak']);
+ echo '' . date($custom_date_format, $beginstreak_newdate) . ' | ';
+ $endstreak_newdate = strtotime($currentstreak['endstreak']);
+ echo '' . date($custom_date_format, $endstreak_newdate) . ' | ';
+ echo '
';
+
+ echo '
';
+ }
+ elseif (is_array($almostcurrentstreak)) {
+ ?>
+ = __("If you make a QSO today, you can continue to extend your streak... or else your current streak will be broken!"); ?>
+ ';
+
echo '';
- echo '| ' . $streak['highstreak'] . ' | ';
- $beginstreak_newdate = strtotime($streak['beginstreak']);
+ echo '' . __("Current streak (continuous days with QSOs)") . ' | ';
+ echo '' . __("Start Date") . ' | ';
+ echo '' . __("End Date") . ' | ';
+ echo '
';
+
+ echo '';
+ echo '| ' . $almostcurrentstreak['highstreak'] . ' | ';
+ $beginstreak_newdate = strtotime($almostcurrentstreak['beginstreak']);
echo '' . date($custom_date_format, $beginstreak_newdate) . ' | ';
- $endstreak_newdate = strtotime($streak['endstreak']);
+ $endstreak_newdate = strtotime($almostcurrentstreak['endstreak']);
echo '' . date($custom_date_format, $endstreak_newdate) . ' | ';
echo '
';
+
+ echo '
';
}
-
- echo '
';
- }
- else {
- echo '' . _pgettext("Days with QSOs", "No streak found!") . '
';
- }
- ?>
-
- = __("Current streak with QSOs in the log"); ?>
- ';
-
- echo '';
- echo '| ' . __("Current streak (continuous days with QSOs)") . ' | ';
- echo '' . __("Start Date") . ' | ';
- echo '' . __("End Date") . ' | ';
- echo '
';
-
- echo '';
- echo '| ' . $currentstreak['highstreak'] . ' | ';
- $beginstreak_newdate = strtotime($currentstreak['beginstreak']);
- echo '' . date($custom_date_format, $beginstreak_newdate) . ' | ';
- $endstreak_newdate = strtotime($currentstreak['endstreak']);
- echo '' . date($custom_date_format, $endstreak_newdate) . ' | ';
- echo '
';
-
- echo '
';
- }
- elseif (is_array($almostcurrentstreak)) {
- ?>
- = __("If you make a QSO today, you can continue to extend your streak... or else your current streak will be broken!"); ?>
- ';
-
- echo '';
- echo '| ' . __("Current streak (continuous days with QSOs)") . ' | ';
- echo '' . __("Start Date") . ' | ';
- echo '' . __("End Date") . ' | ';
- echo '
';
-
- echo '';
- echo '| ' . $almostcurrentstreak['highstreak'] . ' | ';
- $beginstreak_newdate = strtotime($almostcurrentstreak['beginstreak']);
- echo '' . date($custom_date_format, $beginstreak_newdate) . ' | ';
- $endstreak_newdate = strtotime($almostcurrentstreak['endstreak']);
- echo '' . date($custom_date_format, $endstreak_newdate) . ' | ';
- echo '
';
-
- echo '
';
- }
- else {
- echo '' . __("No current streak found!") . '
';
- }
- ?>
+ else {
+ echo '' . __("No current streak found!") . '
';
+ }
+ ?>
+
+
diff --git a/assets/js/sections/dayswithqso.js b/assets/js/sections/dayswithqso.js
index e825a0697..c99d8849d 100644
--- a/assets/js/sections/dayswithqso.js
+++ b/assets/js/sections/dayswithqso.js
@@ -1,51 +1,110 @@
-$.ajax({
- 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: lang_days_with_qso_short,
- data: dataDxcc,
- backgroundColor: 'rgba(54, 162, 235, 0.2)',
- borderColor: 'rgba(54, 162, 235, 1)',
- borderWidth: 2,
- color: color
- }]
- },
- options: {
- scales: {
- y: {
- ticks: {
- beginAtZero: true,
- color: color
+daysPerYear();
+weekDays();
+
+function daysPerYear() {
+ $.ajax({
+ 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: lang_days_with_qso_short,
+ data: dataDxcc,
+ backgroundColor: 'rgba(54, 162, 235, 0.2)',
+ borderColor: 'rgba(54, 162, 235, 1)',
+ borderWidth: 2,
+ color: color
+ }]
+ },
+ options: {
+ scales: {
+ y: {
+ ticks: {
+ beginAtZero: true,
+ color: color
+ }
+ },
+ x: {
+ ticks: {
+ color: color
+ }
}
},
- x: {
- ticks: {
- color: color
- }
- }
- },
- plugins: {
- legend: {
- labels: {
- color: color
+ plugins: {
+ legend: {
+ labels: {
+ color: color
+ }
}
}
}
- }
- });
+ });
+ }
}
- }
-});
+ });
+}
+
+function weekDays() {
+ $.ajax({
+ url: base_url + 'index.php/dayswithqso/get_weekdays',
+ success: function (data) {
+ if ($.trim(data)) {
+ var labels = [];
+ var dataDays = [];
+ $.each(data, function () {
+ labels.push(this.weekday);
+ dataDays.push(this.qsos);
+ });
+ var ctx = document.getElementById("weekdaysChart").getContext('2d');
+ var color = ifDarkModeThemeReturn('white', 'grey');
+ var myChart = new Chart(ctx, {
+ type: 'bar',
+ data: {
+ labels: labels,
+ datasets: [{
+ label: lang_qsos_this_weekday,
+ data: dataDays,
+ backgroundColor: 'rgba(54, 162, 235, 0.2)',
+ borderColor: 'rgba(54, 162, 235, 1)',
+ borderWidth: 2,
+ color: color
+ }]
+ },
+ options: {
+ scales: {
+ y: {
+ ticks: {
+ beginAtZero: true,
+ color: color
+ }
+ },
+ x: {
+ ticks: {
+ color: color
+ }
+ }
+ },
+ plugins: {
+ legend: {
+ labels: {
+ color: color
+ }
+ }
+ }
+ }
+ });
+ }
+ }
+ });
+}