private async Task <int> writeSiteTestToDb(SiteTestModel test) { return(await Task.Run(() => { var testId = Repo.Create(new SiteTest { Title = test.Title, Description = test.Desc }).Id; TestAnswer answer; string sTitle; string q; SiteTestSubject subject; SiteTestQuestion question; for (int i = 0; i < test.Subjects.Length; i++) { sTitle = test.Subjects[i]; subject = SiteTestSubjectsController.Repo.Create(new SiteTestSubject { Title = sTitle, TestId = testId }); for (int y = 0; y < test.Questions.Length; y++) { q = test.Questions[y]; answer = test.Answers.Where(x => x.SubjectTitle == sTitle && x.Question == q).SingleOrDefault(); question = SiteTestQuestionsController.Repo.Create(new SiteTestQuestion { Text = q, TestId = testId }); if (answer.IsCorrect) { Repo.RevertMatrixValue(sTitle, q, 0); } } } return testId; })); }
private async Task<int> writeSiteTestToDb(SiteTestModel test) { return await Task.Run(() => { var testId = Repo.Create(new SiteTest { Title = test.Title, Description = test.Desc }).Id; TestAnswer answer; string sTitle; string q; SiteTestSubject subject; SiteTestQuestion question; for (int i = 0; i < test.Subjects.Length; i++) { sTitle = test.Subjects[i]; subject = SiteTestSubjectsController.Repo.Create(new SiteTestSubject { Title = sTitle, TestId = testId }); for (int y = 0; y < test.Questions.Length; y++) { q = test.Questions[y]; answer = test.Answers.Where(x => x.SubjectTitle == sTitle && x.Question == q).SingleOrDefault(); question = SiteTestQuestionsController.Repo.Create(new SiteTestQuestion { Text = q, TestId = testId }); if (answer.IsCorrect) Repo.RevertMatrixValue(sTitle, q, 0); } } return testId; }); }
public async Task<RedirectToRouteResult> LoadFromFile(HttpPostedFileBase file) { var test = new SiteTestModel(); var questions = new List<string>(); var subjects = new List<string>(); using (var excel = new ExcelPackage(file.InputStream)) { var ws = excel.Workbook.Worksheets.First(); var range = ws.Cells["A1"]; test.Title = range.Value.ToString().Trim(); range = ws.Cells["B1"]; test.Desc = range.Value?.ToString().Trim(); //questions range = ws.Cells["C1"]; var startRow = range.Start.Row; var startColumn = range.Start.Column; var count = 0; while (range.Value != null) { count++; questions.Add(range.Value.ToString().Trim()); range = ws.Cells[startRow, startColumn + count]; } test.Questions = questions.ToArray(); count = 0; range = ws.Cells["A2"]; startRow = range.Start.Row; startColumn = range.Start.Column; while (range.Value != null) { count++; subjects.Add(range.Value.ToString().Trim()); range = ws.Cells[startRow + count, startColumn]; } test.Subjects = subjects.ToArray(); range = ws.Cells["A2"]; TestAnswer answer; string subjectTitle; string subjectDesc; for (int i = 0; i < test.Subjects.Length; i++) { subjectTitle = range.Value.ToString().Trim(); startColumn++; range = ws.Cells[startRow, startColumn]; subjectDesc = range.Value?.ToString().Trim(); for (int y = 0; y < test.Questions.Length; y++) { startColumn++; range = ws.Cells[startRow, startColumn]; answer = new TestAnswer { SubjectTitle = subjectTitle, SubjectDesc = subjectDesc, Question = test.Questions[y], IsCorrect = range.Value != null && range.Value.ToString() == "1" ? true : false }; test.Answers.Add(answer); } startRow++; startColumn = 1; range = ws.Cells[startRow, startColumn]; } var id = await writeSiteTestToDb(test); return RedirectToAction("edit", new { id = id }); } }
public async Task <RedirectToRouteResult> LoadFromFile(HttpPostedFileBase file) { var test = new SiteTestModel(); var questions = new List <string>(); var subjects = new List <string>(); using (var excel = new ExcelPackage(file.InputStream)) { var ws = excel.Workbook.Worksheets.First(); var range = ws.Cells["A1"]; test.Title = range.Value.ToString().Trim(); range = ws.Cells["B1"]; test.Desc = range.Value?.ToString().Trim(); //questions range = ws.Cells["C1"]; var startRow = range.Start.Row; var startColumn = range.Start.Column; var count = 0; while (range.Value != null) { count++; questions.Add(range.Value.ToString().Trim()); range = ws.Cells[startRow, startColumn + count]; } test.Questions = questions.ToArray(); count = 0; range = ws.Cells["A2"]; startRow = range.Start.Row; startColumn = range.Start.Column; while (range.Value != null) { count++; subjects.Add(range.Value.ToString().Trim()); range = ws.Cells[startRow + count, startColumn]; } test.Subjects = subjects.ToArray(); range = ws.Cells["A2"]; TestAnswer answer; string subjectTitle; string subjectDesc; for (int i = 0; i < test.Subjects.Length; i++) { subjectTitle = range.Value.ToString().Trim(); startColumn++; range = ws.Cells[startRow, startColumn]; subjectDesc = range.Value?.ToString().Trim(); for (int y = 0; y < test.Questions.Length; y++) { startColumn++; range = ws.Cells[startRow, startColumn]; answer = new TestAnswer { SubjectTitle = subjectTitle, SubjectDesc = subjectDesc, Question = test.Questions[y], IsCorrect = range.Value != null && range.Value.ToString() == "1" ? true : false }; test.Answers.Add(answer); } startRow++; startColumn = 1; range = ws.Cells[startRow, startColumn]; } var id = await writeSiteTestToDb(test); return(RedirectToAction("edit", new { id = id })); } }