class QuizSession in Quiz 6.x
Default implementation of the quiz session.
Hierarchy
- class \Drupal\quiz\Services\QuizSession implements QuizSessionInterface
Expanded class hierarchy of QuizSession
1 string reference to 'QuizSession'
1 service uses QuizSession
File
- src/
Services/ QuizSession.php, line 12
Namespace
Drupal\quiz\ServicesView source
class QuizSession implements QuizSessionInterface {
/**
* The session.
*
* @var SessionInterface
*/
protected $session;
/**
* Constructs a new QuizSession object.
*
* @param SessionInterface $session
* The session.
*/
public function __construct(SessionInterface $session) {
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public function isTakingQuiz(Quiz $quiz = NULL) {
return (bool) $this
->getResult($quiz);
}
/**
* {@inheritdoc}
*/
public function startQuiz(QuizResult $quiz_result) {
$current_quizzes = $this
->getCurrentQuizzes();
$current_quizzes[$quiz_result
->getQuiz()
->id()][self::RESULT_ID] = $quiz_result
->id();
$current_quizzes[$quiz_result
->getQuiz()
->id()][self::CURRENT_QUESTION] = 1;
$this
->setCurrentQuizzes($current_quizzes);
}
/**
* {@inheritdoc}
*/
public function removeQuiz(Quiz $quiz) {
$current_quizzes = $this
->getCurrentQuizzes();
unset($current_quizzes[$quiz
->id()]);
$this
->setCurrentQuizzes($current_quizzes);
}
/**
* {@inheritdoc}
*/
public function getResult(Quiz $quiz = NULL) {
$current_quizzes = $this
->getCurrentQuizzes();
if ($quiz && isset($current_quizzes[$quiz
->id()]) && !empty($current_quizzes[$quiz
->id()][self::RESULT_ID])) {
$result_id = $current_quizzes[$quiz
->id()][self::RESULT_ID];
return QuizResult::load($result_id);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getTemporaryResult() {
$current_quizzes = $this
->getCurrentQuizzes();
if (!empty($current_quizzes[self::TEMP_ID])) {
$result_id = $current_quizzes[self::TEMP_ID];
return QuizResult::load($result_id);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setResult(QuizResult $quiz_result) {
$current_quizzes = $this
->getCurrentQuizzes();
$current_quizzes[$quiz_result
->getQuiz()
->id()][self::RESULT_ID] = $quiz_result
->id();
$this
->setCurrentQuizzes($current_quizzes);
}
/**
* {@inheritdoc}
*/
public function setTemporaryResult(QuizResult $quiz_result) {
$current_quizzes = $this
->getCurrentQuizzes();
$current_quizzes[self::TEMP_ID] = $quiz_result
->id();
$this
->setCurrentQuizzes($current_quizzes);
}
/**
* {@inheritdoc}
*/
public function getCurrentQuestion(Quiz $quiz) {
$current_quizzes = $this
->getCurrentQuizzes();
if (isset($current_quizzes[$quiz
->id()])) {
return !empty($current_quizzes[$quiz
->id()][self::CURRENT_QUESTION]) ? $current_quizzes[$quiz
->id()][self::CURRENT_QUESTION] : NULL;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setCurrentQuestion(Quiz $quiz, int $current_question) {
$current_quizzes = $this
->getCurrentQuizzes();
if (isset($current_quizzes[$quiz
->id()])) {
$current_quizzes[$quiz
->id()][self::CURRENT_QUESTION] = $current_question;
$this
->setCurrentQuizzes($current_quizzes);
}
}
/**
* Gets the current quizzes the user is taking
*
* @return array
* The quizzes
*/
protected function getCurrentQuizzes() {
$key = $this
->getSessionKey();
return $this->session
->get($key, []);
}
/**
* Gets the current quizzes the user is taking
*
* @return array
* The quizzes
*/
protected function setCurrentQuizzes(array $current_quizzes) {
$key = $this
->getSessionKey();
if (sizeOf($current_quizzes) == 0) {
$this->session
->remove($key);
}
else {
$this->session
->set($key, $current_quizzes);
}
}
/**
* Gets the session key for the quiz session type.
*
* @return string
* The session key.
*/
protected function getSessionKey() {
return 'quiz';
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
QuizSession:: |
protected | property | The session. | |
QuizSession:: |
public | function |
Get the user's current question index for a quiz in the session. Overrides QuizSessionInterface:: |
|
QuizSession:: |
protected | function | Gets the current quizzes the user is taking | |
QuizSession:: |
public | function |
Get the current user's result for a Quiz in the session Overrides QuizSessionInterface:: |
|
QuizSession:: |
protected | function | Gets the session key for the quiz session type. | |
QuizSession:: |
public | function |
Get the current user's temporary result ID (for feedback/review). Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function |
Determine if the current user user has a result for this quiz or a
temporary quiz in the session. Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function |
Remove quiz from session Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function |
Set the user's current question. Overrides QuizSessionInterface:: |
|
QuizSession:: |
protected | function | Gets the current quizzes the user is taking | |
QuizSession:: |
public | function |
Set a quiz result for the current user. Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function |
Set the user's temporary result ID (for feedback/review). Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function |
Put a quiz result into the current user's session. Overrides QuizSessionInterface:: |
|
QuizSession:: |
public | function | Constructs a new QuizSession object. | |
QuizSessionInterface:: |
constant | |||
QuizSessionInterface:: |
constant | |||
QuizSessionInterface:: |
constant |