You are here

public function MultichoiceQuestion::getNodeProperties in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::getNodeProperties()

Implementation of getNodeProperties().

See also

QuizQuestion::getNodeProperties()

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 350

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

public function getNodeProperties() {
  if (isset($this->nodeProperties) && !empty($this->nodeProperties)) {
    return $this->nodeProperties;
  }
  $props = parent::getNodeProperties();
  $res_a = db_query('SELECT choice_multi, choice_random, choice_boolean FROM {quiz_multichoice_properties}
            WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $this->node->nid,
    ':vid' => $this->node->vid,
  ))
    ->fetchAssoc();
  if (is_array($res_a)) {
    $props = array_merge($props, $res_a);
  }

  // Load the answers.
  $res = db_query('SELECT id, answer, answer_format, feedback_if_chosen, feedback_if_chosen_format,
            feedback_if_not_chosen, feedback_if_not_chosen_format, score_if_chosen, score_if_not_chosen, weight
            FROM {quiz_multichoice_answers}
            WHERE question_nid = :question_nid AND question_vid = :question_vid
            ORDER BY weight', array(
    ':question_nid' => $this->node->nid,
    ':question_vid' => $this->node->vid,
  ));

  // Init array so it can be iterated even if empty.
  $props['alternatives'] = array();
  while ($res_arr = $res
    ->fetchAssoc()) {
    $props['alternatives'][] = array(
      'id' => $res_arr['id'],
      'answer' => array(
        'value' => $res_arr['answer'],
        'format' => $res_arr['answer_format'],
      ),
      'feedback_if_chosen' => array(
        'value' => $res_arr['feedback_if_chosen'],
        'format' => $res_arr['feedback_if_chosen_format'],
      ),
      'feedback_if_not_chosen' => array(
        'value' => $res_arr['feedback_if_not_chosen'],
        'format' => $res_arr['feedback_if_not_chosen_format'],
      ),
      'score_if_chosen' => $res_arr['score_if_chosen'],
      'score_if_not_chosen' => $res_arr['score_if_not_chosen'],
      'weight' => $res_arr['weight'],
    );
  }
  $this->nodeProperties = $props;
  return $props;
}