You are here

function quiz_views_pre_view in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 includes/views/quiz.views.inc \quiz_views_pre_view()
  2. 7.5 includes/views/quiz.views.inc \quiz_views_pre_view()
  3. 6.x quiz.module \quiz_views_pre_view()

Implements hook_views_pre_view().

Replace the static field with dynamic fields.

File

includes/views/quiz.views.inc, line 46
This file provides metadata to the Views 2 API for views integration.

Code

function quiz_views_pre_view(view $view) {
  if ($view->name == 'quiz_results') {
    $fields = $view
      ->get_items('field');
    $quiz = node_load($view->args[0]);
    foreach ($fields as $field_name => $field) {
      if ($field['id'] == 'answers') {
        $i = 0;
        foreach ($quiz
          ->getQuestions() as $question) {
          if ($quizQuestion = _quiz_question_get_instance($question)) {
            if ($quizQuestion
              ->isGraded()) {
              $i++;
              $newfield = array();
              $newfield['id'] = 'answer';
              $newfield['table'] = 'quiz_node_results';
              $newfield['field'] = 'answer';
              $newfield['label'] = t('@num. @question', array(
                '@num' => $i,
                '@question' => $question->title,
              ));
              $newfield['question_nid'] = $question->nid;
              $newfield['question_vid'] = $question->vid;
              $view
                ->set_item($view->current_display, 'field', 'answer_' . $question->nid, $newfield);
            }
          }
        }
        $view
          ->set_item($view->current_display, 'field', $field_name, NULL);
      }
    }
  }
}