Skip to content
Snippets Groups Projects
Commit 5cd8c6c3 authored by Tim Hunt's avatar Tim Hunt
Browse files

Implement maths display options when outputting CAS text.

parent e73049bf
Branches
No related tags found
No related merge requests found
......@@ -88,7 +88,7 @@ echo html_writer::tag('p', stack_string('chatintro'));
if (!$varerrs) {
if ($string) {
echo $OUTPUT->box(format_text($displaytext));
echo $OUTPUT->box(stack_ouput_castext($displaytext));
}
}
......
......@@ -140,7 +140,7 @@ function output_cas_text($title, $intro, $castext) {
$ct = new stack_cas_text($castext, null, 0);
echo html_writer::tag('p', format_text($ct->get_display_castext()));
echo html_writer::tag('p', stack_ouput_castext($ct->get_display_castext()));
echo output_debug(stack_string('errors'), $ct->get_errors());
echo output_debug(stack_string('debuginfo'), $ct->get_debuginfo());
}
......
......@@ -27,6 +27,10 @@ class stack_exception extends moodle_exception {
}
}
function stack_ouput_castext($castext) {
return format_text(stack_maths::pre_process_user_input($castext));
}
function stack_string($key, $a = null) {
return stack_maths::process_lang_string(get_string($key, 'qtype_stack', $a));
}
......
......@@ -310,7 +310,8 @@ class qtype_stack_question extends question_graded_automatically_with_countback
}
return parent::format_hint(new question_hint($hint->id,
$hinttext->get_display_castext(), $hint->hintformat), $qa);
stack_maths::process_display_castext($hinttext->get_display_castext()),
$hint->hintformat), $qa);
}
public function format_generalfeedback($qa) {
......@@ -324,8 +325,8 @@ class qtype_stack_question extends question_graded_automatically_with_countback
throw new stack_exception('Error rendering the general feedback text: ' . $gftext->get_errors());
}
return $this->format_text($gftext->get_display_castext(), $this->generalfeedbackformat,
$qa, 'question', 'generalfeedback', $this->id);
return $this->format_text(stack_maths::pre_process_user_input($gftext->get_display_castext()),
$this->generalfeedbackformat, $qa, 'question', 'generalfeedback', $this->id);
}
public function get_expected_data() {
......
......@@ -160,7 +160,7 @@ if (!$question->has_random_variants()) {
}
$notestable->data[] = array(
$choice,
$qn->get_question_summary(),
stack_ouput_castext($qn->get_question_summary()),
);
}
......@@ -222,7 +222,8 @@ echo $quba->render_question($slot, $options);
// Display the question note.
echo $OUTPUT->heading(stack_string('questionnote'), 3);
echo html_writer::tag('p', $question->get_question_summary(), array('class' => 'questionnote'));
echo html_writer::tag('p', stack_ouput_castext($question->get_question_summary()),
array('class' => 'questionnote'));
// Display the question variables.
echo $OUTPUT->heading(stack_string('questionvariables'), 3);
......
......@@ -41,6 +41,7 @@ class qtype_stack_renderer extends qtype_renderer {
$response = $qa->get_last_qt_data();
$questiontext = $qa->get_last_qt_var('_questiontext');
$questiontext = stack_maths::pre_process_user_input($questiontext);
// Replace inputs.
foreach ($question->inputs as $name => $input) {
......@@ -77,7 +78,7 @@ class qtype_stack_renderer extends qtype_renderer {
$feedback = html_writer::nonempty_tag('div', $result->errors,
array('class' => 'stackprtfeedback stackprtfeedback-' . $name));
}
$questiontext = str_replace("[[feedback:{$index}]]", $feedback, $questiontext);
$questiontext = str_replace("[[feedback:{$index}]]", stack_maths::pre_process_user_input($feedback), $questiontext);
}
$result = '';
......@@ -206,7 +207,8 @@ class qtype_stack_renderer extends qtype_renderer {
foreach ($question->prts as $index => $prt) {
$feedback = $this->prt_feedback($index, $response, $qa, $options);
$allempty = $allempty && !$feedback;
$feedbacktext = str_replace("[[feedback:{$index}]]", $feedback, $feedbacktext);
$feedbacktext = str_replace("[[feedback:{$index}]]",
stack_maths::pre_process_user_input($feedback), $feedbacktext);
}
if ($allempty) {
......@@ -316,7 +318,8 @@ class qtype_stack_renderer extends qtype_renderer {
$field = 'prt' . $class;
$format = 'prt' . $class . 'format';
if ($question->$field) {
return html_writer::tag('div', $question->format_text($question->$field,
return html_writer::tag('div', $question->format_text(
stack_maths::pre_process_user_input($question->$field),
$question->$format, $qa, 'qtype_stack', $field, $question->id), array('class' => $class));
}
return '';
......
......@@ -273,12 +273,11 @@ class stack_cas_text {
* This function actually evaluates the castext.
*/
private function instantiate() {
// Until we think more about math embedding (see Future plans) force LaTeX.
$displaymethod = 'LaTeX';
if (!$this->valid) {
return false;
}
// Deal with castext without any CAS variables.
if (null !== $this->session) {
$this->session->instantiate();
......@@ -297,15 +296,6 @@ class stack_cas_text {
}
$this->trimmedcastext = $strin;
if ('MathML' === $displaymethod) {
if (null !== $this->session) {
$this->castext = $this->session->get_display_castext($this->castext);
}
$this->strin = str_replace('\(@', '@', $this->strin); // Mathml doesn't need to be displayed in math mode.
$this->strin = str_replace('@\)', '@', $this->strin);
} else {
// Assume STACK returns raw LaTeX for subsequent processing, e.g. with MathJax.
$this->castext = stack_utils::wrap_around($this->trimmedcastext);
if (null !== $this->session) {
$this->castext = $this->session->get_display_castext($this->castext);
......@@ -315,7 +305,7 @@ class stack_cas_text {
// Bug occurs when maxima returns <html>tags in output, eg plots or div by 0 errors.
$this->castext = str_replace('</html>\)', '', $this->castext);
$this->latex_tidy();
}
$this->instantiated = true;
}
......
......@@ -109,7 +109,8 @@ foreach ($tests as $test) {
'error' => $error,
'casvalid' => s($casvalid),
'casvalue' => $casvalue,
'casdisplay' => s($display) . html_writer::tag('pre', s($casdisplay)),
'casdisplay' => stack_maths::process_lang_string(s($display)) .
html_writer::tag('pre', s($casdisplay)),
'caserrors' => $caserrors,
);
$table->add_data_keyed($row, $class);
......
......@@ -158,6 +158,25 @@ YUI.add('moodle-qtype_stack-input', function(Y) {
this.show_validation_results();
}
/**
* Some browsers cannot execute JavaScript just by inserting script tags.
* To avoid that problem, remove all script tags from the given content,
* and run them later.
* @param
* @param html HTML content
* @return new text with JS removed
*/
stack_input.prototype.extract_scripts = function(html, scriptcommands) {
var scriptregexp = /<script[^>]*>([\s\S]*?)<\/script>/g;
while ((result = scriptregexp.exec(html)) != null) {
scriptcommands.push(result[1]);
}
return html.replace(scriptregexp, '');
}
/**
* Update the validation div to show the results of the validation.
* @param e the data that came back from the ajax validation call.
......@@ -171,14 +190,25 @@ YUI.add('moodle-qtype_stack-input', function(Y) {
var results = this.validationresults[val];
this.lastvalidatedvalue = val;
this.validationdiv.setContent(results.message);
var scriptcommands = [];
var html = this.extract_scripts(results.message, scriptcommands);
this.validationdiv.setContent(html);
// Run script commands.
for (var i=0; i<scriptcommands.length; i++) {
eval(scriptcommands[i]);
}
this.remove_all_classes();
if (!results.message) {
this.validationdiv.addClass('empty');
}
if (typeof MathJax != 'undefined') {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.validationdiv.getDOMNode()]);
}
return true;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment