You are here

class QuizSession in Quiz 6.x

Default implementation of the quiz session.

Hierarchy

Expanded class hierarchy of QuizSession

1 string reference to 'QuizSession'
quiz.services.yml in ./quiz.services.yml
quiz.services.yml
1 service uses QuizSession
quiz.session in ./quiz.services.yml
Drupal\quiz\Services\QuizSession

File

src/Services/QuizSession.php, line 12

Namespace

Drupal\quiz\Services
View 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

Namesort descending Modifiers Type Description Overrides
QuizSession::$session protected property The session.
QuizSession::getCurrentQuestion public function Get the user's current question index for a quiz in the session. Overrides QuizSessionInterface::getCurrentQuestion
QuizSession::getCurrentQuizzes protected function Gets the current quizzes the user is taking
QuizSession::getResult public function Get the current user's result for a Quiz in the session Overrides QuizSessionInterface::getResult
QuizSession::getSessionKey protected function Gets the session key for the quiz session type.
QuizSession::getTemporaryResult public function Get the current user's temporary result ID (for feedback/review). Overrides QuizSessionInterface::getTemporaryResult
QuizSession::isTakingQuiz public function Determine if the current user user has a result for this quiz or a temporary quiz in the session. Overrides QuizSessionInterface::isTakingQuiz
QuizSession::removeQuiz public function Remove quiz from session Overrides QuizSessionInterface::removeQuiz
QuizSession::setCurrentQuestion public function Set the user's current question. Overrides QuizSessionInterface::setCurrentQuestion
QuizSession::setCurrentQuizzes protected function Gets the current quizzes the user is taking
QuizSession::setResult public function Set a quiz result for the current user. Overrides QuizSessionInterface::setResult
QuizSession::setTemporaryResult public function Set the user's temporary result ID (for feedback/review). Overrides QuizSessionInterface::setTemporaryResult
QuizSession::startQuiz public function Put a quiz result into the current user's session. Overrides QuizSessionInterface::startQuiz
QuizSession::__construct public function Constructs a new QuizSession object.
QuizSessionInterface::CURRENT_QUESTION constant
QuizSessionInterface::RESULT_ID constant
QuizSessionInterface::TEMP_ID constant