QuizUtil.php in Quiz 6.x
File
src/Util/QuizUtil.php
View source
<?php
namespace Drupal\quiz\Util;
use Drupal;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizResult;
use function drupal_get_path;
class QuizUtil {
public static function getQuizName() {
$quiz = Drupal::entityTypeManager()
->getDefinition('quiz');
return $quiz
->getLabel();
}
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;
}
static function resultOrTemp(Quiz $quiz) {
$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;
}
}
Classes
Name |
Description |
QuizUtil |
Utility functions that don't belong anywhere else. |