/**
  * Create the actual response data. The response data in the test case may
  * include expressions in terms of the question variables.
  * @param qtype_stack_question $question the question - with $question->session initialised.
  * @return array the respones to send to $quba->process_action.
  */
 public static function compute_response(qtype_stack_question $question, $inputs)
 {
     // If the question has simp:false, then the local options should reflect this.
     // In this case, test constructors (question authors) will need to explicitly simplify their test case constructions.
     $localoptions = clone $question->options;
     // Start with the question variables (note that order matters here).
     $cascontext = new stack_cas_session(null, $localoptions, $question->seed);
     $question->add_question_vars_to_session($cascontext);
     // Turn off simplification - we *always* need test cases to be unsimplified, even if the question option is true.
     $vars = array();
     $cs = new stack_cas_casstring('false');
     $cs->set_key('simp');
     $vars[] = $cs;
     // Now add the expressions we want evaluated.
     foreach ($inputs as $name => $value) {
         if ('' !== $value) {
             $cs = new stack_cas_casstring($value);
             if ($cs->get_valid('t')) {
                 $cs->set_key('testresponse_' . $name);
                 $vars[] = $cs;
             }
         }
     }
     $cascontext->add_vars($vars);
     $cascontext->instantiate();
     $response = array();
     foreach ($inputs as $name => $notused) {
         $computedinput = $cascontext->get_value_key('testresponse_' . $name);
         // In the case we start with an invalid input, and hence don't send it to the CAS
         // We want the response to constitute the raw invalid input.
         // This permits invalid expressions in the inputs, and to compute with valid expressions.
         if ('' == $computedinput) {
             $computedinput = $inputs[$name];
         }
         if (array_key_exists($name, $question->inputs)) {
             $response = array_merge($response, $question->inputs[$name]->maxima_to_response_array($computedinput));
         }
     }
     return $response;
 }
Example #2
0
 /**
  * Display and appropriate piece of standard PRT feedback given the overall
  * state of the question.
  * @param question_attempt $qa
  * @param qtype_stack_question $question the question being displayed.
  * @param array $response the current response.
  * @return string HTML fragment.
  */
 protected function overall_standard_prt_feedback(question_attempt $qa, qtype_stack_question $question, $response)
 {
     $fraction = null;
     foreach ($question->prts as $name => $prt) {
         $relevantresponse = $this->get_applicable_response_for_prt($name, $response, $qa);
         if (is_null($relevantresponse)) {
             continue;
         }
         $result = $question->get_prt_result($name, $relevantresponse, $qa->get_state()->is_finished());
         if (is_null($result->valid)) {
             continue;
         }
         $fraction += $result->fraction;
     }
     if (is_null($fraction)) {
         return '';
     }
     $result = new stack_potentialresponse_tree_state(1, true, $fraction);
     return $this->standard_prt_feedback($qa, $qa->get_question(), $result);
 }