コード例 #1
0
 /**
  * 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;
 }