From 8ad4a477c15f928323da24aff4188bd7f0dca1d9 Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Fri, 2 Feb 2018 16:53:59 +0100 Subject: Ajax dynamic formula display --- renderer.php | 97 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 27 deletions(-) (limited to 'renderer.php') diff --git a/renderer.php b/renderer.php index 529fb0e..ec3d57e 100644 --- a/renderer.php +++ b/renderer.php @@ -38,6 +38,9 @@ class qtype_algebra_renderer extends qtype_renderer { question_display_options $options) { global $CFG; + + $this->page->requires->js_call_amd('qtype_algebra/display', 'init'); + $question = $qa->get_question(); $currentanswer = $qa->get_last_qt_var('answer'); @@ -50,6 +53,7 @@ class qtype_algebra_renderer extends qtype_renderer { 'name' => $inputname, 'value' => $currentanswer, 'id' => $inputname, + 'class' => 'algebra_answer', 'size' => 80, ); @@ -65,13 +69,12 @@ class qtype_algebra_renderer extends qtype_renderer { } else { $fraction = 0; } - $inputattributes['class'] = $this->feedback_class($fraction); + $inputattributes['class'] = $this->feedback_class($fraction). ' algebra_answer'; $feedbackimg = $this->feedback_image($fraction); + } else { + $inputattributes['class'] = 'algebra_answer'; } - - $iframename = $nameprefix.'_if'; - // Name of the javascript function which causes the entered formula to be rendered. - $dfname = $nameprefix.'_display'; + // Create an array of variable names to use when displaying the function entered. $varnames = array(); if ($question and isset($question->variables)) { @@ -80,26 +83,14 @@ class qtype_algebra_renderer extends qtype_renderer { $varnames[] = $var->name; } } - $varnames = implode(',', $varnames); - // Javascript function which the button uses to display the rendering - // This function sents the source of the iframe to the 'displayformula.php' script giving - // it an argument of the formula entered by the student. - $displayfunction = 'function '.$dfname."() {\n". - ' var text="vars='.$varnames.'&expr="+escape(document.getElementsByName("'.$inputname.'")[0].value);'."\n". - " if(text.length != 0) {\n". - ' document.getElementsByName("'.$iframename.'")[0].src="'. - $CFG->wwwroot.'/question/type/algebra/displayformula.php?"+'. - 'text.replace(/\+/g,"%2b")'."\n". - " }\n". - " }\n"; $questiontext = $question->format_questiontext($qa); $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg; $result = html_writer::tag('div', $questiontext, array('class' => 'qtext')); - $result .= html_writer::tag('script', $displayfunction, array('type' => 'text/javascript')); + $result .= html_writer::start_tag('div', array('class' => 'ablock')); $result .= html_writer::start_tag('div', array('class' => 'prompt', 'style' => 'vertical-align: top')); if (isset($question->answerprefix) and !empty($question->answerprefix)) { @@ -118,15 +109,67 @@ class qtype_algebra_renderer extends qtype_renderer { $question->get_validation_error(array('answer' => $currentanswer)), array('class' => 'validationerror')); } - $result .= html_writer::start_tag('div', array('class' => 'dispresponse')); - $result .= html_writer::empty_tag('input', array('type' => 'button', - 'value' => get_string('displayresponse', 'qtype_algebra'), 'onclick' => $dfname.'()')); - $result .= html_writer::start_tag('iframe', - array('name' => $iframename, 'width' => '60%', 'height' => 60, 'align' => 'middle', 'src' => '')); - $result .= html_writer::end_tag('iframe'); - $result .= html_writer::tag('script', $dfname.'();', array('type' => 'text/javascript')); - $result .= html_writer::end_tag('div'); - + if (get_config('qtype_algebra', 'formuladisplay') == 'iframe') { + // Javascript function which the button uses to display the rendering + // This function sents the source of the iframe to the 'displayformula.php' script giving + // it an argument of the formula entered by the student. + $iframename = $nameprefix.'_if'; + // Name of the javascript function which causes the entered formula to be rendered. + $dfname = $nameprefix.'_display'; + $displayfunction = 'function '.$dfname."() {\n". + ' var text="vars='.$varnames.'&expr="+escape(document.getElementsByName("'.$inputname.'")[0].value);'."\n". + " if(text.length != 0) {\n". + ' document.getElementsByName("'.$iframename.'")[0].src="'. + $CFG->wwwroot.'/question/type/algebra/displayformula.php?"+'. + 'text.replace(/\+/g,"%2b")'."\n". + " }\n". + " }\n"; + $result .= html_writer::tag('script', $displayfunction, array('type' => 'text/javascript')); + $result .= html_writer::start_tag('div', array('class' => 'dispresponse')); + $result .= html_writer::empty_tag('input', array('type' => 'button', + 'value' => get_string('displayresponse', 'qtype_algebra'), 'onclick' => $dfname.'()')); + $result .= html_writer::start_tag('iframe', + array('name' => $iframename, 'width' => '60%', 'height' => 60, 'align' => 'middle', 'src' => '')); + $result .= html_writer::end_tag('iframe'); + $result .= html_writer::tag('script', $dfname.'();', array('type' => 'text/javascript')); + $result .= html_writer::end_tag('div'); + } else { + $result .= html_writer::tag('div', $varnames ,array( + 'type' => 'text', + 'name' => $nameprefix . '_vars', + 'id' => $nameprefix . '_vars', + 'size' => 80, + 'style' => 'display:none', + ) + ); + $p = new qtype_algebra_parser; + try { + $vars = explode(',', $varnames); + if (empty($currentanswer)) { + $texexp = ''; + } else { + $exp = $p->parse($currentanswer, $vars); + $texexp = $exp->tex(); + } + } catch (Exception $e) { + $texexp = ''; + } + if ($CFG->qtype_algebra_texdelimiters == 'old') { + $texexp = '$$' . $texexp . '$$'; + } else { + $texexp = '\\[' . $texexp . '\\]'; + } + $display = $question->format_text("" . $texexp ."", + 1 ,$qa, 'question', 'questiontext', $question->id); + $result .= html_writer::tag('div', $display ,array( + 'type' => 'text', + 'name' => $nameprefix . '_display', + 'id' => $nameprefix. '_display', + 'size' => 80, + 'class' => 'displayformula', + ) + ); + } return $result; } -- cgit v1.2.3