/**
  * When an in-progress {@link question_attempt} is re-loaded from the
  * database, this method is called so that the question can re-initialise
  * its internal state as needed by this attempt.
  *
  * For example, the multiple choice question type needs to set the order
  * of the choices to the order that was set up when start_attempt was called
  * originally. All the information required to do this should be in the
  * $step object, which is the first step of the question_attempt being loaded.
  *
  * @param question_attempt_step The first step of the {@link question_attempt}
  *      being loaded.
  */
 public function apply_attempt_state(\question_attempt_step $step)
 {
     global $DB;
     $attemptid = $this->get_attemptid_by_stepid($step->get_id());
     $this->usageid = $this->get_question_usageid($attemptid);
     // Retrive all vars initialized in start_attempt().
     foreach ($step->get_qt_data() as $name => $value) {
         if (substr($name, 0, 5) === '_var_') {
             $varid = substr($name, 5);
             $varname = $this->vars[$varid]->varname;
             $this->questiontext = str_replace('{$' . $varname . '}', $value, $this->questiontext);
             // Store vars (as array form) to be used later to get the correct response
             $this->varvalues[$varid] = explode(',', $value);
             if (!($values = $DB->get_field('qtype_programmedresp_val', 'varvalues', array('attemptid' => $this->usageid, 'varid' => $varid)))) {
                 // Add a new random value
                 $val = new stdClass();
                 $val->attemptid = $this->usageid;
                 $val->varid = $varid;
                 $val->varvalues = programmedresp_serialize($this->varvalues[$varid]);
                 if (!$DB->insert_record('qtype_programmedresp_val', $val)) {
                     print_error('errordb', 'qtype_programmedresp');
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * When an in-progress {@link question_attempt} is re-loaded from the database, this method is called so that the
  * question can re-initialise its internal state as needed by this attempt. For example, the multiple choice
  * question type needs to set the order of the choices to the order that was set up when start_attempt was called
  * originally. All the information required to do this should be in the $step object, which is the first step of the
  * question_attempt being loaded.
  *
  * @param question_attempt_step The first step of the {@link question_attempt} being loaded.
  */
 public function apply_attempt_state(question_attempt_step $step)
 {
     global $DB;
     $stepid = $step->get_id();
     // We need a place to store the feedback generated by JUnit.
     // Therefore we want to know the questionattemptid.
     if (!empty($stepid)) {
         $record = $DB->get_record('question_attempt_steps', array('id' => $stepid), 'questionattemptid');
         if ($record) {
             $this->questionattemptid = $record->questionattemptid;
         }
     }
 }