forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
do_test_header.php
350 lines (324 loc) · 9.5 KB
/
do_test_header.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* \file
* \brief Show test header frame
*
* Call: do_test_header.php?lang=[langid]
* Call: do_test_header.php?text=[textid]
* Call: do_test_header.php?selection=1
* (SQL via $_SESSION['testsql'])
*
* PHP version 8.1
*
* @category User_Interface
* @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';
/**
* Set useful data for the test using SQL query.
*
* @param string &$title Title to be overwritten
* @param string &$p Property URL to be overwritten
*
* @return string SQL query to use
*
* @global string $tbpref Database table prefix
*/
function get_sql_test_data(&$title, &$p)
{
global $tbpref;
$p = "selection=" . $_REQUEST['selection'];
$testsql = do_test_test_from_selection(
$_REQUEST['selection'],
$_SESSION['testsql']
);
$totalcount = get_first_value(
"SELECT count(distinct WoID) AS value FROM $testsql"
);
$title = 'Selected ' . $totalcount . ' Term' . ($totalcount < 2 ? '' : 's');
$cntlang = get_first_value(
'SELECT count(distinct WoLgID) AS value FROM ' . $testsql
);
if ($cntlang > 1) {
$message = 'Error: The selected terms are in ' . $cntlang . ' languages, ' .
'but tests are only possible in one language at a time.';
echo error_message_with_hide($message, true);
return '';
}
$title .= ' IN ' . get_first_value(
"SELECT LgName AS value
FROM {$tbpref}languages, {$testsql} AND LgID = WoLgID
LIMIT 1"
);
return $testsql;
}
/**
* Set useful data for the test using language.
*
* @param string $title Title to be overwritten
* @param string $p Property URL to be overwritten
*
* @return string SQL query to use
*
* @global string $tbpref Database table prefix
*/
function get_lang_test_data(&$title, &$p): string
{
global $tbpref;
$langid = getreq('lang');
$p = "lang=" . $langid;
$title = "All Terms in " . get_first_value(
"SELECT LgName AS value FROM {$tbpref}languages WHERE LgID = $langid"
);
$testsql = ' ' . $tbpref . 'words WHERE WoLgID = ' . $langid . ' ';
return $testsql;
}
/**
* Set useful data for the test using text.
*
* @param string $title Title to be overwritten
* @param string $p Property URL to be overwritten
*
* @return string SQL query to use
*
* @global string $tbpref Database table prefix
*/
function get_text_test_data(&$title, &$p): string
{
global $tbpref;
$textid = getreq('text');
$p = "text=" . $textid;
$title = get_first_value(
'SELECT TxTitle AS value FROM ' . $tbpref . 'texts WHERE TxID = ' . $textid
);
saveSetting('currenttext', $_REQUEST['text']);
$testsql =
' ' . $tbpref . 'words, ' . $tbpref . 'textitems2
WHERE Ti2LgID = WoLgID AND Ti2WoID = WoID AND Ti2TxID = ' . $textid . ' ';
return $testsql;
}
/**
* Return the words count for this test.
*
* @param string $testsql SQL query for this test.
*
* @return array{0: string, 1: string} Total words due and total words learning
*/
function get_test_counts($testsql)
{
$totalcountdue = get_first_value(
"SELECT count(distinct WoID) AS value
FROM " . $testsql . " AND WoStatus BETWEEN 1 AND 5
AND WoTranslation != '' AND WoTranslation != '*' AND WoTodayScore < 0"
);
$totalcount = get_first_value(
"SELECT count(distinct WoID) AS value
FROM " . $testsql . " AND WoStatus BETWEEN 1 AND 5 AND WoTranslation != ''
AND WoTranslation != '*'"
);
return array($totalcountdue, $totalcount);
}
/**
* Make the header row for tests.
*
* @param mixed $_p URL property to use (unnused), will be removed in LWT 3.0.0
*
* @return void
*/
function do_test_header_row($_p)
{
?>
<div class="flex-header">
<div>
<a href="edit_texts.php" target="_top">
<?php echo_lwt_logo(); ?>
</a>
</div>
<?php
// This part only works if $textid is set
if (is_numeric(getreq('text'))) {
$textid = (int) getreq('text');
echo '<div>' . getPreviousAndNextTextLinks(
$textid,
'do_test.php?text=',
false,
''
) . '</div>';
?>
<div>
<a href="do_text.php?start=<?php echo $textid; ?>" target="_top">
<img src="icn/book-open-bookmark.png" title="Read" alt="Read" />
</a>
<a href="print_text.php?text=<?php echo $textid; ?>" target="_top">
<img src="icn/printer.png" title="Print" alt="Print" />
</a>
<?php echo get_annotation_link($textid); ?>
</div>
<?php
}
?>
<div>
<?php quickMenu(); ?>
</div>
</div>
<?php
}
/**
* Prepare JavaScript content for the header.
*
* @return void
*/
function do_test_header_js()
{
?>
<script type="text/javascript">
function setUtteranceSetting () {
const utterancechecked = JSON.parse(
localStorage.getItem('review-utterance-allowed')
);
const utterancecheckbox = document.getElementById('utterance-allowed');
utterancecheckbox.checked = utterancechecked;
utterancecheckbox.addEventListener('change', function () {
localStorage.setItem(
'review-utterance-allowed',
utterancecheckbox.checked
);
});
}
/**
* Reset frames location
*/
function resetFrames() {
parent.frames['ro'].location.href = 'empty.html';
parent.frames['ru'].location.href = 'empty.html';
}
/**
* Prepare frames for testing words
*/
function startWordTest(type, property) {
resetFrames();
window.location.href = 'do_test.php?type=' + type + '&' + property;
}
/**
* Prepare frames for test table.
*/
function startTestTable(property) {
resetFrames();
window.location.href = 'do_test.php?type=table&' + property;
}
$(setUtteranceSetting)
</script>
<?php
}
/**
* Make the header content for tests.
*
* @param string $title Page title
* @param string $p URL property to use
* @param string $totalcountdue Number of words due for today
* @param string $totalcount Total number of words.
* @param string $language L2 language name
*
* @return void
*/
function do_test_header_content($title, $p, $totalcountdue, $totalcount, $language)
{
?>
<h1>TEST ▶ <?php echo tohtml($title) ?></h1>
<div style="margin: 5px;">
Word<?php echo intval($totalcount) > 1 ? 's' : ''; ?> due today:
<?php echo htmlspecialchars($totalcount); ?>,
<span class="todosty" id="not-tested-header"><?php
echo htmlspecialchars($totalcountdue);
?></span> remaining.
</div>
<div class="flex-spaced">
<div>
<input type="button" value="..[<?php echo $language; ?>].."
onclick="startWordTest(1, '<?php echo $p; ?>')" />
<input type="button" value="..[L1].."
onclick="startWordTest(2, '<?php echo $p; ?>')" />
<input type="button" value="..[••].."
onclick="startWordTest(3, '<?php echo $p; ?>')" />
</div>
<div>
<input type="button" value="[<?php echo $language; ?>]"
onclick="startWordTest(4, '<?php echo $p; ?>')" />
<input type="button" value="[L1]"
onclick="startWordTest(5, '<?php echo $p; ?>')" />
</div>
<div>
<input type="button" value="Table"
onclick="startTestTable('<?php echo $p; ?>')" />
</div>
<div>
<input type="checkbox" id="utterance-allowed" />
<label for="utterance-allowed">Read words aloud</label>
</div>
</div>
<?php
}
/**
* Set useful data for the test.
*
* @param string $title Title to be overwritten
* @param string $p Property URL to be overwritten
*
* @return array{0: string, 1: string} Total words due and total words learning
*/
function get_test_data(&$title, &$p)
{
if (isset($_REQUEST['selection']) && isset($_SESSION['testsql'])) {
$testsql = get_sql_test_data($title, $p);
} elseif (isset($_REQUEST['lang'])) {
$testsql = get_lang_test_data($title, $p);
} elseif (isset($_REQUEST['text'])) {
$testsql = get_text_test_data($title, $p);
} else {
$testsql = '';
$p = '';
$title = 'Request Error!';
pagestart($title, true);
my_die("do_test_header.php called with wrong parameters");
}
return get_test_counts($testsql);
}
/**
* Do the header for test page.
*
* @param string $title Page title
* @param string $p URL property to use
* @param string $totalcountdue Number of words due for today
* @param string $totalcount Total number of words.
* @param string $language L2 Language name
*
* @return void
*/
function do_test_header_page($title, $p, $totalcountdue, $totalcount, $language)
{
do_test_header_js();
$_SESSION['teststart'] = time() + 2;
$_SESSION['testcorrect'] = 0;
$_SESSION['testwrong'] = 0;
$_SESSION['testtotal'] = $totalcountdue;
do_test_header_row(null);
do_test_header_content($title, $p, $totalcountdue, $totalcount, $language);
}
/**
* Use requests passed to the page to start it.
*
* @param string $language L2 language name
*
* @return void
*/
function start_test_header_page($language = 'L2')
{
$title = $p = '';
list($totalcountdue, $totalcount) = get_test_data($title, $p);
do_test_header_page($title, $p, $totalcountdue, $totalcount, $language);
}
?>