forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
do_test_table.php
338 lines (306 loc) · 11.4 KB
/
do_test_table.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
<?php
/**
* \file
* \brief Show test frame with vocab table
*
* Call: do_test_table.php?lang=[langid]
* Call: do_test_test.php?text=[textid]
* Call: do_test_test.php?&selection=1 (SQL via $_SESSION['testsql'])
*
* PHP version 8.1
*
* @package Lwt
* @author LWT Project <[email protected]>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/do-test-table.html
* @since 1.5.4
*/
require_once 'inc/session_utility.php';
/**
* Set sql request for the word test.
*
* @return string SQL request string
*
* @global string $tbpref Table prefix
*/
function get_test_table_sql()
{
global $tbpref;
if (isset($_REQUEST['selection']) && isset($_SESSION['testsql'])) {
$testsql = $_SESSION['testsql'];
$cntlang = get_first_value('SELECT count(distinct WoLgID) AS value FROM ' . $testsql);
if ($cntlang > 1) {
echo '<p>Sorry - The selected terms are in ' . $cntlang .
' languages, but tests are only possible in one language at a time.</p>';
exit();
}
} elseif (isset($_REQUEST['lang'])) {
$testsql = ' ' . $tbpref . 'words where WoLgID = ' . $_REQUEST['lang'] . ' ';
} elseif (isset($_REQUEST['text'])) {
$testsql = ' ' . $tbpref . 'words, ' . $tbpref . 'textitems2
WHERE Ti2LgID = WoLgID AND Ti2WoID = WoID AND Ti2TxID = ' . $_REQUEST['text'] . ' ';
} else {
my_die("do_test_table.php called with wrong parameters");
}
return $testsql;
}
/**
* @return (float|int|null|string)[]|false|null
*
* @psalm-return array<string, float|int|null|string>|false|null
*/
function do_test_table_language_settings($testsql)
{
global $tbpref;
$lang = get_first_value('SELECT WoLgID AS value FROM ' . $testsql . ' LIMIT 1');
if (!isset($lang)) {
echo '<p class="center"> <br />
Sorry - No terms to display or to test at this time.</p>';
pageend();
exit();
}
$sql = 'SELECT LgTextSize, LgRegexpWordCharacters, LgRightToLeft
FROM ' . $tbpref . 'languages WHERE LgID = ' . $lang;
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
mysqli_free_result($res);
return $record;
}
/**
* @return int[]
*
* @psalm-return list{0|1, 0|1, 0|1, 0|1, 0|1, 0|1}
*/
function get_test_table_settings(): array
{
$currenttabletestsetting1 = getSettingZeroOrOne('currenttabletestsetting1', 1);
$currenttabletestsetting2 = getSettingZeroOrOne('currenttabletestsetting2', 1);
$currenttabletestsetting3 = getSettingZeroOrOne('currenttabletestsetting3', 0);
$currenttabletestsetting4 = getSettingZeroOrOne('currenttabletestsetting4', 1);
$currenttabletestsetting5 = getSettingZeroOrOne('currenttabletestsetting5', 0);
$currenttabletestsetting6 = getSettingZeroOrOne('currenttabletestsetting6', 1);
return array(
$currenttabletestsetting1, $currenttabletestsetting2, $currenttabletestsetting3,
$currenttabletestsetting4, $currenttabletestsetting5, $currenttabletestsetting6
);
}
function do_test_table_javascript(): void
{
?>
<script type="text/javascript">
//<![CDATA[
$(document).ready( function() {
$('#cbEdit').change(function() {
if($('#cbEdit').is(':checked')) {
$('td:nth-child(1),th:nth-child(1)').show();
do_ajax_save_setting('currenttabletestsetting1','1');
} else {
$('td:nth-child(1),th:nth-child(1)').hide();
do_ajax_save_setting('currenttabletestsetting1','0');
}
$('th,td').css('border-top-left-radius','').css('border-bottom-left-radius','');
$('th:visible').eq(0).css('border-top-left-radius','inherit')
.css('border-bottom-left-radius','0px');
$('tr:last-child>td:visible').eq(0).css('border-bottom-left-radius','inherit');
});
$('#cbStatus').change(function() {
if($('#cbStatus').is(':checked')) {
$('td:nth-child(2),th:nth-child(2)').show();
do_ajax_save_setting('currenttabletestsetting2','1');
} else {
$('td:nth-child(2),th:nth-child(2)').hide();
do_ajax_save_setting('currenttabletestsetting2','0');
}
$('th,td').css('border-top-left-radius','')
.css('border-bottom-left-radius','');
$('th:visible').eq(0).css('border-top-left-radius','inherit')
.css('border-bottom-left-radius','0px');
$('tr:last-child>td:visible').eq(0)
.css('border-bottom-left-radius','inherit');
});
$('#cbTerm').change(function() {
if($('#cbTerm').is(':checked')) {
$('td:nth-child(3)').css('color', 'black').css('cursor', 'auto');
do_ajax_save_setting('currenttabletestsetting3','1');
} else {
$('td:nth-child(3)').css('color', 'white').css('cursor', 'pointer');
do_ajax_save_setting('currenttabletestsetting3','0');
}
});
$('#cbTrans').change(function() {
if($('#cbTrans').is(':checked')) {
$('td:nth-child(4)').css('color', 'black').css('cursor', 'auto');
do_ajax_save_setting('currenttabletestsetting4','1');
} else {
$('td:nth-child(4)').css('color', 'white').css('cursor', 'pointer');
do_ajax_save_setting('currenttabletestsetting4','0');
}
});
$('#cbRom').change(function() {
if ($('#cbRom').is(':checked')) {
$('td:nth-child(5),th:nth-child(5)').show();
do_ajax_save_setting('currenttabletestsetting5','1');
} else {
$('td:nth-child(5),th:nth-child(5)').hide();
do_ajax_save_setting('currenttabletestsetting5','0');
}
$('th,td').css('border-top-right-radius','')
.css('border-bottom-right-radius','');
$('th:visible:last').css('border-top-right-radius','inherit');
$('tr:last-child>td:visible:last')
.css('border-bottom-right-radius','inherit');
});
$('#cbSentence').change(function() {
if($('#cbSentence').is(':checked')) {
$('td:nth-child(6),th:nth-child(6)').show();
do_ajax_save_setting('currenttabletestsetting6','1');
} else {
$('td:nth-child(6),th:nth-child(6)').hide();
do_ajax_save_setting('currenttabletestsetting6','0');
}
$('th,td').css('border-top-right-radius','')
.css('border-bottom-right-radius','');
$('th:visible:last').css('border-top-right-radius','inherit');
$('tr:last-child>td:visible:last')
.css('border-bottom-right-radius','inherit');
});
$('td').on('click', function() {
$(this).css('color', 'black').css('cursor', 'auto');
});
$('td').css('background-color', 'white');
$('#cbEdit').change();
$('#cbStatus').change();
$('#cbTerm').change();
$('#cbTrans').change();
$('#cbRom').change();
$('#cbSentence').change();
});
//]]>
</script>
<?php
}
function do_test_table_settings($settings): void
{
?>
<p>
<input type="checkbox" id="cbEdit" <?php echo get_checked($settings[0]); ?> />
Edit
<input type="checkbox" id="cbStatus" <?php echo get_checked($settings[1]); ?> />
Status
<input type="checkbox" id="cbTerm" <?php echo get_checked($settings[2]); ?> />
Term
<input type="checkbox" id="cbTrans" <?php echo get_checked($settings[3]); ?> />
Translation
<input type="checkbox" id="cbRom" <?php echo get_checked($settings[4]); ?> />
Romanization
<input type="checkbox" id="cbSentence" <?php echo get_checked($settings[5]); ?> />
Sentence
</p>
<?php
}
function do_test_table_header(): void
{
?>
<tr>
<th class="th1">Ed</th>
<th class="th1 clickable">Status</th>
<th class="th1 clickable">Term</th>
<th class="th1 clickable">Translation</th>
<th class="th1 clickable">Romanization</th>
<th class="th1 clickable">Sentence</th>
</tr>
<?php
}
function do_test_table_table_content($lang_record, $testsql): void
{
global $debug;
$textsize = round(((int)$lang_record['LgTextSize'] - 100) / 2, 0) + 100;
$regexword = $lang_record['LgRegexpWordCharacters'];
$rtlScript = $lang_record['LgRightToLeft'];
$span1 = ($rtlScript ? '<span dir="rtl">' : '');
$span2 = ($rtlScript ? '</span>' : '');
$sql = 'SELECT DISTINCT WoID, WoText, WoTranslation, WoRomanization,
WoSentence, WoStatus, WoTodayScore As Score
FROM ' . $testsql . ' AND WoStatus BETWEEN 1 AND 5
AND WoTranslation != \'\' AND WoTranslation != \'*\'
ORDER BY WoTodayScore, WoRandom*RAND()';
if ($debug) {
echo $sql;
}
$res = do_mysqli_query($sql);
while ($record = mysqli_fetch_assoc($res)) {
do_test_table_row($record, $regexword, $textsize, $span1, $span2);
}
mysqli_free_result($res);
}
function do_test_table_row($record, $regexword, $textsize, $span1, $span2): void
{
$sent = tohtml(repl_tab_nl($record["WoSentence"]));
$sent1 = str_replace(
"{",
' <b>[',
str_replace(
"}",
']</b> ',
mask_term_in_sentence($sent, $regexword)
)
);
?>
<tr>
<td class="td1 center" nowrap="nowrap">
<a
href="edit_tword.php?wid=<?php echo $record['WoID']; ?>" target="ro"
onclick="showRightFrames();"
>
<img src="icn/sticky-note--pencil.png" title="Edit Term" alt="Edit Term" />
</a>
</td>
<td class="td1 center" nowrap="nowrap">
<span id="STAT<?php echo $record['WoID']; ?>">
<?php
echo make_status_controls_test_table(
$record['Score'],
$record['WoStatus'],
$record['WoID']
);
?>
</span>
</td>
<td class="td1 center" style="font-size:<?php echo $textsize; ?>%;">
<?php echo $span1; ?>
<span id="TERM<?php echo $record['WoID']; ?>">
<?php echo tohtml($record['WoText']); ?>
</span>
<?php echo $span2; ?>
</td>
<td class="td1 center">
<span id="TRAN<?php echo $record['WoID']; ?>">
<?php echo tohtml($record['WoTranslation']); ?>
</span>
</td>
<td class="td1 center">
<span id="ROMA<?php echo $record['WoID']; ?>">
<?php echo tohtml($record['WoRomanization']); ?>
</span>
</td>
<td class="td1 center" style="color:#000;">
<?php echo $span1; ?>
<span id="SENT<?php echo $record['WoID']; ?>">
<?php echo $sent1; ?></span><?php echo $span2; ?>
</td>
</tr>
<?php
}
function do_test_table(): void
{
$testsql = get_test_table_sql();
$lang_record = do_test_table_language_settings($testsql);
$settings = get_test_table_settings();
do_test_table_javascript();
do_test_table_settings($settings);
echo '<table class="sortable tab2" style="width:auto;" cellspacing="0" cellpadding="5">';
do_test_table_header();
do_test_table_table_content($lang_record, $testsql);
echo '</table>';
}
?>