You are here

function QuizQuestion::saveRelationships in Quiz 7.6

Same name and namespace in other branches
  1. 6.4 question_types/quiz_question/quiz_question.core.inc \QuizQuestion::saveRelationships()
  2. 7 question_types/quiz_question/quiz_question.core.inc \QuizQuestion::saveRelationships()
  3. 7.4 question_types/quiz_question/quiz_question.core.inc \QuizQuestion::saveRelationships()
  4. 7.5 question_types/quiz_question/quiz_question.core.inc \QuizQuestion::saveRelationships()

Save this Question to the specified Quiz.

1 call to QuizQuestion::saveRelationships()
QuizQuestion::save in question_types/quiz_question/quiz_question.core.inc
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/quiz_question.core.inc, line 329
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($nid, $vid) {
  $quiz_node = node_load($nid, $vid);
  if (quiz_has_been_answered($quiz_node)) {

    // We need to revise the quiz node if it has been answered
    $quiz_node->revision = 1;
    $quiz_node->auto_created = TRUE;
    node_save($quiz_node);
    drupal_set_message(t('New revision has been created for the @quiz %n', array(
      '%n' => $quiz_node->title,
      '@quiz' => QUIZ_NAME,
    )));
  }
  $insert_values = array();
  $insert_values['parent_nid'] = $quiz_node->nid;
  $insert_values['parent_vid'] = $quiz_node->vid;
  $insert_values['child_nid'] = $this->node->nid;
  $insert_values['child_vid'] = $this->node->vid;
  $insert_values['max_score'] = $this
    ->getMaximumScore();
  $insert_values['auto_update_max_score'] = $this
    ->autoUpdateMaxScore() ? 1 : 0;
  $insert_values['weight'] = 1 + db_query('SELECT MAX(weight) FROM {quiz_node_relationship} WHERE parent_vid = :vid', array(
    ':vid' => $quiz_node->vid,
  ))
    ->fetchField();
  $randomization = db_query('SELECT randomization FROM {quiz_node_properties} WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $quiz_node->nid,
    ':vid' => $quiz_node->vid,
  ))
    ->fetchField();
  $insert_values['question_status'] = $randomization == 2 ? QUIZ_QUESTION_RANDOM : QUIZ_QUESTION_ALWAYS;
  entity_create('quiz_question_relationship', $insert_values)
    ->save();

  // Update max_score for relationships if auto update max score is enabled
  // for question
  $quizzes_to_update = array();
  $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);
  quiz_update_max_score_properties(array(
    $quiz_node->vid,
  ));
}