You are here

function QuizQuestion::saveRelationships in Quiz 8.4

Handle the add to quiz part of the quiz_question_form

Return value

TRUE if at least one of the questions were kept. FALSE otherwise

1 call to QuizQuestion::saveRelationships()
QuizQuestion::save in question_types/quiz_question/lib/Drupal/quiz_question/QuizQuestion.php
Responsible for handling insert/update of question-specific data. This is typically called from within the Node API, so there is no need to save the node.

File

question_types/quiz_question/lib/Drupal/quiz_question/QuizQuestion.php, line 494
Classes used in the Quiz Question module.

Class

QuizQuestion
A base implementation of a quiz_question, adding a layer of abstraction between the node API, quiz API and the question types.

Namespace

Drupal\quiz_question

Code

function saveRelationships() {
  $quizzes_kept = FALSE;
  unset($_SESSION['quiz_question_kept']);

  /*
   * If the question already is part of quizzes we might have to remove some
   * relationships
   */
  $quizzes_to_update = array();
  if (isset($this->node->add_directly['already']) && is_array($this->node->add_directly['already'])) {
    foreach ($this->node->add_directly['already'] as $key => $checked) {
      if ($checked == 0) {
        $nid_vid = explode('-', $key);
        $dummy_node = new \stdClass();
        $dummy_node->nid = $nid_vid[0];
        $dummy_node->vid = $nid_vid[1];
        if (quiz_has_been_answered($dummy_node)) {

          // We need to revise the quiz node if it has been answered
          $temp_quiz_node = node_load($dummy_node->nid, $dummy_node->vid);
          $temp_quiz_node->revision = 1;
          $temp_quiz_node->auto_created = TRUE;
          $temp_quiz_node
            ->save();
          $nid_vid[1] = $temp_quiz_node
            ->getRevisionId();
          drupal_set_message(t('New revision has been created for the @quiz %n', array(
            '%n' => $temp_quiz_node
              ->getTitle(),
            '@quiz' => QUIZ_NAME,
          )));
        }
        $quizzes_to_update[] = $nid_vid[1];
        db_delete('quiz_node_relationship')
          ->condition('parent_nid', $nid_vid[0])
          ->condition('parent_vid', $nid_vid[1])
          ->condition('child_nid', $this->node
          ->id())
          ->condition('child_vid', $this->node
          ->getRevisionId())
          ->execute();
      }
      else {
        $quizzes_kept = TRUE;
        $_SESSION['quiz_question_kept'][] = $key;
      }
    }
  }

  /*
   * The quiz question might have been added to new quizzes
   */
  if (isset($this->node->add_directly['latest']) && is_array($this->node->add_directly['latest'])) {
    $to_insert = 'VALUES';
    $insert_values = array();
    foreach ($this->node->add_directly['latest'] as $nid => $checked) {
      if ($checked != 0) {
        $nid_vid = explode('-', $checked);
        $dummy_node = new \stdClass();
        $dummy_node->nid = $nid_vid[0];
        $dummy_node->vid = $nid_vid[1];
        if (quiz_has_been_answered($dummy_node)) {
          $temp_quiz_node = node_load($dummy_node->nid, $dummy_node->vid);
          $temp_quiz_node->revision = 1;
          $temp_quiz_node->auto_created = TRUE;
          $temp_quiz_node
            ->save();
          $nid_vid[1] = $temp_quiz_node
            ->getRevisionId();
          drupal_set_message(t('New revision has been created for the @quiz %n', array(
            '%n' => $temp_quiz_node
              ->getTitle(),
            '@quiz' => QUIZ_NAME,
          )));
        }
        $quizzes_to_update[] = $nid_vid[1];
        $insert_values[$nid]['parent_nid'] = $nid_vid[0];
        $insert_values[$nid]['parent_vid'] = $nid_vid[1];
        $insert_values[$nid]['child_nid'] = $this->node
          ->id();
        $insert_values[$nid]['child_vid'] = $this->node
          ->getRevisionId();
        $insert_values[$nid]['max_score'] = $this
          ->getMaximumScore();
        $insert_values[$nid]['auto_update_max_score'] = $this
          ->autoUpdateMaxScore() ? 1 : 0;
        $insert_values[$nid]['weight'] = 1 + db_query('SELECT MAX(weight) FROM {quiz_node_relationship} WHERE parent_vid = :vid', array(
          ':vid' => $nid_vid[1],
        ))
          ->fetchField();
        $randomization = db_query('SELECT randomization FROM {quiz_node_properties} WHERE nid = :nid AND vid = :vid', array(
          ':nid' => $nid_vid[0],
          ':vid' => $nid_vid[1],
        ))
          ->fetchField();
        $insert_values[$nid]['question_status'] = $randomization == 2 ? QUESTION_RANDOM : QUESTION_ALWAYS;
        $delete_values[] = array(
          'vid' => $nid_vid[1],
          'nid' => $this->node
            ->id(),
        );
      }
    }
    if (count($insert_values) > 0) {
      foreach ($delete_values as $delete_value) {
        $delete_qnr = db_delete('quiz_node_relationship');
        $delete_qnr
          ->condition('parent_vid', $delete_value['vid']);
        $delete_qnr
          ->condition('child_nid', $delete_value['nid']);
        $delete_qnr
          ->execute();
      }
      $insert_qnr = db_insert('quiz_node_relationship');
      $insert_qnr
        ->fields(array(
        'parent_nid',
        'parent_vid',
        'child_nid',
        'child_vid',
        'max_score',
        'weight',
        'question_status',
        'auto_update_max_score',
      ));
      foreach ($insert_values as $insert_value) {
        $insert_qnr
          ->values($insert_value);
      }
      $insert_qnr
        ->execute();
    }
  }
  if (drupal_strlen($this->node->add_directly['new']) > 0) {
    $new_node = quiz_make_new($this->node->add_directly['new']);
    $id = db_insert('quiz_node_relationship')
      ->fields(array(
      'parent_nid' => $new_node
        ->id(),
      'parent_vid' => $new_node
        ->getRevisionId(),
      'child_nid' => $this->node
        ->id(),
      'child_vid' => $this->node
        ->getRevisionId(),
      'max_score' => $this
        ->getMaximumScore(),
      'auto_update_max_score' => $this
        ->autoUpdateMaxScore() ? 1 : 0,
    ))
      ->execute();
    $quizzes_to_update[] = $new_node
      ->getRevisionId();
  }

  // Update max_score for relationships if auto update max score is enabled
  // for question
  if ($this->node
    ->id()) {
    $result = db_query('SELECT parent_vid as vid from {quiz_node_relationship} where child_nid = :nid and child_vid = :vid and auto_update_max_score=1', array(
      ':nid' => $this->node
        ->id(),
      ':vid' => $this->node
        ->getRevisionId(),
    ));
    foreach ($result as $record) {
      $quizzes_to_update[] = $record->vid;
    }
    db_update('quiz_node_relationship')
      ->fields(array(
      'max_score' => $this
        ->getMaximumScore(),
    ))
      ->condition('child_nid', $this->node
      ->id())
      ->condition('child_vid', $this->node
      ->getRevisionId())
      ->condition('auto_update_max_score', 1)
      ->execute();
  }
  quiz_update_max_score_properties($quizzes_to_update);
  return $quizzes_kept;
}