-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_ratings.php
77 lines (60 loc) · 2.93 KB
/
update_ratings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
include("connect.php");
$choice = $_POST['choice'];
$image1Id = $_POST['image1Id'];
$image2Id = $_POST['image2Id'];
if ($choice === 'first_one') {
first_one($conn, $image1Id,$image2Id);
} elseif ($choice === 'second_one') {
second_one($conn, $image1Id,$image2Id);
}
function first_one($conn, $image1Id,$image2Id) {
$image1Rating = getImageRating($conn, $image1Id);
$image2Rating = getImageRating($conn, $image2Id);
$kFactor = 50;
$expectedScore1 = getExpectedScore($image2Rating, $image1Rating);
$expectedScore2 = getExpectedScore($image1Rating, $image2Rating);
$newRating1 = calculateNewRating($image1Rating, 1, $expectedScore1, $kFactor);
$newRating2 = calculateNewRating($image2Rating, 0, $expectedScore2, $kFactor);
// Update the Elo ratings in the database
updateImageRating($conn, $image1Id, $newRating1);
updateImageRating($conn, $image2Id, $newRating2);
}
function second_one($conn, $image1Id,$image2Id) {
// Get the Elo ratings of the selected images from the database
$image1Rating = getImageRating($conn, $image1Id);
$image2Rating = getImageRating($conn, $image2Id);
$kFactor = 50; // Adjust the k-factor as per your requirement
$expectedScore1 = getExpectedScore($image2Rating, $image1Rating);
$expectedScore2 = getExpectedScore($image1Rating, $image2Rating);
$newRating1 = calculateNewRating($image1Rating, 0, $expectedScore1, $kFactor);
$newRating2 = calculateNewRating($image2Rating, 1, $expectedScore2, $kFactor);
// Update the Elo ratings in the database
updateImageRating($conn, $image1Id, $newRating1);
updateImageRating($conn, $image2Id, $newRating2);
}
// Function to retrieve the Elo rating of an image from the database
function getImageRating($conn, $imageId) {
$query = "SELECT rating FROM images WHERE id = $imageId";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
return $row['rating'];
}
// Function to calculate the expected score of an image
function getExpectedScore($ratingA, $ratingB) {
$expectedScore = 1 / (1 + pow(10, ($ratingB - $ratingA) / 400));
return $expectedScore;
}
// Function to calculate the new Elo rating of an image
function calculateNewRating($currentRating, $actualScore, $expectedScore, $kFactor) {
$newRating = $currentRating + $kFactor * ($actualScore - $expectedScore);
return $newRating;
}
// Function to update the Elo rating of an image in the database
function updateImageRating($conn, $imageId, $newRating) {
$query = "UPDATE images SET rating = $newRating WHERE id = $imageId";
mysqli_query($conn, $query);
}
// Close the database connection
mysqli_close($conn);
?>