示例#1
0
        public L_ViewTestForm(UserSession currentUser)
        {
            InitializeComponent();
            tsiTests.BackColor = Color.DarkRed;
            this.currentUser   = currentUser;
            tsiAccount.Text    = currentUser.Username;

            //registers the Refresh() method to the event handler
            cbxTests.SelectedIndexChanged += (object obj, EventArgs e) => { Refresh(); };

            //load tests from database
            try
            {
                tests = new DBControl().GetTests(currentUser);
                foreach (Test test in tests)
                {
                    cbxTests.Items.Add(test.TestName);
                }

                cbxTests.SelectedIndex = 0;//set as default selected
            } catch (Exception ex)
            {
                StackLog.GetInstance().Log(ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Failed to load tests", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#2
0
        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            bool submit = true;

            //save current question answer
            test.Questions[questionIndex].StudentAnswer = rbnAnswerA.Checked ? 'a' :
                                                          rbnAnswerB.Checked ? 'b' : rbnAnswerC.Checked ? 'c' : default(char);

            foreach (Question question in test.Questions)
            {
                if (question.StudentAnswer == default(char))
                {
                    DialogResult res = MessageBox.Show(
                        "You have not completed all the answers \nAre you sure you want to submit?",
                        "Incomplete Test",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Warning);
                    if (res == DialogResult.No || res == DialogResult.Cancel)
                    {
                        submit = false;
                        break;
                    }
                }
            }

            if (submit)
            {
                try
                {
                    int mark = 0, percentage;
                    foreach (Question question in test.Questions)
                    {
                        if (question.CorrectAnswer == question.StudentAnswer)
                        {
                            mark++;
                        }
                    }

                    percentage      = (int)((double)mark / (double)test.Questions.Count * 100.0);
                    test.Mark       = mark;
                    test.Percentage = percentage;
                    test.AttemptsMade++;

                    bool subit = new DBControl().SubmitTest(currentUser, test);

                    if (subit)
                    {
                        MessageBox.Show("Test saved successfully", "Success");
                    }

                    new S_ViewMemoForm(currentUser, test).Show();
                    this.Hide();
                }
                catch (Exception ex)
                {
                    StackLog.GetInstance().Log(ex.Message + "\n" + ex.StackTrace);
                    MessageBox.Show("Failed to submit test", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        public S_StudentHome(UserSession currentUser)
        {
            InitializeComponent();
            tsiHome.BackColor = Color.DarkRed;
            this.currentUser  = currentUser;
            tsiAccount.Text   = currentUser.Username;

            int takenCount = 0, average = 0;

            //load tests from database
            try
            {
                tests = new DBControl().GetTests(currentUser);
            }
            catch (Exception ex)
            {
                StackLog.GetInstance().Log(ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            foreach (Test test in tests)
            {
                if (test.AttemptsMade > 0)
                {
                    takenCount++;
                    average += test.Percentage;
                }
                dataGridView1.Rows.Add(
                    test.TestName,
                    test.AttemptsMade + " of " + (test.AttemptsAllowed == -1 ? "unlimited" : test.AttemptsAllowed.ToString()),
                    test.AttemptsMade == 0 ? "--" : test.Mark.ToString() + " / " + test.Questions.Count.ToString(),
                    test.AttemptsMade == 0 ? "--" : test.Percentage.ToString() + "%");
            }

            //update UI with student data
            lblTestsTaken.Text     = takenCount.ToString();
            lblTestTotalCount.Text = tests.Count.ToString();
            if (takenCount > 0)
            {
                average        /= takenCount;
                lblAverage.Text = average.ToString();
            }
            else
            {
                lblAverage.Text = "--";
            }

            //add listner to datagrid view
            dataGridView1.SelectionChanged += (object obj, EventArgs e) =>
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    selectedTest = dataGridView1.SelectedRows[0].Index;
                }
                else if (dataGridView1.SelectedCells.Count > 0)
                {
                    selectedTest = dataGridView1.SelectedCells[0].RowIndex;
                }
            };
        }
示例#4
0
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            Refresh();

            bool isUsername = false;
            bool isPassword = false;

            if (tbxUsername.Text == "")
            {
                lblErrMsgUsername.Visible = true;
            }
            else
            {
                isUsername = true;
            }

            if (tbxPassword.Text == "")
            {
                lblErrMsgPassword.Visible = true;
            }
            else
            {
                isPassword = true;
            }

            if (isUsername && isPassword)
            {
                try
                {
                    UserSession currentUser = new DBControl().LoginUser(tbxUsername.Text, tbxPassword.Text);

                    if (currentUser == null)
                    {
                        MessageBox.Show("Username or/and password is incorrect.",
                                        "Invalid Credentials");
                    }
                    else
                    {
                        switch (currentUser.UserType)
                        {
                        case 's':
                            new S_StudentHome(currentUser).Show(); this.Hide();
                            break;

                        case 'l':
                            new L_ClassListForm(currentUser).Show(); this.Hide();
                            break;

                        case 'r':
                            new R_RegisterForm(currentUser).Show(); this.Hide();
                            break;
                        }
                    }
                } catch (Exception ex)
                { StackLog.GetInstance().Log(ex.StackTrace); MessageBox.Show(ex.Message, "Error"); }
            }
        }
示例#5
0
        public override void Refresh()
        {
            base.Refresh();

            //loads selected test
            dgvTestQAndA.Rows.Clear();
            int selectedTest = cbxTests.SelectedIndex;

            foreach (Question question in tests[selectedTest].Questions)
            {
                int i = dgvTestQAndA.Rows.Add();
                dgvTestQAndA.Rows[i].Cells[0].Value = question.QuestionNum + ". " + question.QuestionText;
                char correctAnswer = question.CorrectAnswer;

                i = dgvTestQAndA.Rows.Add();
                dgvTestQAndA.Rows[i].Cells[0].Value = question.Answers['a'];
                if (correctAnswer == 'a')
                {
                    dgvTestQAndA.Rows[i].Cells[0].Style.BackColor = Color.DarkSeaGreen;
                }

                i = dgvTestQAndA.Rows.Add();
                dgvTestQAndA.Rows[i].Cells[0].Value = question.Answers['b'];
                if (correctAnswer == 'b')
                {
                    dgvTestQAndA.Rows[i].Cells[0].Style.BackColor = Color.DarkSeaGreen;
                }

                i = dgvTestQAndA.Rows.Add();
                dgvTestQAndA.Rows[i].Cells[0].Value = question.Answers['c'];
                if (correctAnswer == 'c')
                {
                    dgvTestQAndA.Rows[i].Cells[0].Style.BackColor = Color.DarkSeaGreen;
                }

                dgvTestQAndA.Rows.Add();
            }

            try
            {
                int average = new DBControl().GetTestAverage(tests[cbxTests.SelectedIndex].TestId);

                if (average == -1)
                {
                    lblAveragePercent.Text = "--";
                }
                else
                {
                    lblAveragePercent.Text = average.ToString();
                }
            } catch (Exception ex) { StackLog.GetInstance().Log(ex.Message + "\n" + ex.StackTrace); }
        }
示例#6
0
        private void BtnCreateText_Click(object sender, EventArgs e)
        {
            bool isValidTest     = ValidateTest();
            bool isValidQuestion = ValidateTestQuestion();
            bool isConcent       = false;

            if (!isValidTest)
            {
                MessageBox.Show("Cannot save test without a name",
                                "Missing name",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                isConcent = ConsentUser(isValidQuestion);

                if (isValidQuestion && isConcent)
                {
                    //save question
                    int QIndex = dispQuestionIndex;
                    test.Questions[QIndex].QuestionNum   = QIndex + 1;
                    test.Questions[QIndex].QuestionText  = tbxQuestion.Text;
                    test.Questions[QIndex].CorrectAnswer = Convert.ToChar(cbxCorrectAnswer.SelectedItem.ToString());
                    test.Questions[QIndex].Answers['a']  = tbxAnswerA.Text;
                    test.Questions[QIndex].Answers['b']  = tbxAnswerB.Text;
                    test.Questions[QIndex].Answers['c']  = tbxAnswerC.Text;
                }
                else
                {
                    test.Questions.RemoveAt(dispQuestionIndex);
                }

                try
                {
                    test.TestName        = tbxTestName.Text;
                    test.AttemptsAllowed = cbxAllowedAttempts.SelectedIndex == 3 ? -1 : cbxAllowedAttempts.SelectedIndex + 1;

                    new DBControl().SaveNewTest(currentUser, test);
                    MessageBox.Show("Test saved!", "Success");

                    new L_ViewTestForm(currentUser).Show();
                    this.Hide();
                }
                catch (Exception ex) { StackLog.GetInstance().Log(ex.StackTrace); MessageBox.Show(ex.Message); }
            }
        }
示例#7
0
        public L_ClassListForm(UserSession currentUser)
        {
            InitializeComponent();

            //initialize data
            tsiClassList.BackColor = Color.DarkRed;
            this.currentUser       = currentUser;
            tsiAccount.Text        = currentUser.Username;
            try {
                tests = new DBControl().GetTests(currentUser);
            }
            catch (Exception ex) { StackLog.GetInstance().Log(ex.Message + "\n" + ex.StackTrace); MessageBox.Show(ex.Message, "Error"); }

            cbxTestName.SelectedIndexChanged += (object obj, EventArgs e) => { LoadDataGrid(this, e); };

            foreach (Test test in tests)
            {
                cbxTestName.Items.Add(test.TestName);
            }
            if (cbxTestName.Items.Count > 0)
            {
                cbxTestName.SelectedIndex = 0;
            }
        }
        private void BtnRegister_Click(object sender, EventArgs e)
        {
            Refresh();
            bool isUsername = false, isName = false, isSurname = false, isPassword = false,
                 isUserType = false;

            //validate inputs
            if (tbxUsername.Text == "")
            {
                lblErrMsgUsername.Visible = true;
            }
            else
            {
                isUsername = true;
            }

            if (tbxName.Text == "")
            {
                lblErrMsgName.Visible = true;
            }
            else
            {
                isName = true;
            }

            if (tbxSurname.Text == "")
            {
                lblErrMsgSurname.Visible = true;
            }
            else
            {
                isSurname = true;
            }

            if (tbxPassword.Text == "")
            {
                lblErrMsgPassword.Visible = true;
            }
            else
            {
                isPassword = true;
            }

            if (cbxUserType.SelectedItem == null)
            {
                lblErrMsgUserType.Visible = true;
            }
            else
            {
                isUserType = true;
            }

            //register user if inputs valid
            if (isUsername && isName && isSurname && isPassword && isUserType)
            {
                bool isRegistered = false;
                try
                {
                    char userType = cbxUserType.SelectedIndex == 0 ? 'r' : cbxUserType.SelectedIndex == 1 ? 's' : 'l';
                    isRegistered = new DBControl().RegisterUser(currentUser, tbxUsername.Text,
                                                                tbxName.Text, tbxSurname.Text, tbxPassword.Text, userType);

                    if (isRegistered)
                    {
                        MessageBox.Show("User successfully registered", "Success");
                        tbxName.Text              = String.Empty;
                        tbxSurname.Text           = String.Empty;
                        tbxPassword.Text          = String.Empty;
                        tbxUsername.Text          = String.Empty;
                        cbxUserType.SelectedIndex = -1;
                    }
                    else
                    {
                        MessageBox.Show("Failed to register user", "Failed");
                    }
                }
                catch (Exception ex)
                {
                    StackLog.GetInstance().Log(ex.StackTrace); MessageBox.Show(ex.Message, "Error");
                }
            }
        }