//Delete Question with all Options public bool DeleteQuestion(int quesData) { bool result = false; using (myProjectEntities context = new myProjectEntities()) { tblQuizQuestion quesDetail = context.tblQuizQuestions.Where(w => w.PK_Question_id == quesData).FirstOrDefault(); float m1 = (float)quesDetail.marks; tblQuizDetail quizDetail = context.tblQuizDetails.Where(a => a.PK_Quiz_id == quesDetail.FK_Quiz_id).FirstOrDefault(); float m2 = (float)quizDetail.quiz_weightage - m1; quizDetail.quiz_weightage = m2; context.Entry(quizDetail).State = EntityState.Modified; context.SaveChanges(); int q = context.spDeleteQuestionWithOption(quesData); if (q != 0) { result = true; //set return to true } } return(result); }
//Create Questions for quiz. This is awesome public int CreateQuestion(questionModel quesData) { int result = 0; using (myProjectEntities context = new myProjectEntities()) { Mapper.CreateMap <questionModel, tblQuizQuestion>(); tblQuizQuestion newQues = Mapper.Map <questionModel, tblQuizQuestion>(quesData); List <tblQuizOption> newOpt = new List <tblQuizOption>(); Mapper.CreateMap <optionModel, tblQuizOption>(); foreach (optionModel opt in quesData.optionModel) { tblQuizOption o = Mapper.Map <optionModel, tblQuizOption>(opt); newOpt.Add(o); } newQues.tblQuizOptions = newOpt.ToList(); context.tblQuizQuestions.Add(newQues); //Execute the add function int q = context.SaveChanges(); if (q > 0) { result = newQues.PK_Question_id; //set return to true } } return(result); }
//Update existing Questions public bool UpdateQuestion(questionModel quesData) { bool result = false; using (myProjectEntities context = new myProjectEntities()) //update weightage { tblQuizQuestion quesDetail = context.tblQuizQuestions.Where(w => w.PK_Question_id == quesData.PK_Question_id).FirstOrDefault(); float m1 = (float)quesDetail.marks; tblQuizDetail quizDetail = context.tblQuizDetails.Where(a => a.PK_Quiz_id == quesData.FK_Quiz_id).FirstOrDefault(); float m2 = (float)quizDetail.quiz_weightage - m1; quizDetail.quiz_weightage = m2 + (float)quesData.marks; context.Entry(quizDetail).State = EntityState.Modified; context.SaveChanges(); } using (myProjectEntities context = new myProjectEntities()) { Mapper.CreateMap <questionModel, tblQuizQuestion>(); tblQuizQuestion newQues = Mapper.Map <questionModel, tblQuizQuestion>(quesData); context.Entry(newQues).State = EntityState.Modified; context.SaveChanges(); } if (quesData.question_type == "Optional" || quesData.question_type == "Multiple Choice") { using (myProjectEntities context = new myProjectEntities()) { List <tblQuizOption> newOpt = context.tblQuizOptions.Where(s => s.FK_Question_id == quesData.PK_Question_id).ToList(); foreach (optionModel item in quesData.optionModel) { foreach (tblQuizOption item1 in newOpt) { if (item.PK_Option_id == item1.PK_Option_id) { item1.isAnswer = item.isAnswer; } } } context.SaveChanges(); } } result = true; return(result); //This is to update just the Question i.e. without option model //Mapper.CreateMap<questionModel, tblQuizQuestion>(); //tblQuizQuestion s = Mapper.Map<questionModel, tblQuizQuestion>(quesData); //context.Entry(s).State = EntityState.Modified; //int q = context.SaveChanges(); //if (q == 1) // result = true; //return result; }
/// <summary> /// Check the student's quiz question answer and save progress if correct. /// </summary> /// <param name="ID">Invitation ID</param> /// <param name="QuizQuestionID">Quiz Question ID</param> /// <param name="AnswerID">Answer ID</param> /// <returns></returns> public ActionResult CompleteQuizQuestion(int ID, int QuizQuestionID, int AnswerID) { tblInvitation Invitation = _ODB.tblInvitations.Where(e => e.ID == ID).SingleOrDefault(); tblQuizQuestion Question = _ODB.tblQuizQuestions.Where(e => e.ID == QuizQuestionID).SingleOrDefault(); tblQuizQuestionAnswer Answer = Question.tblQuizQuestionAnswers.Where(e => e.ID == AnswerID).SingleOrDefault(); tblCompletedQuizQuestion existingAnswer = Invitation.tblCompletedQuizQuestions.Where(e => e.QuizQuestionID == QuizQuestionID).SingleOrDefault(); if (Invitation.ENumber == Current.User.ENumber || Current.User.IsAdmin || Current.User.IsStaff) { if (existingAnswer == null) { if (AnswerID != -1 && Answer != null) { if (Answer.Correct) { tblCompletedQuizQuestion CompletedQuestion = new tblCompletedQuizQuestion(); CompletedQuestion.tblInvitation = Invitation; CompletedQuestion.tblQuizQuestion = Question; CompletedQuestion.DateCompleted = DateTime.Now; _ODB.tblCompletedQuizQuestions.AddObject(CompletedQuestion); _ODB.SaveChanges(); Logging.Log(Logging.LogType.Audit, string.Format("User {0} completed Quiz Question ID {1} for Invitation {2}. tblCompletedQuizQuestion.ID = {3}", Current.User.Username, Question.ID, Invitation.ID, CompletedQuestion.ID)); if (Invitation.HasPendingQuiz) { this.ShowPageMessage(string.Format("Correct! The answer was '{0}'. Please answer the next question", Answer.Answer)); return(this.RedirectToAction <StudentController>(c => c.Quiz(Invitation.ID))); } else { this.ShowPageMessage("You have completed the required Quiz."); return(this.RedirectToAction <StudentController>(c => c.Invitation(Invitation.ID))); } } else { this.ShowPageError(string.Format("The answer you provided was not correct: '{0}'. Please choose another answer.", Answer.Answer)); } } else { this.ShowPageError("Could not find an answer with the submitted values. Please choose an answer. If no answer choices are available for this question, please contact: [email protected]"); } } else { this.ShowPageError("The submitted quiz question has already been completed."); } } else { this.ShowPageError("You are not authorized to view Quiz material for this Invitation"); } return(this.RedirectToAction <StudentController>(c => c.Quiz(Invitation.ID))); }
public int Delete(bool IsDeleted) { tblQuizQuestion Question = new tblQuizQuestion(); Question.IsActive = IsDeleted; context.Entry(Question).State = EntityState.Modified; int result = context.SaveChanges(); return(result); }
public bool DeleteQuestion(int QuizId, int QuestionId) { tblQuizQuestion Question = new tblQuizQuestion(); Question.QuestionId = QuestionId; Question.QuizId = QuizId; Question.IsActive = true; context.Entry(Question).State = EntityState.Deleted; int result = context.SaveChanges(); return(result > 0 ? true : false); }
//Get individual question detail. This is awesome. public questionModel GetQuestionById(int quesId) { using (myProjectEntities context = new myProjectEntities()) { tblQuizQuestion pro = context.tblQuizQuestions.Where(q => q.PK_Question_id == quesId).FirstOrDefault(); List <optionModel> opt = new List <optionModel>(); Mapper.CreateMap <tblQuizOption, optionModel>(); foreach (tblQuizOption item in pro.tblQuizOptions) { optionModel rtr = Mapper.Map <tblQuizOption, optionModel>(item); opt.Add(rtr); } Mapper.CreateMap <tblQuizQuestion, questionModel>(); questionModel result = Mapper.Map <tblQuizQuestion, questionModel>(pro); result.optionModel = opt.ToList(); return(result); } }
//Updated public int Question_Create(questionModel quesData) { int result = 0; using (myProjectEntities context = new myProjectEntities()) { Mapper.CreateMap <questionModel, tblQuizQuestion>(); tblQuizQuestion newQues = Mapper.Map <questionModel, tblQuizQuestion>(quesData); if (quesData.question_type == "Optional" || quesData.question_type == "Multiple Choice")//Check is not options { List <tblQuizOption> newOpt = new List <tblQuizOption>(); Mapper.CreateMap <optionModel, tblQuizOption>(); foreach (optionModel opt in quesData.optionModel) { tblQuizOption o = Mapper.Map <optionModel, tblQuizOption>(opt); newOpt.Add(o); } newQues.tblQuizOptions = newOpt.ToList(); } context.tblQuizQuestions.Add(newQues); //Execute the add function int q = context.SaveChanges(); if (q > 0) { result = newQues.PK_Question_id; //set return to true } tblQuizDetail quizDetail = context.tblQuizDetails.Where(a => a.PK_Quiz_id == newQues.FK_Quiz_id).FirstOrDefault(); quizDetail.quiz_weightage = quizDetail.quiz_weightage + (float)newQues.marks; context.Entry(quizDetail).State = EntityState.Modified; context.SaveChanges(); } return(result); }
public DataModel.Questions UpdateQuestion(DataModel.Questions obj) { tblQuizQuestion Question = context.tblQuizQuestions.Where(q => q.QuestionId == obj.QuestionId).FirstOrDefault(); if (Question == null) { return(obj); } Question.QuestionId = obj.QuestionId; Question.QuizId = obj.QuizId; Question.QuestionText = obj.QuestionText; Question.IsActive = obj.IsActive; Question.UpdatedAt = DateTime.Now; Question.UpdatedBy = obj.UpdatedBy; context.Entry(Question).State = EntityState.Modified; //context.Entry(question).CurrentValues.SetValues(Question); int saved = context.SaveChanges(); if (saved > 0) { obj.UpdatedAt = Question.UpdatedAt.Value; obj.CreatedAt = Question.CreatedAt.Value; obj.Success = true; obj.ErrorMessage = "Question updated successfully."; } else { obj.UpdatedAt = Question.UpdatedAt.Value; obj.CreatedAt = Question.CreatedAt.Value; obj.Success = false; obj.ErrorMessage = "Error occured while updating question."; } return(obj); }
public DataModel.Questions Add(DataModel.Questions obj) { if (obj.QuestionId != 0) { return(UpdateQuestion(obj)); } var existing = context.tblQuizQuestions.Where(x => x.QuestionText == obj.QuestionText && x.QuizId == obj.QuizId).FirstOrDefault(); tblQuizQuestion Question = new tblQuizQuestion(); Question.QuizId = obj.QuizId; Question.QuestionText = obj.QuestionText; Question.IsActive = obj.IsActive; Question.CreatedAt = DateTime.Now; Question.CreatedBy = obj.CreatedBy; Question.UpdatedAt = DateTime.Now; if (existing != null) { obj.ErrorMessage = "Question with same text already exists"; obj.Success = false; obj.CreatedAt = Question.CreatedAt.Value; obj.UpdatedAt = Question.UpdatedAt.Value; return(obj); } context.tblQuizQuestions.Add(Question); context.SaveChanges(); //Assuming the database is generating your Id's for you obj.QuestionId = Question.QuestionId; obj.CreatedAt = Question.CreatedAt.Value; obj.UpdatedAt = Question.UpdatedAt.Value; obj.Success = true; obj.ErrorMessage = "Question saved successfully."; return(obj); }
/// <summary> /// View current/incomplete quiz question for student /// </summary> /// <param name="ID">Invitation ID</param> /// <returns></returns> public ActionResult Quiz(int ID) { tblInvitation Invitation = _ODB.tblInvitations.Where(e => e.ID == ID).SingleOrDefault(); if (Invitation.ENumber == Current.User.ENumber) { tblQuizQuestion Question = null; if (Invitation != null) { Question = Invitation.CurrentQuizQuestion; return(View("Quiz", new Tuple <tblInvitation, tblQuizQuestion>(Invitation, Question))); } else { this.ShowPageError("Could not find an orientation invitation for the given ID"); } } else { this.ShowPageError("You are not authorized to view quiz material for this Invitation"); } return(this.RedirectToAction <StudentController>(c => c.Index())); }
public int AddQuestion(QuizQuestionModel _quizQueModel) { try { using (var context = new MCQ_Quiz_DBEntities()) { /// first add question tblQuizQuestion question = new tblQuizQuestion(); question.QuizId_fk = _quizQueModel.QuizId_fk; question.Question = _quizQueModel.Question; question.Type = _quizQueModel.Type; question.CreatedOn = _quizQueModel.CreatedOn; question.IsActive = _quizQueModel.IsActive; question.CreatedBy = _quizQueModel.CreatedBy; context.tblQuizQuestions.Add(question); context.SaveChanges(); /// second add options //1 tblQuizChoice choice1 = new tblQuizChoice(); choice1.QuestionId = question.QuizQuestionId; choice1.OptionTitle = _quizQueModel.option1; choice1.CreatedON = _quizQueModel.CreatedOn; choice1.CreatedBy = _quizQueModel.CreatedBy; choice1.IsAnswer = false; if (_quizQueModel.option1 == _quizQueModel.Answer) { choice1.IsAnswer = true; } context.tblQuizChoices.Add(choice1); context.SaveChanges(); //2 tblQuizChoice choice2 = new tblQuizChoice(); choice2.QuestionId = question.QuizQuestionId; choice2.OptionTitle = _quizQueModel.option2; choice2.CreatedON = _quizQueModel.CreatedOn; choice2.CreatedBy = _quizQueModel.CreatedBy; choice2.IsAnswer = false; if (_quizQueModel.option2 == _quizQueModel.Answer) { choice2.IsAnswer = true; } context.tblQuizChoices.Add(choice2); context.SaveChanges(); //3 tblQuizChoice choice3 = new tblQuizChoice(); choice3.QuestionId = question.QuizQuestionId; choice3.OptionTitle = _quizQueModel.option3; choice3.CreatedON = _quizQueModel.CreatedOn; choice3.CreatedBy = _quizQueModel.CreatedBy; choice3.IsAnswer = false; if (_quizQueModel.option3 == _quizQueModel.Answer) { choice3.IsAnswer = true; } context.tblQuizChoices.Add(choice3); context.SaveChanges(); //4 tblQuizChoice choice4 = new tblQuizChoice(); choice4.QuestionId = question.QuizQuestionId; choice4.OptionTitle = _quizQueModel.option4; choice4.CreatedON = _quizQueModel.CreatedOn; choice4.CreatedBy = _quizQueModel.CreatedBy; choice4.IsAnswer = false; if (_quizQueModel.option4 == _quizQueModel.Answer) { choice4.IsAnswer = true; } context.tblQuizChoices.Add(choice4); context.SaveChanges(); return(choice4.Quiz_Choice_Id); } } catch (Exception) { return(-1); } }