You are here

public function QuizResultController::save in Quiz 7.5

Same name and namespace in other branches
  1. 7.6 includes/QuizResultController.class.inc \QuizResultController::save()

Save the Quiz result and do any post-processing to the result.

Parameters

type $entity:

\DatabaseTransaction $transaction:

Return value

bool

Overrides EntityAPIController::save

File

includes/QuizResultController.class.inc, line 33

Class

QuizResultController

Code

public function save($entity, \DatabaseTransaction $transaction = NULL) {
  if (empty($entity->time_start)) {
    $entity->time_start = REQUEST_TIME;
  }
  $new = !empty($entity->is_new);
  if (!isset($entity->attempt)) {
    if ($entity->uid == 0) {
      $entity->attempt = 1;
    }
    else {
      $efq = new EntityFieldQuery();
      $result = $efq
        ->entityCondition('entity_type', 'quiz_result')
        ->propertyCondition('nid', $entity->nid)
        ->propertyCondition('uid', $entity->uid)
        ->propertyOrderBy('attempt', 'DESC')
        ->range(0, 1)
        ->execute();
      if (!empty($result['quiz_result'])) {
        $keys = array_keys($result['quiz_result']);
        $existing = quiz_result_load(reset($keys));
        $entity->attempt = $existing->attempt + 1;
      }
    }
  }

  // Save the Quiz result.
  parent::save($entity, $transaction);

  // Post process the result.
  if ($new) {
    $quiz = node_load($entity->nid, $entity->vid);

    // Call @deprecated hook_quiz_begin().
    module_invoke_all('quiz_begin', $quiz, $entity->result_id);

    // Create question list.
    $questions = quiz_build_question_list($quiz);
    if ($questions === FALSE) {
      drupal_set_message(t('Not enough random questions were found. Please add more questions before trying to take this @quiz.', array(
        '@quiz' => QUIZ_NAME,
      )), 'error');
      return FALSE;
    }
    if (count($questions) == 0) {
      drupal_set_message(t('No questions were found. Please !assign_questions before trying to take this @quiz.', array(
        '@quiz' => QUIZ_NAME,
        '!assign_questions' => l(t('assign questions'), 'node/' . $quiz->nid . '/quiz/questions'),
      )), 'error');
      return FALSE;
    }
    $i = 0;
    $j = 0;
    foreach ($questions as $question) {
      $quizQuestion = _quiz_question_get_instance((object) $question);
      $quiz_result_answer = entity_create('quiz_result_answer', array(
        'result_id' => $entity->result_id,
        'question_nid' => $question['nid'],
        'question_vid' => $question['vid'],
        'tid' => !empty($question['tid']) ? $question['tid'] : NULL,
        'number' => ++$i,
        'display_number' => $quizQuestion
          ->isQuestion() ? ++$j : NULL,
      ));
      entity_save('quiz_result_answer', $quiz_result_answer);
    }
    if (!empty($entity->build_on_last)) {

      // Build on the last attempt the user took. If this quiz has build on
      // last attempt set, we need to search for a previous attempt with the
      // same version of the current quiz.
      $quiz_result_old = self::findOldResult($entity);

      // Now clone the answers on top of the new result.
      quiz_clone_quiz_result($quiz_result_old, $entity);
    }
  }
}