public function getRandomQuestion(SessionConfigurationInterface $configuration)
    {
        /** @var QuestionRepository $questionRepository */
        $questionRepository = $this->manager->getRepository('QcmPublicBundle:Question');
        $maxQuestions = $configuration->getMaxQuestions() - $configuration->getQuestions()->count();

        $averagePerCategory = $maxQuestions;
        if ($maxQuestions >= $configuration->getCategories()->count()) {
            $averagePerCategory = floor($maxQuestions/$configuration->getCategories()->count());
        }

        $questionsId = array_map(function($question) {
            return $question->getId();
        }, $configuration->getQuestions()->toArray());

        foreach ($configuration->getCategories() as $category) {
            if ($configuration->getMaxQuestions() - $configuration->getQuestions()->count() > 0) {
                $questions = $questionRepository->getRandomQuestions(
                    $category->getId(),
                    $averagePerCategory,
                    $configuration->getQuestionsLevel(),
                    $questionsId
                );

                /** @var QuestionInterface $question */
                foreach ($questions as $question) {
                    foreach ($question->getAnswers() as $answer) {
                        $question->addAnswer($answer);
                    }
                    $configuration->addQuestion($question);
                }
            }
        }

        return $this;
    }
public function getRandomQuestion(SessionConfigurationInterface $configuration)
    {
        /** @var QuestionRepository $questionRepository */
        $questionRepository = $this->manager->getRepository('QcmPublicBundle:Question');
        $maxQuestions = $configuration->getMaxQuestions() - $configuration->getQuestions()->count();

        $averagePerCategory = $maxQuestions;
        if ($maxQuestions >= $configuration->getCategories()->count()) {
            $averagePerCategory = floor($maxQuestions/$configuration->getCategories()->count());
        }

        $questionsId = array_map(function($question) {
            return $question->getId();
        }, $configuration->getQuestions()->toArray());

        foreach ($configuration->getCategories() as $category) {
            if ($configuration->getMaxQuestions() - $configuration->getQuestions()->count() > 0) {
                $questions = $questionRepository->getRandomQuestions(
                    $category->getId(),
                    $averagePerCategory,
                    $configuration->getQuestionsLevel(),
                    $questionsId
                );

                /** @var QuestionInterface $question */
                foreach ($questions as $question) {
                    foreach ($question->getAnswers() as $answer) {
                        $question->addAnswer($answer);
                    }
                    $configuration->addQuestion($question);
                }
            }
        }

        return $this;
    }
public function getRandomQuestion ( SessionConfigurationInterface $ configuration ) { /** @var QuestionRepository $questionRepository */ $ questionRepository = $ this -> manager -> getRepository ( 'QcmPublicBundle:Question' ) ; $ maxQuestions = $ configuration -> getMaxQuestions ( ) - $ configuration -> getQuestions ( ) -> count ( ) ; $ averagePerCategory = $ maxQuestions ; if ( $ maxQuestions >= $ configuration -> getCategories ( ) -> count ( ) ) { $ averagePerCategory = floor ( $ maxQuestions / $ configuration -> getCategories ( ) -> count ( ) ) ; } $ questionsId = array_map ( function ( $ question ) { return $ question -> getId ( ) ; } , $ configuration -> getQuestions ( ) -> toArray ( ) ) ; foreach ( $ configuration -> getCategories ( ) as $ category ) { if ( $ configuration -> getMaxQuestions ( ) - $ configuration -> getQuestions ( ) -> count ( ) > 0 ) { $ questions = $ questionRepository -> getRandomQuestions ( $ category -> getId ( ) , $ averagePerCategory , $ configuration -> getQuestionsLevel ( ) , $ questionsId ) ; /** @var QuestionInterface $question */ foreach ( $ questions as $ question ) { foreach ( $ question -> getAnswers ( ) as $ answer ) { $ question -> addAnswer ( $ answer ) ; } $ configuration -> addQuestion ( $ question ) ; } } } return $ this ; }
Get random questions by category

@param SessionConfigurationInterface $configuration

@return $this
Get random questions by category