Skip to content

Commit

Permalink
Change the "teacher's answer" output of strings to remove quotes and …
Browse files Browse the repository at this point in the history
…maths environments.

Needed for Parson block output, but a general improvement.
  • Loading branch information
sangwinc committed Nov 6, 2023
1 parent b9f6357 commit 41e2e54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions stack/input/string/string.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function get_teacher_answer_display($value, $display) {
return '';
}

$value = stack_utils::maxima_string_to_php_string($value);
return stack_string('teacheranswershow', array('value' => '<code>'.$value.'</code>', 'display' => $display));
$display = stack_utils::maxima_string_strip_mbox($display);
return stack_string('teacheranswershow_disp', array('display' => $display));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions stack/utils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ public static function php_string_to_maxima_string($string) {
$converted = str_replace("\"", "\\\"", $converted);
return '"' . $converted . '"';
}

/**
* Converts a PHP string object containing a Maxima string as presented by the grind command to a PHP string object.
* @param a string that contains ""-quotes around the content.
Expand All @@ -792,6 +793,22 @@ public static function maxima_string_to_php_string($string) {
return substr($converted, 1, -1);
}

/**
* Remove redundant "mbox" environments from Latex equations strings containing just strings.
* @param a string that contains ""-quotes around the content.
* @return a string without those quotes.
*/
public static function maxima_string_strip_mbox($string) {
$converted = trim($string);
if (substr($converted, 0, 2) == '\(' || substr($converted, 0, 2) == '\[') {
$converted = substr($converted, 2, -2);
}
if (substr(trim($converted), 0, 6) == '\mbox{') {
return substr(trim($converted), 6, -1);
}
return $string;
}

/**
* Translate some strings from Maxima.
* @param string $string
Expand Down
8 changes: 2 additions & 6 deletions tests/input_string_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function test_render_hello_world() {
.'style="width: 13.6em" autocapitalize="none" spellcheck="false" class="maxima-string" value="0" />',
$el->render(new stack_input_state(stack_input::VALID, array('0'), '', '', '', '', ''),
'stack1__ans1', false, null));
$this->assertEquals('The answer <span class="filter_mathjaxloader_equation"><span class="nolink">' .
'\( \\mbox{Hello world} \)</span></span>, which can be typed as <code>Hello world</code>, ' .
'would be correct.',
$this->assertEquals('The answer Hello world would be correct.',
$el->get_teacher_answer_display('"Hello world"', '\\mbox{Hello world}'));
}

Expand All @@ -72,9 +70,7 @@ public function test_validate_string_input() {
$this->assertEquals(stack_input::VALID, $state->status);
$this->assertEquals('"Hello world"', $state->contentsmodified);
$this->assertEquals('\[ \mbox{Hello world} \]', $state->contentsdisplayed);
$this->assertEquals('The answer <span class="filter_mathjaxloader_equation">' .
'<span class="nolink">\[ \[ \mbox{Hello world} \]</span></span> \), ' .
'which can be typed as <code>Hello world</code>, would be correct.',
$this->assertEquals('The answer Hello world would be correct.',
$el->get_teacher_answer_display($state->contentsmodified, $state->contentsdisplayed));
}

Expand Down

0 comments on commit 41e2e54

Please sign in to comment.