You are here

public static function MultichoiceResponse::viewsGetAnswers in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::viewsGetAnswers()
  2. 6.x question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::viewsGetAnswers()

Get answers for a question in a result.

This static method assists in building views for the mass export of question answers.

Overrides QuizResultAnswerEntityTrait::viewsGetAnswers

See also

views_handler_field_prerender_list for the expected return value.

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php, line 195

Class

MultichoiceResponse
Extension of QuizQuestionResponse.

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

public static function viewsGetAnswers(array $result_answer_ids = array()) {
  $ras = entity_load('quiz_result_answer', $result_answer_ids);
  $items = array();
  $nids = db_select('quiz_node_results_answers', 'qra')
    ->fields('qra', array(
    'question_nid',
  ))
    ->condition('result_answer_id', $result_answer_ids)
    ->execute()
    ->fetchAllKeyed(0, 0);
  $nodes = node_load_multiple($nids);
  foreach ($ras as $ra) {
    $question = $nodes[$ra->question_nid];

    /* @var $ra_i QuizQuestionResponse */
    $ra_i = _quiz_question_response_get_instance($ra->result_id, $question);
    $alternatives = array();
    foreach ($question->alternatives as $alternative) {
      $alternatives[$alternative['id']] = $alternative;
    }
    foreach ($ra_i
      ->getResponse() as $answer_id) {
      if (!empty($answer_id)) {
        $items[$ra->result_id][] = array(
          'answer' => $alternatives[$answer_id]['answer']['value'],
        );
      }
    }
  }
  return $items;
}