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); }
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)); }
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()); }
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)); } }
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); }
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(); }
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 }) })); }
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); }
public IHttpActionResult Post(ViewModels.TestDefinitionDetail test) { testRepository.UpdateTest(test); return(Ok()); }