diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 7bb18b5ac..f45fa5284 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -481,6 +481,16 @@ function statistics($key = null) { http_response_code(201); echo json_encode(['Today' => $data['todays_qsos'], 'total_qsos' => $data['total_qsos'], 'month_qsos' => $data['month_qsos'], 'year_qsos' => $data['year_qsos']]); + } + + function recent_qsos($key = null) { + header('Content-type: application/json'); + $this->load->model('logbook_model'); + + $data['qsos'] = $this->logbook_model->recent_qsos(null, $key); + + http_response_code(201); + echo json_encode($data['qsos']); } diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 570f5063e..640c0ee13 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -2238,6 +2238,77 @@ function todays_qsos($StationLocationsArray = null, $api_key = null) } } + + /* Return most recent QSOs */ + function recent_qsos($StationLocationsArray = null, $api_key = null) + { + // Get a reference to the CodeIgniter instance + $CI = &get_instance(); + + // Check if 'detailed' parameter is set to 'true' in the URL + $detailed = $this->input->get('detailed') === 'true' ? true : false; + + // Get 'limit' parameter from the URL, default to 10 if it's not a number + $limit = $this->input->get('limit'); + $limit = is_numeric($limit) ? $limit : 10; + + // Throw an error if limit is greater than 100 + if ($limit > 100) { + show_error('The limit cannot be greater than 100.', 400); + return; + } + + // If no station locations array is provided, get it from the logbooks model + if ($StationLocationsArray == null) { + $CI->load->model('logbooks_model'); + + // If an API key is provided, get the logbooks locations array for the user associated with the API key + if ($api_key != null) { + $CI->load->model('api_model'); + if (strpos($this->api_model->access($api_key), 'r') !== false) { + $this->api_model->update_last_used($api_key); + $user_id = $this->api_model->key_userid($api_key); + $active_station_logbook = $CI->logbooks_model->find_active_station_logbook_from_userid($user_id); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($active_station_logbook); + } else { + $logbooks_locations_array = []; + } + } else { + // If no API key is provided, get the logbooks locations array for the current session's active station logbook + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + } + } else { + $logbooks_locations_array = $StationLocationsArray; + } + + // If there are any logbooks locations, get the recent QSOs + if ($logbooks_locations_array) { + // If 'detailed' is true, select all columns, otherwise select specific columns + if ($detailed) { + $this->db->select('*'); + } else { + $this->db->select('COL_CALL, COL_BAND, COL_FREQ, COL_MODE, COL_SUBMODE, COL_NAME, COL_MY_GRIDSQUARE, COL_COUNTRY, COL_DXCC, COL_CONTEST_ID, COL_FREQ_RX, COL_MY_CITY, COL_MY_CNTY, COL_MY_COUNTRY, COL_COMMENT, COL_DISTANCE, COL_NAME, COL_OPERATOR, COL_RST_RCVD, COL_RST_SENT, COL_STATION_CALLSIGN, COL_TIME_OFF, COL_TIME_ON, COL_TX_PWR'); + } + + // Filter by station ID and order by time on, descending + $this->db->where_in('station_id', $logbooks_locations_array); + $this->db->order_by('COL_TIME_ON', 'DESC'); + + // Limit the number of results + $this->db->limit($limit); + + // Execute the query + $query = $this->db->get($this->config->item('table_name')); + + // If there are any results, return them, otherwise return null + if ($query->num_rows() > 0) { + return $query->result(); + } + } else { + return null; + } + } + /* Return QSOs over a period of days */ function map_week_qsos($start, $end) {