//INSERT QUESTION AND CATALOGUE //INSERT QUESTION public bool AddQuestion(Question Question) { string query = "INSERT INTO Question (Question_Title,Question_Name, Question_Type, Catalogue_ID, Date_Create)" + " VALUES ('" + Question.QuestionTitle.Replace("'", "''") + "','" + Question.NameQuestion.Replace("'", "''") + "','" + Question.TypeQuestion + "','" + Question.IDCatalogue + "','" + Question.Date.ToString("yyyy-MM-dd hh:mm:ss") + "')"; return DA.InsertDatabase(query); }
//SAVE AND CONTINUE TO ADD QUESTION private void btn_SaveAndCreateNewQuestion_Click(object sender, EventArgs e) { QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "" || txt_AnswerContent.Text.Trim() == "") { if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "") { MessageBox.Show("Câu hỏi không được rỗng. Vui lòng nhập thông tin câu hỏi trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Đáp án không được rỗng. Vui lòng nhập thông tin đáp án trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { question.QuestionTitle = txt_NameQuestion.Text.Trim(); question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.TypeQuestion = "shortanswer"; question.IDCatalogue = IDCat; question.Date = DateTime.Now; if (questionBl.AddQuestion(question)) { answer.ContentAnswer = txt_AnswerContent.Text.Trim(); answer.Check = 1; answer.IDQuestion = questionBl.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBl.AddAnswer(answer); //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Thêm câu hỏi thành công."; //notifyIcon1.ShowBalloonTip(2000); MessageBox.Show("Thêm câu hỏi thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //Refesh form txt_ContentQuestion.Text = ""; txt_AnswerContent.Text = ""; } } }
//Copy Question public void CopyQuestion() { Question question = new Question(); Answer answer = new Answer(); QuestionBL questionBL = new QuestionBL(); foreach (DataGridViewRow row in dgv_Question.Rows) { if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value == true) { question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString(); question.NameQuestion = row.Cells["QuestionName"].Value.ToString(); question.TypeQuestion = row.Cells["QuestionType"].Value.ToString(); question.IDCatalogue = IDCat; question.Date = DateTime.Now; if (questionBL.AddQuestion(question)) { question.IDQuestion = Convert.ToInt32(row.Cells["IDQuestion"].Value); List<DO.Answer> AnswerList; AnswerList = questionBL.GetAnswerByQuestionID(question); if (AnswerList != null) { for (int i = 0; i < AnswerList.Count; i++) { answer.ContentAnswer = AnswerList.ElementAt(i).ContentAnswer; if (AnswerList.ElementAt(i).IsCorrect == true) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = questionBL.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBL.AddAnswer(answer); } } } } } }
//SAVE QUESTION private void btn_Save_Click(object sender, EventArgs e) { QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); if (txt_ContentQuestion.Text.Trim() == "" || txt_ContentAnswer.Text.Trim() == "") { if (txt_ContentQuestion.Text.Trim() == "") { MessageBox.Show("Câu hỏi không được rỗng. Vui lòng nhập thông tin câu hỏi trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Đáp án không được rỗng. Vui lòng nhập thông tin đáp án trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { //Update question question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.IDQuestion = IDQuestion; questionBl.EditQuestionbyID(question); //Update answer answer.IDQuestion = IDQuestion; answer.ContentAnswer = txt_ContentAnswer.Text.Trim(); //answer.IsCorrect = true; answer.Check = 1; questionBl.EditAnswerbyID(answer); //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Chỉnh sửa câu hỏi thành công"; //notifyIcon1.ShowBalloonTip(2000); //Close form Form FindForm = this.FindForm(); FindForm.Close(); } }
public void MoveQuestion() { Question question = new Question(); Answer answer = new Answer(); QuestionBL questionBL = new QuestionBL(); foreach (DataGridViewRow row in dgv_Question.Rows) { if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value == true) { question.IDCatalogue = IDCat; question.IDQuestion = Convert.ToInt32(row.Cells["IDQuestion"].Value); questionBL.EditIDCatalogueAnswerByIDQuestion(question); questionBL.EditIDCatalogueQuestionByIDQuestion(question); ////add question //question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString(); //question.NameQuestion = row.Cells["QuestionName"].Value.ToString(); //question.TypeQuestion = row.Cells["QuestionType"].Value.ToString(); //question.IDCatalogue = IDCat; //question.Date = DateTime.Now; //if (questionBL.AddQuestion(question)) //{ // //add answer // question.IDQuestion = Convert.ToInt32(row.Cells["IDQuestion"].Value); // List<DO.Answer> AnswerList; // AnswerList = questionBL.GetAnswerByQuestionID(question); // if (AnswerList != null) // { // for (int i = 0; i < AnswerList.Count; i++) // { // answer.ContentAnswer = AnswerList.ElementAt(i).ContentAnswer; // if (AnswerList.ElementAt(i).IsCorrect == true) // { // answer.Check = 1; // } // else // { // answer.Check = 0; // } // answer.IDQuestion = questionBL.MaxIDQuestion(); // answer.IDCatalogue = IDCat; // questionBL.AddAnswer(answer); // } // } // //Delete answer and question // questionBL.DeleteAnswerByIDQuestion(question); // questionBL.DeleteQuestionByID(question); //} } } }
//LOAD FORM private void EditQuestion_MultiSelect_Load(object sender, EventArgs e) { //GET QUESTION BY ID Question QuestionBL QuestionBL = new QuestionBL(); List<DO.Question> QuestionList; Question question = new Question(); QuestionList = QuestionBL.GetQuestion(); if (QuestionList != null) { for (int i = 0; i < QuestionList.Count; i++) { if (QuestionList.ElementAt(i).IDQuestion == IDQuestion) { txt_ContentQuestion.Text = QuestionList.ElementAt(i).NameQuestion; txt_Date.Text = QuestionList.ElementAt(i).Date.ToString("d"); } } //GET ANSWER BY ID QUESTION List<DO.Answer> AnswerList; Answer answer = new Answer(); question.IDQuestion = IDQuestion; AnswerList = QuestionBL.GetAnswerByQuestionID(question); if (AnswerList != null) { for (int j = 0; j < AnswerList.Count; j++) { Answer_MultiSelect MultiSelectAnswer = new Answer_MultiSelect(); i++; MultiSelectAnswer.Tag = i; MultiSelectAnswer.ID_Answer = i; MultiSelectAnswer.onDelete += MultiSelectAnswer_onDelete; MultiSelectAnswer.chk_Check.Text = Convert.ToChar(a + j).ToString(); //flp_addAnswer.Controls.Add(MultiSelectAnswer); MultiSelectAnswer.txt_AnswerContent.Text = AnswerList.ElementAt(j).ContentAnswer; MultiSelectAnswer.chk_Check.Checked = AnswerList.ElementAt(j).IsCorrect; flp_addAnswer.Controls.Add(MultiSelectAnswer); } } } }
public void CheckQuestion() { WrongQuestion = 0; //int CheckQuestion = 0; Question question = new Question(); Answer answer = new Answer(); QuestionBL questionBL = new QuestionBL(); foreach (DataGridViewRow row in dataGridView1.Rows) { if ( row.Cells["QuestionTitle"].Value.ToString().Trim() != "" && row.Cells["NameQuestion"].Value.ToString().Trim() != "" && row.Cells["AnswerContent"].Value.ToString().Trim() != "" && row.Cells["TypeQuestion"].Value.ToString().Trim() != "") { if (row.Cells["TypeQuestion"].Value.ToString().Trim() == "shortanswer" || row.Cells["TypeQuestion"].Value.ToString().Trim() == "multichoice") { if (row.Cells["TypeQuestion"].Value.ToString().Trim() == "shortanswer") { string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None); int NumAnswer = AnswerContent.Length - 1; if (NumAnswer == 1) { string[] AnswerItem = AnswerContent[0].Split(new string[] { "---" }, StringSplitOptions.None); if (AnswerItem[1].ToString().Trim() != "" && AnswerItem[1].ToString().Trim().Contains("</span>")==false && AnswerItem[0].ToString().Trim() != "") { int PointAnswer; bool isNum = int.TryParse(AnswerItem[0].ToString().Trim(), out PointAnswer); if (isNum = true && Convert.ToInt32(AnswerItem[0].ToString().Trim()) > 0) { row.DefaultCellStyle.BackColor = Color.LightSkyBlue ; } else { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } } else { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } } else { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } } else { string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None); int Numanswer = AnswerContent.Length - 1; if (Numanswer < 2) { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } else { int empty = 0; int CountNumberOfIncorrect = 0; int TotalAnswer = 0; for (int i = 0; i < AnswerContent.Length - 1; i++) { string[] AnswerItem = AnswerContent[i].Split(new string[] { "---" }, StringSplitOptions.None); int PointAnswer; bool isNum = int.TryParse(AnswerItem[0].ToString().Trim(), out PointAnswer); //check null answer or invalid answer if (AnswerItem[1].ToString().Trim() == "" || AnswerItem[1].ToString().Trim().Contains("</span>") == true || AnswerItem[0].ToString().Trim() == "" || isNum == false) { empty++; } //check don't have any correct answer if (int.TryParse(AnswerItem[0].ToString().Trim(), out PointAnswer)) { if (Convert.ToInt32(AnswerItem[0].ToString().Trim()) == 0) { CountNumberOfIncorrect++; } } TotalAnswer++; } if (empty > 0 || TotalAnswer == CountNumberOfIncorrect) { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } else { row.DefaultCellStyle.BackColor = Color.LightSkyBlue ; } } } } else { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } } else { row.DefaultCellStyle.BackColor = Color.LightCoral; row.ReadOnly = true; WrongQuestion++; } }//end foreach }
//EDIT QUESTION private void btn_EditQuestion_Click(object sender, EventArgs e) { btn_EditQuestion.Visible = false; btn_Save.Visible = true; btn_addAnswer.Visible = true; flp_Answer.Controls.Clear(); txt_ContentQuestion.ReadOnly = false; //GET QUESTION BY ID Question QuestionBL QuestionBL = new QuestionBL(); List<DO.Question> QuestionList; Question question = new Question(); QuestionList = QuestionBL.GetQuestion(); if (QuestionList != null) { for (int i = 0; i < QuestionList.Count; i++) { if (QuestionList.ElementAt(i).IDQuestion == IDQuestion) { txt_Catalogue.Text = QuestionList.ElementAt(i).NameCatalogue; txt_ContentQuestion.Text = QuestionList.ElementAt(i).NameQuestion; txt_QuestionType.Text = QuestionList.ElementAt(i).TypeQuestion; txt_Date.Text = QuestionList.ElementAt(i).Date.ToString("d"); } } //GET ANSWER BY ID QUESTION List<DO.Answer> AnswerList; Answer answer = new Answer(); question.IDQuestion = IDQuestion; AnswerList = QuestionBL.GetAnswerByQuestionID(question); if (AnswerList != null) { for (int j = 0; j < AnswerList.Count; j++) { Answer_OnlyOneSelect OneChoiceAnswer = new Answer_OnlyOneSelect(); i++; OneChoiceAnswer.Tag = i; OneChoiceAnswer.ID_Answer = i; OneChoiceAnswer.onDelete += OneChoiceAnswer_onDelete; OneChoiceAnswer.onCheck += OneChoiceAnswer_onCheck; OneChoiceAnswer.rad_check.Text = Convert.ToChar(a+j).ToString(); OneChoiceAnswer.txt_Answercontent.Text = AnswerList.ElementAt(j).ContentAnswer; OneChoiceAnswer.rad_check.Checked = AnswerList.ElementAt(j).IsCorrect; flp_Answer.Controls.Add(OneChoiceAnswer); } } } }
//Save Question private void btn_Save_Click(object sender, EventArgs e) { int NumAnswer = flp_Answer.Controls.Count; if (txt_ContentQuestion.Text.Trim() == "" || NumAnswer < 2) { if (txt_ContentQuestion.Text.Trim() == "") { MessageBox.Show("Vui lòng nhập thông tin câu hỏi trước khi lưu!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Vui lòng nhập hơn một đáp án!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (checkAnswerEmpty() == true) { MessageBox.Show("Không lưu câu hỏi vì tồn tại đáp án rỗng!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (checkBlankCorrectAnswer() ==true) { MessageBox.Show("Vui lòng chọn đáp án cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); //Update question question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.IDQuestion = IDQuestion; questionBl.EditQuestionbyID(question); //DELETE ANSWER question.IDQuestion = IDQuestion; questionBl.DeleteAnswerByIDQuestion(question); foreach (Answer_MultiSelect item in flp_Answer.Controls) { if (item.txt_AnswerContent.Text.Trim() != "") { answer.ContentAnswer = item.txt_AnswerContent.Text.Trim(); //answer.IsCorrect = item.chk_Check.Checked; if (item.chk_Check.Checked == true ) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = IDQuestion; answer.IDCatalogue = IDCatalogue; questionBl.AddAnswer(answer); } } //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = " Chỉnh sửa câu hỏi thành công"; //notifyIcon1.ShowBalloonTip(2000); //Close form this.Close(); } } } }
public List<Question> GetQuestionByID(Question question) { List<Question> QuestionList = new List<Question>(); string query = "SELECT q.Question_ID,q.Question_Title, q.Question_Name, q.Question_Type, q.Catalogue_ID, c.Catalogue_Name, q.Question_Title, q.[Date_Create]" + " FROM Question q" + " INNER JOIN Catalogue c ON c.Catalogue_ID = q.Catalogue_ID" + " WHERE q.Question_ID='" + question.IDQuestion + "'"; DataTable dt = DA.SelectDatabase(query); int i = 1; if (dt != null) { foreach (DataRow item in dt.Rows) { Question Question = new Question(); Question.IDQuestion = Convert.ToInt32(item["Question_ID"]); Question.QuestionTitle = item["Question_Title"].ToString(); Question.NameQuestion = item["Question_Name"].ToString(); Question.TypeQuestion = item["Question_Type"].ToString(); Question.IDCatalogue = Convert.ToInt32(item["Catalogue_ID"]); //Question.NameCatalogue = item["Catalogue_Name"].ToString(); Question.Date = (DateTime)item["Date_Create"]; Question.Sequence = i; Question.Catalogue.NameCatalogue = item["Catalogue_Name"].ToString(); //Question.Catalogue.IDCatalogue = Convert.ToInt32(item["Catalogue_ID"]); QuestionList.Add(Question); i++; } } return QuestionList; }
//LOAD FILE QUESTION // LOAD QUESTION FILE XML public List<Question> GetFileXML(string file) { if (FA.FileContentXML(file) == null) { return null; } else { string content = FA.FileContentXML(file); int stt = 1; DataTable dtb = new DataTable(); dtb.Columns.Add("Sequence", typeof(string)); dtb.Columns.Add("QuestionTitle", typeof(string)); dtb.Columns.Add("NameQuestion", typeof(string)); dtb.Columns.Add("TypeQuestion", typeof(string)); dtb.Columns.Add("AnswerContent", typeof(string)); string[] QuestionContent = content.Split(new string[] { "</question>" }, StringSplitOptions.None); for (int i = 0; i < QuestionContent.Length - 1; i++) { string[] QuestionItem = QuestionContent[i].Split(new string[] { "---" }, StringSplitOptions.None); dtb.Rows.Add(stt.ToString(),QuestionItem[1], QuestionItem[2], QuestionItem[0], QuestionItem[3]); stt++; } List<Question> QuestionList = new List<Question>(); if (dtb != null && dtb.Rows.Count > 0) { foreach (DataRow item in dtb.Rows) { Question Question = new Question(); Question.Sequence = Convert.ToInt32(item["Sequence"]); Question.QuestionTitle = item["QuestionTitle"].ToString(); Question.NameQuestion = item["NameQuestion"].ToString(); Question.TypeQuestion = item["TypeQuestion"].ToString(); //string AnswerContent = item["AnswerContent"].ToString().Trim().Replace("''","'"); string AnswerContent = item["AnswerContent"].ToString().Trim(); AnswerContent = AnswerContent.Replace("+++", "---"); Question.AnswerContent = AnswerContent; QuestionList.Add(Question); } return QuestionList; } else { return null; } } }
//Select Answer by Question ID public List<Answer> GetAnswerByQuestionID(Question Question) { List<Answer> AnswerList = new List<Answer>(); string query = "SELECT a.Answer_ID, a.Answer_Name, a.Question_ID, a.Correct_Answer" + " FROM Answer a " + " WHERE a.Question_ID='" + Question.IDQuestion + "'"; //string query = "SELECT *" // + " FROM Answer a " // + " WHERE a.Question_ID='" + Question.IDQuestion + "'"; DataTable dt = DA.SelectDatabase(query); if (dt != null) { foreach (DataRow item in dt.Rows) { Answer Answer = new Answer(); Answer.IDAnswer = Convert.ToInt32(item["Answer_ID"]); Answer.ContentAnswer = item["Answer_Name"].ToString() ; Answer.IDQuestion = Convert.ToInt32(item["Question_ID"].ToString()); Answer.IsCorrect = (bool)item["Correct_Answer"]; AnswerList.Add(Answer); } } return AnswerList; }
//Update Question Type public bool EditQuestionTypebyID(Question Question) { string query = "UPDATE Question" + " SET Question_Type ='" + Question.TypeQuestion + "'" + " WHERE Question_ID = '" + Question.IDQuestion + "'"; return DA.UpdateDatabase(query); }
//EDIT QUESTION AND ANSWER //EDIT QUESTION public bool EditQuestionbyID(Question Question) { string query = "UPDATE Question" + " SET Question_Name ='" + Question.NameQuestion.Replace("'", "''") + "'" + " WHERE Question_ID = '" + Question.IDQuestion + "'"; return DA.UpdateDatabase(query); }
//SAVE QUESTION private void btn_SaveQuestion_Click(object sender, EventArgs e) { if (cmb_Catalogue.SelectedItem != null) { int NumAnswer = flp_addAnswer.Controls.Count; if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "" || NumAnswer < 2) { if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "") { MessageBox.Show("Vui lòng nhập thông tin câu hỏi trước khi lưu!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Vui lòng nhập hơn một đáp án!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (checkAnswerEmpty() == true) { MessageBox.Show("Không lưu câu hỏi vì tồn tại đáp án rỗng!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (checkBlankCorrectAnswer() == true) { MessageBox.Show("Vui lòng chọn đáp án cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { //GET CATALOGUE ID CatalogueBL CatBL = new CatalogueBL(); List<DO.Catalogue> CatList; CatList = CatBL.GetCatalogue(); if (CatList != null) for (int i = 0; i < CatList.Count; i++) { if (CatList.ElementAt(i).NameCatalogue == cmb_Catalogue.SelectedItem.ToString()) { IDCat = Convert.ToInt32(CatList.ElementAt(i).IDCatalogue); } } //SAVE QUESTION QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); question.QuestionTitle = txt_NameQuestion.Text.Trim(); question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.TypeQuestion = "multichoice"; question.IDCatalogue = IDCat; question.Date = DateTime.Now; if ( questionBl.AddQuestion(question)) { foreach (Answer_MultiSelect item in flp_addAnswer.Controls) { if (item.txt_AnswerContent.Text.Trim() != "") { answer.ContentAnswer = item.txt_AnswerContent.Text.Trim(); if (item.chk_Check.Checked == true) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = questionBl.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBl.AddAnswer(answer); } } //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Thêm câu hỏi thành công."; //notifyIcon1.ShowBalloonTip(2000); MessageBox.Show("Thêm câu hỏi thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //Close Form Form FindForm = this.FindForm(); FindForm.Close(); } } } } } else { MessageBox.Show("Vui lòng chọn chủ đề cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
//SAVE QUESTION AND CONTINUE ADD QUESTION private void btn_SaveAndCreateNewQuestion_Click(object sender, EventArgs e) { if (cmb_Catalogue.SelectedItem != null) { //GET CATALOGUE ID this.Dock = DockStyle.Fill; CatalogueBL CatBL = new CatalogueBL(); List<DO.Catalogue> CatList; CatList = CatBL.GetCatalogue(); if (CatList != null) for (int i = 0; i < CatList.Count; i++) { if (CatList.ElementAt(i).NameCatalogue == cmb_Catalogue.SelectedItem.ToString()) { IDCat = Convert.ToInt32(CatList.ElementAt(i).IDCatalogue); } } //SAVE QUESTION QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "" || txt_AnswerContent.Text.Trim() == "") { if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "") { MessageBox.Show("Câu hỏi không được rỗng. Vui lòng nhập thông tin câu hỏi trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Đáp án không được rỗng. Vui lòng nhập thông tin đáp án trước khi lưu.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { question.QuestionTitle = txt_NameQuestion.Text.Trim(); question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.TypeQuestion = "shortanswer"; question.IDCatalogue = IDCat; question.Date = DateTime.Now; if (questionBl.AddQuestion(question)) { answer.ContentAnswer = txt_AnswerContent.Text.Trim(); answer.Check = 1; answer.IDQuestion = questionBl.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBl.AddAnswer(answer); //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Thêm câu hỏi thành công."; //notifyIcon1.ShowBalloonTip(2000); MessageBox.Show("Thêm câu hỏi thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //Refesh form txt_ContentQuestion.Text = ""; txt_AnswerContent.Text = ""; } } } else { MessageBox.Show("Vui lòng chọn chủ đề cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
//SAVE QUESTION AND ANSWER. CONTINUE INPUT private void btn_SaveAndCreateNewQuestion_Click(object sender, EventArgs e) { QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); int NumAnswer = flp_addAnswer.Controls.Count; if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "" || NumAnswer < 2) { if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "") { MessageBox.Show("Vui lòng nhập thông tin câu hỏi trước khi lưu!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Vui lòng nhập hơn một đáp án!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (checkAnswerEmpty() == true) { MessageBox.Show("Không lưu câu hỏi vì tồn tại đáp án rỗng!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (checkBlankCorrectAnswer()==true) { MessageBox.Show("Vui lòng chọn đáp án cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { question.QuestionTitle = txt_NameQuestion.Text.Trim(); question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.TypeQuestion = "onechoice"; question.IDCatalogue = IDCat; question.Date = DateTime.Now; if (questionBl.AddQuestion(question)) { foreach (Answer_OnlyOneSelect item in flp_addAnswer.Controls) { answer.ContentAnswer = item.txt_Answercontent.Text.Trim(); if (item.rad_check.Checked == true) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = questionBl.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBl.AddAnswer(answer); } //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Thêm câu hỏi thành công."; //notifyIcon1.ShowBalloonTip(2000); MessageBox.Show("Thêm câu hỏi thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //Refesh form this.txt_ContentQuestion.Text = ""; flp_addAnswer.Controls.Clear(); //AUTO ADD 4 ANSWER for (int j = 0; j < 4; j++) { Answer_OnlyOneSelect OneChoiceAnswer = new Answer_OnlyOneSelect(); i++; OneChoiceAnswer.Tag = i; OneChoiceAnswer.ID_Answer = i; OneChoiceAnswer.onDelete += OneChoiceAnswer_onDelete; OneChoiceAnswer.onCheck += OneChoiceAnswer_onCheck; OneChoiceAnswer.rad_check.Text = Convert.ToChar(a + j).ToString(); flp_addAnswer.Controls.Add(OneChoiceAnswer); } } } } } }
//DELETE QUESTION //DELETE QUESTION public bool DeleteQuestionByID(Question Question) { string query = "DELETE FROM Question" + " WHERE Question_ID = '" + Question.IDQuestion + "'"; return DA.UpdateDatabase(query); }
//Load Form private void ViewQuestionShortAnswer_Load(object sender, EventArgs e) { //GET QUESTION BY ID Question QuestionBL QuestionBL = new QuestionBL(); List<DO.Question> QuestionList; Question question = new Question(); QuestionList = QuestionBL.GetQuestion(); if (QuestionList != null) { for (int i = 0; i < QuestionList.Count; i++) { if (QuestionList.ElementAt(i).IDQuestion == IDQuestion) { txt_Catalogue.Text = QuestionList.ElementAt(i).NameCatalogue; txt_ContentQuestion.Text = QuestionList.ElementAt(i).NameQuestion; txt_QuestionType.Text = QuestionList.ElementAt(i).TypeQuestion; txt_Date.Text = QuestionList.ElementAt(i).Date.ToString("d"); } } //GET ANSWER BY ID QUESTION List<DO.Answer> AnswerList; Answer answer = new Answer(); question.IDQuestion = IDQuestion; AnswerList = QuestionBL.GetAnswerByQuestionID(question); if (AnswerList != null) { for (int j = 0; j < AnswerList.Count; j++) { txt_ContentAnswer.Text = AnswerList.ElementAt(j).ContentAnswer; } } } }
//update catalouge by question id public bool EditIDCatalogueQuestionByIDQuestion(Question Question) { string query = "UPDATE Question" + " SET Catalogue_ID ='" + Question.IDCatalogue + "'" + " WHERE Question_ID = '" + Question.IDQuestion + "'"; return DA.UpdateDatabase(query); }
//SAVE QUESTION AND ANSWER private void btn_SaveQuestion_Click(object sender, EventArgs e) { QuestionBL questionBl = new QuestionBL(); Question question = new Question(); Answer answer = new Answer(); int NumAnswer = flp_addAnswer.Controls.Count; if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "" || NumAnswer < 2) { if (txt_ContentQuestion.Text.Trim() == "" || txt_NameQuestion.Text.Trim() == "") { MessageBox.Show("Vui lòng nhập thông tin câu hỏi trước khi lưu!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("Vui lòng nhập hơn một đáp án!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { if (checkAnswerEmpty() == true) { MessageBox.Show("Không lưu câu hỏi vì tồn tại đáp án rỗng!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (checkBlankCorrectAnswer()==true) { MessageBox.Show("Vui lòng chọn đáp án cho câu hỏi!", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { question.QuestionTitle = txt_NameQuestion.Text.Trim(); question.NameQuestion = txt_ContentQuestion.Text.Trim(); question.TypeQuestion = "multichoice"; question.IDCatalogue = IDCat; question.Date = DateTime.Now; if (questionBl.AddQuestion(question)) { foreach (Answer_MultiSelect item in flp_addAnswer.Controls) { if (item.txt_AnswerContent.Text.Trim() != "") { answer.ContentAnswer = item.txt_AnswerContent.Text.Trim(); if (item.chk_Check.Checked == true) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = questionBl.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBl.AddAnswer(answer); } } //Show notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Thêm câu hỏi thành công!"; //notifyIcon1.ShowBalloonTip(1000); MessageBox.Show("Thêm câu hỏi thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); //Close form Form FindForm = this.FindForm(); FindForm.Close(); } } } } }
//SAVE QUESTION private void btn_SaveImport_Click(object sender, EventArgs e) { try { if (txt_FilePath.Text == "") { MessageBox.Show("Vui lòng chọn đường dẫn đến tập tin trước khi lưu!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { int count = 0; foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value== true) { count++; } } if (count>0) { int CheckQuestion = 0; Question question = new Question(); Answer answer = new Answer(); QuestionBL questionBL = new QuestionBL(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["Check"].Value != null && (bool)row.Cells["Check"].Value == true) { if (row.Cells["TypeQuestion"].Value.ToString().Trim() == "shortanswer") { string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None); string[] AnswerItem = AnswerContent[0].Split(new string[] { "---" }, StringSplitOptions.None); //question.QuestionTitle = question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString().Trim(); question.NameQuestion = row.Cells["NameQuestion"].Value.ToString().Trim(); question.TypeQuestion = row.Cells["TypeQuestion"].Value.ToString().Trim(); question.IDCatalogue = IDCat; question.Date = DateTime.Now; questionBL.AddQuestion(question); //answer.IsCorrect = true; answer.Check = 1; answer.ContentAnswer = AnswerItem[1].ToString().Trim(); answer.IDQuestion = questionBL.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBL.AddAnswer(answer); CheckQuestion++; } else { string[] AnswerContent = row.Cells["AnswerContent"].Value.ToString().Trim().Split(new string[] { "</answer>" }, StringSplitOptions.None); //ADD QUESTION MULTIPLE CHOICE question.QuestionTitle = row.Cells["QuestionTitle"].Value.ToString().Trim(); question.NameQuestion = row.Cells["NameQuestion"].Value.ToString().Trim(); question.TypeQuestion = ""; question.IDCatalogue = IDCat; question.Date = DateTime.Now; questionBL.AddQuestion(question); CheckQuestion++; int countMultipleChoice = 0; for (int i = 0; i < AnswerContent.Length - 1; i++) { string[] AnswerItem = AnswerContent[i].Split(new string[] { "---" }, StringSplitOptions.None); if (Convert.ToInt32(AnswerItem[0].ToString().Trim()) > 0) { //answer.IsCorrect = true; answer.Check = 1; answer.ContentAnswer = AnswerItem[1].ToString().Trim(); answer.IDQuestion = questionBL.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBL.AddAnswer(answer); countMultipleChoice++; } else { //answer.IsCorrect = false; answer.Check = 0; answer.ContentAnswer = AnswerItem[1].ToString().Trim(); answer.IDQuestion = questionBL.MaxIDQuestion(); answer.IDCatalogue = IDCat; questionBL.AddAnswer(answer); }//end if }//end for //UPDATE QUESTION TYPE if (countMultipleChoice == 1) { question.TypeQuestion = "onechoice"; question.IDQuestion = questionBL.MaxIDQuestion(); questionBL.EditQuestionTypebyID(question); } else { question.TypeQuestion = "multichoice"; question.IDQuestion = questionBL.MaxIDQuestion(); questionBL.EditQuestionTypebyID(question); } } } }//end foreach //CLOSE FORM if (CheckQuestion > 0) { MessageBox.Show("Nhập " + CheckQuestion + " câu hỏi từ file thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); CheckQuestion = 0; Form FindForm = this.FindForm(); FindForm.Close(); } } else { MessageBox.Show("Vui lòng chọn câu hỏi trước khi lưu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception) { MessageBox.Show("Hệ thống lưu không thành công vì do định dạng file không đúng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//LOAD FORM private void ViewQuestionMultiple_Load(object sender, EventArgs e) { //GET QUESTION BY ID Question QuestionBL QuestionBL = new QuestionBL(); List<DO.Question> QuestionList; Question question = new Question(); QuestionList = QuestionBL.GetQuestion(); if (QuestionList != null) { for (int i = 0; i < QuestionList.Count; i++) { if (QuestionList.ElementAt(i).IDQuestion == IDQuestion) { txt_Catalogue.Text = QuestionList.ElementAt(i).NameCatalogue; txt_ContentQuestion.Text = QuestionList.ElementAt(i).NameQuestion; txt_QuestionType.Text = QuestionList.ElementAt(i).TypeQuestion; txt_Date.Text = QuestionList.ElementAt(i).Date.ToString("d"); } } //GET ANSWER BY ID QUESTION List<DO.Answer> AnswerList; Answer answer = new Answer(); question.IDQuestion = IDQuestion; AnswerList = QuestionBL.GetAnswerByQuestionID(question); if (AnswerList != null) { for (int j = 0; j < AnswerList.Count; j++) { Answer_MultiSelect MultiChoiceAnswer = new Answer_MultiSelect(); i++; MultiChoiceAnswer.Tag = i; MultiChoiceAnswer.ID_Answer = i; MultiChoiceAnswer.onDelete += OneChoiceAnswer_onDelete; MultiChoiceAnswer.chk_Check.Text = Convert.ToChar(a + j).ToString(); MultiChoiceAnswer.txt_AnswerContent.Text = AnswerList.ElementAt(j).ContentAnswer; MultiChoiceAnswer.chk_Check.Checked = AnswerList.ElementAt(j).IsCorrect; MultiChoiceAnswer.txt_AnswerContent.ReadOnly = true; MultiChoiceAnswer.chk_Check.Enabled = false; MultiChoiceAnswer.btn_DelAnswer.Visible = false; flp_Answer.Controls.Add(MultiChoiceAnswer); } } } }
//SAVE QUESTION private void btn_SaveCopy_Click(object sender, EventArgs e) { if (cmb_Catalogue.SelectedItem != null) { //GET ID CATALOGUE AFTER SELECT COMMOBOX CatalogueBL CatBL = new CatalogueBL(); List<DO.Catalogue> CatList; CatList = CatBL.GetCatalogue(); if (CatList != null) for (int i = 0; i < CatList.Count; i++) { if (CatList.ElementAt(i).NameCatalogue == cmb_Catalogue.SelectedItem.ToString()) { IDCatSelected = Convert.ToInt32(CatList.ElementAt(i).IDCatalogue); } } //ADD QUESTION QuestionBL QuestionBL = new QuestionBL(); List<DO.Question> QuestionList; QuestionList = QuestionBL.GetQuestion(); if (QuestionList != null) { for (int i = 0; i < QuestionList.Count; i++) { int count = 0; for (int j = 0; j < IdQ.Length; j++) { if (QuestionList.ElementAt(i).IDQuestion == Convert.ToInt32(IdQ[j])) { count++; } } if (count > 0) { Question question = new Question(); question.QuestionTitle = QuestionList.ElementAt(i).QuestionTitle; question.NameQuestion = QuestionList.ElementAt(i).NameQuestion; question.TypeQuestion = QuestionList.ElementAt(i).TypeQuestion; question.IDCatalogue = IDCatSelected; IDQuestion = QuestionList.ElementAt(i).IDQuestion; question.Date = DateTime.Now; if (QuestionBL.AddQuestion(question)==true) { //ADD ANSWER Question Question = new Question(); Question.IDQuestion = IDQuestion; List<DO.Answer> AnswerList; AnswerList = QuestionBL.GetAnswerByQuestionID(Question); if (AnswerList != null) for (int ii = 0; ii < AnswerList.Count; ii++) { if (AnswerList.ElementAt(ii).IDQuestion == IDQuestion) { Answer answer = new Answer(); answer.ContentAnswer = AnswerList.ElementAt(ii).ContentAnswer; if (AnswerList.ElementAt(ii).IsCorrect == true) { answer.Check = 1; } else { answer.Check = 0; } answer.IDQuestion = QuestionBL.MaxIDQuestion(); answer.IDCatalogue = IDCatSelected; QuestionBL.AddAnswer(answer); } } } } } //Notify //notifyIcon1.Icon = SystemIcons.Information; //notifyIcon1.BalloonTipText = "Sao Chép câu hỏi sang chủ đề \"" + cmb_Catalogue.SelectedItem.ToString() + "\" thành công."; //notifyIcon1.ShowBalloonTip(2000); MessageBox.Show("Sao Chép câu hỏi sang chủ đề \"" + cmb_Catalogue.SelectedItem.ToString() + "\" thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } } else { MessageBox.Show("Vui lòng chọn chủ đề!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }