public ActionResult Subscribe(int testId, int groupId) { try { int currentAccountId = AppHelpers.GetCurrentUser().AccountId; Account currentAccount = db.Accounts.FirstOrDefault((a) => a.AccountId == currentAccountId); var test = db.Tests.FirstOrDefault(t => t.TestId == testId); var group = db.Audiences.FirstOrDefault(a => a.AudienceId == groupId); List <TestParticipation> participations = new List <TestParticipation>(); foreach (var testTaker in group.TestTakers) { if (test.TestParticipations.Any(tp => tp.TestTakerId == testTaker.TestTakerId)) { continue; } var participation = new TestParticipation(test, testTaker); participations.Add(participation); } db.TestParticipations.AddRange(participations); db.SaveChanges(); return(Json(new { success = true })); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } }
public ActionResult PostAnswers(JsonTest test) { try { TestParticipation participation = db.TestParticipations.Where(p => p.AuthToken == test.token).FirstOrDefault(); Test realTest = db.Tests.Include(t => t.Sections.Select(s => s.Questions.Select(q => q.Options))) .FirstOrDefault(t => t.TestId == test.id); int numberOfQuestion = 0; int numberOfRightAnswers = 0; foreach (var section in test.sections.Values) { Section realSection = realTest.Sections.FirstOrDefault(s => s.SectionId == section.id); foreach (var question in section.questions.Values) { Question realQuestion = realSection.Questions.FirstOrDefault(q => q.QuestionId == question.id); string type = realQuestion.Type; bool allRight = true; foreach (var answer in question.answers.Values) { Option realOption = realQuestion.Options.FirstOrDefault(o => o.OptionId == answer.id); bool isWrongAnswer = realOption.IsRight != answer.isRight; if (isWrongAnswer) { allRight = false; } if (answer.isRight) { TestParticipationAnswer answerToAdd = new TestParticipationAnswer(participation, realQuestion, realOption, isWrongAnswer); db.TestParticipationAnswers.Add(answerToAdd); } } numberOfQuestion++; if (allRight) { numberOfRightAnswers++; } } } int result = 0; if (numberOfQuestion != 0) { result = 100 * numberOfRightAnswers / numberOfQuestion; } participation.SetComplete(result); db.SaveChanges(); return(Json(new { success = true, result = result })); } catch (Exception exc) { throw exc; return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } }