예제 #1
0
 //List<Topic> selectedTopics
 public Test(List <Question> questions, QuizPreference preferences, User currentUser)
 {
     InitializeComponent();
     this.questions   = questions;
     this.topics      = preferences.Topics;
     this.currentUser = currentUser;
     this.preferences = preferences;
     prepareQuestions(true);
     prepareUi();
 }
예제 #2
0
        // Create custom Quiz
        private void button6_Click(object sender, EventArgs e)
        {
            if (checkedListBox2.CheckedItems.Count > 0)
            {
                QuizPreference preferences = new QuizPreference();
                preferences.Easy         = checkedListBox3.GetItemChecked(0);
                preferences.Intermediate = checkedListBox3.GetItemChecked(1);
                preferences.Hard         = checkedListBox3.GetItemChecked(2);

                preferences.Bookwork    = checkedListBox4.GetItemChecked(0);
                preferences.Background  = checkedListBox4.GetItemChecked(1);
                preferences.Application = checkedListBox4.GetItemChecked(2);

                preferences.QuizSize = Int32.Parse(textBox1.Text);

                List <Topic> selectedTopics = new List <Topic>();

                foreach (object o in checkedListBox2.CheckedItems)
                {
                    selectedTopics.Add((Topic)o);
                }

                preferences.Topics = selectedTopics;
                preferences.Help   = checkBox1.Checked;

                List <Question> questions = new List <Question>();

                // If user wants to give priority to failed questions
                if (checkBox3.Checked)
                {
                    questions.AddRange(db.getPastFailedQuestions(currentUser, preferences.Topics, preferences.QuizSize));
                    // recalculate preferences for the rest of questions
                    preferences.QuizSize = preferences.QuizSize - questions.Count;
                }


                questions.AddRange(db.getQuestionsFromPreferences(preferences));

                this.Hide();
                Form form = new Test(questions, preferences, currentUser);
                //Form form = new Test(selectedTopics, currentUser);
                form.Closed += (s, args) => this.Show();
                form.Show();
            }
            else
            {
                MessageBox.Show("Please select a topic");
            }
        }
예제 #3
0
        public List <Question> getQuestionsFromPreferences(QuizPreference preferences)
        {
            List <Question> questions            = new List <Question>();
            bool            difficultyPreference = false;
            bool            naturePreference     = false;
            String          sqlCommand           = "SELECT q.id, q.question, q.question_type, q.difficulty, q.nature, q.feedback, q.topic, t.name FROM questions q, topics t WHERE ";

            if (preferences.Easy)
            {
                sqlCommand          += "(q.difficulty = 'Easy' ";
                difficultyPreference = true;
            }

            if (preferences.Intermediate)
            {
                if (difficultyPreference)
                {
                    sqlCommand += "OR q.difficulty = 'Intermediate' ";
                }
                else
                {
                    sqlCommand          += "(q.difficulty = 'Intermediate' ";
                    difficultyPreference = true;
                }
            }

            if (preferences.Hard)
            {
                if (difficultyPreference)
                {
                    sqlCommand += "OR q.difficulty = 'Hard') ";
                }
                else
                {
                    sqlCommand          += "(q.difficulty = 'Hard') ";
                    difficultyPreference = true;
                }
            }
            else
            {
                if (difficultyPreference)
                {
                    sqlCommand += ") ";
                }
            }

            if (preferences.Application || preferences.Background || preferences.Bookwork)
            {
                if (difficultyPreference)
                {
                    sqlCommand += "AND ";
                }

                if (preferences.Application)
                {
                    sqlCommand      += "(q.nature = 'Application' ";
                    naturePreference = true;
                }

                if (preferences.Background)
                {
                    if (naturePreference)
                    {
                        sqlCommand += "OR q.nature = 'Background' ";
                    }
                    else
                    {
                        sqlCommand      += "(q.nature = 'Background' ";
                        naturePreference = true;
                    }
                }

                if (preferences.Bookwork)
                {
                    if (naturePreference)
                    {
                        sqlCommand += "OR q.nature = 'Bookwork') ";
                    }
                    else
                    {
                        sqlCommand      += "(q.nature = 'Bookwork') ";
                        naturePreference = true;
                    }
                }
                else if (naturePreference)
                {
                    sqlCommand += ") ";
                }
            }

            if (preferences.Topics.Count > 0)
            {
                sqlCommand += "AND (";
                foreach (Topic t in preferences.Topics)
                {
                    sqlCommand += "q.topic=" + t.Id + " OR ";
                }
                sqlCommand  = sqlCommand.Remove(sqlCommand.Length - 3);
                sqlCommand += ") ";
            }

            sqlCommand += "AND (q.topic = t.id) ORDER BY RAND() LIMIT " + preferences.QuizSize + ";";

            MySqlCommand    command = new MySqlCommand(sqlCommand, conn);
            MySqlDataReader rdr     = command.ExecuteReader();

            while (rdr.Read())
            {
                questions.Add(new Question(Int32.Parse(rdr[0].ToString()), rdr[1].ToString(), new Topic(Int32.Parse(rdr[6].ToString()), rdr[7].ToString()), rdr[2].ToString(), rdr[3].ToString(), rdr[4].ToString(), rdr[5].ToString()));
            }
            rdr.Close();
            System.Diagnostics.Debug.WriteLine(sqlCommand);
            return(questions);
        }