Skip to content

Commit

Permalink
CASText now checks before creating new names for variables, and when …
Browse files Browse the repository at this point in the history
…creating questions only some of the variables are really stored in the session.
  • Loading branch information
Chris Sangwin authored and Chris Sangwin committed Mar 31, 2012
1 parent c0ca5b0 commit ecb6ce2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
6 changes: 6 additions & 0 deletions doc/en/Students/Answer_input.md
Expand Up @@ -78,6 +78,12 @@ If you type `a+b/c+d`, then STACK will think that you mean

\[a+\frac{b}{c}+d.\]

Think carefully about the expression `a/b/c`. What do you think this means? There are two options

\[\frac{a}{b}\cdot\frac{1}{c} = \frac{a}{bc}\quad\mbox{or}\quad\frac{a}{\frac{b}{c}}=\frac{ac}{b}.\]

Maxima interprets this as $\frac{a}{bc}$. If in doubt use brackets.

Note that in this context you should always use ordinary round bracket (like (a+b)), not square or curly ones (like [a+b] or {a+b}).

* `{a+b}` means a set,
Expand Down
26 changes: 15 additions & 11 deletions question.php
Expand Up @@ -195,42 +195,46 @@ public function start_attempt(question_attempt_step $step, $variant) {
$response[$name] = $cs;
}
$session->add_vars($response);

// Now instantiate the session:
$session->instantiate();
$this->session = clone $session;
if ($session->get_errors()) {
throw new Exception('qtype_stack_question : CAS error when instantiating the session: ' .
$session->get_errors());
}
$session_length = count($session->get_session());

// 3. CAS bits inside the question text.
$questiontext = new stack_cas_text($this->questiontext, clone $session, $this->seed, 't', false, true);
$questiontext = new stack_cas_text($this->questiontext, $session, $this->seed, 't', false, true);
if ($questiontext->get_errors()) {
throw new Exception('qtype_stack_question : Error in the the question text: ' .
$questiontext->get_errors());
}

// 4. CAS bits inside the specific feedback.
$feedbacktext = new stack_cas_text($this->specificfeedback, clone $session, $this->seed, 't', false, true);
$feedbacktext = new stack_cas_text($this->specificfeedback, $session, $this->seed, 't', false, true);
if ($questiontext->get_errors()) {
throw new Exception('qtype_stack_question : Error in the feedback text: ' .
$feedbacktext->get_errors());
}

// 5. CAS bits inside the question note.
$notetext = new stack_cas_text($this->questionnote, clone $session, $this->seed, 't', false, true);
$notetext = new stack_cas_text($this->questionnote, $session, $this->seed, 't', false, true);
if ($questiontext->get_errors()) {
throw new Exception('qtype_stack_question : Error in the question note: ' .
$notetext->get_errors());
}

// Now instantiate the session:
$session->instantiate();
if ($session->get_errors()) {
throw new Exception('qtype_stack_question : CAS error when instantiating the session: ' .
$session->get_errors());
}

// Now store the values that depend on the instantiated session.
$step->set_qt_var('_questionvars', $session->get_keyval_representation());
$step->set_qt_var('_questiontext', $questiontext->get_display_castext());
$step->set_qt_var('_feedback', $feedbacktext->get_display_castext());
$this->questionnoteinstantiated = $notetext->get_display_castext();
$step->set_qt_var('_questionnote', $this->questionnoteinstantiated);

// Finally, store only those values really needed for later.
$session->prune_session($session_length);
$this->session = $session;
}

public function apply_attempt_state(question_attempt_step $step) {
Expand Down
8 changes: 8 additions & 0 deletions stack/cas/cassession.class.php
Expand Up @@ -375,6 +375,14 @@ public function get_session() {
return $this->session;
}

public function prune_session($len) {
if (!is_int($len)) {
throw new Exception('stack_cas_session: prune_session $len must be an integer.');
}
$new_session = array_slice($this->session, 0, $len);
$this->session = $new_session;
}

public function get_all_keys() {
if (null===$this->valid) {
$this->validate();
Expand Down
11 changes: 9 additions & 2 deletions stack/cas/castext.class.php
Expand Up @@ -209,15 +209,22 @@ private function extract_cas_commands() {
$cmdarray = array();
$labels = array();

$session_keys = array();
if (is_a($this->session, 'stack_cas_session')) {
$session_keys = $this->session->get_all_keys();
}
foreach ($temp as $cmd) {
// Trim of surrounding white space and CAS commands.
$cmd = stack_utils::trim_commands($cmd);

$cs = new stack_cas_casstring($cmd);
$cs->validate($this->security, $this->insertstars, $this->syntax);

$key = 'caschat'.$i;
$i++;
do { // ... make sure names are not already in use.
$key = 'caschat'.$i;
$i++;
} while (in_array($key, $session_keys));
$sesion_keys[] = $key;
$labels[] = $key;
$cs->set_key($key, true);
$cmdarray[] = $cs;
Expand Down
3 changes: 3 additions & 0 deletions stack/cas/simpletest/test.cassession.class.php
Expand Up @@ -79,6 +79,9 @@ public function test_keyval_representation_1() {
$at1 = new stack_cas_session($s1, null, 0);
$this->assertEqual('a=x^2; b=1/(1+x^2); c=e^(i*pi);', $at1->get_keyval_representation());
$this->assertEqual(array('a', 'b', 'c'), $at1->get_all_keys());

$at1->prune_session(1);
$this->assertEqual(array('a'), $at1->get_all_keys());
}

public function test_keyval_representation_2() {
Expand Down
17 changes: 17 additions & 0 deletions stack/cas/simpletest/test.castext.class.php
Expand Up @@ -117,6 +117,23 @@ public function check_external_forbidden_words($ct, $val, $words) {

}

public function test_auto_generated_key_names() {

$a2=array('a:x^2', 'caschat0:x^3');
$s2=array();
foreach ($a2 as $s) {
$cs = new stack_cas_casstring($s);
$cs->validate('t');
$s2[] = $cs;
}
$cs2 = new stack_cas_session($s2, null, 0);

$at1 = new stack_cas_text("This is some text @x^2@, @x^3@", $cs2, 0);
$at1->get_display_castext();
$session = $at1->get_session();
$this->assertEqual(array('a', 'caschat0', 'caschat1', 'caschat2'), $session->get_all_keys());
}

public function testcheck_external_forbidden_words() {
$cases = array(
array('', false, array()),
Expand Down

0 comments on commit ecb6ce2

Please sign in to comment.