public async Task <ActionResult> Draft([FromBody] ExamSubmission submission)
        {
            if (submission == null || submission.ExamAnswers == null ||
                submission.ExamAnswers.Count == 0 || string.IsNullOrWhiteSpace(submission.ExamVersion))
            {
                return(BadRequest());
            }
            if (!IsValidVersion(submission.ExamVersion))
            {
                return(NotFound());
            }

            var drafts = await UserSubmissionsAsync();

            var draft = drafts.Value.SingleOrDefault(s => s.Version == submission.ExamVersion);

            if (draft == null)
            {
                draft = new AuditExamSubmission
                {
                    IsSubmitted = false,
                    UserID      = drafts.Key.Id,
                    Version     = submission.ExamVersion,
                };
                db_.AuditExamSubmissions.Add(draft);
            }
            draft.RawSubmission = JsonConvert.SerializeObject(submission);
            draft.SubmitTime    = DateTime.Now;
            await db_.SaveChangesAsync();

            return(Ok());
        }
                  e.Course.TrainerId == userId);  // course trainer

        public async Task <bool> CreateAsync(int courseId, string userId, string fileName, string fileUrl)
        {
            var studentCourseFound = await this.db
                                     .Courses
                                     .Where(c => c.Id == courseId)
                                     .Where(c => c.Students.Any(sc => sc.StudentId == userId))
                                     .AnyAsync();

            if (!studentCourseFound ||
                string.IsNullOrWhiteSpace(fileName) ||
                string.IsNullOrWhiteSpace(fileUrl))
            {
                return(false);
            }

            var examSubmission = new ExamSubmission
            {
                CourseId       = courseId,
                StudentId      = userId,
                FileName       = fileName,
                FileUrl        = fileUrl,
                SubmissionDate = DateTime.UtcNow
            };

            await this.db.ExamSubmissions.AddAsync(examSubmission);

            var result = await this.db.SaveChangesAsync();

            var success = result > 0;

            return(success);
        }
        public async Task <ActionResult> Draft(string version)
        {
            if (!IsValidVersion(version))
            {
                return(NotFound());
            }
            var draft = await db_.AuditExamSubmissions.SingleOrDefaultAsync(u => u.User.UserName == User.Identity.Name && u.Version == version && !u.IsSubmitted);

            ExamSubmission submission = null;

            if (draft != null)
            {
                submission = JsonConvert.DeserializeObject <ExamSubmission>(draft.RawSubmission);
            }
            return(Json(submission));
        }
Beispiel #4
0
        private async Task <UniversityDbContext> PrepareUserExams()
        {
            var user = new User {
                Id = UserIdValid
            };
            var userOther = new User {
                Id = "AnotherUser"
            };

            var course1 = new Course {
                Id = CourseIdSecond, Name = CourseNameSecond
            };
            var course2 = new Course {
                Id = CourseIdFirst, Name = CourseNameFirst
            };

            var exam1 = new ExamSubmission {
                Id = 1, CourseId = CourseIdSecond, SubmissionDate = DateFirst, StudentId = UserIdValid
            };
            var exam2 = new ExamSubmission {
                Id = 2, CourseId = CourseIdSecond, SubmissionDate = DateSecond, StudentId = UserIdValid
            };
            var exam3 = new ExamSubmission {
                Id = 3, CourseId = CourseIdFirst, SubmissionDate = DateThird, StudentId = UserIdValid
            };

            var examOther = new ExamSubmission {
                Id = 4, CourseId = CourseIdSecond, SubmissionDate = DateThird, StudentId = UserIdInvalid
            };

            var db = Tests.InitializeDatabase();
            await db.Courses.AddRangeAsync(course1, course2);

            await db.ExamSubmissions.AddRangeAsync(exam1, exam2, exam3, examOther);

            await db.Users.AddRangeAsync(user, userOther);

            await db.SaveChangesAsync();

            return(db);
        }
        public async Task <ActionResult> Submit([FromBody] ExamSubmission submission)
        {
            if (submission == null || submission.ExamAnswers == null || submission.ExamAnswers.Count == 0)
            {
                return(BadRequest());
            }
            if (!IsValidVersion(submission.ExamVersion))
            {
                return(NotFound());
            }
            var submissions = await UserSubmissionsAsync();

            var submit = submissions.Value.SingleOrDefault(s => s.Version == submission.ExamVersion);

            if (submit == null)
            {
                submit = new AuditExamSubmission
                {
                    UserID  = submissions.Key.Id,
                    Version = submission.ExamVersion,
                };
            }
            else if (submit.IsSubmitted)
            {
                return(BadRequest());
            }
            submit.IsSubmitted   = true;
            submit.RawSubmission = JsonConvert.SerializeObject(submission);

            ExamResult result = GetResult(submission);

            submit.SubmitTime = result.SubmitTime;
            submit.HasPassed  = result.HasPassed;
            submit.Score      = result.Score;
            submit.RawResult  = JsonConvert.SerializeObject(result);
            await db_.SaveChangesAsync();

            return(Json(result));
        }
        private ExamResult GetResult(ExamSubmission submission)
        {
            var submissionDict = submission.ExamAnswers.ToDictionary(e => e.QuestionId);
            var exam           = config_.Exams.FirstOrDefault(e => e.Version == submission.ExamVersion);

            var examResult = exam.Answers.Aggregate(new ExamResult
            {
                ExamAnswers = new List <ExamResult.ExamAnswer>(),
                ExamVersion = exam.Version,
                Score       = 0,
                SubmitTime  = DateTime.Now,
                IsSubmitted = true,
            }, (results, actual) =>
            {
                var result = new ExamResult.ExamAnswer
                {
                    Point      = 0,
                    QuestionId = actual.Id,
                    Comment    = actual.Comment,
                    Correct    = false,
                };
                if (submissionDict.ContainsKey(actual.Id))
                {
                    var submittedAnswer = submissionDict[actual.Id];
                    result.Answer       = submittedAnswer.Answer;
                    result.Correct      = AnswerEquals(submittedAnswer.Answer, actual.Answer);
                    result.Point        = result.Correct ? actual.Point : 0;
                }
                results.ExamAnswers.Add(result);
                results.Score += result.Point;
                return(results);
            });

            examResult.HasPassed = exam.PassScore <= examResult.ExamAnswers.Sum(e => e.Point);
            return(examResult);
        }
        private async Task <UniversityDbContext> PrepareStudentInCourseExamSubmissions()
        {
            var pastDate   = DateTime.UtcNow.AddDays(-2);
            var futureDate = DateTime.UtcNow.AddDays(1);

            var student = new User {
                Id = StudentEnrolled
            };
            var trainer = new User {
                Id = TrainerValid
            };

            var coursePast = new Course {
                Id = CourseValid, TrainerId = TrainerValid, EndDate = pastDate
            };

            coursePast.Students.Add(new StudentCourse {
                StudentId = StudentEnrolled, GradeBg = PreviousGradeBg
            });

            var courseCurrent = new Course {
                Id = CourseCurrent, TrainerId = TrainerValid, EndDate = futureDate
            };

            courseCurrent.Students.Add(new StudentCourse {
                StudentId = StudentEnrolled
            });

            var exam1 = new ExamSubmission
            {
                Id             = ExamValid,
                CourseId       = CourseValid,
                StudentId      = StudentEnrolled,
                SubmissionDate = new DateTime(2019, 7, 10, 14, 15, 00), // latest
                FileUrl        = FileUrl
            };
            var exam2 = new ExamSubmission
            {
                Id             = 2,
                CourseId       = CourseValid,
                StudentId      = StudentEnrolled,
                SubmissionDate = new DateTime(2019, 7, 1, 14, 18, 00),
                FileUrl        = $"{FileUrl}-2"
            };
            var exam3 = new ExamSubmission
            {
                Id             = 3,
                CourseId       = CourseCurrent,
                StudentId      = StudentEnrolled,
                SubmissionDate = new DateTime(2019, 7, 1, 14, 20, 50),
                FileUrl        = $"{FileUrl}-3"
            };

            var db = Tests.InitializeDatabase();
            await db.Users.AddRangeAsync(student, trainer);

            await db.Courses.AddRangeAsync(coursePast, courseCurrent);

            await db.ExamSubmissions.AddRangeAsync(new List <ExamSubmission> {
                exam1, exam2, exam3
            });

            await db.SaveChangesAsync();

            return(db);
        }