diff options
| -rw-r--r-- | displayformula.php | 12 | ||||
| -rw-r--r-- | lang/en/qtype_algebra.php | 6 | ||||
| -rw-r--r-- | parser.php | 42 | ||||
| -rw-r--r-- | pix/icon.svg | 56 | ||||
| -rw-r--r-- | settings.php | 7 | ||||
| -rw-r--r-- | tests/behat/preview.feature | 4 | ||||
| -rw-r--r-- | tests/parser_test.php | 6 | ||||
| -rw-r--r-- | version.php | 2 | 
8 files changed, 104 insertions, 31 deletions
| 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'); +}  ?>  <html>      <head> 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 @@ -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 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<svg +   xmlns:dc="http://purl.org/dc/elements/1.1/" +   xmlns:cc="http://creativecommons.org/ns#" +   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" +   xmlns:svg="http://www.w3.org/2000/svg" +   xmlns="http://www.w3.org/2000/svg" +   version="1.1" +   id="svg2" +   preserveAspectRatio="xMinYMid meet" +   height="16" +   width="16" +   style="isolation:isolate"> +  <metadata +     id="metadata17"> +    <rdf:RDF> +      <cc:Work +         rdf:about=""> +        <dc:format>image/svg+xml</dc:format> +        <dc:type +           rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> +        <dc:title></dc:title> +      </cc:Work> +    </rdf:RDF> +  </metadata> +  <defs +     id="defs4"> +    <clipPath +       id="a"> +      <path +         id="path7" +         d="M0 0h16v16H0z" /> +    </clipPath> +  </defs> +  <g +     transform="translate(0,2.9491527)" +     id="g9" +     clip-path="url(#a)"> +    <path +       id="path11" +       d="m 16,6 0,-1 -16,0 0,6 16,0 0,-1 -15,0 0,-4 14,0 0,4 1,0 0,-4 z" /> +    <path +       style="fill:#ffffff" +       id="path13" +       d="M 1,6 15,6 15,10 1,10 1,6 Z" /> +  </g> +  <text +     id="text3345" +     y="5.6271191" +     x="-3.2833064e-008" +     style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:7.5px;line-height:125%;font-family:Sans;-inkscape-font-specification:'Sans, Bold';text-align:start;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" +     xml:space="preserve"><tspan +       y="5.6271191" +       x="-3.2833064e-008" +       id="tspan3347">x=y</tspan></text> +</svg> 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'; | 
