public async Task <LearningQuiz> CreateLearningQuizAsync(Guid quizId, string userId)
        {
            var isPublic = await _quizzesRepository.IsPublicAsync(quizId);

            if (!isPublic)
            {
                var access = await _quizzesRepository.HaveReadAccessToQuizAsync(userId, quizId);

                if (!access)
                {
                    return(null);
                }
            }

            var creationTime = CurrentTime;

            var questionsQuery = _questionsRepository
                                 .GetAllByQuizId(quizId)
                                 .Where(q => q.CreationTime <= creationTime);

            var learningQuiz = new LearningQuiz {
                CreationTime      = creationTime,
                UserId            = userId,
                QuizId            = quizId,
                NumberOfQuestions = await questionsQuery.CountAsync()
            };

            var settings = await _userSettingsRepository.GetByIdOrDefault(userId);

            var questions = questionsQuery
                            .Select(q => new LearningQuizQuestion {
                QuestionId    = q.Id,
                Reoccurrences = settings.ReoccurrencesOnStart
            });

            await _learningQuizzesRepository.Create(learningQuiz);

            _learningQuizzesRepository.AddQuestions(learningQuiz, questions);
            var result = await Context.SaveChangesAsync() > 0;

            return(result ? learningQuiz : null);
        }
Exemplo n.º 2
0
        public async Task <IList <FlatQuestion> > QuestionsInQuizAsync(Guid quizId, string userId, long?maxTime = null)
        {
            var access = await _quizzesRepository.HaveReadAccessToQuizAsync(userId, quizId);

            if (!access)
            {
                return(null);
            }

            return(await _questionsRepository
                   .GetAllByQuizId(quizId, maxTime)
                   .Include(q => q.Versions)
                   .Select(q => q.FlatVersionProps(maxTime))
                   .Select(q => new FlatQuestion {
                Id = q.Id,
                QuizId = q.QuizId,
                Value = q.Value,
                CreationTime = q.CreationTime
            })
                   .ToListAsync());
        }
 private IQueryable <Question> RandomQuizQuestionsQuery(Guid id)
 {
     return(_questionsRepository
            .GetAllByQuizId(id)
            .Shuffle());
 }