The following files exists in this folder. Click to view.
db_connect.php
dict.xml
practice.php
user.php
util.php
practice.php
124 lines ASCII Windows (CRLF)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// scentences
function new_scentence($translationId, $scentence, $gramaticlyCorrect = false, $gramaticlySound = false, $verified = false)
{
quick_statment("INSERT INTO scentence (translation_id, scentence, gramaticly_correct, gramaticly_sound, verified) VALUES(?, ?, ?, ?, ?)", "isiii", $translationId, $scentence, $gramaticlyCorrect, $gramaticlySound, $verified);
}
function verify_scentence($scentenceId, $gramaticlyCorrect, $gramaticlySound)
{
quick_statment("UPDATE scentence SET gramaticly_correct=? gramaticly_sound=? verified=true WHERE id=?", "iiii", $gramaticlyCorrect, $gramaticlySound, $scentenceId);
}
// translation functions
function get_users_available_translations($userId)
{
return quick_statment("SELECT * FROM translation WHERE user_id=?", "i", $userId)->fetch_all(MYSQLI_ASSOC);
}
// try_record functions
function next_checkpoint(&$tries) // IMPORTANT - WILL SORT THE ORIGINAL ARRAY
{
if (count($tries) < 5)
return 0;
usort($tries, function ($a, $b) {
return $b["time"] - $a["time"];
});
// sort
// figure out next
$streak = 0;
for ($j = 0; $j < count($tries); $j++) {
$try = $tries[$j];
if ($try["success"] && $streak != -1)
$streak++;
else
$streak = -1;
if ($streak > 3)
break;
}
if ($streak < 4) {
return 0;
}
return 2 * $tries[0]["time"] - $tries[$streak - 1]["time"];
}
function to_recent($try)
{
$tryCooldownTime = 30; // 30 seconds
return time() - $try["time"] < $tryCooldownTime;
}
function get_practice_problem()
{
global $db;
$userId = user_id();
$availableTranslations = get_users_available_translations($userId);
$problem = null;
$backupProblem = null;
$leftToLearn = 0;
// check for checkpoints
foreach ($availableTranslations as $translation) {
$tries = quick_statment("SELECT * FROM try_record WHERE translation_id=?", "i", $translation["id"])->fetch_all(MYSQLI_ASSOC);
$nextCheckpoint = next_checkpoint($tries);
//echo $nextCheckpoint;
if (time() > $nextCheckpoint) {
if ($nextCheckpoint != 0) {
if (time() - $tries[0]["time"] < 60 && $problem == null) {
$problem = $translation;
continue;
} else {
$problem = $translation;
break;
}
}
if (time() - $tries[0]["time"] < 60) {
if ($backupProblem == null) {
$backupProblem = $translation;
}
} else {
$backupProblem = $translation;
}
}
}
if ($backupProblem == null && $problem == null)
die("???");
if ($problem == null)
$problem = $backupProblem;
$wordObj = quick_statment("SELECT * FROM word WHERE id=?", "i", $problem["word_id"])->fetch_assoc();
$translations = [];
foreach ($availableTranslations as $translation) {
if ($translation["word_id"] == $wordObj["id"])
$translations[] = $translation;
}
return
[
"word" => $wordObj,
"translations" => $translations
];
}
function record_practice(int $wordId, bool $success)
{
global $db;
$stmt = $db->prepare("INSERT INTO try_record (word_id, success) VALUES (?, ?)");
$stmt->bind_param("ib", $wordId, $success);
$stmt->execute();
}