public ActionResult ResetPassword(StudentViewModel model) { using (CyberSchoolEntities _context = new CyberSchoolEntities()) { var account = _context.Student.Where(x => x.Email == model.UserEmail).FirstOrDefault(); if (account != null) { model.FirstName = account.FirstName; account.ResetPassCode = Guid.NewGuid().ToString(); _context.Entry(account).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); model.ResetPasswordCode = account.ResetPassCode; if (IsSentMail(model)) { TempData["message"] = @"We've sent an email to you. Click the link in the email to reset your password. If you don't see the email, check other places it might be, like your junk, spam, social or other folders."; return(RedirectToAction("ResetPassword")); } else { TempData["message"] = "There was a problem in sending the email. Please contact your network administrator"; return(RedirectToAction("ResetPassword")); } } else { TempData["ResultCode"] = "003"; TempData["message"] = "There are no accounts registered with this email account. Please check your email and try angain"; return(RedirectToAction("ResetPassword")); } } }
//Get final result public ActionResult FinalResult(string token) { using (CyberSchoolEntities context = new CyberSchoolEntities()) { TestEntry objTestEntry = context.TestEntry.Where(x => x.Token == token).FirstOrDefault(); if (objTestEntry == null) { TempData["message"] = "You have invalid token. Please re-login and try again."; return(RedirectToAction("Index", "Login")); } int marks = 0; if (objTestEntry.TotalMarks != null) { marks = (int)objTestEntry.TotalMarks; } int testID = (int)objTestEntry.TestID; List <Question> objQuestionList = context.Question.Where(x => x.TestId == testID && x.IsActive == 1 && x.IsDeleted == 0).ToList(); double totalMarks = 0; foreach (var item in objQuestionList) { totalMarks += (double)item.PointsOfQuestion; } TempData["FinalMarks"] = (int)((marks / totalMarks) * 100); Test objTest = context.Test.Where(o => o.Id == objTestEntry.TestID).FirstOrDefault(); TempData["Grade"] = objTest.GradeVsSubject.Grade.Grade1.ToString(); TempData["Subject"] = objTest.GradeVsSubject.Subject.SubjectName; TempData["TestName"] = objTest.TestName; TempData["Part"] = objTest.PaperPart; try { objTestEntry.Token = Guid.NewGuid().ToString(); objTestEntry.TokenExpireTime = DateTime.Now; context.Entry(objTestEntry).State = EntityState.Modified; context.SaveChanges(); } catch (Exception) { TempData["dbError"] = "Server Error. Please Login again."; return(RedirectToAction("Index", "Login")); } } return(View()); }
public ActionResult PostAnswer(AnswerViewModel model) { if (model.token == null) { TempData["message"] = "You have invalid token. Please re-login and try again."; return(RedirectToAction("Index", "Login")); } using (CyberSchoolEntities context = new CyberSchoolEntities()) { try { int isCorrect = 0; TestEntry objTestEntry = context.TestEntry.Where(x => x.Token == model.token).FirstOrDefault(); List <Answers> objAnswerList; if (objTestEntry.TotalMarks == null) { objTestEntry.TotalMarks = 0; } if (objTestEntry.RightAnswers != null) { string answers = objTestEntry.RightAnswers; objAnswerList = JsonConvert.DeserializeObject <List <Answers> >(answers); if (objAnswerList.Any(x => x.QuestionID == model.QuestionID)) { var updateChoiceID = objAnswerList.FirstOrDefault(x => x.QuestionID == model.QuestionID); if (updateChoiceID != null) { if (model.CorrectAnswer == model.choiceID && updateChoiceID.ChoiceID != model.choiceID) { updateChoiceID.IsCorrect = 1; objTestEntry.TotalMarks += model.mark; } else { if (updateChoiceID.ChoiceID == model.CorrectAnswer && model.CorrectAnswer != model.choiceID) { updateChoiceID.IsCorrect = 0; objTestEntry.TotalMarks -= model.mark; } } updateChoiceID.ChoiceID = model.choiceID; //updateChoiceID.QuestionNumber = model.QuestionNumber; objTestEntry.RightAnswers = JsonConvert.SerializeObject(objAnswerList.ToList()); context.Entry(objTestEntry).State = EntityState.Modified; context.SaveChanges(); } } else { if (model.CorrectAnswer == model.choiceID) { isCorrect = 1; } else { isCorrect = 0; } objAnswerList.Add(new Answers { QuestionID = model.QuestionID, ChoiceID = model.choiceID, IsCorrect = isCorrect, QuestionNumber = model.QuestionNumber }); if (isCorrect == 1) { objTestEntry.TotalMarks += model.mark; } objTestEntry.RightAnswers = JsonConvert.SerializeObject(objAnswerList.ToList()); context.Entry(objTestEntry).State = EntityState.Modified; context.SaveChanges(); } } else { objAnswerList = new List <Answers>(); if (model.CorrectAnswer == model.choiceID) { isCorrect = 1; } else { isCorrect = 0; } objAnswerList.Add(new Answers { QuestionID = model.QuestionID, ChoiceID = model.choiceID, IsCorrect = isCorrect, QuestionNumber = model.QuestionNumber }); if (isCorrect == 1) { objTestEntry.TotalMarks = model.mark; } objTestEntry.RightAnswers = JsonConvert.SerializeObject(objAnswerList.ToList()); context.Entry(objTestEntry).State = EntityState.Modified; context.SaveChanges(); } model.AnswerList = objAnswerList; //model.AnswerList.Add(model.choiceID.ToString()); string answerModel = JsonConvert.SerializeObject(model.AnswerList.ToList()); } catch (Exception) { TempData["dbError"] = "Server Error. Please Login again."; return(RedirectToAction("Index", "Login")); } } if (model.Direction == "forward") { model.QuestionNumber++; } else if (model.Direction == "backward") { model.QuestionNumber--; } else if (model.SelectQuestionNumber != 0) { model.QuestionNumber = model.SelectQuestionNumber; } else { return(RedirectToAction("FinalResult", "Exam", new { token = model.token })); } return(RedirectToAction("ViewQuestion", "Exam", new { questionNumber = model.QuestionNumber, token = model.token, answerModel = JsonConvert.SerializeObject(model.AnswerList.ToList()) })); }
public ActionResult Index(OpenExcelViewModel model) { try { Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook wb; Microsoft.Office.Interop.Excel.Worksheet ws; if (!ModelState.IsValid) { TempData["ErrorMessage"] = "Please select a excel file"; return(View(model)); } if (model.ExcelPaperFile.ContentLength == 0) { TempData["ErrorMessage"] = "Please select a excel file"; return(View(model)); } else { if (model.ExcelPaperFile.FileName.EndsWith("xls") || model.ExcelPaperFile.FileName.EndsWith("xlsx")) { //string fileName = "Exam" + DateTime.Now.ToString("yyMMddhhmmss") + Path.GetExtension(model.ExcelPaperFile.FileName); //model.SavedFileUrl = WebConfigurationManager.AppSettings["BaseDirectory"] + fileName; //fileName = Path.Combine( // Server.MapPath(WebConfigurationManager.AppSettings["BaseDirectory"]), fileName); //model.ExcelPaperFile.SaveAs(fileName); //(WebConfigurationManager.AppSettings["BaseDirectory"] + model.ExcelPaperFile.FileName); string path = WebConfigurationManager.AppSettings["BaseDirectory"] + "Paper" + DateTime.Now.ToString("yyMMddhhmmss") + Path.GetExtension(model.ExcelPaperFile.FileName); model.ExcelPaperFile.SaveAs(path); wb = excel.Workbooks.Open(path); ws = wb.Worksheets[1]; model.Grade = ws.Cells[1, 6].value2 == null ? 0 : Convert.ToInt32(ws.Cells[1, 6].value2); model.Subject = ws.Cells[2, 6].value2 == null ? "" : ws.Cells[2, 6].value2; model.PaperName = ws.Cells[3, 6].value2 == null ? "" : ws.Cells[3, 6].value2; model.PaperPart = Convert.ToInt32(ws.Cells[4, 6].value2); model.PaperTime = Convert.ToInt32(ws.Cells[5, 6].value2); if (!ValidateTheFields(model)) { return(View(model)); } System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("Question Number", typeof(int)); dt.Columns.Add("Question", typeof(string)); dt.Columns.Add("Choice 1", typeof(string)); dt.Columns.Add("Choice 2", typeof(string)); dt.Columns.Add("Choice 3", typeof(string)); dt.Columns.Add("Choice 4", typeof(string)); dt.Columns.Add("Answer", typeof(int)); dt.Columns.Add("Lesson", typeof(int)); dt.Columns.Add("Points", typeof(int)); for (int i = 10; i < 170; i = i + 4) { if (ws.Cells[i, 2].value2 == null) { break; } int lesson = 0; if (ws.Cells[i, 17].value2 != null) { lesson = Convert.ToInt32(ws.Cells[i, 17].value2); } if (ws.Cells[i, 16].value2 == null) { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = "Uploading failed. The correct answer column for the question " + (i - 6) / 4 + " is not specified. Please recheck and upload"; return(View(model)); } if (ws.Cells[i, 18].value2 == null) { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = "Uploading failed. The Marks column for the question " + (i - 6) / 4 + " is not specified. Please recheck and upload"; return(View(model)); } dt.Rows.Add(ws.Cells[i, 1].value2, ws.Cells[i, 2].value2, ws.Cells[i, 12].value2, ws.Cells[i, 13].value2, ws.Cells[i, 14].value2, ws.Cells[i, 15].value2, ws.Cells[i, 16].value2, lesson, ws.Cells[i, 18].value2); } model.QuestionTable = dt; wb.Close(); excel.Quit(); Marshal.FinalReleaseComObject(excel); Marshal.FinalReleaseComObject(wb); Marshal.FinalReleaseComObject(ws); Test testModel = new Test(); testModel.GradeVsSubject = _context.GradeVsSubject.Where(x => x.Grade.Grade1 == model.Grade && x.Subject.SubjectName == model.Subject).FirstOrDefault(); testModel.TestCode = DateTime.Now.ToString("yyMMddhhmm"); testModel.TestName = model.PaperName; testModel.PaperPart = model.PaperPart; testModel.DurationInMinutes = model.PaperTime; testModel.PaperCreator = model.UploaderName; testModel.IsActive = (int)IsActive.YES; testModel.IsDeleted = (int)IsDeleted.NO; testModel.TestDescription = "x"; _context.Test.Add(testModel); _context.SaveChanges(); foreach (DataRow row in dt.Rows) { Question question = new Question(); question.TestId = testModel.Id; question.QuestionNumber = Convert.ToInt32(row["Question Number"]); question.Question1 = row["Question"].ToString(); question.PointsOfQuestion = row["Points"] == null ? 1 : Convert.ToDouble(row["Points"]); question.IsActive = (int)IsActive.YES; question.IsDeleted = (int)IsDeleted.NO; _context.Question.Add(question); _context.SaveChanges(); for (int i = 1; i < 5; i++) { Choice choices = new Choice(); choices.QuestionId = question.Id; choices.ChoiceLabel = row["Choice " + i].ToString(); choices.ChoiceNumber = i; choices.IsActive = (int)IsActive.YES; choices.IsDeleted = (int)IsDeleted.NO; _context.Choice.Add(choices); _context.SaveChanges(); if (choices.ChoiceNumber == Convert.ToInt32(row["Answer"])) { question.CorrectAnswer = choices.Id; _context.Entry(question).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } } } TempData["ResultCode"] = (int)ResultCode.SUCCESS; TempData["ResultMessage"] = "Added Successfully"; return(View(model)); } else { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = "The class is already exists."; return(View(model)); } } } catch (Exception e) { TempData["ResultCode"] = (int)ResultCode.FAILED; TempData["ResultMessage"] = e.ToString(); return(View(model)); } }