Skip to content

Commit

Permalink
Activates voice on text reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoFara committed Jan 8, 2024
2 parents 4305299 + 5dff66c commit 08823c2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 61 deletions.
161 changes: 100 additions & 61 deletions do_test_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
/**
* Get the SQL string to perform tests.
*
* @param int|null $selection Test is of type selection
* @param int|null $selection Test is of type selection
* @param string|null $sess_testsql SQL string for test
* @param int|null $lang Test is of type language, for the language $lang ID
* @param int|null $lang Test is of type language, for the language
* $lang ID
* @param int|null $text Testing text with ID $text
*
* @return (int|int[]|string)[] Test identifier as an array(key, value)
Expand Down Expand Up @@ -74,7 +75,8 @@ function do_test_get_identifier($selection, $sess_testsql, $lang, $text): array
*
* @param bool|null $selection Test is of type selection
* @param string|null $sess_testsql SQL string for test
* @param int|null $lang Test is of type language, for the language $lang ID
* @param int|null $lang Test is of type language, for the language
* $lang ID
* @param int|null $text Testing text with ID $text
*
* @return string SQL projection (selection) string
Expand All @@ -89,7 +91,7 @@ function do_test_get_test_sql($selection, $sess_testsql, $lang, $text)
/**
* Get the test type clamped between 1 and 5 (included)
*
* @param int $testype Initial test type value
* @param int $testtype Initial test type value
*
* @return int Clamped $testtype
* - 1: Test type is ..[L2]..
Expand Down Expand Up @@ -219,7 +221,8 @@ function do_test_test_finished($testsql, $totaltests, $ajax = false)
* @global string $tbpref Table prefix
* @global int $debug Echo the passage number if 1.
*
* @return (int|null|string)[] Sentence with escaped word and not a 0 if sentence was found.
* @return (int|null|string)[] Sentence with escaped word and not a 0 if sentence
* was found.
*
* @since 2.5.3-fork Properly return sentences with at least 70% of known words.
* Previously, it was supposed to be 100%, but buggy.
Expand Down Expand Up @@ -287,8 +290,13 @@ function do_test_test_sentence($wid, $lang, $wordlc): array
*
* @psalm-return list{string, string}
*/
function do_test_get_term_test($wo_record, $sent, $testtype, $nosent, $regexword): array
{
function do_test_get_term_test(
$wo_record,
$sent,
$testtype,
$nosent,
$regexword
): array {
$wid = $wo_record['WoID'];
$word = $wo_record['WoText'];
$trans = repl_tab_nl($wo_record['WoTranslation']) .
Expand Down Expand Up @@ -436,11 +444,10 @@ function get_test_solution($testtype, $wo_record, $nosent, $wo_text)
/**
* Preforms the HTML of the test area, to update through AJAX.
*
* @param string $selector Type of test to run.
* @param array|int $selection Items to run the test on.
* @param int $totaltests Total number of tests to do.
* @param int $count Number of tests left.
* @param int $testtype Type of test.
* @param string $selector Type of test to run.
* @param array|int $selection Items to run the test on.
* @param int $count Number of tests left.
* @param int $testtype Type of test.
*
* @return int Number of tests left to do.
*
Expand All @@ -449,8 +456,12 @@ function get_test_solution($testtype, $wo_record, $nosent, $wo_text)
*
* @psalm-return int<0, max>
*/
function do_test_prepare_ajax_test_area($selector, $selection, $count, $testtype): int
{
function do_test_prepare_ajax_test_area(
$selector,
$selection,
$count,
$testtype
): int {
global $tbpref;

$nosent = false;
Expand All @@ -475,38 +486,43 @@ function do_test_prepare_ajax_test_area($selector, $selection, $count, $testtype
$lang = array(
'wb1' => isset($record['LgDict1URI']) ? $record['LgDict1URI'] : "",
'wb2' => isset($record['LgDict2URI']) ? $record['LgDict2URI'] : "",
'wb3' => isset($record['LgGoogleTranslateURI']) ? $record['LgGoogleTranslateURI'] : "",
'wb3' => isset($record['LgGoogleTranslateURI'])
? $record['LgGoogleTranslateURI'] : "",
'textsize' => $record['LgTextSize'],
'removeSpaces' => $record['LgRemoveSpaces'],
'regexword' => $record['LgRegexpWordCharacters'],
'rtlScript' => $record['LgRightToLeft']
);
mysqli_free_result($res);

$review_data = array(
"total_tests" => $count,
"test_key" => $selector,
"selection" => $selection,
"word_mode" => $nosent,
"lg_id" => $lgid,
"word_regex" => (string)$lang['regexword'],
"type" => $testtype
);

?>
<script type="text/javascript">
/**
* Get a new word test.
*
* @param {array} review_data Data to use
*/
function get_new_word()
function get_new_word(review_data)
{
const review_data = <?php echo json_encode(array(
"total_tests" => $count,
"test_key" => $selector,
"selection" => $selection,
"word_mode" => $nosent,
"lg_id" => $lgid,
"word_regex" => (string)$lang['regexword'],
"type" => $testtype
)); ?>;

query_next_term(review_data);

// Close any previous tooltip
cClick();
}

$(get_new_word);
$(function () {
get_new_word(<?php echo json_encode($review_data); ?>)
});
</script>

<p id="term-test" dir="<?php echo($lang['rtlScript'] ? 'rtl' : 'ltr'); ?>"
Expand All @@ -518,7 +534,12 @@ function get_new_word()
</div>
<?php

do_test_test_interaction_globals($lang['wb1'], $lang['wb2'], $lang['wb3']);
do_test_test_interaction_globals(
$lang['wb1'],
$lang['wb2'],
$lang['wb3'],
$lgid
);

return $count;
}
Expand Down Expand Up @@ -564,7 +585,8 @@ function prepare_test_area($testsql, $totaltests, $count, $testtype): int
$record = mysqli_fetch_assoc($res);
$wb1 = isset($record['LgDict1URI']) ? $record['LgDict1URI'] : "";
$wb2 = isset($record['LgDict2URI']) ? $record['LgDict2URI'] : "";
$wb3 = isset($record['LgGoogleTranslateURI']) ? $record['LgGoogleTranslateURI'] : "";
$wb3 = isset($record['LgGoogleTranslateURI']) ?
$record['LgGoogleTranslateURI'] : "";
$textsize = $record['LgTextSize'];
$removeSpaces = $record['LgRemoveSpaces'];
$regexword = $record['LgRegexpWordCharacters'];
Expand Down Expand Up @@ -673,28 +695,25 @@ function prepare_test_area($testsql, $totaltests, $count, $testtype): int
/**
* Prepare JavaScript code globals so that you can click on words.
*
* @param array $wo_record Word record. Associative array with keys 'WoID',
* 'WoTranslation'.
* @param string $wb1 URL of the first dictionary.
* @param string $wb2 URL of the secondary dictionary.
* @param string $wb3 URL of the google translate dictionary.
* @param int $testtype Type of test
* @param int $nosent 1 to use single word instead of sentence.
* @param string $save Word or sentence to use for the test
* @param string $wb1 URL of the first dictionary.
* @param string $wb2 URL of the secondary dictionary.
* @param string $wb3 URL of the google translate dictionary.
* @param int $lg_id Language ID (since 2.10.0-fork)
*
* @return void
*
* @global string $tbpref Database table prefix
* @global string $angDefs Languages definition array
* @since 2.10.0-fork Takes a language ID as the fourth argument
*/
function do_test_test_interaction_globals($wb1, $wb2, $wb3)
function do_test_test_interaction_globals($wb1, $wb2, $wb3, $lg_id)
{
$lang_code = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY);
?>
<script type="text/javascript">
LWT_DATA.language.id = <?php echo json_encode($lg_id); ?>;
LWT_DATA.language.dict_link1 = <?php echo json_encode($wb1); ?>;
LWT_DATA.language.dict_link2 = <?php echo json_encode($wb2); ?>;
LWT_DATA.language.translator_link = <?php echo json_encode($wb3); ?>;
LANG = getLangFromDict(LWT_DATA.language.translator_link);
LANG = <?php echo json_encode($lang_code); ?>;
if (LANG && LANG != LWT_DATA.language.translator_link) {
$("html").attr('lang', LANG);
}
Expand All @@ -709,12 +728,7 @@ function do_test_test_interaction_globals($wb1, $wb2, $wb3)
*
* @param array $wo_record Word record. Associative array with keys 'WoID',
* 'WoTranslation'.
* @param string $wb1 URL of the first dictionary.
* @param string $wb2 URL of the secondary dictionary.
* @param string $wb3 URL of the google translate dictionary.
* @param int $testtype Type of test
* @param int $nosent 1 to use single word instead of sentence.
* @param string $save Word or sentence to use for the test
* @param string $solution Solution to the test (as HTML)
*
* @return void
*/
Expand Down Expand Up @@ -777,7 +791,7 @@ function do_test_test_javascript_interaction(
$nosent,
$save
) {
do_test_test_interaction_globals($wb1, $wb2, $wb3);
do_test_test_interaction_globals($wb1, $wb2, $wb3, (int) $wo_record['WoLgID']);
$solution = get_test_solution($testtype, $wo_record, (bool) $nosent, $save);
do_test_test_javascript_clickable($wo_record, $solution);
}
Expand Down Expand Up @@ -834,7 +848,8 @@ function do_test_footer($notyettested, $wrong, $correct)
/><img
id="correct-tests-box" class="borderr"
src="<?php print_file_path('icn/test_correct.png');?>"
title="Correct" alt="Correct" height="10" width="<?php echo $l_correct; ?>" />
title="Correct" alt="Correct" height="10"
width="<?php echo $l_correct; ?>" />
</span>
<span style="margin-left: 15px; margin-right: 15px;">
<span title="Total number of tests" id="total_tests">
Expand Down Expand Up @@ -866,19 +881,22 @@ function do_test_footer($notyettested, $wrong, $correct)
*/
function do_test_test_javascript($count)
{
$time_data = array(
"wait_time" => (int) getSettingWithDefault(
'set-test-edit-frame-waiting-time'
),
"time" => time(),
"start_time" => $_SESSION['teststart'],
"show_timer" => ($count ? 0 : 1)
);
?>
<script type="text/javascript">
/**
* Prepare the different frames for a test.
*/
function prepare_test_frames()
{
const time_data = <?php echo json_encode(array(
"wait_time" => (int)getSettingWithDefault('set-test-edit-frame-waiting-time'),
"time" => time(),
"start_time" => $_SESSION['teststart'],
"show_timer" => ($count ? 0 : 1)
)) ?>;
const time_data = <?php echo json_encode($time_data) ?>;

window.parent.frames['ru'].location.href = 'empty.html';
if (time_data.wait_time <= 0) {
Expand All @@ -894,13 +912,27 @@ function prepare_test_frames()
);
}

/**
* Adds a word reading event.
*
* @param {string} term_text Term to read
* @param {int} lang_id Language ID
*/
function prepareWordReading (term_text, lang_id) {
$('.word')
.on('click', function() {
speechDispatcher(term_text, lang_id)
});
}


/**
* Insert a new word test.
*
* @param {number} word_id Word ID
* @param {string} solution Test answer
* @param {string} group
* @param {number} word_id Word ID
* @param {string} solution Test answer
* @param {string} group HTML group to display (either term, translation,
* sentence...)
*/
function insert_new_word(word_id, solution, group) {

Expand All @@ -911,7 +943,7 @@ function insert_new_word(word_id, solution, group) {

$(document).on('keydown', keydown_event_do_test_test);
$('.word')
.on('click', word_click_event_do_test_test)
.on('click', word_click_event_do_test_test);
}

/**
Expand Down Expand Up @@ -944,8 +976,13 @@ function (tomorrow_test) {
);
} else {
insert_new_word(
current_test.word_id, current_test.solution, current_test.group
current_test.word_id,
current_test.solution,
current_test.group
);
if ($('#utterance-allowed').prop('checked')) {
prepareWordReading(current_test.word_text, LWT_DATA.language.id);
};
}
}

Expand Down Expand Up @@ -999,6 +1036,8 @@ function do_test_finished(total_tests)
* @global int $debug Show debug informations
*
* @return void
*
* @deprecated Since 2.10.0-fork, use do_test_test_content_ajax instead
*/
function do_test_test_content()
{
Expand Down Expand Up @@ -1059,13 +1098,13 @@ function do_test_test_content_ajax($selector, $selection)
}
$notyettested = (int) $count;

prepare_test_footer($notyettested);
$total_tests = do_test_prepare_ajax_test_area(
$selector,
$selection,
$notyettested,
$testtype
);
prepare_test_footer($notyettested);
do_test_test_javascript($total_tests);
}

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Solves [#129](https://github.com/HugoFara/lwt/issues/129), thanks to the help of
* the space keyboard shortcut may have been inoperating.
* On `api.php`, tests were always set to "multi-word" due to a missing variable
type conversion. Pull request [#175](https://github.com/HugoFara/lwt/pull/175).
* Text-To-Speech was not working (suggested by [#187](https://github.com/HugoFara/lwt/pull/187)).
* An explicit dependency to [php-dom](https://www.php.net/manual/en/book.dom.php) was
missing in `INSTALL.sh` (and `composer.json`), as stated in
[#178](https://github.com/HugoFara/lwt/pull/178).
Expand Down
1 change: 1 addition & 0 deletions docs/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2356,6 +2356,7 @@ <h4>Fixed</h4>
<li>the space keyboard shortcut may have been inoperating.</li>
<li>On <code>api.php</code>, tests were always set to "multi-word" due to a missing variable
type conversion. Pull request <a href="https://github.com/HugoFara/lwt/pull/175">#175</a>.</li>
<li>Text-To-Speech was not working (suggested by <a href="https://github.com/HugoFara/lwt/pull/187">#187</a>).</li>
</ul>
</li>
<li>An explicit dependency to <a href="https://www.php.net/manual/en/book.dom.php">php-dom</a> was
Expand Down

0 comments on commit 08823c2

Please sign in to comment.