//вибір дії private void comboBoxAct_SelectedIndexChanged(object sender, EventArgs e) { string act = comboBoxAct.SelectedItem.ToString(); switch (act) { case "Add test to DB": { OpenFileDialog file = new OpenFileDialog(); file.ShowDialog(); string xmlTest = File.ReadAllText(file.FileName); var time = DateTime.Now; using (ExamVceDB test = new ExamVceDB()) { test.TestDBs.Add(new TestDB() { IdFromPerson = loginPerson.ID, Test = xmlTest, DateLoad = time, }); test.SaveChanges(); MessageBox.Show("test was added"); } ChangeIDForTest(); break; } case "Send test to": { break; } case "Add student to DB": { AddStudentToDB addStudent = new AddStudentToDB(); addStudent.ShowDialog(); break; } case "Choose exam": { listBoxExam.Enabled = true; using (ExamVceDB exam = new ExamVceDB()) { foreach (var item in exam.TestDBs) { MakeTest subject = DeserialiseFromString(item.Test); listBoxExam.Items.Add($"{item.ID}* {subject.Subject}"); } } break; } } if (comboBoxAct.SelectedItem.Equals("Add test to DB")) { } }
//запис айди тесту void ChangeIDForTest() { using (ExamVceDB test = new ExamVceDB()) { //var last = test.TestDBs.Last(); TestDB last = null; foreach (var item in test.TestDBs) { last = item; } var result = test.TestDBs.FirstOrDefault(i => i.ID == last.ID); var serializer = new XmlSerializer(typeof(MakeTest)); MakeTest desr; using (TextReader reader = new StringReader(result.Test)) { desr = (MakeTest)serializer.Deserialize(reader); } desr.IdTest = last.ID; XmlSerializer xmlSerializer = new XmlSerializer(typeof(MakeTest)); var stringWriter = new StringWriter(); xmlSerializer.Serialize(stringWriter, desr); last.Test = stringWriter.ToString(); test.SaveChanges(); } }
void FillTable() { using (ExamVceDB pass = new ExamVceDB()) { //var query = pass.PassExams.Join(pass.Students, c => c.Student.Name); foreach (var item in pass.PassExams) { try { ListViewItem i = new ListViewItem(item.ID.ToString()); var studentName = pass.Students.Where(q => q.ID == item.Id_Student).ToList(); i.SubItems.Add(studentName[0].Name); string testForm = pass.TestDBs.First(q => q.ID == item.Id_Test).Test; MakeTest test = DeserialiseFromString(testForm); i.SubItems.Add(test.Subject); i.SubItems.Add(item.Date_Pass.ToString()); i.SubItems.Add(item.Mark.ToString()); listViewPassExam.Items.Add(i); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void buttonLogin_Click(object sender, EventArgs e) { buttonLogin.Enabled = false; Task.Run(() => { using (ExamVceDB staff = new ExamVceDB()) { foreach (var item in staff.Staffs) { char[] login = item.Login.ToCharArray(); var password = item.Password.ToCharArray(); int endPosLogin = 0; int endPosPass = 0; while (true) { if (login[endPosLogin] == ' ' || endPosLogin > item.Login.Length - 1) { break; } endPosLogin++; } while (true) { if (password[endPosPass] == ' ' || endPosPass > item.Login.Length - 1) { break; } endPosPass++; } string log = new string(login, 0, endPosLogin); string pass = new string(password, 0, endPosPass); if (textBoxLogin.Text.Equals(log) && textBoxPassword.Text.Equals(pass)) { person = item; isSuccess = true; break; } } } if (isSuccess) { MessageBox.Show("Success"); this.Close(); return; } MessageBox.Show($"Login: {textBoxLogin.Text} not found. Check your password and login"); buttonLogin.Enabled = true; }); }
void FillUserFromDataBase() { using (ExamVceDB users = new ExamVceDB()) { foreach (var item in users.Students) { ListViewItem list = new ListViewItem(item.ID.ToString()); list.SubItems.Add(item.Login); list.SubItems.Add(item.Name); list.SubItems.Add(item.Specialisation); listViewUsers.Items.Add(list); } } }
void SendExam(User client) { using (ExamVceDB test = new ExamVceDB()) { try { if (idChooseExam < 0) { new Exception(); } SendAnswer(GetTestForSendStudent(idChooseExam), client, MessageType.Test); } catch { SendAnswer("error xml", client, MessageType.Error); } } }
//приховування відповідей private string GetTestForSendStudent(int idTest) { using (ExamVceDB test = new ExamVceDB()) { try { var result = test.TestDBs.FirstOrDefault(i => i.ID == idTest); var serializer = new XmlSerializer(typeof(MakeTest)); MakeTest desr; using (TextReader reader = new StringReader(result.Test)) { desr = (MakeTest)serializer.Deserialize(reader); } //var jsonMakeTest = JsonConvert.SerializeObject(desr); //MakeTest testFromJson = JsonConvert.DeserializeObject<MakeTest>(jsonMakeTest); foreach (var item in desr.questions) { foreach (var a in item.answers) { if (a.IsCorrect) { a.IsCorrect = !a.IsCorrect; } } } //var readyResult = JsonConvert.SerializeObject(testFromJson); XmlSerializer xmlSerializer = new XmlSerializer(typeof(MakeTest)); using (StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, desr); return(textWriter.ToString()); } }catch { MessageBox.Show("Test not foudn"); } return(""); } }
private void button1_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textBoxName.Text) && !string.IsNullOrEmpty(textBoxLogin.Text) && !string.IsNullOrEmpty(textBoxPassword.Text) && !string.IsNullOrEmpty(textBoxCourse.Text) && !string.IsNullOrEmpty(textBoxGroup.Text) && !string.IsNullOrEmpty(textBoxSpecialisation.Text)) { string password; using (MD5 md5Hash = MD5.Create()) { password = GetMd5Hash(md5Hash, textBoxPassword.Text); } using (ExamVceDB student = new ExamVceDB()) { try { student.Students.Add(new Student() { Name = textBoxName.Text, Course = textBoxCourse.Text, Password = password, Login = textBoxLogin.Text, Specialisation = textBoxSpecialisation.Text, GroupName = textBoxGroup.Text }); student.SaveChanges(); MessageBox.Show("Student was added"); textBoxCourse.Clear(); textBoxGroup.Clear(); textBoxLogin.Clear(); textBoxName.Clear(); textBoxPassword.Clear(); textBoxSpecialisation.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } else { MessageBox.Show("Заповніть всі поля"); } }
//вибір типу отриманого повідомлення void FunctionForSwich(TransferInfo info, string recive, User client) { switch (info.Type) { case MessageType.CheckLogIn: { User reciveUser = JsonConvert.DeserializeObject <User>(recive); string passRecive; using (MD5 md5Hash = MD5.Create()) { passRecive = GetMd5Hash(md5Hash, reciveUser.Password); } using (ExamVceDB userLogin = new ExamVceDB()) { var student = userLogin.Students.Where(i => i.Login == reciveUser.Login).FirstOrDefault(); char[] login = student.Login.ToCharArray(); var password = student.Password.ToCharArray(); int endPosLogin = 0; int endPosPass = 0; while (true) { if (login[endPosLogin] == ' ' || endPosLogin > student.Login.Length - 1) { break; } endPosLogin++; } while (true) { if (password[endPosPass] == ' ' || endPosPass > student.Login.Length - 1) { break; } endPosPass++; } string log = new string(login, 0, endPosLogin); string pass = new string(password, 0, endPosPass); bool isCorrectMD5 = false; if (reciveUser.Login.Equals(log) && passRecive.Equals(pass)) { client.ID = student.ID; SendAnswer(student.ID.ToString(), client, MessageType.GetAnswer); return; } ; SendAnswer("not correct", client, MessageType.GetAnswer); } break; } case MessageType.Test: { using (ExamVceDB test = new ExamVceDB()) { try { if (idChooseExam < 0) { new Exception(); } SendAnswer(GetTestForSendStudent(idChooseExam), client, MessageType.Test); } catch { SendAnswer("error xml", client, MessageType.Error); } } break; } case MessageType.CheckTestFromStudent: { MakeTest testFromUser = DeserialiseFromString(recive); int mark = 0; bool isWriteToDB = true; using (ExamVceDB test = new ExamVceDB()) { try { var result = test.TestDBs.FirstOrDefault(i => i.ID == testFromUser.IdTest); MakeTest correctTest = DeserialiseFromString(result.Test); for (int i = 0; i < correctTest.questions.Count; i++) { for (int j = 0; j < correctTest.questions[i].answers.Count; j++) { if (correctTest.questions[i].answers[j].IsCorrect) { if (testFromUser.questions[i].answers[j].IsCorrect) { mark += correctTest.questions[i].Weight; } } } } //MessageBox.Show(mark.ToString()); } catch { isWriteToDB = false; MessageBox.Show("Test not foudn"); } } if (isWriteToDB) { try { using (ExamVceDB passExam = new ExamVceDB()) { passExam.PassExams.Add(new PassExam() { Id_Sender = loginPerson.ID, Id_Student = client.ID, Id_Test = testFromUser.IdTest, Mark = mark, Date_Pass = DateTime.Now }); passExam.SaveChanges(); MessageBox.Show("Test pass writed in DB"); } }catch (Exception ex) { MessageBox.Show(ex.Message); } } SendAnswer($"Your mark is: {mark}", client, MessageType.SendMark); break; } } }