-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewExam.cs
143 lines (123 loc) · 3.43 KB
/
NewExam.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MtiExamSystem
{
public partial class NewExam : Form
{
int examId = 0;
public NewExam()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
{
MessageBox.Show("Only numbers are allowed !");
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
private async void button1_Click(object sender, EventArgs e)
{
if (nameInput.Text.Length == 0 || textBox1.Text.Length == 0) {
MessageBox.Show("Can't add questions until you fill in the exam data !");
return;
}
var startTime = startsAtTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
var result = await ExamSystemAPI.GetInstance().NewExam(nameInput.Text, int.Parse(textBox1.Text), startTime);
if (result == -1) {
MessageBox.Show("Error adding new exam !");
return;
}
examId = result;
panel1.Hide();
panel2.Visible = true;
}
private void truefalseButton_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = truefalseButton.Checked;
if (isChecked)
{
optionsList.Items.Clear();
optionsList.Items.Add("true");
optionsList.Items.Add("false");
addChoiceButton.Hide();
choiceInput.Hide();
}
else {
optionsList.Items.Clear();
addChoiceButton.Show();
choiceInput.Show();
}
}
private void addChoiceButton_Click(object sender, EventArgs e)
{
if (choiceInput.Text.Length == 0) {
MessageBox.Show("Please fill in the choice input");
return;
}
optionsList.Items.Add(choiceInput.Text);
choiceInput.Text = "";
}
private async void newQuestionButton_Click(object sender, EventArgs e)
{
if (marksInput.Text.Length == 0)
{
MessageBox.Show("Please enter the marks for this question");
}
if (questionInput.Text.Length == 0)
{
MessageBox.Show("Enter the question title !");
return;
}
if (optionsList.Items.Count == 0)
{
MessageBox.Show("There needs to be choices !");
return;
}
if (optionsList.SelectedIndex < 0)
{
MessageBox.Show("You need to select an answer !");
return;
}
String options = "[";
foreach (var item in optionsList.Items)
{
options += '"' + item.ToString() + '"' + ",";
}
options = options.Remove(options.Length - 1);
options += "]";
String type = truefalseButton.Checked ? "truefalse" : "multichoice";
var addQuestion = await ExamSystemAPI.GetInstance().AddQuestion(examId, questionInput.Text, optionsList.SelectedItem.ToString(), options, type, marksInput.Text);
if (addQuestion)
{
MessageBox.Show("Question added successfully");
}
else
{
MessageBox.Show("Something went wrong when adding the question !");
}
}
private void NewExam_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
startsAtTime.Format = DateTimePickerFormat.Custom;
startsAtTime.CustomFormat = "yyyy-MM-dd hh:mm:ss";
}
private void marksInput_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(marksInput.Text, "[^0-9]"))
{
MessageBox.Show("Only numbers are allowed !");
marksInput.Text = marksInput.Text.Remove(marksInput.Text.Length - 1);
}
}
}
}