Created
June 22, 2023 04:22
-
-
Save cm8263/f08778a149fc057332e21ff5bd96144d to your computer and use it in GitHub Desktop.
Hacky changes to Sentral Education's Sickbay Edit Screen. Requires something like https://github.com/Ruud14/Page-Manipulator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let interval; | |
let checkedOut; | |
const isNew = window.location.href.split("/").pop().toLowerCase() === "new"; | |
const injection = () => { | |
const mainControls = document.querySelector(".content-box-controls"); | |
if (mainControls === null) { | |
console.log("No sick bay controls found, dipping out."); | |
return; | |
} | |
console.log("Injecting sick bay edit page custom code"); | |
const form = document.getElementById("sickbay-form"); | |
form.addEventListener("submit", (event) => event.preventDefault()); | |
buttons(mainControls); | |
notiCentre(); | |
updateRows(); | |
updateTime(); | |
if (!checkedOut) interval = setInterval(updateTime, 1000); | |
} | |
const updateRows = () => { | |
const rowsToRemove = [0, 1, 7]; | |
const rows = document.querySelector("#sickbay-form-box").querySelector("tbody").querySelectorAll("tr"); | |
rowsToRemove.forEach(row => { | |
if (row === 1) { | |
if(rows[row].querySelector("th").textContent !== "Last Updated By:") return; | |
} | |
if (row === 7) { | |
if(rows[row].querySelector("th").textContent === "Action Taken:") return rows[8].remove(); | |
} | |
rows[row].remove(); | |
}); | |
const formattedDetails = []; | |
const details = document.querySelector("#student-info-menu-year").textContent.split("\n"); | |
details.forEach(detail => { | |
detail = detail.trim(); | |
if (detail === "") return; | |
formattedDetails.push(detail); | |
}); | |
const studentImage = document.querySelector("#student-info-menu-img").querySelector("img"); | |
document.querySelector("#context-bar").querySelector("a").innerHTML = `${document.querySelector("#student-info-menu-name").textContent} - ${formattedDetails[2]} - ${formattedDetails[3]} <span style="background-image: url('${studentImage.src}'); background-size: 150%; background-position: center;"></span>`; | |
const checkOutrowsToRemove = [3, 4]; | |
const checkOutRows = document.querySelector("#check-out-time").querySelector("tbody").querySelectorAll("tr"); | |
checkOutrowsToRemove.forEach(row => checkOutRows[row]?.remove()); | |
const timeSince = document.newElement("p"); | |
timeSince.className = "time-since"; | |
rows.forEach(row => { | |
const title = row.querySelector("th"); | |
if (title.textContent === "Check Out") { | |
checkedOut = row.querySelector("label").className.split(" ").contains("selected"); | |
} else if (title.textContent === "Check In Time:") { | |
row.querySelector("td").appendChild(timeSince.clone()); | |
} | |
}); | |
checkOutRows.forEach(row => { | |
const title = row.querySelector("th"); | |
if (title.textContent === "Check Out Time:") { | |
row.querySelector("td").appendChild(timeSince.clone()); | |
} | |
}); | |
} | |
const buttons = (mainControls) => { | |
const mainButtons = mainControls.querySelectorAll("button"); | |
if (mainButtons.length < 2) { | |
console.log("No sick bay buttons found, dipping out."); | |
return; | |
} | |
mainButtons[1].remove(); | |
const recordsButton = document.createElement("a"); | |
const attendanceButton = document.createElement("a"); | |
const saveButton = document.createElement("button"); | |
const recordsLink = document.querySelector("#navmenu-id-sickbay").querySelector("a").href; | |
recordsButton.href = recordsLink; | |
recordsButton.target = "_blank"; | |
recordsButton.className = "btn btn-warning btn-small"; | |
recordsButton.innerHTML = '<span class="icon icon-history"></span> Records'; | |
const searchTerm = "student/"; | |
const student = recordsLink.slice(recordsLink.indexOf(searchTerm) + searchTerm.length).split("/")[0]; | |
attendanceButton.href = `/attendance/administration/student/${student}`; | |
attendanceButton.target = "_blank"; | |
attendanceButton.style.margin = "3px"; | |
attendanceButton.className = "btn btn-info btn-small"; | |
attendanceButton.innerHTML = '<span class="icon icon-calendar"></span> Attendance'; | |
saveButton.className = "btn btn-primary btn-small save-btn"; | |
saveButton.innerHTML = '<span class="icon icon-save"></span> Save'; | |
saveButton.onclick = saveEntry; | |
mainControls.querySelector("button").innerHTML = '<span class="icon icon-times-circle"></span> Save & Close'; | |
mainControls.insertBefore(recordsButton, mainControls.childNodes[2]); | |
mainControls.insertBefore(attendanceButton, mainControls.childNodes[2]); | |
!isNew && mainControls.insertBefore(saveButton, mainControls.childNodes[0]); | |
const controls = document.querySelector(".controls"); | |
if (controls === null) return; | |
const buttons = controls.querySelectorAll("button"); | |
if (buttons.length < 2) return; | |
buttons[1].remove(); | |
} | |
const saveEntry = async (noti = true) => { | |
return await jQuery.ajax({ | |
"url" : "/health/student/sickbay/save", | |
"method": "POST", | |
"data" : jQuery("#sickbay-form").serialize() | |
}).done((json) => { | |
response = jQuery.parseJSON(json); | |
if (response.status == 'OK') { | |
if (!noti) return; | |
"Entry was manaully saved!".confirm({ | |
'title' : 'Entry Saved!', | |
'buttons' : ['Ok'] | |
}); | |
} else { | |
responce.message.confirm({ | |
'title' : 'Error!', | |
'buttons' : ['Ok'] | |
}); | |
} | |
}); | |
} | |
const msToTime = (duration) => { | |
let seconds = Math.floor((duration / 1000) % 60); | |
let minutes = Math.floor((duration / (1000 * 60)) % 60); | |
let hours = Math.floor((duration / (1000 * 60 * 60)) % 24); | |
if (hours === 0) { | |
hours = ""; | |
} else { | |
hours += " hours, "; | |
} | |
if (minutes === 0) { | |
minutes = ""; | |
} else { | |
minutes += " minutes"; | |
} | |
if (seconds === 0) { | |
seconds = ""; | |
} else if (minutes === "") { | |
seconds += " seconds"; | |
} else { | |
seconds = `, ${seconds} seconds`; | |
} | |
return hours + minutes + seconds; | |
} | |
const updateTime = () => { | |
const pastDate = new Date(); | |
const times = document.querySelectorAll(".picker-timeonly"); | |
const timeSince = document.querySelectorAll(".time-since"); | |
const currentTime = times[0].dataset.value.split(":"); | |
if (currentTime[0].contains(" ")) { | |
currentTime[0] = currentTime[0].substring(currentTime[0].indexOf(" ") + 1); | |
} | |
pastDate.setHours(...currentTime); | |
let tempTime; | |
if (checkedOut) { | |
const checkOutTime = new Date(); | |
const rawCheckOutTime = times[1].dataset.value.split(":"); | |
if (rawCheckOutTime[0].contains(" ")) { | |
rawCheckOutTime[0] = rawCheckOutTime[0].substring(rawCheckOutTime[0].indexOf(" ") + 1); | |
} | |
checkOutTime.setHours(...rawCheckOutTime); | |
tempTime = msToTime(checkOutTime.getTime() - pastDate.getTime()); | |
} else { | |
tempTime = msToTime(new Date().getTime() - pastDate.getTime()); | |
} | |
timeSince.forEach(element => element.textContent = tempTime); | |
} | |
const notiCentre = () => { | |
const centre = document.getElementById("notification-container"); | |
const notiHr = document.createElement("hr"); | |
const notiDiv = document.createElement("div"); | |
const notiButton = document.createElement("button"); | |
notiDiv.className = "span11"; | |
notiDiv.style.paddingTop = "10px"; | |
notiButton.className = "btn btn-warning input-block-level"; | |
notiButton.innerHTML = '<span class="icon icon-bullhorn"></span> Notify Parents'; | |
notiButton.onclick = saveAndNoti; | |
notiDiv.appendChild(notiHr); | |
if (!isNew) notiDiv.appendChild(notiButton); | |
centre.appendChild(notiDiv); | |
centre.querySelectorAll("option").forEach(element => { | |
const item = element.textContent; | |
if (item !== "Choose a value..." && item !== "Phone" && item !== "Push Notification") element.remove(); | |
}); | |
} | |
const notiParent = () => { | |
new SFx.Dialog.Request({ | |
"url" : "/attendance/dialogs/sickbay_feed_item", | |
"title" : "Notify Parents", | |
"width" : "40", | |
"height": "28", | |
"action": "/attendance/sickbay/register" | |
}).show({ | |
"student" : document.getElementsByName("student_id")[0].value, | |
"incursion" : document.getElementsByName("incursion_id")[0].value | |
}); | |
} | |
const saveAndNoti = () => saveEntry(false).then(() => notiParent()); | |
injection(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment