Exemplo n.º 1
0
        public IHttpActionResult EndExam(ViewModels.ExamDetail exam)
        {
            ViewModels.ExamDetail existingExam = examRepository.GetExamByID(exam.ID);

            if (existingExam == null)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }

            if (existingExam.EndDate < DateTime.Now || existingExam.IsEnded)
            {
                return(StatusCode(HttpStatusCode.NotAcceptable));
            }

            if (existingExam.Answers.Length != exam.Answers.Length || !existingExam.Answers.All(ea => exam.Answers.Any(a => a.QuestionID == ea.QuestionID)))
            {
                return(StatusCode(HttpStatusCode.NotAcceptable));
            }

            examService.EvaluateAnswers(existingExam, exam);
            existingExam.EndDate = DateTime.Now;
            examRepository.UpdateExam(existingExam);

            ViewModels.TestDefinitionDetail test = testRepository.GetTestByID(exam.TestID);

            return(Ok(test.ShowScoreWhenCompleted ? existingExam.TotalScore.ToString() : string.Empty));
        }
Exemplo n.º 2
0
        public void UpdateExam(ViewModels.ExamDetail exam)
        {
            repository.Upsert(exam.DeepCopyTo <Exam>());

            foreach (var item in exam.Answers)
            {
                repository.Upsert(item);
            }

            repository.SaveChanges();
        }
Exemplo n.º 3
0
        public void EvaluateAnswers(ViewModels.ExamDetail initialExam, ViewModels.ExamDetail submitedExam)
        {
            OptionDefinition[] options = questionRepository.GetOptionsByQuestionIDs(initialExam.Answers.Select(a => a.QuestionID).ToArray());

            foreach (ExamAnswer answer in initialExam.Answers)
            {
                answer.Answers = submitedExam.Answers.First(a => a.QuestionID == answer.QuestionID).Answers;
            }

            initialExam.TotalScore = submitedExam.Answers.Count(optionAnswer => optionAnswer.Answers != null && optionAnswer.Answers.Length == options.Count(o => o.QuestionID == optionAnswer.QuestionID) && options.Where(o => o.QuestionID == optionAnswer.QuestionID).All(o => optionAnswer.Answers.Contains(o.ID)));
        }
Exemplo n.º 4
0
        public ViewModels.ExamDetail GetExamByID(Guid id)
        {
            ViewModels.ExamDetail exam  = repository.GetByID <Exam>(id).DeepCopyTo <ViewModels.ExamDetail>();
            TestDefinition[]      tests = repository.All <TestDefinition>().ToArray();
            exam.Title          = (tests.FirstOrDefault(s => s.ID == exam.TestID) ?? new TestDefinition {
                Title = string.Empty
            }).Title;
            exam.Answers        = repository.All <ExamAnswer>().Where(o => o.ExamID == id).ToArray();
            exam.MinutesAllowed = (tests.FirstOrDefault(s => s.ID == exam.TestID) ?? new TestDefinition {
                MinutesAllowed = 0
            }).MinutesAllowed;

            return(exam);
        }
Exemplo n.º 5
0
        public IHttpActionResult TakeExam(Exam exam)
        {
            if (!Authorizer.IsAdmin(HttpContext.Current.User.Identity.Name) && string.IsNullOrWhiteSpace(exam.CandidateName))
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }

            exam.CandidateName = exam.CandidateName ?? HttpContext.Current.User.Identity.Name;

            ViewModels.TestDefinitionDetail test = testRepository.GetTestByID(exam.TestID);

            if (!test.IsPublished)
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }

            ViewModels.ExamDetail examCreated = examService.CreateExam(test, exam.CandidateName);

            examRepository.UpdateExam(examCreated);

            return(Ok(new { CandidateName = examCreated.CandidateName, TestID = examCreated.TestID, ID = examCreated.ID, Answers = examCreated.Answers.Select(a => new { QuestionID = a.QuestionID }) }));
        }
Exemplo n.º 6
0
        public ViewModels.ExamDetail CreateExam(ViewModels.TestDefinitionDetail test, string candidateName)
        {
            Random rnd = new Random();

            Guid[] questionsSelected;

            if (test.QuestionsSelectedRandomly)
            {
                int[] noOfQuestionsPerSection = GetTheNumberOfQuestionsPerSection(test);
                questionsSelected = GetRandomQuestionsBySection(test, noOfQuestionsPerSection);
            }
            else
            {
                questionsSelected = test.Questions.Select(q => q.QuestionID).ToArray();
            }

            ViewModels.ExamDetail exam = new ViewModels.ExamDetail
            {
                ID            = Guid.NewGuid(),
                AllQuestions  = test.NumberOfQuestions,
                StartDate     = DateTime.Now.AddSeconds(5),
                EndDate       = DateTime.Now.AddSeconds(5).AddMinutes(test.MinutesAllowed),
                CandidateName = candidateName,
                TestID        = test.ID,
                IsEnded       = false
            };

            exam.Answers = questionsSelected.OrderBy(q => rnd.Next()).Select(q => new ExamAnswer
            {
                ID         = Guid.NewGuid(),
                ExamID     = exam.ID,
                QuestionID = q
            }).ToArray();

            exam.AllQuestions = exam.Answers.Length;
            return(exam);
        }