/// <summary> /// /// </summary> /// <param name="theAnswer"></param> public void CreateAnswer(AnswerData theAnswer) { theAnswer.Id = NextId; if (DataReader != null) DataReader.Close(); SQL = "INSERT INTO `answers` (`answer_id`, `text`, `is_correct`) VALUES (\"" + theAnswer.Id + "\", \"" + theAnswer.Text + "\", \"" + theAnswer.Correct + "\");"; InitializeCommand(); OpenConnection(); int result = ExecuteStoredProcedure(); CloseConnection(); if (result == 0) throw new Exception("Could not add the answer to the database"); }
public static void GetQuestions(QuizData quiz) { //TODO replace code to get questions from database quiz.questions.Clear(); QuestionData newQuestion = new QuestionData(0); AnswerData correctAnswer = new AnswerData(0); AnswerData wrongAnswer = new AnswerData(0); correctAnswer.Correct = true; correctAnswer.Text = "This is the correct answer"; wrongAnswer.Text = "This is a wrong answer"; newQuestion.Text = "This is a question"; newQuestion.answers.Add(correctAnswer); newQuestion.answers.Add(wrongAnswer); newQuestion.answers.Add(wrongAnswer); newQuestion.answers.Add(wrongAnswer); for (int i = 0; i < 10; i++) { quiz.questions.Add(newQuestion); } }
public void AddAnswer(AnswerData answerIn) { answers_internal.Add(answerIn); }
/// <summary> /// handles ok button click event. /// Updates external reference if question is valid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOK_Click(object sender, EventArgs e) { //validate question if (txtQuestion.Text == "") { MessageBox.Show("Question does not have text!", "Invalid Question"); } //validate answers else if ( (txtAnswer1.Text == "") || (txtAnswer2.Text == "") || (txtAnswer3.Text == "") || (txtAnswer4.Text == "") ) { MessageBox.Show("All answers must have text!", "Invalid Question"); } else if ((rbtn1.Checked == false) && (rbtn2.Checked == false) && (rbtn3.Checked == false) && (rbtn4.Checked == false)) { MessageBox.Show("No correct answer has been selected!", "Invalid Question"); } else { //are we creating a new question or updating an old one? if (isNewQuestion) //new question { externalQuestion.Text = txtQuestion.Text; AnswerData answer; answer = new AnswerData(0); answer.Text = txtAnswer1.Text; answer.Correct = rbtn1.Checked; externalQuestion.AddAnswer(answer); answer = new AnswerData(0); answer.Text = txtAnswer2.Text; answer.Correct = rbtn2.Checked; externalQuestion.AddAnswer(answer); answer = new AnswerData(0); answer.Text = txtAnswer3.Text; answer.Correct = rbtn3.Checked; externalQuestion.AddAnswer(answer); answer = new AnswerData(0); answer.Text = txtAnswer4.Text; answer.Correct = rbtn4.Checked; externalQuestion.AddAnswer(answer); this.Disposed -= frmQuestion_Disposed; this.Dispose(); } //change the old question else { externalQuestion.Text = txtQuestion.Text; externalQuestion.Answers[0].Text = txtAnswer1.Text; externalQuestion.Answers[0].Correct = rbtn1.Checked; externalQuestion.Answers[1].Text = txtAnswer2.Text; externalQuestion.Answers[1].Correct = rbtn2.Checked; externalQuestion.Answers[2].Text = txtAnswer3.Text; externalQuestion.Answers[2].Correct = rbtn3.Checked; externalQuestion.Answers[3].Text = txtAnswer4.Text; externalQuestion.Answers[3].Correct = rbtn4.Checked; this.Disposed -= frmQuestion_Disposed; this.Dispose(); } } }
/// <summary> /// /// </summary> /// <param name="theAnswer"></param> public void DeleteAnswer(AnswerData theAnswer) { if (DataReader != null) DataReader.Close(); SQL = "DELETE FROM `answers` WHERE `answer_id` = \"" + theAnswer.Id + "\";"; InitializeCommand(); OpenConnection(); int result = ExecuteStoredProcedure(); if (result == 0) throw new Exception("Could not delete the answer from the database"); if( DataReader != null) DataReader.Close(); SQL = "DELETE FROM `rel_questions_answers` WHERE `answer_id` = \"" + theAnswer.Id + "\";"; InitializeCommand(); result = ExecuteStoredProcedure(); if (result == 0) throw new Exception("Could not delete the answer relation from the database"); if (DataReader != null) DataReader.Close(); SQL = "DELETE FROM `rel_answers_users` WHERE `answer_id` = \"" + theAnswer.Id + "\";"; InitializeCommand(); result = ExecuteStoredProcedure(); CloseConnection(); if (result == 0) throw new Exception("Could not clear the results for that answer"); }
/// <summary> /// /// </summary> /// <param name="theAnswer"></param> public void UpdateAnswer(AnswerData theAnswer) { if (DataReader != null) DataReader.Close(); SQL = "UPDATE `answer` a SET a.`text` = \"" + theAnswer.Text + "\", a.`correct` = \"" + theAnswer.Correct + "\" WHERE a.`answer_id` = \"" + theAnswer.Id + "\";"; InitializeCommand(); OpenConnection(); int result = ExecuteStoredProcedure(); CloseConnection(); if (result == 0) throw new Exception("The Answer Could Not Be Updated"); }
/// <summary> /// Gets a list of Answers object associated with a question /// </summary> /// <param name="theQuestion">the question being queried</param> /// <returns>a list of Answer objects</returns> public List<AnswerData> ReadAnswers(QuestionData theQuestion) { List<AnswerData> return_data = new List<AnswerData>(); if (DataReader != null) DataReader.Close(); SQL = "SELECT * from `answers` a INNER JOIN `rel_questions_answers` r ON r.`answer_id` = a.`answer_id` WHERE r.`question_id` = \"" + theQuestion.id + "\";"; InitializeCommand(); OpenConnection(); DataReader = Command.ExecuteReader(); if (DataReader.HasRows) { while (DataReader.Read()) { AnswerData temp = new AnswerData(DataReader.GetUInt16("answer_id")); temp.Correct = DataReader.GetBoolean("is_correct"); temp.Text = DataReader.GetString("text"); return_data.Add(temp); } } CloseConnection(); return return_data; }
/// <summary> /// Gets a single answer object from the database with the specified Text /// </summary> /// <param name="text">the text of the answer</param> /// <returns>an Answer object</returns> public AnswerData ReadAnswer(String text) { AnswerData return_value = null; if (DataReader != null) DataReader.Close(); SQL = "SELECT * FROM `answers` a WHERE a.`text` = \"" + text + "\";"; InitializeCommand(); OpenConnection(); DataReader = Command.ExecuteReader(); if (DataReader.HasRows) { DataReader.Read(); return_value = new AnswerData(DataReader.GetUInt16("answer_id")); return_value.Text = DataReader.GetString("text"); return_value.Correct = DataReader.GetBoolean("is_correct"); } CloseConnection(); if (return_value == null) throw new Exception("The Answer did not exist on the database"); return return_value; }
public static bool ValidateAnswer(AnswerData theAnswer) { return string.IsNullOrEmpty(theAnswer.Text); }