diff --git a/js/ticket_time_tracking.js b/js/ticket_time_tracking.js index ee682a08..95a24b06 100644 --- a/js/ticket_time_tracking.js +++ b/js/ticket_time_tracking.js @@ -1,146 +1,136 @@ -// Description: This file contains the javascript for the ticket time tracking feature +// Description: This file contains the JavaScript for the ticket time tracking feature -document.addEventListener("DOMContentLoaded", function() { - // Initialize variables - var timerInterval = null; - var isPaused = false; - var ticketID = getCurrentTicketID(); - var elapsedSecs = getElapsedSeconds(); - - // Get the ticket ID from the URL - // Inputs: None - // Outputs: The ticket ID from the URL - // Document Interactions: None +(function() { + document.addEventListener("DOMContentLoaded", function() { + // Initialize variables + var timerInterval = null; + var isPaused = false; + var ticketID = getCurrentTicketID(); + var elapsedSecs = getElapsedSeconds(); - function getCurrentTicketID() { - const urlParams = new URLSearchParams(window.location.search); - return urlParams.get('ticket_id'); - } - - - // Get the local storage key for the ticket - // Inputs: The suffix to append to the key - // Outputs: The local storage key for the ticket - // Document Interactions: None - - function getLocalStorageKey(suffix) { - return ticketID + "-" + suffix; - } - - - // Get the elapsed seconds from local storage - // Inputs: None - // Outputs: The elapsed seconds from local storage - // Document Interactions: None - - function getElapsedSeconds() { - let storedStartTime = localStorage.getItem(getLocalStorageKey("startTime")); - let pausedTime = parseInt(localStorage.getItem(getLocalStorageKey("pausedTime")) || "0"); - // If there is no start time, return the paused time - if (!storedStartTime) return pausedTime; - // Otherwise, return the paused time plus the time since the start time - let timeSinceStart = Math.floor((Date.now() - parseInt(storedStartTime)) / 1000); - return pausedTime + timeSinceStart; - } - - // Display the elapsed time - // Inputs: None - // Outputs: None - // Document Interactions: Updates the time worked input - - function displayTime() { - let totalSeconds = elapsedSecs; - let hours = Math.floor(totalSeconds / 3600); - totalSeconds %= 3600; - let minutes = Math.floor(totalSeconds / 60); - let seconds = totalSeconds % 60; - // Update the time worked input - document.getElementById("time_worked").value = `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`; - } - - // Pad a number with a leading zero if it is less than 10 - // Inputs: The number to pad - // Outputs: The padded number - // Document Interactions: None - - function pad(val) { - return val < 10 ? "0" + val : val; - } - - // Count the time - // Inputs: None - // Outputs: None - // Document Interactions: Updates the elapsed time - - function countTime() { - elapsedSecs++; - displayTime(); - } - - - // Start the timer - // Inputs: None - // Outputs: None - // Document Interactions: None - - function startTimer() { - if (!localStorage.getItem(getLocalStorageKey("startTime"))) { - localStorage.setItem(getLocalStorageKey("startTime"), Date.now().toString()); + function getCurrentTicketID() { + const urlParams = new URLSearchParams(window.location.search); + return urlParams.get('ticket_id'); } - if (!isPaused && timerInterval === null) { + + function getLocalStorageKey(suffix) { + return ticketID + "-" + suffix; + } + + function getElapsedSeconds() { + let storedStartTime = parseInt(localStorage.getItem(getLocalStorageKey("startTime")) || "0"); + let pausedTime = parseInt(localStorage.getItem(getLocalStorageKey("pausedTime")) || "0"); + if (!storedStartTime) return pausedTime; + let timeSinceStart = Math.floor((Date.now() - storedStartTime) / 1000); + return pausedTime + timeSinceStart; + } + + function displayTime() { + let totalSeconds = elapsedSecs; + let hours = Math.floor(totalSeconds / 3600); + totalSeconds %= 3600; + let minutes = Math.floor(totalSeconds / 60); + let seconds = totalSeconds % 60; + + document.getElementById("hours").value = pad(hours); + document.getElementById("minutes").value = pad(minutes); + document.getElementById("seconds").value = pad(seconds); + } + + function pad(val) { + return val < 10 ? "0" + val : val; + } + + function countTime() { + elapsedSecs++; + displayTime(); + } + + function startTimer() { + if (!localStorage.getItem(getLocalStorageKey("startTime"))) { + localStorage.setItem(getLocalStorageKey("startTime"), Date.now().toString()); + } timerInterval = setInterval(countTime, 1000); + isPaused = false; + document.getElementById("startStopTimer").innerText = "Pause"; } - } - // Pause the timer - // Inputs: None - // Outputs: None - // Document Interactions: None + function pauseTimer() { + if (timerInterval) { + clearInterval(timerInterval); + timerInterval = null; + } + let currentElapsed = getElapsedSeconds(); + localStorage.setItem(getLocalStorageKey("pausedTime"), currentElapsed.toString()); + localStorage.removeItem(getLocalStorageKey("startTime")); + isPaused = true; + document.getElementById("startStopTimer").innerText = "Start"; + } - function pauseTimer() { - if (timerInterval) { + function clearTimeStorage() { + localStorage.removeItem(getLocalStorageKey("startTime")); + localStorage.removeItem(getLocalStorageKey("pausedTime")); + } + + function resetTimer() { + if (confirm("Are you sure you want to reset the timer?")) { + clearInterval(timerInterval); + timerInterval = null; + elapsedSecs = 0; + clearTimeStorage(); + displayTime(); + document.getElementById("startStopTimer").innerText = "Start"; + } + } + + function forceResetTimer() { clearInterval(timerInterval); timerInterval = null; + elapsedSecs = 0; + clearTimeStorage(); + displayTime(); + document.getElementById("startStopTimer").innerText = "Start"; } - let currentElapsed = getElapsedSeconds(); - localStorage.setItem(getLocalStorageKey("pausedTime"), currentElapsed.toString()); - localStorage.removeItem(getLocalStorageKey("startTime")); - } + - // Clear the time storage - // Inputs: None - // Outputs: None - // Document Interactions: None + function handleInputFocus() { + if (!isPaused) { + pauseTimer(); + } + } - function clearTimeStorage() { - localStorage.removeItem(getLocalStorageKey("startTime")); - localStorage.removeItem(getLocalStorageKey("pausedTime")); - } + document.getElementById("hours").addEventListener('focus', handleInputFocus); + document.getElementById("minutes").addEventListener('focus', handleInputFocus); + document.getElementById("seconds").addEventListener('focus', handleInputFocus); - // Add event listeners + document.getElementById("startStopTimer").addEventListener('click', function() { + if (timerInterval === null) { + startTimer(); + } else { + pauseTimer(); + } + }); - // When toggleTimer is clicked, toggle the timer - document.getElementById("toggleTimer").addEventListener('click', function() { - if (isPaused) { - // If the timer is paused, start it - startTimer(); - isPaused = false; - } else { - // If the timer is running, pause it - pauseTimer(); - isPaused = true; + document.getElementById("resetTimer").addEventListener('click', function() { + resetTimer(); + }); + + document.getElementById("ticket_add_reply").addEventListener('click', function() { + // Wait for other synchronous actions (if any) to complete before resetting the timer. + setTimeout(forceResetTimer, 100); // 100ms delay should suffice, but you can adjust as needed. + }); + + try { + displayTime(); + if (!localStorage.getItem(getLocalStorageKey("startTime")) && !localStorage.getItem(getLocalStorageKey("pausedTime"))) { + // If first time, start the timer automatically + startTimer(); + } else if (localStorage.getItem(getLocalStorageKey("startTime"))) { + // Continue timer if it was running before + startTimer(); + } + } catch (error) { + console.error("There was an issue initializing the timer:", error); } }); - - // When the ticket is submitted, clear the time storage - document.getElementById("ticket_add_reply").addEventListener('click', function() { - pauseTimer(); - clearTimeStorage(); - }); - - // Initialize on page load - // If the timer is paused, start it - displayTime(); - startTimer(); - -}); +})(); \ No newline at end of file diff --git a/post/ticket.php b/post/ticket.php index 10e01300..037737d1 100644 --- a/post/ticket.php +++ b/post/ticket.php @@ -459,8 +459,17 @@ if (isset($_POST['add_ticket_reply'])) { $ticket_reply = $_POST['ticket_reply']; $ticket_status_escaped = sanitizeInput($_POST['status']); $ticket_status = $_POST['status']; - $ticket_reply_time_worked_escaped = sanitizeInput($_POST['time']); - $ticket_reply_time_worked = $_POST['time']; + // Handle the time inputs for hours, minutes, and seconds + $hours = intval($_POST['hours']); + $minutes = intval($_POST['minutes']); + $seconds = intval($_POST['seconds']); + + //var_dump($_POST); + //exit; + + // Combine into a single time string + $ticket_reply_time_worked = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds); + $ticket_reply_time_worked_escaped = sanitizeInput($ticket_reply_time_worked); $client_id = intval($_POST['client_id']); diff --git a/ticket.php b/ticket.php index 4d921e60..a9eadb85 100644 --- a/ticket.php +++ b/ticket.php @@ -315,20 +315,44 @@ if (isset($_GET['ticket_id'])) { -