Ticket time tracking

Bugfix for ticket time tracking as this was broken since the open ticket timer was removed
This commit is contained in:
Marcus Hill 2024-05-26 17:05:42 +01:00
parent f1037975a7
commit 059a5516fc
3 changed files with 7 additions and 169 deletions

View File

@ -1,147 +0,0 @@
// ticketCounter.js
(function() {
function getRunningTicketCount() {
let count = 0;
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.includes("ticket-timer-running")) {
let isRunning = JSON.parse(localStorage.getItem(key));
if (isRunning) {
count++;
}
}
}
return count;
}
function updateTicketCountDisplay() {
let count = getRunningTicketCount();
let countDisplay = document.getElementById("runningTicketsCount");
if (countDisplay) {
countDisplay.innerText = count;
}
if (count == 0) {
countDisplay.classList.remove('badge-danger');
} else {
//check to see if more than one ticket
if (count > 1) {
countDisplay.classList.add('badge-danger');
}
//if count is one, check to see if its open in the current window by looking at the post variable ticket_id in url
if (count == 1) {
let urlParams = new URLSearchParams(window.location.search);
let ticketID = urlParams.get('ticket_id');
console.log(ticketID);
// If ticket number equals one in local storage, then add badge-danger class
if (localStorage.getItem("ticket-timer-running-") == ticketID) {
countDisplay.classList.add('badge-danger');
} else {
countDisplay.classList.remove('badge-danger');
}
}
}
}
function getElapsedSeconds(ticketID) {
let storedStartTime = parseInt(localStorage.getItem(ticketID + "-startTime") || "0");
let pausedTime = parseInt(localStorage.getItem(ticketID + "-pausedTime") || "0");
if (!storedStartTime) return pausedTime;
let timeSinceStart = Math.floor((Date.now() - storedStartTime) / 1000);
return pausedTime + timeSinceStart;
}
function formatTime(seconds) {
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor((seconds % 3600) / 60);
let secs = seconds % 60;
return `${hours}h ${minutes}m ${secs}s`;
}
function loadOpenTickets() {
let openTicketsContainer = document.getElementById('openTicketsContainer');
openTicketsContainer.innerHTML = ''; // Clear existing content
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.startsWith("ticket-timer-running-")) {
let ticketID = key.replace("ticket-timer-running-", "");
let isRunning = JSON.parse(localStorage.getItem(key));
let ticketDiv = document.createElement('div');
ticketDiv.classList.add('card', 'card-outline', 'mb-3');
// Add class based on ticket status
ticketDiv.classList.add(isRunning ? 'card-info' : 'card-warning');
ticketDiv.id = 'ticket-' + ticketID;
let elapsedSecs = getElapsedSeconds(ticketID);
let timeString = formatTime(elapsedSecs);
ticketDiv.innerHTML = `
<div class="card-header">
<h3 class="card-title">Ticket ID: ${ticketID}</h3>
<a href="/ticket.php?ticket_id=${ticketID}" class="btn btn-primary float-right">View Ticket</a>
</div>
<div class="card-body">
<p id="time-${ticketID}">Total Time: ${timeString}</p>
</div>
`;
openTicketsContainer.appendChild(ticketDiv);
}
}
requestAnimationFrame(() => updateRunningTickets());
}
function updateRunningTickets() {
let runningTickets = document.querySelectorAll('[id^="ticket-"]');
runningTickets.forEach(ticket => {
let ticketID = ticket.id.replace("ticket-", "");
let isRunning = JSON.parse(localStorage.getItem("ticket-timer-running-" + ticketID));
if (isRunning) {
let updatedTime = formatTime(getElapsedSeconds(ticketID));
document.getElementById('time-' + ticketID).innerText = 'Total Time: ' + updatedTime;
}
});
requestAnimationFrame(updateRunningTickets);
}
function clearAllTimers() {
// Collect keys to be removed
let keysToRemove = [];
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.startsWith("ticket-timer-running-") || key.endsWith("-startTime") || key.endsWith("-pausedTime")) {
keysToRemove.push(key);
}
}
// Remove collected keys
keysToRemove.forEach(key => localStorage.removeItem(key));
// Update the display and redirect
updateTicketCountDisplay();
window.location.href = "/tickets.php";
}
// Initial update on script load
updateTicketCountDisplay();
// update every 10 seconds
setInterval(updateTicketCountDisplay, 10000);
// Add event listener to modal
document.addEventListener('DOMContentLoaded', function() {
let modal = document.getElementById('openTicketsModal');
if (modal) {
$('#openTicketsModal').on('show.bs.modal', loadOpenTickets);
}
});
// Add event listener to clear all timers button
document.getElementById('clearAllTimers').addEventListener('click', clearAllTimers);
})();

View File

@ -8,8 +8,6 @@
var ticketID = getCurrentTicketID();
var elapsedSecs = getElapsedSeconds();
updateRunningTicketsCount();
function getCurrentTicketID() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('ticket_id');
@ -55,7 +53,6 @@
timerInterval = setInterval(countTime, 1000);
isPaused = false;
document.getElementById("startStopTimer").innerText = "Pause";
updateRunningTicketsCount();
localStorage.setItem("ticket-timer-running-" + ticketID, "true");
}
@ -70,7 +67,6 @@
localStorage.removeItem(getLocalStorageKey("startTime"));
isPaused = true;
document.getElementById("startStopTimer").innerText = "Start";
updateRunningTicketsCount();
localStorage.setItem("ticket-timer-running-" + ticketID, "false");
}
@ -91,7 +87,6 @@
document.getElementById("startStopTimer").innerText = "Start";
}
localStorage.setItem("ticket-timer-running-" + ticketID, "false");
updateRunningTicketsCount();
}
function forceResetTimer() {
@ -102,7 +97,7 @@
displayTime();
document.getElementById("startStopTimer").innerText = "Start";
}
function handleInputFocus() {
if (!isPaused) {
pauseTimer();
@ -114,7 +109,7 @@
const minutes = parseInt(document.getElementById("minutes").value, 10) || 0;
const seconds = parseInt(document.getElementById("seconds").value, 10) || 0;
elapsedSecs = (hours * 3600) + (minutes * 60) + seconds;
// Update local storage so the manually entered time is retained even if the page is reloaded.
if (!timerInterval) {
localStorage.setItem(getLocalStorageKey("pausedTime"), elapsedSecs.toString());
@ -125,18 +120,6 @@
}
}
function updateRunningTicketsCount() {
let runningTickets = parseInt(document.getElementById('runningTicketsCount').innerText, 10);
if (!isPaused && timerInterval) {
runningTickets += 1;
} else {
runningTickets = Math.max(0, runningTickets - 1);
}
document.getElementById('runningTicketsCount').innerText = runningTickets.toString();
}
// Function to check status and pause timer
function checkStatusAndPauseTimer() {
var status = document.querySelector('select[name="status"]').value;
@ -148,7 +131,7 @@
document.getElementById("hours").addEventListener('change', updateTimeFromInput);
document.getElementById("minutes").addEventListener('change', updateTimeFromInput);
document.getElementById("seconds").addEventListener('change', updateTimeFromInput);
document.getElementById("hours").addEventListener('focus', handleInputFocus);
document.getElementById("minutes").addEventListener('focus', handleInputFocus);
document.getElementById("seconds").addEventListener('focus', handleInputFocus);
@ -184,11 +167,11 @@
} else if (localStorage.getItem(getLocalStorageKey("startTime"))) {
startTimer();
}
// Check and pause timer if status is pending
checkStatusAndPauseTimer();
} catch (error) {
console.error("There was an issue initializing the timer:", error);
}
});
})();
})();

View File

@ -12,6 +12,8 @@
<!-- Center navbar links -->
<ul class="navbar-nav ml-auto">
<!-- SEARCH FORM -->
<form class="form-inline" action="global_search.php">
<div class="input-group input-group-sm">