Skip to content

Commit

Permalink
Refactor: Remove 'authority' rating from teacher comparison and ratin…
Browse files Browse the repository at this point in the history
…g functions
  • Loading branch information
judemont committed May 20, 2024
1 parent fa6e1a8 commit 74058ef
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 32 deletions.
7 changes: 2 additions & 5 deletions api/getTeachers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
function compareTeachers($a, $b, $sort)
{
if ($sort == "best_score") {
return $b["teaching_quality"] + $b["kindness"] + $b["authority"] + $b["humor"] - $a["teaching_quality"] - $a["kindness"] - $a["authority"] - $a["humor"];
return $b["teaching_quality"] + $b["kindness"] + $b["humor"] - $a["teaching_quality"] - $a["kindness"] - $a["humor"];
} elseif ($sort == "most_votes") {
return $b["votes_count"] - $a["votes_count"];
} elseif ($sort == "least_votes") {
return $a["votes_count"] - $b["votes_count"];
} elseif ($sort == "worst_score") {
return $a["teaching_quality"] + $a["kindness"] + $a["authority"] + $a["humor"] - $b["teaching_quality"] - $b["kindness"] - $b["authority"] - $b["humor"];
return $a["teaching_quality"] + $a["kindness"] + $a["humor"] - $b["teaching_quality"] - $b["kindness"] - $b["humor"];
}
}

Expand All @@ -38,23 +38,20 @@ function compareTeachers($a, $b, $sort)

$teachingQualityTotal = 0;
$kindnessTotal = 0;
$authorityTotal = 0;
$humorTotal = 0;

$votesCount = 0;
foreach ($votesData as $vote) {
$votesCount++;
$teachingQualityTotal += $vote["teaching_quality"];
$kindnessTotal += $vote["kindness"];
$authorityTotal += $vote["authority"];
$humorTotal += $vote["humor"];
}

$teacher["comments_count"] = count($comments);
$teacher["votes_count"] = $votesCount;
$teacher["teaching_quality"] = ($votesCount > 0) ? $teachingQualityTotal / $votesCount : 0;
$teacher["kindness"] = ($votesCount > 0) ? $kindnessTotal / $votesCount : 0;
$teacher["authority"] = ($votesCount > 0) ? $authorityTotal / $votesCount : 0;
$teacher["humor"] = ($votesCount > 0) ? $humorTotal / $votesCount : 0;
}

Expand Down
20 changes: 9 additions & 11 deletions api/newRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@
$db = new Database;
$teachingQualityRating = $db->escapeStrings($_POST["teachingQuality"]);
$kindnessRating = $db->escapeStrings($_POST["kindness"]);
$authorityRating = $db->escapeStrings($_POST["authority"]);
$humorRating = $db->escapeStrings($_POST["humor"]);

$teacherId = $db->escapeStrings($_POST["teacherId"]);

$clientIp = getClientIp();
$hashedIp = $db->escapeStrings(hash("sha256", $clientIp. HASH_SECRET));
$hashedIp = $db->escapeStrings(hash("sha256", $clientIp . HASH_SECRET));

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 ||
$humorRating > 10 || $humorRating <= 0
) {
echo "invalid values";
exit();
}

$sameUserVotes = $db->select("SELECT * FROM cescoleaks_votes WHERE teacher_ID = '$teacherId' AND IP = '$hashedIp'");
if(count($sameUserVotes) >= 1){
if (count($sameUserVotes) >= 1) {
$db->query("UPDATE cescoleaks_votes
SET teaching_quality = '$teachingQualityRating',
kindness = '$kindnessRating',
authority = '$authorityRating',
humor = '$humorRating'
WHERE teacher_ID = '$teacherId' AND IP = '$hashedIp'
");
Expand All @@ -42,9 +41,8 @@
exit();
}

$db->query("INSERT INTO cescoleaks_votes (teacher_ID, IP, teaching_quality, kindness, authority, humor) VALUES
('$teacherId', '$hashedIp', '$teachingQualityRating', '$kindnessRating', '$authorityRating', '$humorRating')");
$db->query("INSERT INTO cescoleaks_votes (teacher_ID, IP, teaching_quality, kindness, humor) VALUES
('$teacherId', '$hashedIp', '$teachingQualityRating', '$kindnessRating', '$humorRating')");

echo json_encode(array());
exit(); // we never know
?>
7 changes: 0 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ <h1 class="main-header-title-text2">cl</h1>
alt="close icon" />
</button>
<div class="standard-popup-content rating-popup">
<div class="score">
<p class="score-title">Autorité</p>
<div class="score-value">
<input id="ratingAuthority" class="value" type="range" min="1" max="10" value="5" />
<p id="ratingText1">5</p>
</div>
</div>
<div class="score">
<p class="score-title">Humour</p>
<div class="score-value">
Expand Down
7 changes: 1 addition & 6 deletions scripts/loadTeachers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,12 @@ function addTeachers(teachers) {
Math.round(
(teacher.teaching_quality +
teacher.kindness +
teacher.authority +
teacher.humor) /
4
3
)
)}
</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>
Expand Down
1 change: 0 additions & 1 deletion scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function openRatingPopup(popupId, profId) {
profId,
parseFloat(document.getElementById("ratingTeachingQuality").value),
parseFloat(document.getElementById("ratingKindness").value),
parseFloat(document.getElementById("ratingAuthority").value),
parseFloat(document.getElementById("ratingHumor").value)
).then(() => {
closePopup(popupId);
Expand Down
3 changes: 1 addition & 2 deletions scripts/sendRating.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
async function sendVotes(profId, teachingQuality, kindness, authority, humor) {
async function sendVotes(profId, teachingQuality, kindness, humor) {
let response = await fetch("api/newRating.php", {
method: "POST",
body: objectToFormData({
teacherId: profId,
teachingQuality: teachingQuality,
kindness: kindness,
authority: authority,
humor: humor,
}),
});
Expand Down

0 comments on commit 74058ef

Please sign in to comment.