You are here

function QuizQuestion::saveRelationships in OG Quiz 7

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 includes/og_quiz_question.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

includes/og_quiz_question.php, line 557
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.

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;
          node_save($temp_quiz_node);
          $nid_vid[1] = $temp_quiz_node->vid;
          drupal_set_message(t('New revision has been created for the @quiz %n', array(
            '%n' => $temp_quiz_node->title,
            '@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->nid)
          ->condition('child_vid', $this->node->vid)
          ->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;
          node_save($temp_quiz_node);
          $nid_vid[1] = $temp_quiz_node->vid;
          drupal_set_message(t('New revision has been created for the @quiz %n', array(
            '%n' => $temp_quiz_node->title,
            '@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->nid;
        $insert_values[$nid]['child_vid'] = $this->node->vid;
        $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->nid,
        );
      }
    }
    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->nid,
      'parent_vid' => $new_node->vid,
      'child_nid' => $this->node->nid,
      'child_vid' => $this->node->vid,
      'max_score' => $this
        ->getMaximumScore(),
      'auto_update_max_score' => $this
        ->autoUpdateMaxScore() ? 1 : 0,
    ))
      ->execute();
    $quizzes_to_update[] = $new_node->vid;
  }

  // Update max_score for relationships if auto update max score is enabled
  // for question
  if (isset($this->node->nid)) {
    $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->nid,
      ':vid' => $this->node->vid,
    ));
    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->nid)
      ->condition('child_vid', $this->node->vid)
      ->condition('auto_update_max_score', 1)
      ->execute();
  }
  quiz_update_max_score_properties($quizzes_to_update);
  return $quizzes_kept;
}