/// <summary> /// The ExportToPdfView /// </summary> /// <param name="examPaperId">The examPaperId<see cref="int"/></param> /// <returns>The <see cref="ActionResult"/></returns> public ActionResult ExportToPdfView(int examPaperId) { try { Models.ExamPaper examPaper = new Models.ExamPaper(); examPaper = examPaperService.GetExamPaperById(examPaperId); return(new ActionAsPdf("_ExportToPdf", new { examPaperId = examPaperId }) { FileName = examPaper.Title + ".pdf" }); } catch (Exception e) { Failure = e.Message; return(Json(new { status = false }, JsonRequestBehavior.AllowGet)); } }
public ActionResult ExamPaper(Models.ExamPaper examPaper) { try { if (ModelState.IsValid) { if (examPaper.ExamPaperID == 0) { examPaper.NumberOfQuestion = 0; examPaper.CreatedDate = DateTime.Now; examPaper.CreatedBy = int.Parse(Session["Name"].ToString()); examPaper.ModifiedBy = int.Parse(Session["Name"].ToString()); if (examPaperService.Create(examPaper) > 0) { Success = "Insert exam paper successfully!"; return(RedirectToAction("ExamPapers")); } } else { examPaper.ModifiedDate = DateTime.Now; examPaper.ModifiedBy = int.Parse(Session["Name"].ToString()); //lay so cau hoi examPaper.NumberOfQuestion = examPaperService.GetNumberOfQuestionByExamPaperId(examPaper.ExamPaperID); if (examPaperService.Edit(examPaper) > 0) { Success = "Update exam paper successfully!"; return(RedirectToAction("ExamPapers")); } } } Failure = "Something went wrong, please try again!"; return(new JsonResult { Data = new { status = false } }); } catch (Exception exception) { Failure = exception.Message; return(View(examPaper)); } }
public ActionResult ExamPaper(int?examPaperId) { var model = new Models.ExamPaper(); var questions = new List <QuestionDto>(); if (examPaperId == null || examPaperId == 0) { ViewBag.IsUpdate = false; ViewBag.Questions = questions; return(View(model)); } model = examPaperService.GetExamPaperById(examPaperId.Value); if (model != null) { questions = questionService.GetQuestionsByExamPaperId(examPaperId.Value).ToList(); ViewBag.Questions = questions; } ViewBag.Status = model.Status; ViewBag.IsUpdate = true; return(View(model)); }
/// <summary> /// The ExportToPdf /// </summary> /// <param name="examPaperId">The examPaperId<see cref="int"/></param> /// <returns>The <see cref="ActionResult"/></returns> public ActionResult _ExportToPdf(int examPaperId) { try { Models.ExamPaper examPaper = new Models.ExamPaper(); examPaper = examPaperService.GetExamPaperById(examPaperId); List <QuestionDto> questions = new List <QuestionDto>(); questions = questionService.GetQuestionsByExamPaperId(examPaper.ExamPaperID).ToList(); List <Answer> answers = new List <Answer>(); foreach (var item in questions) { var answesTemp = questionService.GetAnswersByQuestionId(item.QuestionID); answers.AddRange(answesTemp); } ViewBag.Answers = answers; ViewBag.ExamPaper = examPaper; return(View(questions)); } catch (Exception e) { Failure = e.Message; return(Json(new { status = false }, JsonRequestBehavior.AllowGet)); } }
public ActionResult ImportExamPaper(HttpPostedFileBase excelfile) { if (excelfile == null) { Failure = "Please choose excel file to import exam paper"; return(RedirectToAction("ImportExamPaper")); } else { if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx")) { try { string path = Path.Combine(Server.MapPath("~/FileExcel/"), Guid.NewGuid().ToString() + Path.GetExtension(excelfile.FileName)); excelfile.SaveAs(path); Excel.Application application = new Excel.Application { Visible = true }; Excel.Workbook workbook = application.Workbooks.Open(path); Excel.Worksheet worksheet = workbook.Sheets[@"ExamPaper"]; Excel.Range range = worksheet.UsedRange; Models.ExamPaper examPaper = new Models.ExamPaper(); examPaper.Title = ((Excel.Range)range.Cells[3, 2]).Text; examPaper.Time = int.Parse(((Excel.Range)range.Cells[4, 2]).Text); if (((Excel.Range)range.Cells[5, 2]).Text == "Public") { examPaper.Status = true; } else if (((Excel.Range)range.Cells[5, 2]).Text == "Draff") { examPaper.Status = false; } else { Failure = "Exam paper status must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } examPaper.IsActive = Boolean.Parse(((Excel.Range)range.Cells[6, 2]).Text); examPaper.CreatedBy = int.Parse(Session["Name"].ToString()); examPaper.CreatedDate = DateTime.Now; examPaper.ModifiedBy = int.Parse(Session["Name"].ToString()); examPaper.ModifiedDate = DateTime.Now; int examPaperId = examPaperService.Create(examPaper); var listQuestionCategory = questionCategorySevice.GetAll(); for (int row = 11; row <= range.Rows.Count; row++) { int level = 0; if (((Excel.Range)range.Cells[row, 2]).Text == "Hard") { level = 3; } else if (((Excel.Range)range.Cells[row, 2]).Text == "Normal") { level = 2; } else if (((Excel.Range)range.Cells[row, 2]).Text == "Easy") { level = 1; } else { Failure = "Question level must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } int categoryId = 0; int k = 0; foreach (var item in listQuestionCategory) { if (((Excel.Range)range.Cells[row, 3]).Text == item.Name) { categoryId = item.CategoryID; k++; } } if (k == 0) { Failure = "Question category must be select from dropdown list"; return(RedirectToAction("ImportExamPaper")); } Models.Question question = new Models.Question { Content = ((Excel.Range)range.Cells[row, 1]).Text, CategoryID = categoryId, Level = level, IsActive = true, CreatedBy = int.Parse(Session["Name"].ToString()), CreatedDate = DateTime.Now, ModifiedBy = int.Parse(Session["Name"].ToString()), ModifiedDate = DateTime.Now }; int questionId = questionService.AddQuestion(question); Answer answer = new Answer(); int j = 5; for (int i = 4; i <= 13; i += 2) { string content = ((Excel.Range)range.Cells[row, i]).Text; if (content != "") { answer.AnswerContent = content; answer.IsCorrect = Boolean.Parse(((Excel.Range)range.Cells[row, j]).Text); answer.QuestionID = questionId; answerService.AddAnswer(answer); } else { continue; } j += 2; } examPaperQuestionService.InsertExamPaperQuestion(examPaperId, questionId); } } catch (Exception ex) { Failure = ex.Message; return(RedirectToAction("ImportExamPaper")); } Success = "Import exam paper successfully!"; return(RedirectToAction("ExamPapers")); } else { Failure = "Please choose excel file to import exam paper"; return(RedirectToAction("ImportExamPaper")); } } }