Example #1
0
 /**
  * Builds the table of answers for a given quiz state.
  *
  * @param \Drupal\quiz\UserQuizStatusInterface $state
  * @return array()
  *    Returns a rendable array.
  */
 public function getResultsTable(UserQuizStatusInterface $state)
 {
     $answerStorage = static::entityTypeManager()->getStorage('answer');
     $header = array();
     $header['id'] = 'No.';
     $header['question'] = 'Question';
     $header['expected'] = 'Correct Answer';
     $header['received'] = 'Your Answer';
     $rows = array();
     $answers = $state->getAnswers();
     $c = 1;
     foreach ($answers as $answer) {
         /* @var $answer \Drupal\quiz\Entity\Answer */
         $question = $answer->getQuestion();
         /* @var $question \Drupal\quiz\Entity\Question */
         $rows[$answer->id()]['id'] = $c++;
         $rows[$answer->id()]['question'] = $answer->getQuestion()->get('question')->value;
         $rows[$answer->id()]['expected'] = '';
         $rows[$answer->id()]['received'] = '';
         $possible = array();
         //display the correct answer for the question
         if ($question->getType() == 'multiple_choice_question') {
             foreach ($question->get('field_multiple_answer') as $field) {
                 if ($field->value == 1) {
                     $rows[$answer->id()]['expected'] .= $field->name . ', ';
                 }
                 $possible[] = $field->name;
             }
         }
         if ($question->getType() == 'text_question') {
             $rows[$answer->id()]['expected'] = $question->get('field_text_answer')->value;
         }
         if ($question->getType() == 'true_or_false') {
             if ($question->get('field_true_or_false')->value == 0) {
                 $rows[$answer->id()]['expected'] = 'False';
             } else {
                 $rows[$answer->id()]['expected'] = 'True';
             }
         }
         //display the user answer for the question
         if ($answer->getType() == 'multiple_choice_answer') {
             foreach ($answer->get('field_multiple_answer') as $delta => $field) {
                 if ($field->value == 1) {
                     $rows[$answer->id()]['received'] .= $possible[$delta] . ', ';
                 }
             }
         }
         if ($answer->getType() == 'text_answer') {
             $rows[$answer->id()]['received'] = $answer->get('field_text_answer')->value;
         }
         if ($answer->getType() == 'true_or_false') {
             if ($answer->get('field_true_or_false')->value == 0) {
                 $rows[$answer->id()]['received'] = 'False';
             } else {
                 $rows[$answer->id()]['received'] = 'True';
             }
         }
     }
     $build['table'] = array('#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => $this->t('You didn\'t answer any question.'), '#cache' => ['contexts' => $answerStorage->getEntityType()->getListCacheContexts(), 'tags' => $answerStorage->getEntityType()->getListCacheTags()]);
     return $build;
 }
Example #2
0
 public function getUserQuizStateAnswersCount(AccountInterface $account, UserQuizStatusInterface $state)
 {
     $answerStorage = static::entityTypeManager()->getStorage('answer');
     $query = $answerStorage->getQuery();
     $aids = $query->Condition('user_id', $account->id())->Condition('question', $this->id())->Condition('user_quiz_status', $state->id())->execute();
     return count($aids);
 }