forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathset_word_status.php
199 lines (183 loc) · 5.5 KB
/
set_word_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
<?php
/**
* \file
* \brief Change status of term while reading
*
* Call: set_word_status.php?...
* ... tid=[textid]&wid=[wordid]&status=1..5/98/99
*
* PHP version 8.1
*
* @category Helper_Frame
* @package Lwt
* @author LWT Project <lwt-project@hotmail.com>
* @license Unlicense <http://unlicense.org/>
* @since 1.0.3
*/
require_once 'inc/session_utility.php';
/**
* Get various data for the word corresponding to the ID.
*
* @param string $wid ID of the word
*
* @return array{0: string, 1: string, 2: string} The word in plain text,
* his translation and his romanization
*
* @global string $tbpref
*/
function get_word_data($wid)
{
global $tbpref;
$sql = "SELECT WoText, WoTranslation, WoRomanization
FROM {$tbpref}words WHERE WoID = $wid";
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
if (!$record) {
my_die("Word not found in set_word_status.php");
}
$word = $record['WoText'];
$trans = repl_tab_nl($record['WoTranslation']) . getWordTagList($wid, ' ', 1, 0);
$roman = $record['WoRomanization'];
mysqli_free_result($res);
return array($word, $trans, $roman);
}
/**
* Sent an AJAX request to change a word satus.
*
* @param string $wid ID of the word status to change
* @param string $status New status to set
*/
function set_word_status_ajax($wid, $status): void
{
?>
<script type="text/javascript">
const wordid = parseInt(<?php echo $wid; ?>, 10);
const status = parseInt(<?php echo $status; ?>, 10);
$.post(
'api.php/v1/terms/' + wordid + '/status/' + status,
{},
function (data) {
if (data == "" || "error" in data) {
word_update_error();
} else {
apply_word_update(wordid, status);
}
},
"json"
);
</script>
<?php
}
/**
* Edit the term status in the database.
*
* @param string $wid ID of the word to update
* @param string $status New status to set
*
* @return string Some edit message, number of affected rows or error message
*
* @global string $tbpref
*/
function set_word_status_database($wid, $status)
{
global $tbpref;
$m1 = runsql(
"UPDATE {$tbpref}words
SET WoStatus = $status, WoStatusChanged = NOW()," .
make_score_random_insert_update('u') . "
WHERE WoID = $wid",
'Status changed'
);
return $m1;
}
/**
* Do the JavaScript action for changing display of the word.
*
* @param string $tid Text ID
* @param string $wid ID of the word that changed status, unnused
* @param string $status New status, unnused
* @param string $word Word in plain text
* @param string $trans Translation of the word
* @param string $roman Romanization of the word
*
* @return void
*/
function set_word_status_javascript($tid, $wid, $status, $word, $trans, $roman)
{
$todo_content = todo_words_content((int) $tid);
?>
<script type="text/javascript">
function word_update_error() {
$('#status_change_log').text("Word status update failed!");
cleanupRightFrames();
}
function update_word_display(wid, status, word, trans, roman, tid) {
let context = window.parent.document.getElementById('frame-l');
let contexth = window.parent.document.getElementById('frame-h');
let title = '';
if (!window.parent.LWT_DATA.settings.jQuery_tooltip) {
title = make_tooltip(word, trans, roman, status);
}
$('.word' + wid, context)
.removeClass('status98 status99 status1 status2 status3 status4 status5')
.addClass('status' + status)
.attr('data_status', status)
.attr('title', title);
$('#learnstatus', contexth).html(tid);
}
function apply_word_update(wid, status) {
$('#status_change_log').text('Term status changed to ' + status);
update_word_display(
wid, status,
<?php echo json_encode($word); ?>,
<?php echo json_encode($trans); ?>,
<?php echo json_encode($roman); ?>,
<?php echo json_encode($todo_content); ?>
);
cleanupRightFrames();
}
</script>
<?php
}
/**
* Echo the HTML content of the page.
*
* @param string $tid Text ID
* @param string $wid ID of the word that changed status
* @param string $status New status
* @param string $word Word in plain text
* @param string $trans Translation of the word
* @param string $roman Romanization of the word
*
* @return void
*/
function set_word_status_display_page($tid, $wid, $status, $word, $trans, $roman)
{
pagestart("Term: $word", false);
echo "<p id='status_change_log'>Term status updating...</p>";
set_word_status_javascript($tid, $wid, $status, $word, $trans, $roman);
pageend();
}
/**
* Complete workflow for updating a word.
* It edits the database, show the success message
* and do JavaScript action to change its display.
*
* @param string $textid ID of the affected text
* @param string $wordid ID of the word to update
* @param string $status New status for this word
*
* @return void
*
* @since 2.0.4-fork
*/
function do_set_word_status($textid, $wordid, $status)
{
list($word, $trans, $roman) = get_word_data($wordid);
set_word_status_display_page($textid, $wordid, $status, $word, $trans, $roman);
set_word_status_ajax($wordid, $status);
}
if (getreq('tid') != '' && getreq('wid') != '' && getreq('status') != '') {
do_set_word_status(getreq('tid'), getreq('wid'), getreq('status'));
}
?>