From c3cc23141194f4c0e56d6802f7e9ff082d21c5fb Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Sun, 4 Feb 2018 11:13:51 +0100 Subject: Display formula using Ajax --- ajax.php | 24 ++++++++++++++++++------ displayformula.php | 19 +++++++++++++++---- lang/en/qtype_algebra.php | 2 ++ question.php | 39 ++++++++++++++++++++++++--------------- renderer.php | 28 ++++++---------------------- settings.php | 4 +++- version.php | 4 ++-- 7 files changed, 70 insertions(+), 50 deletions(-) diff --git a/ajax.php b/ajax.php index 156db22..9231012 100644 --- a/ajax.php +++ b/ajax.php @@ -38,18 +38,30 @@ if (!confirm_sesskey()) { try { $vars = explode(',', $vars); if (empty($expr)) { - $texexp = ''; + $texexp = ' '; } else { $exp = $p->parse($expr, $vars); $texexp = $exp->tex(); } } catch (Exception $e) { - $texexp = ''; + $texexp = ' '; } -if ($CFG->qtype_algebra_texdelimiters == 'old') { - $texexp = '$$' . $texexp . '$$'; -} else { - $texexp = '\\[' . $texexp . '\\]'; + +$delimiters = $CFG->qtype_algebra_texdelimiters; +switch($delimiters) { + case 'old': + $texexp = '$$' . $texexp . '$$'; + break; + case 'new': + $texexp = '\\[' . $texexp . '\\]'; + break; + case 'simple'; + $texexp = '$' . $texexp . '$'; + break; + case 'inline': + $texexp = '\\(' . $texexp . '\\)'; + break; } + header('Content-Type: application/json; charset: utf-8'); echo json_encode($texexp); diff --git a/displayformula.php b/displayformula.php index 9693af3..6ba12fb 100644 --- a/displayformula.php +++ b/displayformula.php @@ -48,11 +48,22 @@ try { $texexp = ''; } else { $exp = $p->parse($m[2], $vars); - if ($CFG->qtype_algebra_texdelimiters == 'old') { - $texexp = '$$'.$exp->tex().'$$'; - } else { - $texexp = '\['.$exp->tex().'\]'; + $texexp = $exp->tex(); + switch($CFG->qtype_algebra_texdelimiters) { + case 'old': + $texexp = '$$' . $texexp . '$$'; + break; + case 'new': + $texexp = '\\[' . $texexp . '\\]'; + break; + case 'simple'; + $texexp = '$' . $texexp . '$'; + break; + case 'inline': + $texexp = '\\(' . $texexp . '\\)'; + break; } + } } catch (Exception $e) { $validanswer = false; diff --git a/lang/en/qtype_algebra.php b/lang/en/qtype_algebra.php index 4006858..8c8ed2e 100644 --- a/lang/en/qtype_algebra.php +++ b/lang/en/qtype_algebra.php @@ -142,6 +142,8 @@ $string['uri'] = 'uri of SAGE server'; $string['texdelimiters'] = 'Delimiters for TeX expressions'; $string['dollars'] = '$$...$$'; $string['brackets'] = '\[...\]'; +$string['braces'] = '\\(...\\)'; +$string['dollar'] = '$...$'; $string['invalidanswer'] = 'Invalid or unrecongnized answer'; $string['multiplyoperator'] = 'TeX operator for multiplication'; $string['times'] = '\\times'; diff --git a/question.php b/question.php index 52f8392..dffbeef 100644 --- a/question.php +++ b/question.php @@ -126,14 +126,15 @@ class qtype_algebra_question extends question_graded_by_strategy * @param $expr expression which will be parsed * @return top term of the parse tree or a string if an exception is thrown */ - public function formated_expression($text) { + public function formated_expression($text, $vars = null) { global $CFG; - - // Create an array of variable names for the parser from the question if defined. - $varnames = array(); - if (isset($this->variables)) { - foreach ($this->variables as $var) { - $varnames[] = $var->name; + if ($vars == null) { + // Create an array of variable names for the parser from the question if defined. + $vars = array(); + if (isset($this->variables)) { + foreach ($this->variables as $var) { + $vars[] = $var->name; + } } } // We now assume that we have a string to parse. Create a parser instance to @@ -142,16 +143,24 @@ class qtype_algebra_question extends question_graded_by_strategy // Perform the actual parsing inside a try-catch block so that any exceptions // can be caught and converted into errors. try { - $exp = $p->parse($text, $varnames); - if ($CFG->qtype_algebra_texdelimiters == 'old') { - return '$$'.$exp->tex().'$$'; - } else { - return '\['.$exp->tex().'\]'; - } - + $exp = $p->parse($text, $vars); + $texexp = $exp->tex(); } catch (Exception $e) { - return ''; + $texexp = ' '; } + + $delimiters = $CFG->qtype_algebra_texdelimiters; + switch($delimiters) { + case 'old': + return '$$' . $texexp . '$$'; + case 'new': + return '\\[' . $texexp . '\\]'; + case 'simple'; + return '$' . $texexp . '$'; + case 'inline': + return '\\(' . $texexp . '\\)'; + } + } public function is_same_response(array $prevresponse, array $newresponse) { diff --git a/renderer.php b/renderer.php index ec3d57e..e19ae11 100644 --- a/renderer.php +++ b/renderer.php @@ -76,14 +76,14 @@ class qtype_algebra_renderer extends qtype_renderer { } // Create an array of variable names to use when displaying the function entered. - $varnames = array(); + $vars = array(); if ($question and isset($question->variables)) { $variables = $question->variables; foreach ($question->variables as $var) { - $varnames[] = $var->name; + $vars[] = $var->name; } } - $varnames = implode(',', $varnames); + $varnames = implode(',', $vars); $questiontext = $question->format_questiontext($qa); @@ -142,25 +142,9 @@ class qtype_algebra_renderer extends qtype_renderer { '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); + + $display = $question->format_text("" . $question->formated_expression($currentanswer, $vars) ."", + FORMAT_MOODLE ,$qa, 'question', 'questiontext', $question->id); $result .= html_writer::tag('div', $display ,array( 'type' => 'text', 'name' => $nameprefix . '_display', diff --git a/settings.php b/settings.php index 1c6fa30..485a723 100644 --- a/settings.php +++ b/settings.php @@ -45,7 +45,9 @@ if ($ADMIN->fulltree) { new lang_string('texdelimiters', 'qtype_algebra'), '', 'old', array('old' => new lang_string('dollars', 'qtype_algebra'), - 'new' => new lang_string('brackets', 'qtype_algebra') + 'simple' => new lang_string('dollar', 'qtype_algebra'), + 'new' => new lang_string('brackets', 'qtype_algebra'), + 'inline' => new lang_string('braces', 'qtype_algebra'), ))); // TeX operator for multiplication. $settings->add(new admin_setting_configselect('qtype_algebra/multiplyoperator', diff --git a/version.php b/version.php index 1db1702..a0cbc70 100644 --- a/version.php +++ b/version.php @@ -23,8 +23,8 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_algebra'; -$plugin->version = 2018020100; +$plugin->version = 2018020400; $plugin->requires = 2013050100; -$plugin->release = '1.6 for Moodle 2.8, ... 3.5'; +$plugin->release = '1.7 for Moodle 2.8, ... 3.5'; $plugin->maturity = MATURITY_STABLE; -- cgit v1.2.3