You are here

public function MatchingQuestion::save in Quiz 6.3

Same name and namespace in other branches
  1. 6.6 question_types/matching/matching.classes.inc \MatchingQuestion::save()
  2. 6.5 question_types/matching/matching.classes.inc \MatchingQuestion::save()

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. This function is only responsible for saving data specific to the implement ation.

The $is_new flag is set to TRUE whenever the node is being initially created.

A save function is required to handle the following three situations:

  • A new node is created ($is_new is TRUE)
  • A new node *revision* is created ($is_new is NOT set, because the node itself is not new).
  • An existing node revision is modified.

Parameters

$is_new: TRUE when the node is initially created.

Overrides QuizQuestion::save

File

question_types/matching/matching.classes.inc, line 30
quiz_directions.classes

Class

MatchingQuestion
Implementation of Matching.

Code

public function save($is_new = FALSE) {
  foreach ($this->node->match as $match) {
    $match['feedback'] = isset($match['feedback']) ? $match['feedback'] : '';

    // match_id is not so it is a new question.
    if (empty($match['match_id'])) {
      if (!empty($match['question']) && !empty($match['answer'])) {
        $sql = "INSERT INTO {quiz_matching_node} (nid, vid, question, answer, feedback) VALUES (%d, %d, '%s', '%s', '%s')";
        db_query($sql, $this->node->nid, $this->node->vid, $match['question'], $match['answer'], $match['feedback']);
      }
    }
    else {
      if (empty($match['question']) && empty($match['answer'])) {

        // remove sub question.
        db_query("DELETE FROM {quiz_matching_node} WHERE match_id = %d", $match['match_id']);
      }
      else {

        // update sub question.
        $sql = "UPDATE {quiz_matching_node} SET question = '%s', answer = '%s', feedback = '%s' WHERE match_id = %d";
        db_query($sql, $match['question'], $match['answer'], $match['feedback'], $match['match_id']);
      }
    }
  }
}