forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
set_test_status.php
246 lines (223 loc) · 7.01 KB
/
set_test_status.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* \file
* \brief Change status of term while testing
*
* Call: set_test_status.php?wid=[wordid]&stchange=+1/-1&[ajax=1]
* set_test_status.php?wid=[wordid]&status=1..5/98/99&[ajax=1]
*
* PHP version 8.1
*
* @category Helper_Frame
* @package Lwt
* @author LWT Project <[email protected]>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/do-test-header.html
* @since 1.0.3
*/
require_once 'inc/session_utility.php';
require_once 'inc/start_session.php';
/**
* Echo the page HTML content when setting word status.
*
* @param int $status New learning status for the word
* @param int $oldstatus Previous learning status
* @param int $newscore New score for the word
* @param int $oldscore Previous score
*
* @return void
*/
function do_set_test_status_html($status, $oldstatus, $newscore, $oldscore)
{
if ($oldstatus == $status) {
echo '<p>Status ' . get_colored_status_msg($status) . ' not changed.</p>';
} else {
echo '<p>
Status changed from ' . get_colored_status_msg($oldstatus) .
' to ' . get_colored_status_msg($status) . '
.</p>';
}
echo "<p>Old score was $oldscore, new score is now $newscore.</p>";
}
/**
* Increment the session progress in learning new words.
*
* @param int $stchange -1, 0, or 1 if status is rising or not
*
* @return int[] Tests data
*
* @psalm-return array{total: int, wrong: int, correct: int, nottested: int}
*/
function set_test_status_change_progress($stchange): array
{
$totaltests = (int)$_SESSION['testtotal'];
$wrong = (int) $_SESSION['testwrong'];
$correct = (int) $_SESSION['testcorrect'];
$notyettested = $totaltests - $correct - $wrong;
if ($notyettested > 0) {
if ($stchange >= 0) {
$correct++;
$_SESSION['testcorrect']++;
} else {
$wrong++;
$_SESSION['testwrong']++;
}
$notyettested--;
}
return array(
"total" => $totaltests, "wrong" => $wrong, "correct" => $correct,
"nottested" => $notyettested
);
}
/**
* Make the JavaScript action for setting a word status.
*
* @param int $wid Word ID
* @param int $status New learning status for the word
* @param int $stchange -1, 0, or 1 if status is rising or not
*
* @return void
*/
function do_set_test_status_javascript(
$wid, $status, $stchange, $tests_status=array(), $ajax=false
) {
?>
<script type="text/javascript">
const context = window.parent;
$('.word<?php echo $wid; ?>', context.document)
.removeClass('todo todosty')
.addClass('done<?php echo ($stchange >= 0 ? 'ok' : 'wrong'); ?>sty')
.attr('data_status','<?php echo $status; ?>')
.attr('data_todo','0');
// Waittime <= 0 causes the page to loop-reloading
const waittime = <?php
echo json_encode((int)getSettingWithDefault('set-test-main-frame-waiting-time'));
?> + 500;
function page_reloader(waittime, target) {
if (waittime <= 0) {
target.location.reload();
} else {
setTimeout(window.location.reload.bind(target.location), waittime);
}
}
/**
* Update remaining words count.
*/
function update_tests_count(tests_status, cont_document) {
let width_divisor = .01;
if (tests_status["total"] > 0) {
width_divisor = tests_status["total"] / 100;
}
$("#not-tested-box", cont_document)
.width(tests_status["nottested"] / width_divisor);
$("#wrong-tests-box", cont_document)
.width(tests_status["wrong"] / width_divisor);
$("#correct-tests-box", cont_document)
.width(tests_status["correct"] / width_divisor);
$("#not-tested-header", cont_document).text(tests_status["nottested"]);
$("#not-tested", cont_document).text(tests_status["nottested"]);
$("#wrong-tests", cont_document).text(tests_status["wrong"]);
$("#correct-tests", cont_document).text(tests_status["correct"]);
}
/**
* Get a new word.
*/
function ajax_reloader(waittime, target, tests_status) {
if (waittime <= 0) {
context.get_new_word();
} else {
setTimeout(target.get_new_word, waittime);
}
}
if (<?php echo json_encode($ajax); ?>) {
// Update status footer
update_tests_count(
<?php echo json_encode($tests_status); ?>, context.document
);
// Get new word
ajax_reloader(waittime, context);
} else {
page_reloader(waittime, context);
}
</script>
<?php
}
/**
* Make the page content of the word status page.
*
* @param int $wid Word ID
* @param int $status New learning status for the word
* @param int $oldstatus Previous learning status
* @param int $stchange -1, 0, or 1 if status is rising or not
*
* @return void
*
* @global string $tbpref Database table prefix
*/
function do_set_test_status_content($wid, $status, $oldstatus, $stchange, $ajax=false)
{
global $tbpref;
$word = get_first_value(
"SELECT WoText AS value FROM {$tbpref}words
WHERE WoID = $wid"
);
$oldscore = (int)get_first_value(
"SELECT greatest(0,round(WoTodayScore,0)) AS value
FROM {$tbpref}words WHERE WoID = $wid"
);
runsql(
"UPDATE {$tbpref}words SET WoStatus = $status, WoStatusChanged = NOW()," .
make_score_random_insert_update('u') . "
WHERE WoID = $wid",
'Status changed'
);
$newscore = (int)get_first_value(
"SELECT greatest(0,round(WoTodayScore,0)) AS value
FROM {$tbpref}words WHERE WoID = $wid"
);
pagestart("Term: " . $word, false);
do_set_test_status_html($status, $oldstatus, $newscore, $oldscore);
$tests = set_test_status_change_progress($stchange);
do_set_test_status_javascript($wid, $status, $stchange, $tests, $ajax);
pageend();
}
/**
* Start the word status set page.
*
* @return void
*
* @global string $tbpref Database table prefix
*/
function start_set_text_status()
{
global $tbpref;
if (!is_numeric(getreq('status')) && !is_numeric(getreq('stchange'))) {
my_die('status or stchange should be specified!');
}
$wid = (int)getreq('wid');
$oldstatus = (int)get_first_value(
"SELECT WoStatus AS value FROM {$tbpref}words
WHERE WoID = $wid"
);
if (is_numeric(getreq('stchange'))) {
$stchange = (int)getreq('stchange');
$status = $oldstatus + $stchange;
if ($status < 1) {
$status = 1;
} else if ($status > 5) {
$status = 5;
}
} else {
$status = (int)getreq('status');
$stchange = $status - $oldstatus;
if ($stchange <= 0) {
$stchange = -1;
} else if ($stchange > 0) {
$stchange = 1;
}
}
$use_ajax = array_key_exists("ajax", $_REQUEST);
do_set_test_status_content($wid, $status, $oldstatus, $stchange, $use_ajax);
}
start_set_text_status();
?>