Skip to content

Commit

Permalink
Crossword: remove hard-coded English string, and fix wrong unit test …
Browse files Browse the repository at this point in the history
…#732333
  • Loading branch information
Khoa Nguyen Dang committed Oct 27, 2023
1 parent daedafe commit dbb0d95
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion amd/build/crossword_grid.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/crossword_grid.min.js.map

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions amd/src/crossword_grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class CrosswordGrid extends CrosswordQuestion {
* Add each cell into table.
*/
addCell() {
let {words, previewSetting, rowsNum, colsNum} = this.options;
let {words, previewSetting, rowsNum, colsNum, crosswordEl} = this.options;
let labelPlaceholder = crosswordEl.dataset.label;
const orientationMarks = ['→', '↓'];
// Don't draw empty words.
if (words.length === 0) {
Expand Down Expand Up @@ -123,7 +124,14 @@ export class CrosswordGrid extends CrosswordQuestion {

if (j === 0) {
const labelEl = squareEl.querySelector('.word-label');
const labelText = 'W' + (words[i]?.no ?? number) + (orientationMarks[words[i].orientation]);
const labelParams = {
number: words[i]?.no ?? number,
orientation: orientationMarks[words[i].orientation],
};
let labelText = labelPlaceholder;
for (let index in labelParams) {
labelText = labelText.replace(`{${index}}`, labelParams[index]);
}
if (!labelEl) {
let spanEl = document.createElement('span');
spanEl.className = 'word-label text-left';
Expand Down
3 changes: 2 additions & 1 deletion edit_crossword_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ protected function add_question_section(MoodleQuickForm $mform): void {
$mform->addElement('button', 'refresh', get_string('preview', 'qtype_crossword'), ['disabled' => 'disabled']);

// Add preview section.
$mform->addElement('html', '<div class="crossword-contain mx-3" id="crossword"></div>');
$mform->addElement('html', '<div class="crossword-contain mx-3" data-label="' .
get_string('wordlabel', 'qtype_crossword') . '" id="crossword"></div>');

// Add answer options.
$mform->addElement('header', 'answeroptionsheader', get_string('answeroptions', 'qtype_crossword'));
Expand Down
3 changes: 2 additions & 1 deletion lang/en/qtype_crossword.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
$string['correctanswer'] = 'Correct answer: {$a}';
$string['down'] = 'Down';
$string['inputlabel'] = '{$a->number} {$a->orientation}. {$a->clue} Answer length {$a->length}';
$string['wordlabel'] = 'W{number}{orientation}';
$string['missingresponse'] = '-';
$string['mustbealphanumeric'] = 'The answer must be alphanumeric characters only';
$string['notenoughwords'] = 'This type of question requires at least {$a} word';
Expand Down Expand Up @@ -66,7 +67,7 @@
$string['words_help'] = 'Please set at least one word and its matching clue, and define its direction and start position. Remember that the words are numbered in the grid according to their order in this section.';
$string['wrongadjacentcharacter'] = 'Two or more consecutive new word breaks detected. Please use a maximum of one between individual words. Note that this does not limit the number of new words in the answer itself.';
$string['wrongintersection'] = 'The letter at the intersection of two words do not match. The word cannot be placed here.';
$string['wrongoverlappingwords'] = 'There cannot be two words startingg in the same place, in the same direction. This clue starts in the same place as "{$a}" above.';
$string['wrongoverlappingwords'] = 'There cannot be two words starting in the same place, in the same direction. This clue starts in the same place as "{$a}" above.';
$string['wrongpositionhyphencharacter'] = 'Please do not add a hyphen before or after the last alphanumeric character.';
$string['wrongpositionspacecharacter'] = 'Please do not add a space before or after the last alphanumeric character.';
$string['yougotnright'] = '{$a->num} of your answers are correct.';
Expand Down
23 changes: 18 additions & 5 deletions tests/question_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,24 @@ public function test_get_num_parts_partial(array $answeroptions): void {
* @covers \qtype_crossword_question::is_full_fraction
* @dataProvider grading_provider
*/
public function is_full_fraction(array $answeroptions): void {
public function test_is_full_fraction(array $answeroptions): void {
$this->resetAfterTest();
$question = \test_question_maker::make_question('crossword', 'not_accept_wrong_accents');
$question->start_attempt(new question_attempt_step(), 1);
$question->accentgradingtype = $answeroptions['options']['accentgradingtype'];
$question->accentpenalty = $answeroptions['options']['accentpenalty'];
foreach ($answeroptions['answers'] as $answerdata) {
$numanswerspartial = $question->is_full_fraction($answerdata);
$this->assertEquals($answer['numpartialanswer'], $numanswerspartial);
foreach ($answeroptions['answers'] as $answers) {
$index = 0;
foreach ($answers['answers'] as $answer) {
$result = $question->is_full_fraction($question->answers[$index], $answer);
$this->assertEquals($answers['expected'][$index], $result);
$index++;
}
}
}

/**
* Data provider for the get_num_parts_right and grading test.
* Data provider for the get_num_parts_right, grading test and is_full_fraction.
*
* @return array
*/
Expand All @@ -281,20 +285,23 @@ public static function grading_provider(): array {
'answers' => [
'Answer is absolutely correct' => [
'answers' => ['sub0' => 'PÂTÉ', 'sub1' => 'TÉLÉPHONE'],
'expected' => [true, true],
'numrightanswer' => 2,
'numpartialanswer' => 0,
'fraction' => 1,
'state' => \question_state::$gradedright,
],
'Answers with incorrect accents' => [
'answers' => ['sub0' => 'PATE', 'sub1' => 'TELEPHONE'],
'expected' => [false, false],
'numrightanswer' => 0,
'numpartialanswer' => 0,
'fraction' => 0,
'state' => \question_state::$gradedwrong,
],
'Answers are wrong' => [
'answers' => ['sub0' => 'PETE', 'sub1' => 'TALAPHONE'],
'expected' => [false, false],
'numrightanswer' => 0,
'numpartialanswer' => 0,
'fraction' => 0,
Expand All @@ -312,20 +319,23 @@ public static function grading_provider(): array {
'answers' => [
'Answer is absolutely correct' => [
'answers' => ['sub0' => 'PÂTÉ', 'sub1' => 'TÉLÉPHONE'],
'expected' => [true, true],
'numrightanswer' => 2,
'numpartialanswer' => 0,
'fraction' => 1,
'state' => \question_state::$gradedright,
],
'Answers with incorrect accents' => [
'answers' => ['sub0' => 'PATE', 'sub1' => 'TELEPHONE'],
'expected' => [false, false],
'numrightanswer' => 0,
'numpartialanswer' => 2,
'fraction' => 0.9,
'state' => \question_state::$gradedpartial,
],
'Answers are wrong' => [
'answers' => ['sub0' => 'PETE', 'sub1' => 'TALAPHONE'],
'expected' => [false, false],
'numrightanswer' => 0,
'numpartialanswer' => 0,
'fraction' => 0,
Expand All @@ -343,20 +353,23 @@ public static function grading_provider(): array {
'answers' => [
'Answer is absolutely correct' => [
'answers' => ['sub0' => 'PÂTÉ', 'sub1' => 'TÉLÉPHONE'],
'expected' => [true, true],
'numrightanswer' => 2,
'numpartialanswer' => 0,
'fraction' => 1,
'state' => \question_state::$gradedright,
],
'Answers with incorrect accents' => [
'answers' => ['sub0' => 'PATE', 'sub1' => 'TELEPHONE'],
'expected' => [true, true],
'numrightanswer' => 2,
'numpartialanswer' => 0,
'fraction' => 1,
'state' => \question_state::$gradedright,
],
'Answers are wrong' => [
'answers' => ['sub0' => 'PETE', 'sub1' => 'TALAPHONE'],
'expected' => [false, false],
'numrightanswer' => 0,
'numpartialanswer' => 0,
'fraction' => 0,
Expand Down

0 comments on commit dbb0d95

Please sign in to comment.