/** * This is the basic validation of the student's "answer". * This method is only called if the input is not blank. * * Only a few input methods need to modify this method. * For example, Matrix types have two dimensional contents arrays to loop over. * * @param array $contents the content array of the student's input. * @return array of the validity, errors strings and modified contents. */ protected function validate_contents($contents, $forbiddenkeys) { $errors = $this->extra_validation($contents); $valid = !$errors; // Now validate the input as CAS code. $modifiedcontents = array(); $allowwords = $this->get_parameter('allowWords', ''); foreach ($contents as $val) { $answer = new stack_cas_casstring($val); $answer->get_valid('s', $this->get_parameter('strictSyntax', true), $this->get_parameter('insertStars', 0), $allowwords); // Ensure student hasn't used a variable name used by the teacher. if ($forbiddenkeys) { $answer->check_external_forbidden_words($forbiddenkeys); } $forbiddenwords = $this->get_parameter('forbidWords', ''); if ($forbiddenwords) { $answer->check_external_forbidden_words_literal($forbiddenwords); } $modifiedcontents[] = $answer->get_casstring(); $valid = $valid && $answer->get_valid(); $errors .= $answer->get_errors(); } return array($valid, $errors, $modifiedcontents); }
public function test_implied_complex_mult3() { // This function name ends in an "i", so we need to check *s are not being inserted too many times here. $s = 'sa:cdf_bernoulli(x,p)'; $at1 = new stack_cas_casstring($s); $this->assertTrue($at1->get_valid('s', false, 1)); $this->assertEquals('cdf_bernoulli(x,p)', $at1->get_casstring()); }
public static function run_test($test) { // Note: What we would really like to do is the following. // $el = stack_input_factory::make('algebraic', 'sans1', 'x'); // $el->set_parameter('insertStars', 1); // $el->set_parameter('strictSyntax', false); // $el->set_parameter('sameType', false); // $cs = $el->validate_student_response($test->rawstring); // However, we want to pull apart the bits to expose where the various errors occur. $cs = new stack_cas_casstring($test->rawstring); $cs->get_valid('s', false, 1); $cs->set_cas_validation_casstring('sans1', true, true, false, null); $phpvalid = $cs->get_valid(); if ($phpvalid) { // Trim off stack_validate_typeless([..], true, true). $phpcasstring = $cs->get_casstring(); $phpcasstring = substr($phpcasstring, 25); $phpcasstring = substr($phpcasstring, 0, strlen($phpcasstring) - 12); $outputphpcasstring = $phpcasstring; } else { $phpcasstring = ''; $outputphpcasstring = 'N/A...'; } $errors = $cs->get_errors(); $passed = true; if ('php_true' === $test->phpvalid) { $expected = true; } else { $expected = false; } if ($phpvalid != $expected) { $passed = false; $errors .= ' ' . stack_string('phpvalidatemismatch'); } if ($phpvalid && $phpcasstring != $test->phpcasstring) { $passed = false; $errors .= ' ' . stack_maxima_format_casstring($phpcasstring) . ' \\(\\neq \\) ' . stack_maxima_format_casstring($test->phpcasstring); } $casvalid = ''; $caserrors = ''; $casvalue = ''; $casdisplay = ''; if ($cs->get_valid()) { $options = new stack_options(); $options->set_option('simplify', false); $session = new stack_cas_session(array($cs), $options, 0); $session->instantiate(); $session = $session->get_session(); $cs = $session[0]; $caserrors = stack_maxima_translate($cs->get_errors()); $casvalue = stack_maxima_format_casstring($cs->get_value()); if ('cas_true' == $test->casvalid) { $casexpected = true; } else { $casexpected = false; } if ('' == $cs->get_value()) { $casvalid = false; } else { $casvalid = true; } if ($casexpected != $casvalid) { $passed = false; $caserrors .= ' ' . stack_string('casvalidatemismatch'); } $casdisplay = $cs->get_display(); } $answernote = $cs->get_answernote(); if ($answernote != $test->ansnotes) { $passed = false; $errors .= ' ' . stack_string('ansnotemismatch'); } return array($passed, $phpvalid, $phpcasstring, $errors, $casvalid, $caserrors, $casdisplay, $casvalue, $answernote); }