From dbc45f6b27e0772d7f3b12700c98120aac051e6b Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 15 Aug 2020 15:51:05 +0300 Subject: [PATCH] Do not allow appointments being created with duration less than the predefined minimum (#805). --- application/config/constants.php | 2 ++ application/models/Appointments_model.php | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/application/config/constants.php b/application/config/constants.php index 1d57d408..6e42bd86 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -87,5 +87,7 @@ define('CALENDAR_VIEW_TABLE', 'table'); define('AVAILABILITIES_TYPE_FLEXIBLE', 'flexible'); define('AVAILABILITIES_TYPE_FIXED', 'fixed'); +define('EVENT_MINIMUM_DURATION', 5); // Minutes + /* End of file constants.php */ /* Location: ./application/config/constants.php */ diff --git a/application/models/Appointments_model.php b/application/models/Appointments_model.php index bf8d60fe..98f659f2 100644 --- a/application/models/Appointments_model.php +++ b/application/models/Appointments_model.php @@ -63,13 +63,12 @@ class Appointments_Model extends CI_Model { { $this->load->helper('data_validation'); - // If a appointment id is given, check whether the record exists - // in the database. + // If a appointment id is given, check whether the record exists in the database. if (isset($appointment['id'])) { - $num_rows = $this->db->get_where('appointments', - ['id' => $appointment['id']])->num_rows(); - if ($num_rows == 0) + $num_rows = $this->db->get_where('appointments', ['id' => $appointment['id']])->num_rows(); + + if ($num_rows === 0) { throw new Exception('Provided appointment id does not exist in the database.'); } @@ -86,6 +85,15 @@ class Appointments_Model extends CI_Model { throw new Exception('Appointment end datetime is invalid.'); } + // Ensure the appointment lasts longer than the minimum duration (in minutes). + $diff = (strtotime($appointment['end_datetime']) - strtotime($appointment['start_datetime'])) / 60; + + if ($diff < EVENT_MINIMUM_DURATION) + { + throw new Exception('The appointment duration is less than the minimum duration (' + . EVENT_MINIMUM_DURATION . ' minutes).'); + } + // Check if the provider's id is valid. $num_rows = $this->db ->select('*')