public ActionResult SaveAnswer(Guid id, List <Guid> answers)
        {
            _logger.LogInformation("GET Test/PassTest");
            User user = null;

            if (Get("user") != null)
            {
                Guid user_id = Guid.Parse(Get("user"));
                user = _userRepo.GetUser(user_id);
            }
            else
            {
                _logger.LogInformation("User is not logged!");
            }
            if (user != null && user.Role == "Student")
            {
                var taskResult = _taskResultRepo.GetTaskResult(id);
                if (taskResult != null)
                {
                    var    totalCount        = taskResult.Task.Options.Count;
                    var    goalCount         = taskResult.Task.Options.Count(x => x.IsCorrect);
                    var    actualTotalCount  = answers.Count;
                    double penaltyMultiplier = 1.0;
                    if (actualTotalCount == totalCount)
                    {
                        penaltyMultiplier = 0;
                    }
                    if (actualTotalCount > goalCount)
                    {
                        penaltyMultiplier = 0.5;
                    }
                    var actualCount = answers.Count(x => _optionRepo.GetOption(x).IsCorrect);
                    var result      = ((double)actualCount / (double)goalCount) * penaltyMultiplier;
                    taskResult.Percent = result;
                    _taskResultRepo.UpdateTaskResult(taskResult.Id, taskResult);
                    foreach (var option in taskResult.Task.Options)
                    {
                        var optionResult = _optionResultRepo.GetOptionResult(taskResult.Id, option.Id);
                        if (optionResult != null)
                        {
                            optionResult.IsChecked = answers.Exists(x => x == option.Id);
                            _optionResultRepo.UpdateOptionResult(optionResult.Id, optionResult);
                        }
                        else
                        {
                            optionResult = new OptionResult(taskResult, option, answers.Exists(x => x == option.Id));
                            _optionResultRepo.AddOptionResult(optionResult);
                        }
                    }
                    return(RedirectToAction("CurrentTest", new { testResultId = taskResult.TestResult.Id }));
                }
            }
            if (user != null)
            {
                _logger.LogInformation("User is not student!");
            }
            else
            {
                _logger.LogInformation("User authorised but not exist!");
            }
            return(RedirectToAction("Error"));
        }
Exemple #2
0
        //
        // GET: /Option/Edit/5

        public ActionResult Edit(int id)
        {
            var option = _optionRepository.GetOption(id);

            return(View(option));
        }