You are here

function question_extract_responses in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/lib/questionlib.php \question_extract_responses()

Extracts responses from submitted form

This can extract the responses given to one or several questions present on a page It returns an array with one entry for each question, indexed by question id Each entry is an object with the properties ->event The event that has triggered the submission. This is determined by which button the user has pressed. ->responses An array holding the responses to an individual question, indexed by the name of the corresponding form element. ->timestamp A unix timestamp

Parameters

array $questions an array containing at least all questions that are used on the form:

array $formdata the data submitted by the form on the question page:

integer $defaultevent the event type used if no 'mark' or 'validate' is submitted:

Return value

array array of action objects, indexed by question ids.

File

includes/moodle/lib/questionlib.php, line 1069

Code

function question_extract_responses($questions, $formdata, $defaultevent = QUESTION_EVENTSAVE) {
  $time = time();
  $actions = array();
  foreach ($formdata as $key => $response) {

    // Get the question id from the response name
    if (false !== ($quid = question_get_id_from_name_prefix($key))) {

      // check if this is a valid id
      if (!isset($questions[$quid])) {
        error('Form contained question that is not in questionids');
      }

      // Remove the name prefix from the name

      //decrypt trying
      $key = substr($key, strlen($questions[$quid]->name_prefix));
      if (false === $key) {
        $key = '';
      }

      // Check for question validate and mark buttons & set events
      if ($key === 'validate') {
        $actions[$quid]->event = QUESTION_EVENTVALIDATE;
      }
      else {
        if ($key === 'submit') {
          $actions[$quid]->event = QUESTION_EVENTSUBMIT;
        }
        else {
          $actions[$quid]->event = $defaultevent;
        }
      }

      // Update the state with the new response
      $actions[$quid]->responses[$key] = $response;

      // Set the timestamp
      $actions[$quid]->timestamp = $time;
    }
  }
  foreach ($actions as $quid => $notused) {
    ksort($actions[$quid]->responses);
  }
  return $actions;
}