You are here

function save_question_session in Quiz 6.6

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

Saves the current state of the question session to the database

The state object representing the current state of the session for the question is saved to the question_states table with ->responses[''] saved to the answer field of the database table. The information in the question_sessions table is updated. The question type specific data is then saved.

Parameters

object $question The question for which session is to be saved.:

object $state The state information to be saved. In particular the: most recent responses are in ->responses. The object is updated to hold the new ->id.

Return value

mixed The id of the saved or updated state or false

1 call to save_question_session()
regrade_question_in_attempt in includes/moodle/lib/questionlib.php
For a given question in an attempt we walk the complete history of states and recalculate the grades as we go along.

File

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

Code

function save_question_session(&$question, &$state) {
  global $QTYPES;

  // Check if the state has changed
  if (!$state->changed && isset($state->id)) {
    return $state->id;
  }

  // Set the legacy answer field
  $state->answer = isset($state->responses['']) ? $state->responses[''] : '';

  // Save the state
  if (!empty($state->update)) {

    // this forces the old state record to be overwritten
    update_record('question_states', $state);
  }
  else {
    if (!($state->id = insert_record('question_states', $state))) {
      unset($state->id);
      unset($state->answer);
      return false;
    }
  }

  // create or update the session
  if (!($session = get_record('question_sessions', 'attemptid', $state->attempt, 'questionid', $question->id))) {
    $session->attemptid = $state->attempt;
    $session->questionid = $question->id;
    $session->newest = $state->id;

    // The following may seem weird, but the newgraded field needs to be set
    // already even if there is no graded state yet.
    $session->newgraded = $state->id;
    $session->sumpenalty = $state->sumpenalty;
    $session->manualcomment = $state->manualcomment;
    if (!insert_record('question_sessions', $session)) {
      error('Could not insert entry in question_sessions');
    }
  }
  else {
    $session->newest = $state->id;
    if (question_state_is_graded($state) or $state->event == QUESTION_EVENTOPEN) {

      // this state is graded or newly opened, so it goes into the lastgraded field as well
      $session->newgraded = $state->id;
      $session->sumpenalty = $state->sumpenalty;
      $session->manualcomment = $state->manualcomment;
    }
    else {
      $session->manualcomment = addslashes($session->manualcomment);
    }
    update_record('question_sessions', $session);
  }
  unset($state->answer);

  // Save the question type specific state information and responses
  if (!$QTYPES[$question->qtype]
    ->save_session_and_responses($question, $state)) {
    return false;
  }

  // Reset the changed flag
  $state->changed = false;
  return $state->id;
}