From cd5af0a39cf71fd9c53886badf3ea0384d51fa22 Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Sat, 27 Jan 2018 10:35:50 +0100 Subject: svg icon, multiply setting --- displayformula.php | 12 ++++++++-- lang/en/qtype_algebra.php | 6 ++++- parser.php | 42 ++++++++++++++-------------------- pix/icon.svg | 56 +++++++++++++++++++++++++++++++++++++++++++++ settings.php | 7 ++++++ tests/behat/preview.feature | 4 ++-- tests/parser_test.php | 6 +++++ version.php | 2 +- 8 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 pix/icon.svg diff --git a/displayformula.php b/displayformula.php index 8c4cf8c..09f4cff 100644 --- a/displayformula.php +++ b/displayformula.php @@ -26,13 +26,16 @@ * the mathJax filter to be present. */ + require_once('../../../config.php'); require_once("$CFG->dirroot/question/type/algebra/parser.php"); -global $PAGE, $CFG; +global $PAGE; require_login(); $p = new qtype_algebra_parser; +$validanswer = true; + try { $query = urldecode($_SERVER['QUERY_STRING']); $m = array(); @@ -52,12 +55,17 @@ try { } } } catch (Exception $e) { + $validanswer = false; $texexp = get_string('parseerror', 'qtype_algebra', $e->getMessage()); } $formatoptions = new stdClass; $formatoptions->para = false; $PAGE->set_context(context_system::instance()); -$text = format_text($texexp, FORMAT_MOODLE, $formatoptions); +if ($validanswer) { + $text = format_text($texexp, FORMAT_MOODLE, $formatoptions); +} else { + $text = get_string('invalidanswer', 'qtype_algebra'); +} ?> diff --git a/lang/en/qtype_algebra.php b/lang/en/qtype_algebra.php index 7760584..26b6fe1 100644 --- a/lang/en/qtype_algebra.php +++ b/lang/en/qtype_algebra.php @@ -140,4 +140,8 @@ $string['port'] = 'Port of SAGE server'; $string['uri'] = 'uri of SAGE server'; $string['texdelimiters'] = 'Delimiters for TeX expressions'; $string['dollars'] = '$$...$$'; -$string['brackets'] = '\[...\]'; \ No newline at end of file +$string['brackets'] = '\[...\]'; +$string['invalidanswer'] = 'Invalid or unrecongnized answer'; +$string['multiplyoperator'] = 'TeX operator for multiplication'; +$string['times'] = '\\times'; +$string['cdot'] = '\\cdot'; \ No newline at end of file diff --git a/parser.php b/parser.php index 766797a..6068b25 100644 --- a/parser.php +++ b/parser.php @@ -21,20 +21,9 @@ defined('MOODLE_INTERNAL') || die(); -// From the PHP manual: check for the existance of lcfirst and -// if not found create one. -if (!function_exists('lcfirst')) { - /** - * Make a string's first character lowercase - * - * @param string $str - * @return string the resulting string. - */ - function lcfirst( $str ) { - $str[0] = strtolower($str[0]); - return (string)$str; - } -} +require_once(__DIR__.'/../../../config.php'); + +require_login(); /** * Helper function which will compare two strings using their length only. @@ -42,20 +31,22 @@ if (!function_exists('lcfirst')) { * This function is intended for use in sorting arrays of strings by their string * length. This is used to order arrays for regular expressions so that the longest * expressions are checked first. + * In this version, if both strings have equal length, string order is used. So this + * version of the sort is stable. * * @param $a first string to compare * @param $b second string to compare - * @return -1 if $a is longer than $b, 0 if they are the same length and +1 if $a is shorter + * @return -1 if $a is longer than $b, and +1 if $a is shorter */ function qtype_algebra_parser_strlen_sort($a, $b) { // Get the two string lengths once so we don't have to repeat the function call. $alen = strlen($a); $blen = strlen($b); - // If the two lengths are equal return zero. + // If the two lengths are equal use strings order. if ($alen == $blen) { - return 0; + return ($a > $b) ? -1 : +1; } - // Otherwise return +1 if a > b or -1 if a < b. + // Otherwise return +1 if a is shorter or -1 if longer. return ($alen > $blen) ? -1 : +1; } @@ -73,6 +64,7 @@ class qtype_algebra_parser_term { public $_arguments = array(); // Array of arguments in class form. public $_formats; // Array of format strings. public $_nargs; // Number of arguments for this term. + /** * Constructor for the generic parser term. * @@ -434,7 +426,8 @@ class qtype_algebra_parser_term { * throw an exception in such cases. */ class qtype_algebra_parser_nullterm extends qtype_algebra_parser_term { - + /** @var The TeX multiply operator. */ + public $id; /** * Constructs an instance of a null term. * @@ -505,7 +498,7 @@ class qtype_algebra_parser_number extends qtype_algebra_parser_term { $this->_base = $m[1]; $this->_exp = $m[2]; $eformats = array('str' => '%sE%s', - 'tex' => '%s \\times 10^{%s}'); + 'tex' => '%s \\' . get_config('qtype_algebra', 'multiplyoperator') . '10^{%s}'); parent::__construct(self::NARGS, $eformats, $text); } else { $this->_base = $text; @@ -654,7 +647,7 @@ class qtype_algebra_parser_variable extends qtype_algebra_parser_term { // Extract the remaining characters for use as the subscript. $this->_subscript = substr($text, strlen($m[1])); // If the first letter of the subscript is an underscore then remove it. - if ($this->_subscript[0] == '_') { + if (strlen($this->_subscript) != 0 && $this->_subscript[0] == '_') { $this->_subscript = substr($this->_subscript, 1); } // Call the base class constructor with the variable text set to the combination of the @@ -879,7 +872,7 @@ class qtype_algebra_parser_multiply extends qtype_algebra_parser_term { */ public function __construct($text) { $this->mformats = array('*' => array('str' => '%s*%s', - 'tex' => '%s \\times %s'), + 'tex' => '%s \\' . get_config('qtype_algebra', 'multiplyoperator') . ' %s'), '.' => array('str' => '%s %s', 'tex' => '%s %s', 'sage' => '%s*%s') @@ -900,7 +893,7 @@ class qtype_algebra_parser_multiply extends qtype_algebra_parser_term { parent::set_arguments($args); // Set the default explicit format. $this->_formats = $this->mformats['*']; - // Only allow the implicit multipication if the second argument is either a + // Only allow the implicit multiplication if the second argument is either a // special, variable, function or bracket and not negative. In all other cases the operator must be // explicitly written. if (is_a($args[1], 'qtype_algebra_parser_bracket') or @@ -918,7 +911,7 @@ class qtype_algebra_parser_multiply extends qtype_algebra_parser_term { // power terms are parsed before multiplication ones and are required to // have two arguments. $powargs = $args[1]->arguments(); - // Allow the implicit multipication if the power's first argument is either a + // Allow the implicit multiplication if the power's first argument is either a // special, variable, function or bracket and not negative. if (is_a($powargs[0], 'qtype_algebra_parser_bracket') or is_a($powargs[0], 'qtype_algebra_parser_variable') or @@ -1728,4 +1721,3 @@ class qtype_algebra_parser { // Sort static arrays once here by inverse string length. usort(qtype_algebra_parser_variable::$greek, 'qtype_algebra_parser_strlen_sort'); usort(qtype_algebra_parser::$functions, 'qtype_algebra_parser_strlen_sort'); - diff --git a/pix/icon.svg b/pix/icon.svg new file mode 100644 index 0000000..7f4ba9e --- /dev/null +++ b/pix/icon.svg @@ -0,0 +1,56 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + x=y + diff --git a/settings.php b/settings.php index 399c04c..525324a 100644 --- a/settings.php +++ b/settings.php @@ -47,4 +47,11 @@ if ($ADMIN->fulltree) { array('old' => new lang_string('dollars', 'qtype_algebra'), 'new' => new lang_string('brackets', 'qtype_algebra') ))); + // TeX operator for multiplication. + $settings->add(new admin_setting_configselect('qtype_algebra/multiplyoperator', + new lang_string('multiplyoperator', 'qtype_algebra'), + '', 'times', + array('times' => new lang_string('times', 'qtype_algebra'), + 'cdot' => new lang_string('cdot', 'qtype_algebra') + ))); } diff --git a/tests/behat/preview.feature b/tests/behat/preview.feature index 24f53ab..fec1b29 100644 --- a/tests/behat/preview.feature +++ b/tests/behat/preview.feature @@ -1,7 +1,7 @@ @qtype @qtype_algebra -Feature: Preview a Short answer question +Feature: Preview an Algebra question As a teacher - In order to check my Short answer questions will work for students + In order to check my Algebra questions will work for students I need to preview them Background: diff --git a/tests/parser_test.php b/tests/parser_test.php index 5d5eaad..309984c 100644 --- a/tests/parser_test.php +++ b/tests/parser_test.php @@ -26,6 +26,8 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; +require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); +require_once($CFG->dirroot . '/question/type/algebra/tests/helper.php'); require_once($CFG->dirroot . '/question/type/algebra/parser.php'); @@ -61,5 +63,9 @@ class qtype_algebra_parser_test extends advanced_testcase { $this->assertEquals('\sin \left( 4 x_{} \right) + \cos \left( 5 y_{} \right)', $expr->tex()); $expr = $p->parse('sin(6*x) + cos(7*y)'); $this->assertEquals('\sin \left( 6 x_{} \right) + \cos \left( 7 y_{} \right)', $expr->tex()); + $expr = $p->parse('3x y'); + $this->assertEquals('3 x_{} y_{}', $expr->tex()); + $expr = $p->parse('x*y*3'); + $this->assertEquals('x_{} y_{} \times 3 ', $expr->tex()); } } diff --git a/version.php b/version.php index aac10d7..7eb6e41 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_algebra'; -$plugin->version = 2017121900; +$plugin->version = 2018010400; $plugin->requires = 2013050100; $plugin->release = '1.6 for Moodle 2.8, ... 3.5'; -- cgit v1.2.3