Example #1
0
        public async Task <ActionResult> ScoreExercise(int id, string nextUrl, string exerciseScore, bool?prohibitFurtherReview, string errorUrl = "", bool recheck = false)
        {
            if (string.IsNullOrEmpty(errorUrl))
            {
                errorUrl = nextUrl;
            }

            using (var transaction = db.Database.BeginTransaction())
            {
                var checking = slideCheckingsRepo.FindManualCheckingById <ManualExerciseChecking>(id);

                if (checking.IsChecked && !recheck)
                {
                    return(Redirect(errorUrl + "Эта работа уже была проверена"));
                }

                if (!checking.IsLockedBy(User.Identity))
                {
                    return(Redirect(errorUrl + "Эта работа проверяется другим инструктором"));
                }

                var course   = courseManager.GetCourse(checking.CourseId);
                var slide    = (ExerciseSlide)course.GetSlideById(checking.SlideId);
                var exercise = slide.Exercise;

                int score;
                /* Invalid form: score isn't integer */
                if (!int.TryParse(exerciseScore, out score))
                {
                    return(Redirect(errorUrl + "Неверное количество баллов"));
                }

                /* Invalid form: score isn't from range 0..MAX_SCORE */
                if (score < 0 || score > exercise.MaxReviewScore)
                {
                    return(Redirect(errorUrl + $"Неверное количество баллов: {score}"));
                }

                checking.ProhibitFurtherManualCheckings = false;
                await slideCheckingsRepo.MarkManualCheckingAsChecked(checking, score);

                if (prohibitFurtherReview.HasValue && prohibitFurtherReview.Value)
                {
                    await slideCheckingsRepo.ProhibitFurtherExerciseManualChecking(checking);
                }
                await visitsRepo.UpdateScoreForVisit(checking.CourseId, checking.SlideId, checking.UserId);

                transaction.Commit();
            }

            return(Redirect(nextUrl));
        }
Example #2
0
        public async Task <ActionResult> ScoreQuiz(int id, string nextUrl, string errorUrl = "")
        {
            metricSender.SendCount("quiz.manual_score");

            if (string.IsNullOrEmpty(errorUrl))
            {
                errorUrl = nextUrl;
            }

            using (var transaction = db.Database.BeginTransaction())
            {
                var checking = slideCheckingsRepo.FindManualCheckingById <ManualQuizChecking>(id);

                if (checking.IsChecked)
                {
                    return(Redirect(errorUrl + "Эта работа уже была проверена"));
                }

                if (!checking.IsLockedBy(User.Identity))
                {
                    return(Redirect(errorUrl + "Эта работа проверяется другим инструктором"));
                }

                var course = courseManager.GetCourse(checking.CourseId);
                var unit   = course.FindUnitBySlideId(checking.SlideId);

                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}");
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}");

                var answers = userQuizzesRepo.GetAnswersForUser(checking.CourseId, checking.SlideId, checking.UserId);

                QuizVersion quizVersion;
                /* If there is no user's answers for quiz, get the latest quiz version */
                if (answers.Count == 0)
                {
                    quizVersion = quizzesRepo.GetLastQuizVersion(checking.CourseId, checking.SlideId);
                }
                else
                {
                    var firstAnswer = answers.FirstOrDefault().Value.FirstOrDefault();
                    quizVersion = firstAnswer != null
                                                ? firstAnswer.QuizVersion
                                                : quizzesRepo.GetFirstQuizVersion(checking.CourseId, checking.SlideId);
                }

                var totalScore = 0;

                var quiz = quizVersion.GetRestoredQuiz(course, unit);
                foreach (var question in quiz.Blocks.OfType <AbstractQuestionBlock>())
                {
                    var scoreFieldName = "quiz__score__" + question.Id;
                    var scoreStr       = Request.Form[scoreFieldName];
                    /* Invalid form: score isn't integer */
                    if (!int.TryParse(scoreStr, out var score))
                    {
                        return(Redirect(errorUrl + $"Неверное количество баллов в задании «{question.QuestionIndex}. {question.Text.TruncateWithEllipsis(50)}»"));
                    }
                    /* Invalid form: score isn't from range 0..MAX_SCORE */
                    if (score < 0 || score > question.MaxScore)
                    {
                        return(Redirect(errorUrl + $"Неверное количество баллов в задании «{question.QuestionIndex}. {question.Text.TruncateWithEllipsis(50)}»: {score}"));
                    }

                    await userQuizzesRepo.SetScoreForQuizBlock(checking.CourseId, checking.UserId, checking.SlideId, question.Id, score);

                    totalScore += score;
                }

                await slideCheckingsRepo.MarkManualCheckingAsChecked(checking, totalScore);

                await visitsRepo.UpdateScoreForVisit(checking.CourseId, checking.SlideId, checking.UserId);

                transaction.Commit();

                metricSender.SendCount($"quiz.manual_score.score", totalScore);
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.score", totalScore);
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}.score", totalScore);
                if (totalScore == quiz.MaxScore)
                {
                    metricSender.SendCount($"quiz.manual_score.full_scored");
                    metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.full_scored");
                    metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}.full_scored");
                }

                await NotifyAboutManualQuizChecking(checking);
            }

            return(Redirect(nextUrl));
        }
Example #3
0
        public async Task <ActionResult> ScoreQuiz(int id, string nextUrl, string errorUrl = "")
        {
            metricSender.SendCount("quiz.manual_score");

            if (string.IsNullOrEmpty(errorUrl))
            {
                errorUrl = nextUrl;
            }

            using (var transaction = db.Database.BeginTransaction())
            {
                var checking = slideCheckingsRepo.FindManualCheckingById <ManualQuizChecking>(id);

                var course = courseManager.GetCourse(checking.CourseId);
                var unit   = course.FindUnitBySlideId(checking.SlideId);
                var slide  = course.GetSlideById(checking.SlideId);

                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}");
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}");

                var totalScore = 0;

                var quiz = course.FindSlideById(checking.SlideId);
                if (quiz == null)
                {
                    return(Redirect(errorUrl + "Этого теста больше нет в курсе"));
                }

                foreach (var question in quiz.Blocks.OfType <AbstractQuestionBlock>())
                {
                    var scoreFieldName = "quiz__score__" + question.Id;
                    var scoreStr       = Request.Form[scoreFieldName];
                    /* Invalid form: score isn't integer */
                    if (!int.TryParse(scoreStr, out var score))
                    {
                        return(Redirect(errorUrl + $"Неверное количество баллов в задании «{question.QuestionIndex}. {question.Text.TruncateWithEllipsis(50)}»"));
                    }
                    /* Invalid form: score isn't from range 0..MAX_SCORE */
                    if (score < 0 || score > question.MaxScore)
                    {
                        return(Redirect(errorUrl + $"Неверное количество баллов в задании «{question.QuestionIndex}. {question.Text.TruncateWithEllipsis(50)}»: {score}"));
                    }

                    await userQuizzesRepo.SetScoreForQuizBlock(checking.Submission.Id, question.Id, score).ConfigureAwait(false);

                    totalScore += score;
                }

                await slideCheckingsRepo.MarkManualCheckingAsChecked(checking, totalScore).ConfigureAwait(false);

                await visitsRepo.UpdateScoreForVisit(checking.CourseId, checking.SlideId, slide.MaxScore, checking.UserId).ConfigureAwait(false);

                transaction.Commit();

                metricSender.SendCount($"quiz.manual_score.score", totalScore);
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.score", totalScore);
                metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}.score", totalScore);
                if (totalScore == quiz.MaxScore)
                {
                    metricSender.SendCount($"quiz.manual_score.full_scored");
                    metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.full_scored");
                    metricSender.SendCount($"quiz.manual_score.{checking.CourseId}.{checking.SlideId}.full_scored");
                }

                if (unit != null && unitsRepo.IsUnitVisibleForStudents(course, unit.Id))
                {
                    await NotifyAboutManualQuizChecking(checking).ConfigureAwait(false);
                }
            }

            return(Redirect(nextUrl));
        }