You are here

class QuizUtil in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/Util/QuizUtil.php \Drupal\quiz\Util\QuizUtil
  2. 8.5 src/Util/QuizUtil.php \Drupal\quiz\Util\QuizUtil

Utility functions that don't belong anywhere else.

Hierarchy

Expanded class hierarchy of QuizUtil

17 files declare their use of QuizUtil
LongAnswerResponse.php in question_types/quiz_long_answer/src/Plugin/quiz/QuizQuestion/LongAnswerResponse.php
MatchingResponse.php in question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingResponse.php
MultichoiceResponse.php in question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php
quiz.module in ./quiz.module
Contains quiz.module
QuizAdminForm.php in src/Form/QuizAdminForm.php

... See full list

File

src/Util/QuizUtil.php, line 13

Namespace

Drupal\quiz\Util
View source
class QuizUtil {

  /**
   * Get the quiz name variable and set it as a constant so we don't have to
   * keep calling it in every function.
   *
   * @return string
   *   Quiz name variable.
   */
  public static function getQuizName() {
    $quiz = Drupal::entityTypeManager()
      ->getDefinition('quiz');
    return $quiz
      ->getLabel();
  }

  /**
   * Helper function to facilitate icon display, like "correct" or "selected".
   *
   * @param string $type
   *
   * @return array
   *   Render array.
   */
  static function icon($type) {
    $options = [];
    switch ($type) {
      case 'correct':
        $options['path'] = 'check_008000_64.png';
        $options['alt'] = t('Correct');
        break;
      case 'incorrect':
        $options['path'] = 'times_ff0000_64.png';
        $options['alt'] = t('Incorrect');
        break;
      case 'unknown':
        $options['path'] = 'question_808080_64.png';
        $options['alt'] = t('Unknown');
        break;
      case 'should':
        $options['path'] = 'check_808080_64.png';
        $options['alt'] = t('Should have chosen');
        break;
      case 'should-not':
        $options['path'] = 'times_808080_64.png';
        $options['alt'] = t('Should not have chosen');
        break;
      case 'almost':
        $options['path'] = 'check_ffff00_64.png';
        $options['alt'] = t('Almost');
        break;
      case 'selected':
        $options['path'] = 'arrow-right_808080_64.png';
        $options['alt'] = t('Selected');
        break;
      case 'unselected':
        $options['path'] = 'circle-o_808080_64.png';
        $options['alt'] = t('Unselected');
        break;
      default:
        $options['path'] = '';
        $options['alt'] = '';
    }
    if (!empty($options['path'])) {
      $options['path'] = drupal_get_path('module', 'quiz') . '/images/' . $options['path'];
    }
    if (!empty($options['alt'])) {
      $options['title'] = $options['alt'];
    }
    $image = [
      '#theme' => 'image',
      '#uri' => $options['path'],
      '#alt' => $options['title'],
      '#attributes' => [
        'class' => [
          'quiz-score-icon',
          $type,
        ],
      ],
    ];
    return $image;
  }

  /**
   * Use in the case where a quiz may have ended and the temporary result ID
   * must be used instead.
   *
   * @param Quiz $quiz
   *   The quiz.
   *
   * @return QuizResult
   *   Quiz result from the current user's session.
   */
  static function resultOrTemp(Quiz $quiz) {

    /* @var $quiz_session \Drupal\quiz\Services\QuizSessionInterface */
    $quiz_session = \Drupal::service('quiz.session');
    if ($quiz_result = $quiz_session
      ->getResult($quiz)) {
      return $quiz_result;
    }
    elseif ($quiz_result = $quiz_session
      ->getTemporaryResult()) {
      return $quiz_result;
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QuizUtil::getQuizName public static function Get the quiz name variable and set it as a constant so we don't have to keep calling it in every function.
QuizUtil::icon static function Helper function to facilitate icon display, like "correct" or "selected".
QuizUtil::resultOrTemp static function Use in the case where a quiz may have ended and the temporary result ID must be used instead.