public ActionResult ConfirmGrades() { if (!IsAuthenticated()) { return(RedirectToAction("Index", "Main")); } if (Request.Form.AllKeys.Length == 0) { return(RedirectToAction("SelectStudent", "Student")); } List <SelectStudentModel> selectStudentModel = (List <SelectStudentModel>)Session["SelectStudentModel"]; Student selectedStudent = (Student)Session["SelectedStudent"]; var form = Request.Form; List <int> grades = new List <int>(); int previousSubjectQuestionID = -1; for (int i = 0; i < form.AllKeys.Length; i++) { string key = form.AllKeys.ElementAt(i); string[] splitkey = key.Split('-'); if (splitkey.Length == 2) //Formaat key moet *-* zijn, anders geen cijfer { int subjectQuestionID = int.Parse(splitkey[0]); if (previousSubjectQuestionID == subjectQuestionID || previousSubjectQuestionID == -1) { grades.Add(int.Parse(form[form.AllKeys[i]])); } else //add grade to db and empty grade list { AddGrade(grades, previousSubjectQuestionID, selectStudentModel.First().Week, selectStudentModel.First().Project.Id, selectedStudent.Studentnr); grades = new List <int>(); grades.Add(int.Parse(form[form.AllKeys[i]])); } previousSubjectQuestionID = subjectQuestionID; } } if (grades.Count != 0) { AddGrade(grades, previousSubjectQuestionID, selectStudentModel.First().Week, selectStudentModel.First().Project.Id, selectedStudent.Studentnr); } _db.SaveChanges(); return(View()); }
public ActionResult SubmitToDb() { string className = Request.Form["classCode"]; var gradeData = from sg in _db.StudentGrades join s in _db.Students on sg.Student.Studentnr equals s.Studentnr join spg in _db.StudentProjectGroups on s.Studentnr equals spg.Student.Studentnr where spg.ProjectGroup.ClassCode == className select new { StudentGrades = sg, Students = s }; var gData = gradeData.ToList(); foreach (var g in gData) { int data = int.Parse(Request.Form[g.Students.Studentnr.ToString()]); g.StudentGrades.TutorGrading = data; } _db.SaveChanges(); return(RedirectToAction("Index", "Main")); }
public ActionResult MakeProject(HttpPostedFileBase file) { if (!IsAuthenticated()) { return(RedirectToAction("Index", "Main")); } string path = null; List <Student> students = new List <Student>(); if (Request.Form["projectName"].Count() == 0) { return(RedirectToAction("Error", "Main", new { errorMessage = "Er is geen projectnaam ingevuld" })); } if (file != null) { var fileName = Path.GetFileName(file.FileName); path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName; file.SaveAs(path); // Read the CSV file data StreamReader sr = new StreamReader(path); string line = sr.ReadLine(); string[] value = line.Split(';'); DataTable dt = new DataTable(); DataRow row; int columnCount = 0; foreach (string dc in value) { if (dc.Equals("FirstName") || dc.Equals("LastName") || dc.Equals("StudentNr") || dc.Equals("Year") || dc.Equals("ProjectGroup")) { dt.Columns.Add(new DataColumn(dc)); columnCount++; } else { return(RedirectToAction("Error", "Main", new { errorMessage = "Één kolom header is niet correct" })); } } if (columnCount != 5) { return(RedirectToAction("Error", "Main", new { errorMessage = "Het aantal kolommen is niet correct" })); } while (!sr.EndOfStream) { value = sr.ReadLine().Split(';'); if (value.Length == dt.Columns.Count) { row = dt.NewRow(); row.ItemArray = value; dt.Rows.Add(row); } } Project currentProject = new Project(Request.Form["projectName"], null, new DateTime(2014, 1, 1), new DateTime(2014, 1, 1), null); ProjectGroup currentGroup = null; Student currentStudent = null; List <ProjectGroup> groupsToAdd = new List <ProjectGroup>(); List <Student> studentsToAdd = new List <Student>(); var myEnumerable = dt.AsEnumerable(); foreach (var item in myEnumerable) { string pgroup = item.Field <String>("ProjectGroup"); int studnr = int.Parse(item.Field <String>("StudentNr")); currentStudent = _db.Students.Find(studnr); //var groupModel = from r in _db.ProjectGroups // where r.ClassCode == pgroup // select r; //dbGroup = groupModel.FirstOrDefault(); if (currentGroup == null) { ProjectGroup newGroup = new ProjectGroup(pgroup, currentProject); groupsToAdd.Add(newGroup); currentGroup = newGroup; } else { if (!currentGroup.ClassCode.Equals(pgroup)) { ProjectGroup newGroup = new ProjectGroup(pgroup, currentProject); currentProject.ProjectGroups.Add(newGroup); groupsToAdd.Add(newGroup); currentGroup = newGroup; } } if (currentStudent == null) { Student newStudent = new Student( int.Parse(item.Field <String>("StudentNr")), item.Field <String>("FirstName"), item.Field <String>("LastName"), int.Parse(item.Field <String>("Year")), null); // mentor studentsToAdd.Add(newStudent); currentStudent = newStudent; } if (currentStudent != null && currentGroup != null) { StudentProjectGroups spg = new StudentProjectGroups(currentStudent, currentGroup); currentStudent.StudentProjectGroup.Add(spg); if (currentGroup.StudentProjectGroup == null) { currentGroup.StudentProjectGroup = new List <StudentProjectGroups>(); } currentGroup.StudentProjectGroup.Add(spg); } } _db.Projects.Add(currentProject); foreach (Student stud in studentsToAdd) { _db.Students.Add(stud); } foreach (ProjectGroup group in groupsToAdd) { _db.ProjectGroups.Add(group); } sr.Close(); file = null; System.IO.File.Delete(path); _db.SaveChanges(); return(RedirectToAction("CheckProjectGroup")); } return(RedirectToAction("Error", "Main", new { errorMessage = "Er is geen bestand geselecteerd" })); }