Пример #1
0
        public SessionCompletedVM GetSessionCompletedVM(int testSessionId, SessionCompletedReason sessionCompletedReason)
        {
            var testSession = context.TestSessions
                              .Include(ts => ts.Test)
                              .ThenInclude(t => t.Questions)
                              .ThenInclude(q => q.Answers)
                              .Include(ts => ts.User)
                              .Single(ts => ts.Id == testSessionId);


            //var user = _users.Single(u => _testSessions.Single(o => o.Id == testSessionId).UserId == u.Id);
            //var testSession = _testSessions.Single(o => o.Id == testSessionId);
            //var test = _tests.Single(o => o.Id == testSession.TestId);
            var questionsAndAnswers = new QuestionsAndAnswersUtils()
            {
                Questions = context.Questions.ToArray(),
                Answers   = context.Answers.ToArray()
            };

            return(new SessionCompletedVM()
            {
                TestSessionId = testSessionId,
                Date = DateTime.UtcNow.Date.ToString("dd/MM/yyyy"),
                IsSuccessful = GradeUtils.CheckHasPassed(testSession, testSession.Test.PassPercentage, questionsAndAnswers),
                UserName = $"{testSession.User.FirstName} {testSession.User.Lastname}",
                SessionCompletedReason = sessionCompletedReason
            });
        }
Пример #2
0
        public async Task <IActionResult> CreateReview([FromBody] ReviewRequest request)
        {
            var deadline = await _settingsService.GetDeadline(_userContext.GetCredentials());

            var currentTerm = await _usosService.GetCurrentAcademicYear();

            var thesis = await _thesisRepository.GetAsync(request.ThesisGuid);

            var sendGradeConflictEmail = false;
            var sendGradeConfirmEmail  = false;

            if (DateTime.Now > deadline && thesis.TermId != currentTerm.Id)
            {
                return(new BadRequestObjectResult("Nie można dodać recenzji po upływie terminu końcowego."));
            }

            var currentUser = await _userRepository.GetAsync(HttpContext.CurrentUserUsosId());

            if (thesis == null)
            {
                return(new NotFoundResult());
            }
            var isPromoter = thesis.Promoter == currentUser;
            var isReviewer = thesis.Reviewer == currentUser;

            //Current user is nor promoter neither reviewer
            if (!isPromoter && !isReviewer)
            {
                return(new ForbidResult());
            }

            if (thesis.Reviews.Any(r => r.CreatedBy == currentUser))
            {
                return(new ConflictObjectResult("Recenzja dla tej pracy już istnieje."));
            }

            var questionIds     = request.QnAs.Keys;
            var questions       = _questionRepository.GetAll().Where(q => questionIds.Contains(q.Id)).ToList();
            var activeQuestions = _questionRepository.GetAll().Where(q => q.IsActive).ToList();

            //Not all active questions have been answered
            if (!activeQuestions.Select(q => q.Id).All(questions.Select(q => q.Id).Distinct().Contains) || activeQuestions.Count != questions.Count)
            {
                return(new BadRequestObjectResult("Nieprawidłowa lista pytań."));
            }

            if (request.IsConfirmed)
            {
                var requiredQuestions = activeQuestions.Where(p => p.IsRequired).Select(p => p.Id).ToList();
                if (requiredQuestions.Any(q => string.IsNullOrWhiteSpace(request.QnAs[q])))
                {
                    return(new BadRequestObjectResult("Brak odpowiedzi na jedno lub więcej wymaganych pytań"));
                }

                var reviewerReview = thesis.Reviews.FirstOrDefault(r => r.CreatedBy == thesis.Reviewer);
                var promoterReview = thesis.Reviews.FirstOrDefault(r => r.CreatedBy == thesis.Promoter);

                if ((isPromoter && reviewerReview != null && reviewerReview.IsConfirmed) ||
                    (isReviewer && promoterReview != null && promoterReview.IsConfirmed))
                {
                    var grades = new List <string>()
                    {
                        request.Grade, isPromoter ? reviewerReview.Grade : promoterReview.Grade
                    };

                    if (GradeUtils.TryGetAverageGrade(grades, out var average))
                    {
                        thesis.Grade          = average;
                        sendGradeConfirmEmail = true;
                    }
                    else
                    {
                        sendGradeConflictEmail = true;
                    }
                }
            }

            var reviewGuid = Guid.NewGuid();
            var review     = new Review
            {
                Guid        = reviewGuid,
                Thesis      = thesis,
                CreatedBy   = currentUser,
                Grade       = request.Grade,
                IsConfirmed = request.IsConfirmed,
                ReviewQnAs  = new List <ReviewQnA>()
            };

            foreach (var qna in request.QnAs)
            {
                review.ReviewQnAs.Add(new ReviewQnA
                {
                    Review   = review,
                    Question = questions.First(q => q.Id == qna.Key),
                    Answer   = new Answer
                    {
                        Text      = qna.Value,
                        CreatedBy = currentUser
                    }
                });
            }

            await _reviewRepository.AddAsync(review);

            //Saving change to log
            thesis.LogChange(currentUser, ModificationType.ReviewAdded);
            if (request.IsConfirmed)
            {
                thesis.LogChange(currentUser, ModificationType.ReviewConfirmed);
            }
            await _thesisRepository.UpdateAsync(thesis);

            if (sendGradeConflictEmail)
            {
                await _thesisService.SendEmailGradeConflict(thesis.Guid);
            }
            if (sendGradeConfirmEmail)
            {
                await _thesisService.SendEmailGradeConfirmed(thesis.Guid);
            }

            return(new CreatedResult("/reviews", reviewGuid));
        }