Esempio n. 1
0
        private int[] GetTheNumberOfQuestionsPerSection(ViewModels.TestDefinitionDetail test)
        {
            int[] noOfQuestionsInASection = new int[test.Sections.Count()];
            for (int i = 0; i < test.Sections.Count(); i++)
            {
                noOfQuestionsInASection[i] = questionRepository.GetQuestionIDsBySctions(new Guid[] { test.Sections[i].SectionID }).ToArray().Count();
            }

            int[] result = new int[test.Sections.Count()];
            int   noOfQuestionsToDistribute = test.NumberOfQuestions;

            while (noOfQuestionsToDistribute > 0)
            {
                bool resultUpdated = false;

                for (int i = 0; i < result.Length && noOfQuestionsToDistribute > 0; i++)
                {
                    if (noOfQuestionsInASection[i] > 0)
                    {
                        result[i]++;
                        noOfQuestionsInASection[i]--;
                        resultUpdated = true;
                        noOfQuestionsToDistribute--;
                    }
                }

                if (!resultUpdated)
                {
                    break;
                }
            }

            return(result);
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        private Guid[] GetRandomQuestionsBySection(ViewModels.TestDefinitionDetail test, int[] noOfQuestionsPerSection)
        {
            List <Guid> result = new List <Guid>();
            Random      rnd    = new Random();

            for (int i = 0; i < noOfQuestionsPerSection.Length; i++)
            {
                Guid[] questionsOfASection = questionRepository.GetQuestionIDsBySctions(new Guid[] { test.Sections[i].SectionID }).ToArray();
                result.AddRange(questionsOfASection.OrderBy(q => rnd.Next()).Take(noOfQuestionsPerSection[i]).ToList());
            }

            return(result.ToArray());
        }
Esempio n. 4
0
        public IHttpActionResult Get(Guid id)
        {
            ViewModels.TestDefinitionDetail test = testRepository.GetTestByID(id);

            if (test.IsPublished || Authorizer.IsAdmin(HttpContext.Current.User.Identity.Name))
            {
                return(Ok(test));
            }
            else
            {
                return(StatusCode(System.Net.HttpStatusCode.Unauthorized));
            }
        }
Esempio n. 5
0
        public ViewModels.TestDefinitionDetail GetTestByID(Guid id)
        {
            ViewModels.TestDefinitionDetail test = repository.GetByID <TestDefinition>(id).DeepCopyTo <ViewModels.TestDefinitionDetail>();

            if (test.QuestionsSelectedRandomly)
            {
                test.Sections = repository.All <TestSection>().Where(c => c.TestID == id).ToArray();
            }
            else
            {
                test.Questions = repository.All <TestQuestion>().Where(c => c.TestID == id).ToArray();
            }

            return(test);
        }
Esempio n. 6
0
        public void UpdateTest(ViewModels.TestDefinitionDetail test)
        {
            repository.Upsert(test.DeepCopyTo <TestDefinition>());

            TestSection[]  existingSections  = repository.All <TestSection>().Where(o => o.TestID == test.ID).ToArray();
            TestQuestion[] existingQuestions = repository.All <TestQuestion>().Where(o => o.TestID == test.ID).ToArray();

            if (test.QuestionsSelectedRandomly)
            {
                foreach (TestSection sectionToUpdate in test.Sections)
                {
                    TestSection existingItem = existingSections.FirstOrDefault(s => s.SectionID == sectionToUpdate.SectionID);
                    if (existingItem == null)
                    {
                        sectionToUpdate.ID     = Guid.NewGuid();
                        sectionToUpdate.TestID = test.ID;
                        repository.Upsert(sectionToUpdate);
                    }
                }
            }
            else
            {
                foreach (TestQuestion questionToupdate in test.Questions)
                {
                    TestQuestion existingItem = existingQuestions.FirstOrDefault(s => s.QuestionID == questionToupdate.QuestionID);
                    if (existingItem == null)
                    {
                        questionToupdate.ID     = Guid.NewGuid();
                        questionToupdate.TestID = test.ID;
                        repository.Upsert(questionToupdate);
                    }
                }
            }

            foreach (TestSection item in existingSections.Where(eq => test.Sections == null || !test.Sections.Any(q => q.SectionID == eq.SectionID)))
            {
                repository.Delete(item);
            }

            foreach (TestQuestion item in existingQuestions.Where(eq => test.Questions == null || !test.Questions.Any(q => q.QuestionID == eq.QuestionID)))
            {
                repository.Delete(item);
            }

            repository.SaveChanges();
        }
Esempio n. 7
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 }) }));
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
 public IHttpActionResult Post(ViewModels.TestDefinitionDetail test)
 {
     testRepository.UpdateTest(test);
     return(Ok());
 }