Files
wavelog/application/models/Cq.php
Andreas Kristiansen 71b14a60b6 Consolidate queries
2026-02-05 08:19:41 +01:00

468 lines
14 KiB
PHP

<?php
class CQ extends CI_Model{
function __construct() {
$this->load->library('Genfunctions');
}
function get_cq_array($bands, $postdata, $location_list) {
$cqZ = array(); // Used for keeping track of which states that are not worked
for ($i = 1; $i <= 40; $i++) {
$cqZ[$i]['count'] = 0; // Inits each cq zone's count
}
$qsl = $this->genfunctions->gen_qsl_from_postdata($postdata);
// Initialize all bands to dash
foreach ($bands as $band) {
for ($i = 1; $i <= 40; $i++) {
$bandCq[$i][$band] = '-'; // Sets all to dash to indicate no result
}
}
// Get all worked zones for all bands in ONE query
if ($postdata['worked'] != NULL) {
$cqBand = $this->getCQWorkedAllBands($location_list, $bands, $postdata);
foreach ($cqBand as $line) {
$band = $line->col_band;
$bandCq[$line->col_cqz][$band] = '<div class="bg-danger awardsBgWarning"><a href=\'javascript:displayContacts("' . str_replace("&", "%26", $line->col_cqz) . '","' . $band . '","All", "All","'. $postdata['mode'] . '","CQZone","")\'>W</a></div>';
$cqZ[$line->col_cqz]['count']++;
}
}
// Get all confirmed zones for all bands in ONE query
if ($postdata['confirmed'] != NULL) {
$cqBand = $this->getCQConfirmedAllBands($location_list, $bands, $postdata);
foreach ($cqBand as $line) {
$band = $line->col_band;
$bandCq[$line->col_cqz][$band] = '<div class="bg-success awardsBgSuccess"><a href=\'javascript:displayContacts("' . str_replace("&", "%26", $line->col_cqz) . '","' . $band . '","All", "All","'. $postdata['mode'] . '","CQZone","'.$qsl.'")\'>C</a></div>';
$cqZ[$line->col_cqz]['count']++;
}
}
// We want to remove the worked zones in the list, since we do not want to display them
if ($postdata['worked'] == NULL) {
// Use optimized query for all bands at once
$cqBand = $this->getCQWorkedAllBands($location_list, $bands, $postdata);
foreach ($cqBand as $line) {
unset($bandCq[$line->col_cqz]);
}
}
// We want to remove the confirmed zones in the list, since we do not want to display them
if ($postdata['confirmed'] == NULL) {
// Use optimized query for all bands at once
$cqBand = $this->getCQConfirmedAllBands($location_list, $bands, $postdata);
foreach ($cqBand as $line) {
unset($bandCq[$line->col_cqz]);
}
}
if ($postdata['notworked'] == NULL) {
for ($i = 1; $i <= 40; $i++) {
if ($cqZ[$i]['count'] == 0) {
unset($bandCq[$i]);
};
}
}
if (isset($bandCq)) {
return $bandCq;
} else {
return 0;
}
}
/*
* Function returns all worked, but not confirmed states
* $postdata contains data from the form, in this case Lotw or QSL are used
*/
function getCQWorked($location_list, $band, $postdata) {
$bindings=[];
$sql = "SELECT distinct col_cqz FROM " . $this->config->item('table_name') . " thcv
where station_id in (" . $location_list . ") and col_cqz <= 40 and col_cqz <> ''";
if ($postdata['mode'] != 'All') {
$sql .= " and (col_mode = ? or col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " and col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " and col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addBandToQuery($band,$bindings);
$sql .= " and not exists (select 1 from " . $this->config->item('table_name') .
" where station_id in (" . $location_list .
") and col_cqz = thcv.col_cqz and col_cqz <> '' ";
if ($postdata['mode'] != 'All') {
$sql .= " and (col_mode = ? or col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " and col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " and col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addBandToQuery($band,$bindings);
$sql .= $this->genfunctions->addQslToQuery($postdata);
$sql .= ")";
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Function returns all confirmed states on given band and on LoTW or QSL
* $postdata contains data from the form, in this case Lotw or QSL are used
*/
function getCQConfirmed($location_list, $band, $postdata) {
$bindings=[];
$sql = "SELECT distinct col_cqz FROM " . $this->config->item('table_name') . " thcv
where station_id in (" . $location_list . ") and col_cqz <= 40 and col_cqz <> ''";
if ($postdata['mode'] != 'All') {
$sql .= " and (col_mode = ? or col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " and col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " and col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addBandToQuery($band,$bindings);
$sql .= $this->genfunctions->addQslToQuery($postdata);
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Function returns all worked (but not confirmed) states for ALL bands in one query
* Returns both col_cqz and col_band to avoid N+1 queries
*/
function getCQWorkedAllBands($location_list, $bands, $postdata) {
$bindings=[];
$sql = "SELECT DISTINCT col_cqz, col_band FROM " . $this->config->item('table_name') . " thcv
WHERE station_id IN (" . $location_list . ")
AND col_cqz <= 40 AND col_cqz <> ''
AND col_band IN ('" . implode("','", $bands) . "')";
if ($postdata['mode'] != 'All') {
$sql .= " AND (col_mode = ? OR col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " AND col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " AND col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= " AND col_prop_mode != 'SAT'";
// Optimized NOT IN subquery instead of NOT EXISTS
/*$sql .= " AND col_cqz NOT IN (
SELECT col_cqz FROM " . $this->config->item('table_name') . "
WHERE station_id IN (" . $location_list . ")
AND col_cqz <> ''
AND col_band IN ('" . implode("','", $bands) . "')
AND col_prop_mode != 'SAT'";
if ($postdata['mode'] != 'All') {
$sql .= " AND (col_mode = ? OR col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " AND col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " AND col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addQslToQuery($postdata);
$sql .= ")";*/
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Function returns all confirmed states for ALL bands in one query
* Returns both col_cqz and col_band to avoid N+1 queries
*/
function getCQConfirmedAllBands($location_list, $bands, $postdata) {
$bindings=[];
$sql = "SELECT DISTINCT col_cqz, col_band FROM " . $this->config->item('table_name') . " thcv
WHERE station_id IN (" . $location_list . ")
AND col_cqz <= 40 AND col_cqz <> ''
AND col_band IN ('" . implode("','", $bands) . "')";
if ($postdata['mode'] != 'All') {
$sql .= " AND (col_mode = ? OR col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " AND col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " AND col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= " AND col_prop_mode != 'SAT'";
$sql .= $this->genfunctions->addQslToQuery($postdata);
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Function gets worked and confirmed summary on each band on the active stationprofile
* Optimized to use just 2 queries instead of N queries per band
*/
function get_cq_summary($bands, $postdata, $location_list) {
$bandslots = $this->bands->get_worked_bands('cq');
foreach ($bandslots as $band) {
$cqSummary['worked'][$band] = '-';
$cqSummary['confirmed'][$band] = '-';
}
// Get all worked counts in ONE query
$workedAll = $this->getSummaryByBandAllBands($bands, $postdata, $location_list, $bandslots);
foreach ($workedAll as $row) {
$cqSummary['worked'][$row->col_band] = $row->count;
}
// Get all confirmed counts in ONE query
$confirmedAll = $this->getSummaryByBandConfirmedAllBands($bands, $postdata, $location_list, $bandslots);
foreach ($confirmedAll as $row) {
$cqSummary['confirmed'][$row->col_band] = $row->count;
}
$workedTotal = $this->getSummaryByBand($postdata['band'], $postdata, $location_list, $bandslots);
$confirmedTotal = $this->getSummaryByBandConfirmed($postdata['band'], $postdata, $location_list, $bandslots);
$cqSummary['worked']['Total'] = $workedTotal[0]->count;
$cqSummary['confirmed']['Total'] = $confirmedTotal[0]->count;
return $cqSummary;
}
function getSummaryByBand($band, $postdata, $location_list, $bandslots) {
$bindings=[];
$sql = "SELECT count(distinct thcv.col_cqz) as count FROM " . $this->config->item('table_name') . " thcv";
$sql .= " where station_id in (" . $location_list . ') and col_cqz <= 40 and col_cqz > 0';
if ($band == 'SAT') {
$sql .= " and thcv.col_prop_mode = ?";
$bindings[]=$band;
} else if ($band == 'All') {
$this->load->model('bands');
// $bandslots = $this->bands->get_worked_bands('cq');
$bandslots_list = "'".implode("','",$bandslots)."'";
$sql .= " and thcv.col_band in (" . $bandslots_list . ")" .
" and thcv.col_prop_mode !='SAT'";
} else {
$sql .= " and thcv.col_prop_mode !='SAT'";
$sql .= " and thcv.col_band = ?";
$bindings[]=$band;
}
if ($postdata['mode'] != 'All') {
$sql .= " and (col_mode = ? or col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " and col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " and col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$query = $this->db->query($sql,$bindings);
return $query->result();
}
function getSummaryByBandConfirmed($band, $postdata, $location_list, $bandslots){
$bindings=[];
$sql = "SELECT count(distinct thcv.col_cqz) as count FROM " . $this->config->item('table_name') . " thcv";
$sql .= " where station_id in (" . $location_list . ') and col_cqz <= 40 and col_cqz > 0';
if ($band == 'SAT') {
$sql .= " and thcv.col_prop_mode = ?";
$bindings[]=$band;
} else if ($band == 'All') {
$this->load->model('bands');
// $bandslots = $this->bands->get_worked_bands('cq');
$bandslots_list = "'".implode("','",$bandslots)."'";
$sql .= " and thcv.col_band in (" . $bandslots_list . ")" .
" and thcv.col_prop_mode !='SAT'";
} else {
$sql .= " and thcv.col_prop_mode !='SAT'";
$sql .= " and thcv.col_band = ?";
$bindings[]=$band;
}
if ($postdata['mode'] != 'All') {
$sql .= " and (col_mode = ? or col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " and col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " and col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addQslToQuery($postdata);
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Gets worked CQ zone counts for ALL bands in one query using GROUP BY
* Returns col_band and count for each band
*/
function getSummaryByBandAllBands($bands, $postdata, $location_list, $bandslots) {
$bindings=[];
$sql = "SELECT col_band, COUNT(DISTINCT thcv.col_cqz) as count FROM " . $this->config->item('table_name') . " thcv";
$sql .= " WHERE station_id IN (" . $location_list . ') AND col_cqz <= 40 AND col_cqz > 0';
$sql .= " AND col_band IN ('" . implode("','", $bands) . "')";
$sql .= " AND col_prop_mode != 'SAT'";
if ($postdata['mode'] != 'All') {
$sql .= " AND (col_mode = ? OR col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " AND col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " AND col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= " GROUP BY col_band";
$query = $this->db->query($sql,$bindings);
return $query->result();
}
/*
* Gets confirmed CQ zone counts for ALL bands in one query using GROUP BY
* Returns col_band and count for each band
*/
function getSummaryByBandConfirmedAllBands($bands, $postdata, $location_list, $bandslots) {
$bindings=[];
$sql = "SELECT col_band, COUNT(DISTINCT thcv.col_cqz) as count FROM " . $this->config->item('table_name') . " thcv";
$sql .= " WHERE station_id IN (" . $location_list . ') AND col_cqz <= 40 AND col_cqz > 0';
$sql .= " AND col_band IN ('" . implode("','", $bands) . "')";
$sql .= " AND col_prop_mode != 'SAT'";
if ($postdata['mode'] != 'All') {
$sql .= " AND (col_mode = ? OR col_submode = ?)";
$bindings[]=$postdata['mode'];
$bindings[]=$postdata['mode'];
}
if ($postdata['datefrom'] != NULL) {
$sql .= " AND col_time_on >= ?";
$bindings[]=$postdata['datefrom'] . ' 00:00:00';
}
if ($postdata['dateto'] != NULL) {
$sql .= " AND col_time_on <= ?";
$bindings[]=$postdata['dateto'] . ' 23:59:59';
}
$sql .= $this->genfunctions->addQslToQuery($postdata);
$sql .= " GROUP BY col_band";
$query = $this->db->query($sql,$bindings);
return $query->result();
}
}