diff --git a/application/config/migration.php b/application/config/migration.php index 595bcece..7c7fc559 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 116; +$config['migration_version'] = 117; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Contesting.php b/application/controllers/Contesting.php index 07103a83..e32a87a5 100644 --- a/application/controllers/Contesting.php +++ b/application/controllers/Contesting.php @@ -42,17 +42,39 @@ class Contesting extends CI_Controller { } public function getSessionQsos() { - //load model $this->load->model('Contesting_model'); $qso = $this->input->post('qso'); - // get QSOs to fill the table - $data = $this->Contesting_model->getSessionQsos($qso); + header('Content-Type: application/json'); + echo json_encode($this->Contesting_model->getSessionQsos($qso)); + } + + public function getSession() { + $this->load->model('Contesting_model'); + + header('Content-Type: application/json'); + echo json_encode($this->Contesting_model->getSession()); + } + + public function deleteSession() { + $this->load->model('Contesting_model'); + + $qso = $this->input->post('qso'); + + $data = $this->Contesting_model->deleteSession($qso); return json_encode($data); } + public function setSession() { + $this->load->model('Contesting_model'); + + $this->Contesting_model->setSession(); + + return json_encode("ok"); + } + public function create() { $this->load->model('Contesting_model'); $this->load->library('form_validation'); @@ -161,10 +183,10 @@ class Contesting extends CI_Controller { $band = $this->input->post('band'); $mode = $this->input->post('mode'); $contest = $this->input->post('contest'); - $qso = $this->input->post('qso'); $this->load->model('Contesting_model'); - $result = $this->Contesting_model->checkIfWorkedBefore($call, $band, $mode, $contest, $qso); + + $result = $this->Contesting_model->checkIfWorkedBefore($call, $band, $mode, $contest); header('Content-Type: application/json'); if ($result->num_rows()) { diff --git a/application/migrations/117_add_contest_session_table.php b/application/migrations/117_add_contest_session_table.php new file mode 100644 index 00000000..bdf4b385 --- /dev/null +++ b/application/migrations/117_add_contest_session_table.php @@ -0,0 +1,69 @@ +db->table_exists('contest_session')) { + $this->dbforge->add_field(array( + 'id' => array( + 'type' => 'INT', + 'constraint' => 20, + 'unsigned' => TRUE, + 'auto_increment' => TRUE, + 'unique' => TRUE + ), + 'contestid' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'exchangetype' => array( + 'type' => 'VARCHAR', + 'constraint' => 20, + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'exchangesent' => array( + 'type' => 'VARCHAR', + 'constraint' => 20, + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'serialsent' => array( + 'type' => 'VARCHAR', + 'constraint' => 20, + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'copytodok' => array( + 'type' => 'bigint', + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'qso' => array( + 'type' => 'VARCHAR', + 'constraint' => 100, + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + 'station_id' => array( + 'type' => 'bigint', + 'unsigned' => TRUE, + 'auto_increment' => FALSE + ), + )); + + $this->dbforge->add_key('id', TRUE); + + $this->dbforge->create_table('contest_session'); + } + } + + public function down() + { + $this->dbforge->drop_table('contest_session'); + } +} \ No newline at end of file diff --git a/application/models/Contesting_model.php b/application/models/Contesting_model.php index 79918db1..35e9eb03 100644 --- a/application/models/Contesting_model.php +++ b/application/models/Contesting_model.php @@ -26,8 +26,78 @@ class Contesting_model extends CI_Model { " ORDER BY COL_PRIMARY_KEY ASC"; $data = $this->db->query($sql); - header('Content-Type: application/json'); - echo json_encode($data->result()); + return $data->result(); + } + + function getSession() { + $CI =& get_instance(); + $CI->load->model('Stations'); + $station_id = $CI->Stations->find_active(); + + $sql = "SELECT * from contest_session where station_id = " . $station_id; + + $data = $this->db->query($sql); + return $data->row(); + } + + + + function deleteSession() { + $CI =& get_instance(); + $CI->load->model('Stations'); + $station_id = $CI->Stations->find_active(); + + $sql = "delete from contest_session where station_id = " . $station_id; + + $this->db->query($sql); + return; + } + + function setSession() { + $CI =& get_instance(); + $CI->load->model('Stations'); + $station_id = $CI->Stations->find_active(); + + $qso = ""; + + if ($this->input->post('callsign')) { + $qso = xss_clean($this->input->post('start_date', true)) . ' ' . xss_clean($this->input->post('start_time', true)) . ',' . xss_clean($this->input->post('callsign', true)) . ',' . xss_clean($this->input->post('contestname', true)); + } + + $data = array( + 'contestid' => xss_clean($this->input->post('contestname', true)), + 'exchangetype' => xss_clean($this->input->post('exchangetype', true)), + 'exchangesent' => xss_clean($this->input->post('exch_sent', true)), + 'serialsent' => xss_clean($this->input->post('exch_serial_s', true)), + 'copytodok' => xss_clean($this->input->post('copyexchangetodok', true)), + 'qso' => $qso, + 'station_id' => $station_id, + ); + + $sql = "SELECT * from contest_session where station_id = " . $station_id; + + $querydata = $this->db->query($sql); + + if ($querydata->num_rows() == 0) { + $this->db->insert('contest_session', $data); + return; + } + + $result = $querydata->row(); + + if ($result->qso != "") { + $data['qso'] = $result->qso; + } + + $this->updateSession($data, $station_id); + + return; + } + + function updateSession($data, $station_id) { + $this->db->where('station_id', $station_id); + + $this->db->update('contest_session', $data); } function getActivecontests() { @@ -137,27 +207,32 @@ class Contesting_model extends CI_Model { return true; } - function checkIfWorkedBefore($call, $band, $mode, $contest, $qso) { + function checkIfWorkedBefore($call, $band, $mode, $contest) { $CI =& get_instance(); $CI->load->model('Stations'); $station_id = $CI->Stations->find_active(); - $qsoarray = explode(',', $qso); - - $date = DateTime::createFromFormat('d-m-Y H:i:s', $qsoarray[0]); - $date = $date->format('Y-m-d H:i:s'); - - $this->db->where('STATION_ID', $station_id); - $this->db->where('COL_CALL', xss_clean($call)); - $this->db->where("COL_BAND", xss_clean($band)); - $this->db->where("COL_CONTEST_ID", xss_clean($contest)); - $this->db->where("COL_TIME_ON >=", $date); - $this->db->group_start(); - $this->db->where("COL_MODE", xss_clean($mode)); - $this->db->or_where("COL_SUBMODE", xss_clean($mode)); - $this->db->group_end(); - $query = $this->db->get($this->config->item('table_name')); - - return $query; + $contest_session = $this->getSession(); + + if ($contest_session) { + $qsoarray = explode(',', $contest_session->qso); + + $date = DateTime::createFromFormat('d-m-Y H:i:s', $qsoarray[0]); + $date = $date->format('Y-m-d H:i:s'); + + $this->db->where('STATION_ID', $station_id); + $this->db->where('COL_CALL', xss_clean($call)); + $this->db->where("COL_BAND", xss_clean($band)); + $this->db->where("COL_CONTEST_ID", xss_clean($contest)); + $this->db->where("COL_TIME_ON >=", $date); + $this->db->group_start(); + $this->db->where("COL_MODE", xss_clean($mode)); + $this->db->or_where("COL_SUBMODE", xss_clean($mode)); + $this->db->group_end(); + $query = $this->db->get($this->config->item('table_name')); + + return $query; + } + return; } } diff --git a/assets/js/sections/contesting.js b/assets/js/sections/contesting.js index 1df60607..4d569ece 100644 --- a/assets/js/sections/contesting.js +++ b/assets/js/sections/contesting.js @@ -2,11 +2,11 @@ $("#callsign").focus(); $(document).ready(function () { - restoreContestSession(); + getSession().done(restoreContestSession); setRst($("#mode").val()); }); -// This erases the contest logging session which is stored in localStorage +// Resets the logging form and deletes session from database function reset_contest_session() { $('#name').val(""); $('.callsign-suggestions').text(""); @@ -18,7 +18,6 @@ function reset_contest_session() { $('#exch_sent').val(""); $('#exch_rcvd').val(""); $("#exch_gridsquare_r").val(""); - $("#exch_gridsquare_s").val(""); $("#callsign").focus(); setRst($("#mode").val()); @@ -28,28 +27,42 @@ function reset_contest_session() { $(".contest_qso_table_contents").empty(); $('#copyexchangetodok').prop('checked', false); - localStorage.removeItem("contestid"); - localStorage.removeItem("exchangetype"); - localStorage.removeItem("qso"); - localStorage.removeItem("exchangereceived"); - localStorage.removeItem("exchangesent"); - localStorage.removeItem("serialreceived"); - localStorage.removeItem("serialsent"); - localStorage.removeItem("gridsquarereceived"); - localStorage.removeItem("gridsquaresent"); - localStorage.removeItem("copytodok"); + $.ajax({ + url: base_url + 'index.php/contesting/deleteSession', + type: 'post', + success: function (data) { + + } + }); } // Storing the contestid in contest session $('#contestname').change(function () { - localStorage.setItem("contestid", $("#contestname").val()); + var formdata = new FormData(document.getElementById("qso_input")); + setSession(formdata); }); // Storing the exchange type in contest session $('#exchangetype').change(function () { - localStorage.setItem("exchangetype", $('#exchangetype').val()); + var exchangetype = $("#exchangetype").val(); + var formdata = new FormData(document.getElementById("qso_input")); + setSession(formdata); + setExchangetype(exchangetype); }); +function setSession(formdata) { + $.ajax({ + url: base_url + 'index.php/contesting/setSession', + type: 'post', + data: formdata, + processData: false, + contentType: false, + success: function (data) { + + } + }); +} + // realtime clock if ( ! manual ) { $(function ($) { @@ -212,23 +225,25 @@ $("#callsign").keyup(function () { }); function checkIfWorkedBefore() { - $('#callsign_info').text(""); - $.ajax({ - url: base_url + 'index.php/contesting/checkIfWorkedBefore', - type: 'post', - data: { - 'call': $("#callsign").val(), - 'mode': $("#mode").val(), - 'band': $("#band").val(), - 'contest': $("#contestname").val(), - 'qso': localStorage.getItem("qso") - }, - success: function (result) { - if (result.message == 'Worked before') { - $('#callsign_info').text("Worked before!"); + var call = $("#callsign").val(); + if (call.length >= 3) { + $('#callsign_info').text(""); + $.ajax({ + url: base_url + 'index.php/contesting/checkIfWorkedBefore', + type: 'post', + data: { + 'call': call, + 'mode': $("#mode").val(), + 'band': $("#band").val(), + 'contest': $("#contestname").val() + }, + success: function (result) { + if (result.message == 'Worked before') { + $('#callsign_info').text("Worked before!"); + } } - } - }); + }); + } } function reset_log_fields() { @@ -295,12 +310,16 @@ $('#band').change(function () { checkIfWorkedBefore(); }); -$('#exchangetype').change(function () { - var exchangetype = $("#exchangetype").val(); - setExchangetype(exchangetype); -}); +function setSerial(data) { + var serialsent = 1; + if (data.serialsent != "") { + serialsent = data.serialsent; + } + $("#exch_serial_s").val(serialsent); +} function setExchangetype(exchangetype) { + getSession().done(setSerial); // Perhaps a better approach is to hide everything, then just enable the things you need $(".exchanger").hide(); $(".exchanges").hide(); @@ -310,29 +329,21 @@ function setExchangetype(exchangetype) { $(".gridsquares").hide(); $("#exch_serial_s").val(""); - var serialsent = localStorage.getItem("serialsent"); - if (serialsent == null) { - serialsent = 1; - } - if (exchangetype == 'Exchange') { $(".exchanger").show(); $(".exchanges").show(); } else if (exchangetype == 'Serial') { - $("#exch_serial_s").val(serialsent); $(".serials").show(); $(".serialr").show(); } else if (exchangetype == 'Serialexchange') { - $("#exch_serial_s").val(serialsent); $(".exchanger").show(); $(".exchanges").show(); $(".serials").show(); $(".serialr").show(); } else if (exchangetype == 'Serialgridsquare') { - $("#exch_serial_s").val(serialsent); $(".serials").show(); $(".serialr").show(); $(".gridsquarer").show(); @@ -428,10 +439,7 @@ function logQso() { contentType: false, enctype: 'multipart/form-data', success: function (html) { - if (localStorage.getItem("qso") == null) { - localStorage.setItem("qso", $("#start_date").val() + ' ' + $("#start_time").val() + ',' + $("#callsign").val().toUpperCase() + ',' + $("#contestname").val()); - } - + setSession(formdata); $('#name').val(""); $('#callsign').val(""); @@ -445,17 +453,6 @@ function logQso() { } $("#callsign").focus(); - // Store contest session - localStorage.setItem("contestid", $("#contestname").val()); - localStorage.setItem("exchangetype", $("#exchangetype").val()); - localStorage.setItem("exchangereceived", $("#exch_rcvd").val().toUpperCase()); - localStorage.setItem("exchangesent", $("#exch_sent").val().toUpperCase()); - localStorage.setItem("serialreceived", $("#exch_serial_r").val()); - localStorage.setItem("serialsent", $("#exch_serial_s").val()); - localStorage.setItem("gridsquarereceived", $("#exch_gridsquare_r").val()); - localStorage.setItem("gridsquaresent", $("#exch_gridsquare_s").val()); - localStorage.setItem("copytodok", $('#copyexchangetodok').is(":checked")); - var qTable = $('.qsotable').DataTable(); qTable.search('').draw(); } @@ -463,98 +460,80 @@ function logQso() { } } -// We are restoring the settings in the contest logging form here -function restoreContestSession() { - var dokcopy = localStorage.getItem("copytodok"); - if (dokcopy != null) { - $('#copyexchangetodok').prop('checked', true); - } - - var contestname = localStorage.getItem("contestid"); - if (contestname != null) { - $("#contestname").val(contestname); - } - - var exchangetype = localStorage.getItem("exchangetype"); - if (exchangetype != null) { - $("#exchangetype").val(exchangetype); - setExchangetype(exchangetype); - } - - var exchangereceived = localStorage.getItem("exchangereceived"); - if (exchangereceived != null) { - $("#exch_rcvd").val(exchangereceived); - } - - var exchangesent = localStorage.getItem("exchangesent"); - if (exchangesent != null) { - $("#exch_sent").val(exchangesent); - } - - var serialreceived = localStorage.getItem("serialreceived"); - if (serialreceived != null) { - $("#exch_serial_r").val(serialreceived); - } - - var serialsent = localStorage.getItem("serialsent"); - if (serialsent != null) { - $("#exch_serial_s").val(serialsent); - } - - var gridsquarereceived = localStorage.getItem("gridsquarereceived"); - if (gridsquarereceived != null) { - $("#exch_gridsquare_r").val(gridsquarereceived); - } - - var gridsquaresent = localStorage.getItem("gridsquaresent"); - if (gridsquaresent != null) { - $("#exch_gridsquare_s").val(gridsquaresent); - } - - if (localStorage.getItem("qso") != null) { - var qsodata = localStorage.getItem("qso"); - $.ajax({ - url: base_url + 'index.php/contesting/getSessionQsos', - type: 'post', - data: { 'qso': qsodata, }, - success: function (html) { - var mode = ''; - - $.each(html, function () { - if (this.col_submode == null || this.col_submode == '') { - mode = this.col_mode; - } else { - mode = this.col_submode; - } - - $(".qsotable tbody").prepend('' + - '' + this.col_time_on + '' + - '' + this.col_call + '' + - '' + this.col_band + '' + - '' + mode + '' + - '' + this.col_rst_sent + '' + - '' + this.col_rst_rcvd + '' + - '' + this.col_stx_string + '' + - '' + this.col_srx_string + '' + - '' + this.col_stx + '' + - '' + this.col_srx + '' + - '' + this.col_gridsquare + '' + - '' + this.col_vucc_grids + '' + - ''); - }); - if (!$.fn.DataTable.isDataTable('.qsotable')) { - $('.qsotable').DataTable({ - "stateSave": true, - "pageLength": 25, - responsive: false, - "scrollY": "400px", - "scrollCollapse": true, - "paging": false, - "scrollX": true, - "order": [[0, "desc"]] +function getSession() { + return $.ajax({ + url: base_url + 'index.php/contesting/getSession', + type: 'post', + }); +} + +function restoreContestSession(data) { + if (data) { + if (data.copytodok == "1") { + $('#copyexchangetodok').prop('checked', true); + } + + if (data.contestid != "") { + $("#contestname").val(data.contestid); + } + + if (data.exchangetype != "") { + $("#exchangetype").val(data.exchangetype); + setExchangetype(data.exchangetype); + } + + if (data.exchangesent != "") { + $("#exch_sent").val(data.exchangesent); + } + + if (data.serialsent != "") { + $("#exch_serial_s").val(data.serialsent); + } + + if (data.qso != "") { + $.ajax({ + url: base_url + 'index.php/contesting/getSessionQsos', + type: 'post', + data: { 'qso': data.qso, }, + success: function (html) { + var mode = ''; + + $.each(html, function () { + if (this.col_submode == null || this.col_submode == '') { + mode = this.col_mode; + } else { + mode = this.col_submode; + } + + $(".qsotable tbody").prepend('' + + '' + this.col_time_on + '' + + '' + this.col_call + '' + + '' + this.col_band + '' + + '' + mode + '' + + '' + this.col_rst_sent + '' + + '' + this.col_rst_rcvd + '' + + '' + this.col_stx_string + '' + + '' + this.col_srx_string + '' + + '' + this.col_stx + '' + + '' + this.col_srx + '' + + '' + this.col_gridsquare + '' + + '' + this.col_vucc_grids + '' + + ''); }); + if (!$.fn.DataTable.isDataTable('.qsotable')) { + $('.qsotable').DataTable({ + "stateSave": true, + "pageLength": 25, + responsive: false, + "scrollY": "400px", + "scrollCollapse": true, + "paging": false, + "scrollX": true, + "order": [[0, "desc"]] + }); + } } - } - }); + }); + } } }