From 80e5c9211e1f48694a4121742c088204322dedd3 Mon Sep 17 00:00:00 2001 From: JdM Date: Wed, 15 May 2024 15:09:16 +0200 Subject: [PATCH] Added rating system --- api/newRating.php | 40 ++++++------ index.html | 16 +++-- scripts/loadTeachers.js | 102 +++++++++++++++--------------- scripts/popup.js | 59 +++++++++++------ scripts/rating.js | 16 +++++ scripts/utils/objectToFormData.js | 9 +++ 6 files changed, 142 insertions(+), 100 deletions(-) create mode 100644 scripts/rating.js create mode 100644 scripts/utils/objectToFormData.js diff --git a/api/newRating.php b/api/newRating.php index 44fa82f..434676c 100644 --- a/api/newRating.php +++ b/api/newRating.php @@ -1,32 +1,28 @@ alert("Vous ne pouvez pas voter deux fois sur le même professeur!");'; +$db = new Database; +$teachingQualityRating = $db->escapeStrings($_POST["teachingQuality"]); +$kindnessRating = $db->escapeStrings($_POST["kindness"]); +$authorityRating = $db->escapeStrings($_POST["authority"]); +$humorRating = $db->escapeStrings($_POST["humor"]); -// header("location: index.php"); -// exit(); -// } +$teacherId = $db->escapeStrings($_POST["teacherId"]); -if($teachingQualityRating > 10 || $teachingQualityRating <= 0 || $kindnessRating > 10 || $kindnessRating <= 0 || $authorityRating > 10 || $authorityRating <= 0 || $humorRating > 10 || $humorRating <= 0) { +if($teachingQualityRating > 10 || $teachingQualityRating <= 0 || + $kindnessRating > 10 || $kindnessRating <= 0 || + $authorityRating > 10 || $authorityRating <= 0 || + $humorRating > 10 || $humorRating <= 0) { echo "invalid values"; exit(); } -$db = new Database; -$db -> query("INSERT INTO cescoleaks_votes (teacher_ID, teaching_quality, kindness, authority, humor) VALUES - ('$teacherId', '$teachingQualityRating', '$kindnessRating', '$authorityRating', '$humorRating')"); -header("location: index.php"); +$db->query("INSERT INTO cescoleaks_votes (teacher_ID, teaching_quality, kindness, authority, humor) VALUES + ('$teacherId', '$teachingQualityRating', '$kindnessRating', '$authorityRating', '$humorRating')"); -exit(); // Because we never know... -?> \ No newline at end of file +exit(); // we never know +?> diff --git a/index.html b/index.html index 8626e25..f02a2ea 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,9 @@ - + + + CESCO LEAK @@ -112,32 +114,32 @@

cl

Autorité

- -

3

+ 3

Humour

- +

3

Qualité d'enseignement

- +

3

Gentilesse

- +

3

-
Enregistrer
+ diff --git a/scripts/loadTeachers.js b/scripts/loadTeachers.js index dc851f0..be9284c 100644 --- a/scripts/loadTeachers.js +++ b/scripts/loadTeachers.js @@ -13,64 +13,66 @@ getTeachers().then(function (teachers) { console.log(teachers); teachers.forEach((teacher) => { console.log(teacher); - htmlResult += ` -
-
- undefined -
-

${teacher.name}

- + htmlResult += /*html*/ ` +
+
+ undefined +
+

${teacher.name}

+ +
-
-
-

Moyenne

+
+

Moyenne

-
- ${getStarsHtml( - Math.round( - (teacher.teaching_quality + - teacher.kindness + - teacher.authority + - teacher.humor) / - 4 - ) - )} -
+
+ ${getStarsHtml( + Math.round( + (teacher.teaching_quality + + teacher.kindness + + teacher.authority + + teacher.humor) / + 4 + ) + )} +
-

Autorité

-
- ${getStarsHtml(teacher.authority)} -
+

Autorité

+
+ ${getStarsHtml(teacher.authority)} +
-

Humour

-
${getStarsHtml(teacher.humor)}
+

Humour

+
${getStarsHtml(teacher.humor)}
-

Qualité d'enseignement

-
- ${getStarsHtml(teacher.teaching_quality)} -
+

Qualité d'enseignement

+
+ ${getStarsHtml(teacher.teaching_quality)} +
-

Gentilesse

-
${getStarsHtml(teacher.kindness)}
-
- -
- Lire les avis +

Gentilesse

+
+ ${getStarsHtml(teacher.kindness)} +
+
+ +
+ Lire les avis +
-
`; }); diff --git a/scripts/popup.js b/scripts/popup.js index 83be28f..7b7b49d 100644 --- a/scripts/popup.js +++ b/scripts/popup.js @@ -1,30 +1,47 @@ function openPopup(id) { - const popup = document.getElementById(id); - popup.classList.add("show"); - localStorage.setItem("openPopupId", id); + const popup = document.getElementById(id); + popup.classList.add("show"); + // localStorage.setItem("openPopupId", id); +} + +function openRatingPopup(popupId, profId) { + openPopup(popupId); + + const voteRatingButton = document.getElementById("voteRatingButton"); + voteRatingButton.addEventListener("click", () => { + sendVotes( + profId, + parseFloat(document.getElementById("ratingTeachingQuality").value), + parseFloat(document.getElementById("ratingKindness").value), + parseFloat(document.getElementById("ratingAuthority").value), + parseFloat(document.getElementById("ratingHumor").value) + ).then(() => { + closePopup(popupId); + }); + }); } function closePopup(id) { - const popup = document.getElementById(id); - popup.classList.add("closing"); - setTimeout(() => { - popup.classList.remove("show", "closing"); - }, 250); - localStorage.removeItem("openPopupId"); + const popup = document.getElementById(id); + popup.classList.add("closing"); + setTimeout(() => { + popup.classList.remove("show", "closing"); + }, 250); + // localStorage.removeItem("openPopupId"); } -document.addEventListener("DOMContentLoaded", function() { - const openPopupId = localStorage.getItem("openPopupId"); - if (openPopupId) { - openPopup(openPopupId); - } -}); +// document.addEventListener("DOMContentLoaded", function () { +// const openPopupId = localStorage.getItem("openPopupId"); +// if (openPopupId) { +// openPopup(openPopupId); +// } +// }); document.addEventListener("keydown", function (event) { - if (event.key === "Escape") { - const popups = document.querySelectorAll(".show"); - popups.forEach(function (popup) { - closePopup(popup.id); - }); - } + if (event.key === "Escape") { + const popups = document.querySelectorAll(".show"); + popups.forEach(function (popup) { + closePopup(popup.id); + }); + } }); diff --git a/scripts/rating.js b/scripts/rating.js new file mode 100644 index 0000000..78b13a1 --- /dev/null +++ b/scripts/rating.js @@ -0,0 +1,16 @@ +async function sendVotes(profId, teachingQuality, kindness, authority, humor) { + const formData = new FormData(); + + let response = await fetch("api/newRating.php", { + method: "POST", + body: objectToFormData({ + teacherId: profId, + teachingQuality: teachingQuality, + kindness: kindness, + authority: authority, + humor: humor, + }), + }); + + console.log(response); +} diff --git a/scripts/utils/objectToFormData.js b/scripts/utils/objectToFormData.js new file mode 100644 index 0000000..439e644 --- /dev/null +++ b/scripts/utils/objectToFormData.js @@ -0,0 +1,9 @@ +function objectToFormData(obj) { + const formData = new FormData(); + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + formData.append(key, obj[key]); + } + } + return formData; +}