示例#1
0
        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);
            }
        }
示例#2
0
        public static bool ValidateQuestion(QuestionData theQuestion)
        {
            bool is_valid = true;

            if (theQuestion.Text.Length <= 4) {
                is_valid = false;
            }

            foreach (AnswerData data in theQuestion.Answers) {
                is_valid = AnswerController.ValidateAnswer(data);
                if (!is_valid)
                    break;
            }

            if (is_valid)
                if (theQuestion.Answers.Count < 2)
                    is_valid = false;

            if (is_valid) {
                int count = 0;
                for (int i = 0; i < theQuestion.Answers.Count; i++)
                    if (theQuestion.Answers[i].Correct)
                        count++;

                is_valid = (count == 1);
            }

            return is_valid;
        }
示例#3
0
        /// <summary>
        /// Gets the number of correct answers for a question
        /// </summary>
        /// <param name="theQuestion"></param>
        /// <returns></returns>
        public int CountCorrect(QuestionData theQuestion)
        {
            try
            {
                if (DataReader != null)
                    DataReader.Close();

                this.SQL = "SELECT COUNT(ra.`user_id`) AS `CountCorrect` FROM `questions` q " +
                    "JOIN `rel_questions_answers` qa ON q.`question_id` = qa.`question_id` " +
                    "JOIN `answers` a ON qa.`answer_id` = a.`answer_id` " +
                    "JOIN `rel_answers_users` ra ON a.`answer_id` = ra.`answer_id` " +
                    "WHERE a.`is_correct` = 1 AND q.`question_id` = " + theQuestion.Id + " " +
                    "GROUP BY q.`question_id`;";
                this.InitializeCommand();
                OpenConnection();

                this.DataReader = this.Command.ExecuteReader();

                if (this.DataReader.HasRows)
                {
                    this.DataReader.Read();
                    return DataReader.GetUInt16("CountCorrect");
                }
                else
                    return 0;
            }
            catch (System.Exception e)
            {
                throw new System.Exception(e.Message, e.InnerException);
            }
            finally
            {

            }
        }
示例#4
0
        public void AddQuestion(QuizData theQuiz, QuestionData theQuestion)
        {
            if (DataReader != null)
                DataReader.Close();

            SQL = "INSERT INTO `rel_quizzes_questions` (`quiz_id`, `question_id`) VALUES (\"" + theQuiz.Id + "\", \"" + theQuestion.Id + "\");";

            InitializeCommand();

            int result = ExecuteStoredProcedure();

            if (result == 0)
                throw new Exception("Unable to add question to quiz");
        }
示例#5
0
        public static void SaveQuestion(QuestionData theQuestion)
        {
            if (theQuestion.Id == 0) {

                if (ValidateQuestion(theQuestion))
                    theEntity.UpdateQuestion(theQuestion);

            } else {

                if (ValidateQuestion(theQuestion))
                    theEntity.UpdateQuestion(theQuestion);

            }
        }
示例#6
0
        public void CreateQuestion(QuestionData theQuestion)
        {
            theQuestion.Id = NextId;

            AnswerEntity temp;

            temp = new AnswerEntity();

            for (int i = 0; i < theQuestion.Answers.Count; i++) {
                temp.CreateAnswer(theQuestion.Answers[i]);
            }
            temp.Dispose();

            if (DataReader != null)
                DataReader.Close();

            SQL = "INSERT INTO `questions` (`question_id`, `question`) VALUES (\"" + theQuestion.Id + "\", \"" + theQuestion.Text + "\");";

            OpenConnection();
            InitializeCommand();

            int result = ExecuteStoredProcedure();

            if (DataReader != null)
                DataReader.Close();

            if (result == 0)
                throw new Exception("Cannot add the selected question to the database");

            for (int i = 0; i < theQuestion.Answers.Count; i++) {
                if (DataReader != null)
                    DataReader.Close();

                SQL = "INSERT INTO `rel_questions_answers` (`question_id`, `answer_id`) VALUES (\"" +
                    theQuestion.Id +
                    "\", \"" +
                    theQuestion.Answers[i].Id +
                    "\");";
                InitializeCommand();

                result = ExecuteStoredProcedure();
                if (result == 0)
                    throw new Exception("One or more of the answers could not be added to the database");

            }

            CloseConnection();
        }
示例#7
0
        /// <summary>
        /// Default Constructor
        /// Stores references to external variables
        /// </summary>
        /// <param name="question">question to be editing</param>
        public frmQuestion(QuestionData question)
        {
            externalQuestion = question; //copy external reference
            isNewQuestion = (question.Answers.Count == 0); //if we have no questions, we know were new

            InitializeComponent();

            //if we have data then display it
            if (externalQuestion.Answers.Count > 0)
            {
                txtQuestion.Text = externalQuestion.Text;
                txtAnswer1.Text = externalQuestion.Answers[0].Text;
                txtAnswer2.Text = externalQuestion.Answers[1].Text;
                txtAnswer3.Text = externalQuestion.Answers[2].Text;
                txtAnswer4.Text = externalQuestion.Answers[3].Text;
                rbtn1.Checked = externalQuestion.Answers[0].Correct;
                rbtn2.Checked = externalQuestion.Answers[1].Correct;
                rbtn3.Checked = externalQuestion.Answers[2].Correct;
                rbtn4.Checked = externalQuestion.Answers[3].Correct;
            }
        }
示例#8
0
            public QuestionBox(QuestionData question, int number)
            {
                //
                // rbtnAnswers
                //
                rbtnAnswers = new RadioButton[question.answers.Count];
                for (int i = question.answers.Count - 1; i >= 0; i--)
                {
                    rbtnAnswers[i] = new RadioButton();
                    rbtnAnswers[i].Text = question.answers[i].text;
                    rbtnAnswers[i].Dock = DockStyle.Top;
                    rbtnAnswers[i].Padding = new Padding(15, 3, 3, 3);
                    Controls.Add(rbtnAnswers[i]);

                }
                //
                // lblQuestion
                //
                lblQuestion = new Label();
                lblQuestion.Text = question.Text;
                lblQuestion.Dock = DockStyle.Top;
                lblQuestion.Padding = new Padding(10, 3, 3, 3);
                Controls.Add(lblQuestion);
                //
                // lblHeader
                //
                lblHeader = new Label();
                lblHeader.Text = "#" + number;
                lblHeader.TextAlign = ContentAlignment.MiddleLeft;
                lblHeader.Font = new Font("Lucida Handwriting", 24, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
                lblHeader.Dock = DockStyle.Top;
                lblHeader.Height = 32;
                lblHeader.BackColor = Color.Maroon;
                lblHeader.ForeColor = Color.White;
                Controls.Add(lblHeader);
                //
                // QuestionBox
                //
                this.Height = rbtnAnswers[rbtnAnswers.GetLength(0) - 1].Bottom;
            }
示例#9
0
        public QuestionData ReadQuestion(int id)
        {
            QuestionData return_data = null;

            if (DataReader != null)
                DataReader.Close();

            SQL = "SELECT * FROM `questions` q WHERE q.`question_id` = \"" + id + "\";";

            InitializeCommand();
            OpenConnection();
            DataReader = Command.ExecuteReader();

            if (DataReader.HasRows) {
                DataReader.Read();
                return_data = new QuestionData(DataReader.GetUInt16("question_id"));
                return_data.Text = DataReader.GetString("question");
                DataReader.Close();
            }

            if (return_data != null) {
                AnswerEntity temp = new AnswerEntity();
                List<AnswerData> temp1 = temp.ReadAnswers(return_data);
                for (int i = 0; i < temp1.Count; i++)
                    return_data.AddAnswer(temp1[i]);
                temp.Dispose();
            }

            CloseConnection();

            return return_data;
        }
示例#10
0
        public void UpdateQuestion(QuestionData theQuestion)
        {
            AnswerEntity temp = new AnswerEntity();

            for (int i = 0; i < theQuestion.Answers.Count; i++)
                temp.UpdateAnswer(theQuestion.Answers[i]);

            temp.Dispose();

            if (DataReader != null)
                DataReader.Close();

            SQL = "UPDATE `questions` q SET q.`question` = \"" + theQuestion.Text + "\" WHERE q.`question_id` = \"" + theQuestion.Id + "\";";

            OpenConnection();
            InitializeCommand();

            int result = ExecuteStoredProcedure();
            CloseConnection();

            if (result == 0)
                throw new Exception("Unable to edit the question on database");
        }
示例#11
0
        /// <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;
        }
示例#12
0
        public void DeleteQuestion(QuestionData theQuestion)
        {
            if (DataReader != null)
                DataReader.Close();

            SQL = "DELETE `questions`, `answers`, `rel_questions_answers` FROM `questions` INNER JOIN `rel_questions_answers` on `questions`.`question_id` = `rel_questions_answers`.`question_id` INNER JOIN `answers` ON `answers`.`answer_id` = `rel_questions_answers`.`answer_id` WHERE `questions`.`question_id` = \"" + theQuestion.Id + "\";";

            InitializeCommand();
            OpenConnection();

            int result = ExecuteStoredProcedure();

            CloseConnection();

            if (result == 0)
                throw new Exception("Could not delete the data from the database");
        }
示例#13
0
        void btnAddQuestion_Click(object sender, EventArgs e)
        {
            QuestionData newQuestion = new QuestionData(0);
            frmQuestion questionForm = new frmQuestion(newQuestion);
            questionForm.ShowDialog();

            //check to see the question is valid ie user didnt cancel
            if (newQuestion.Answers.Count > 0)
            {
                //add question to display
                QuestionBox questionBox = new QuestionBox(newQuestion, questionBoxes.Count + 1, myObjective);
                questionBox.Disposed += new EventHandler(questionBox_Disposed);
                questionBoxes.Add(questionBox);
                pnlMain.Controls.Add(questionBox);

                //add question to currentQuiz we are editing
                GlobalData.currentQuiz.addQuestion(newQuestion);

                //set locations via resize
                pnlMain_Resize(null, null);

                pnlMain.ScrollControlIntoView(questionBox);
            }
        }
示例#14
0
            public QuestionBox(QuestionData question, int number, Objective objective)
            {
                //copy question
                myQuestion = question;

                //copy objective
                myObjective = objective;

                //create default components
                InitializeComponent();

                //Create objective specific controls
                InitializeObjectiveComponent();

                //
                // QuestionBox
                //
                this.BackColor = Color.White;
                this.BorderStyle = BorderStyle.FixedSingle;
                this.Resize += new EventHandler(QuestionBox_Resize);

                //set the number
                Number = number;
            }
示例#15
0
 public static int GetWrongCount(QuestionData theQuestion)
 {
     return theEntity.CountWrong(theQuestion);
 }
示例#16
0
 public static void DeleteQuestion(QuestionData theQuestion)
 {
     theEntity.DeleteQuestion(theQuestion);
 }
示例#17
0
 public void addQuestion(QuestionData dataIn)
 {
     questions_internal.Add(dataIn);
 }
示例#18
0
 public void addQuestion(QuestionData dataIn)
 {
     questions_internal.Add(dataIn);
 }
示例#19
0
 public static int GetCorrectCount(QuestionData theQuestion)
 {
     return theEntity.CountCorrect(theQuestion);
 }