public function test_do_test_2() { $sans = new stack_cas_casstring('sans'); $sans->get_valid('t'); $tans = new stack_cas_casstring('ta'); $tans->get_valid('t'); $node = new stack_potentialresponse_node($sans, $tans, 'Diff', 'x', false); $node->add_branch(0, '=', 0, '', -1, 'Can not diff!', FORMAT_HTML, '1-0-0'); $node->add_branch(1, '=', 1, '', 1, 'Ok, you can diff. ', FORMAT_HTML, '1-0-1'); $potentialresponses[] = $node; $sans = new stack_cas_casstring('sans'); $sans->get_valid('t'); $tans = new stack_cas_casstring('ta'); $tans->get_valid('t'); $node = new stack_potentialresponse_node($sans, $tans, 'FacForm', 'x', true); $node->add_branch(0, '+', 0, '', -1, 'Do not expand!', FORMAT_HTML, '1-1-0'); $node->add_branch(1, '+', 0, '', -1, 'Yeah!', FORMAT_HTML, '1-1-1'); $potentialresponses[] = $node; $tree = new stack_potentialresponse_tree('', '', true, 5, null, $potentialresponses, 0); $seed = 12345; $options = new stack_options(); $questionvars = new stack_cas_keyval('n:3; p:(x+1)^n; ta:diff(p,x);', $options, $seed, 't'); $questionvars->instantiate(); $answers = array('sans' => '3*x^2+6*x+3'); $result = $tree->evaluate_response($questionvars->get_session(), $options, $answers, $seed); $this->assertTrue($result->valid); $this->assertEquals('', $result->errors); $this->assertEquals(1, $result->score); $this->assertEquals(0, $result->penalty); $this->assertEquals(2, count($result->feedback)); $this->assertEquals('Ok, you can diff.', $result->feedback[0]->feedback); $this->assertEquals('Do not expand!', $result->feedback[1]->feedback); $this->assertEquals(array('ATDiff_true.', '1-0-1', 'ATFacForm_notfactored.', '1-1-0'), $result->answernotes); // Now have another attempt at the same PRT! // Need this test to ensure PRT is "reset" and has no hangover data inside the potential resposnes. $answers = array('sans' => '3*(x+1)^2'); $result = $tree->evaluate_response($questionvars->get_session(), $options, $answers, $seed); $this->assertTrue($result->valid); $this->assertEquals('', $result->errors); $this->assertEquals(1, $result->score); $this->assertEquals(0, $result->penalty); $this->assertEquals(2, count($result->feedback)); $this->assertEquals('Ok, you can diff.', $result->feedback[0]->feedback); $this->assertEquals('Yeah!', $result->feedback[1]->feedback); $this->assertEquals(array('ATDiff_true.', '1-0-1', 'ATFacForm_true.', '1-1-1'), $result->answernotes); $this->assertEquals(array('NULL' => 'NULL', '1-0-1' => '1-0-1', '1-0-0' => '1-0-0', '1-1-1' => '1-1-1', '1-1-0' => '1-1-0'), $tree->get_all_answer_notes()); }
/** * Validate all the maxima code in the question. * * This is done last, and separate from the other validation for two reasons: * 1. The rest of the validation is organised to validate the form in order, * to match the way the form is defined. Here we need to validate in the * order that the CAS is evaluated at run-time. * 2. This is the slowest part of validation, so we only do it at the end if * everything else is OK. * * @param array $errors the errors array that validation is assembling. * @param array $fromform the submitted data to validate. * @return array updated $errors array. */ protected function validate_question_cas_code($errors, $fromform, $fixingdollars) { $keyval = new stack_cas_keyval($fromform['questionvariables'], $this->options, $this->seed, 't'); $keyval->instantiate(); $session = $keyval->get_session(); if ($session->get_errors()) { $errors['questionvariables'][] = $session->get_errors(); return $errors; } // Instantiate all text fields and look for errors. $castextfields = array('questiontext', 'specificfeedback', 'generalfeedback'); foreach ($castextfields as $field) { $errors = $this->validate_cas_text($errors, $fromform[$field]['text'], $field, $fixingdollars, clone $session); } $errors = $this->validate_cas_text($errors, $fromform['questionnote'], 'questionnote', $fixingdollars, clone $session); // Make a list of all inputs, instantiate it and then look for errors. $inputs = $this->get_input_names_from_question_text(); $inputvalues = array(); foreach ($inputs as $inputname => $notused) { $cs = new stack_cas_casstring($inputname . ':' . $fromform[$inputname . 'modelans']); $cs->get_valid('t'); $inputvalues[] = $cs; if ($fromform[$inputname . 'options']) { $cs = new stack_cas_casstring('optionsfor' . $inputname . ':' . $fromform[$inputname . 'options']); $cs->get_valid('t'); $inputvalues[] = $cs; } } $inputsession = clone $session; $inputsession->add_vars($inputvalues); $inputsession->instantiate(); foreach ($inputs as $inputname => $notused) { if ($inputsession->get_errors_key($inputname)) { $errors[$inputname . 'modelans'][] = $inputsession->get_errors_key($inputname); // TODO: Send the acutal value to to input, and ask it to validate it. // For example, the matrix input type could check that the model answer is a matrix. } if ($fromform[$inputname . 'options'] && $inputsession->get_errors_key('optionsfor' . $inputname)) { $errors[$inputname . 'options'][] = $inputsession->get_errors_key('optionsfor' . $inputname); } // else TODO: Send the acutal value to the input, and ask it to validate it. } // At this point if we have errors, especially with inputs, there is no point in executing any of the PRTs. if (!empty($errors)) { return $errors; } // TODO: loop over all the PRTs in a similar manner.... // Remember, to use // clone $inputsession // as the base session to have all the teacher's answers instantiated. // Otherwise we are likley to do illigitimate things to the various inputs. return $errors; }
public function test_remove_comment_fail() { $at1 = new stack_cas_keyval("a:1\n /* This is a comment \n b:2\n */\n c:3", null, 123, 's', true, 0); $this->assertTrue($at1->get_valid()); $a3 = array('a:1', 'c:4'); $s3 = array(); foreach ($a3 as $s) { $s3[] = new stack_cas_casstring($s); } $cs3 = new stack_cas_session($s3, null, 123); $cs3->instantiate(); $at1->instantiate(); // This looks strange, but the cache layer gives inconsistent results if the first // of these populates the cache, and the second one uses it. $this->assertNotEquals($cs3->get_session(), $at1->get_session()->get_session()); }