public IActionResult AddQuestionsToDb(Questions newQuestion) { if (ModelState.IsValid) { _context.Questions.Add(newQuestion); _context.SaveChanges(); } return(RedirectToAction("AddQuestionsToDb")); }
public IActionResult AssignARole(string FirstName, string LastName, string role, string password, int classId) { var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; if (password == "1234" && role == "Teacher") { //1-creating a new teacher object //2- get Fname and Lname from the userform //3-saves the new teacher to SQL Teachers teacher = new Teachers(); teacher.FirstName = FirstName; teacher.LastName = LastName; teacher.UserId = userId; _context.Teachers.Add(teacher); _context.SaveChanges(); return(RedirectToAction("AssignAsTeacher")); } else if (password == "5678" && role == "Admin") { return(RedirectToAction("AssignAsAdmin")); } else if (password == null || password == "" && role == "Student") { //1-creating a new student object //2- get Fname and Lname and ClassroomId from the userform //3-saves the new student to SQL Students student = new Students(); student.FirstName = FirstName; student.LastName = LastName; student.UserId = userId; student.ClassroomId = classId; _context.Students.Add(student); _context.SaveChanges(); return(RedirectToAction("AssignAsStudent")); } else { string message = "Please enter valid information."; return(RedirectToAction("AssignARole", new { msg = message })); } }
public async Task <IActionResult> ResultsPlanetsQuiz(double g, List <string> answered, string engname) { //find logged in userid string id = User.FindFirst(ClaimTypes.NameIdentifier).Value; //create a list of students where student id matches logged in person id List <Students> students = _context.Students.Where(x => x.UserId == id).ToList(); List <Questions> questions = _context.Questions.Where(x => x.QuizId == 2).ToList(); List <Body> planetsList = await CreatePlanetsListFromAPIAsync(); Body testedPlanet = new Body(); //Retrieve the 4 planets in multipe choice for (int i = 0; i < planetsList.Count; i++) { if (planetsList[i].englishName == engname) { testedPlanet = planetsList[i]; } } int numberOfMoons = 0; if (testedPlanet.moons == null) { numberOfMoons = 0; } if (testedPlanet.moons != null) { numberOfMoons = testedPlanet.moons.Length; } if (testedPlanet.discoveredBy.Length < 1 || testedPlanet.discoveredBy == null) { testedPlanet.discoveredBy = "Unknown"; } if (testedPlanet.discoveryDate.Length < 1 || testedPlanet.discoveryDate == null) { testedPlanet.discoveryDate = "Unknown"; } List <string> correctAnswers = new List <string>() { testedPlanet.mass.massValue.ToString() + "^" + testedPlanet.mass.massExponent.ToString(), testedPlanet.vol.volValue.ToString() + "^" + testedPlanet.vol.volExponent.ToString(), numberOfMoons.ToString(), testedPlanet.discoveredBy.ToString(), testedPlanet.discoveryDate.ToString(), testedPlanet.gravity.ToString() }; int studentId = students[0].Id; //planets quiz int quizId = 2; Grades newGrade = new Grades(); Math.Round(g); newGrade.StudentId = studentId; newGrade.QuizId = quizId; newGrade.Grade = g; _context.Grades.Add(newGrade); _context.SaveChanges(); ViewBag.studentname = students[0].FirstName.ToString() + students[0].LastName.ToString(); ViewBag.grade = g; ViewBag.questions = questions; ViewBag.correctanswers = correctAnswers; ViewBag.englishname = engname; SaveAverageGrade(); return(View(answered)); }