Skip to content

Commit

Permalink
Added rating system
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed May 15, 2024
1 parent f0e4e42 commit 80e5c92
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 100 deletions.
40 changes: 18 additions & 22 deletions api/newRating.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
<?php
include_once(dirname(__FILE__) . "/database.php");

session_start();

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$teachingQualityRating = $_GET["teachingQuality"];
$kindnessRating = $_GET["kindness"];
$authorityRating = $_GET["authority"];
$humorRating = $_GET["humor"];

$teacherId = $_GET["teacherId"];
include_once(dirname(__FILE__) . "/database.php");

// if($_SESSION["alreadyVoted".$teacherId] == true){
// echo '<script>alert("Vous ne pouvez pas voter deux fois sur le même professeur!");</script>';
$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...
?>
exit(); // we never know
?>
16 changes: 9 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

<!-- Scripts -->
<script src="./scripts/loadTeachers.js" defer></script>
<script src="./scripts/utils/getTeachers.js"></script>
<script src="./scripts/utils/objectToFormData.js"></script>
<script src="./scripts/getTeachers.js"></script>
<script src="./scripts/rating.js"></script>
<script src="./scripts/popup.js" defer></script>

<title>CESCO LEAK</title>
Expand Down Expand Up @@ -112,32 +114,32 @@ <h1 class="main-header-title-text2">cl</p>
<div class="score">
<p class="score-title">Autorité</p>
<div class="score-value">
<input id="ratingValue1" class="value" type="range" min="0" max="5" value="3">
<p id="ratingText1">3</p>
<input id="ratingAuthority" class="value" type="range" min="1" max="10" value="5>
<p id=" ratingText1">3</p>
</div>
</div>
<div class="score">
<p class="score-title">Humour</p>
<div class="score-value">
<input id="ratingValue2" class="value" type="range" min="0" max="5" value="3">
<input id="ratingHumor" class="value" type="range" min="1" max="10" value="5">
<p id="ratingText2">3</p>
</div>
</div>
<div class="score">
<p class="score-title">Qualité d'enseignement</p>
<div class="score-value">
<input id="ratingValue3" class="value" type="range" min="0" max="5" value="3">
<input id="ratingTeachingQuality" class="value" type="range" min="1" max="10" value="5">
<p id="ratingText3">3</p>
</div>
</div>
<div class="score">
<p class="score-title">Gentilesse</p>
<div class="score-value">
<input id="ratingValue4" class="value" type="range" min="0" max="5" value="3">
<input id="ratingKindness" class="value" type="range" min="1" max="10" value="5">
<p id="ratingText4">3</p>
</div>
</div>
<div class="standard-button save-ratings">Enregistrer</div>
<button id="voteRatingButton" class="standard-button save-ratings">Voter</button>
</div>
</div>
</div>
Expand Down
102 changes: 52 additions & 50 deletions scripts/loadTeachers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,66 @@ getTeachers().then(function (teachers) {
console.log(teachers);
teachers.forEach((teacher) => {
console.log(teacher);
htmlResult += `
<div class='standard-card'>
<div class="standard-card-top">
<img
class="standard-card-image"
src="${teacher.image_url}"
alt="undefined"
/>
<div class="standard-card-top-right">
<h1 class="standard-card-name">${teacher.name}</h1>
<button
class="standard-button standard-card-top-right-button"
onclick="openPopup('ratingPopup')"
>
Voter
</button>
htmlResult += /*html*/ `
<div class="standard-card">
<div class="standard-card-top">
<img
class="standard-card-image"
src="${teacher.image_url}"
alt="undefined"
/>
<div class="standard-card-top-right">
<h1 class="standard-card-name">${teacher.name}</h1>
<button
class="standard-button standard-card-top-right-button"
onclick="openRatingPopup('ratingPopup',${teacher.ID})"
>
Voter
</button>
</div>
</div>
</div>
<div class="standard-card-stars-container">
<p class="standart-card-stars-text">Moyenne</p>
<div class="standard-card-stars-container">
<p class="standart-card-stars-text">Moyenne</p>
<div class="standard-card-stars">
${getStarsHtml(
Math.round(
(teacher.teaching_quality +
teacher.kindness +
teacher.authority +
teacher.humor) /
4
)
)}
</div>
<div class="standard-card-stars">
${getStarsHtml(
Math.round(
(teacher.teaching_quality +
teacher.kindness +
teacher.authority +
teacher.humor) /
4
)
)}
</div>
<p class="standart-card-stars-text">Autorité</p>
<div class="standard-card-stars">
${getStarsHtml(teacher.authority)}
</div>
<p class="standart-card-stars-text">Autorité</p>
<div class="standard-card-stars">
${getStarsHtml(teacher.authority)}
</div>
<p class="standart-card-stars-text">Humour</p>
<div class="standard-card-stars">${getStarsHtml(teacher.humor)}</div>
<p class="standart-card-stars-text">Humour</p>
<div class="standard-card-stars">${getStarsHtml(teacher.humor)}</div>
<p class="standart-card-stars-text">Qualité d'enseignement</p>
<div class="standard-card-stars">
${getStarsHtml(teacher.teaching_quality)}
</div>
<p class="standart-card-stars-text">Qualité d'enseignement</p>
<div class="standard-card-stars">
${getStarsHtml(teacher.teaching_quality)}
</div>
<p class="standart-card-stars-text">Gentilesse</p>
<div class="standard-card-stars">${getStarsHtml(teacher.kindness)}</div>
</div>
<!-- Open comments popup -->
<div
class="standard-button standard-card-button"
onclick="openPopup('commentPopup')"
>
Lire les avis
<p class="standart-card-stars-text">Gentilesse</p>
<div class="standard-card-stars">
${getStarsHtml(teacher.kindness)}
</div>
</div>
<!-- Open comments popup -->
<div
class="standard-button standard-card-button"
onclick="openPopup('commentPopup')"
>
Lire les avis
</div>
</div>
</div>
`;
});

Expand Down
59 changes: 38 additions & 21 deletions scripts/popup.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
});
16 changes: 16 additions & 0 deletions scripts/rating.js
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions scripts/utils/objectToFormData.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 80e5c92

Please sign in to comment.