public async Task <IActionResult> Edit(int id, [Bind("StudentQuestionId,StudentResponseId,StudentId,QuestionNumber,Question,QuestionHint,QuestionTypeName")] StudentQuestion studentQuestion) { if (id != studentQuestion.StudentQuestionId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(studentQuestion); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentQuestionExists(studentQuestion.StudentQuestionId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["StudentResponseId"] = new SelectList(_context.StudentResponses, "StudentResponseId", "StudentResponseId", studentQuestion.StudentResponseId); return(View(studentQuestion)); }
public async Task <IActionResult> Create([Bind("StudentQuestionId,StudentResponseId,StudentId,QuestionNumber,Question,QuestionHint,QuestionTypeName")] StudentQuestion studentQuestion) { if (ModelState.IsValid) { _context.Add(studentQuestion); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["StudentResponseId"] = new SelectList(_context.StudentResponses, "StudentResponseId", "StudentResponseId", studentQuestion.StudentResponseId); return(View(studentQuestion)); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } StudentQuestion = await _context.StudentQuestions.FindAsync(id); if (StudentQuestion != null) { _context.StudentQuestions.Remove(StudentQuestion); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } StudentQuestion = await _context.StudentQuestions .Include(s => s.Question) .Include(s => s.Simulation).FirstOrDefaultAsync(m => m.SimulationID == id); if (StudentQuestion == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } StudentQuestion = await _context.StudentQuestions .Include(s => s.Question) .Include(s => s.Simulation).FirstOrDefaultAsync(m => m.SimulationID == id); if (StudentQuestion == null) { return(NotFound()); } ViewData["SimulationID"] = new SelectList(_context.Questions, "SimulationID", "AnswerString"); ViewData["SimulationID"] = new SelectList(_context.Simulations, "SimulationID", "Name"); return(Page()); }
// GET: Subjects/Edit/5 public async Task <IActionResult> TakeExam(int id) { var rule = _context.QuestionRules.FirstOrDefault(x => x.SubjectId.Equals(id)); var questions = _context.Questions.Include(i => i.QuestionOptions).Where(x => x.SubjectId.Equals(id)); var studentResponse = new StudentResponse { StudentId = 1, TotalQuestion = rule.TotalQuestion, DateTaken = DateTime.Now, }; _context.Add(studentResponse); _context.SaveChanges(); // randomize based on total question in rule foreach (var question in questions.Take(rule.TotalQuestion)) { var studentQuestion = new StudentQuestion { StudentResponseId = studentResponse.StudentResponseId, QuestionNumber = question.QuestionNumber, Question = question.QuestionName, QuestionHint = question.QuestionHint, StudentId = 1, QuestionTypeName = question.QuestionType.ToString(), StudentAnswers = question.QuestionOptions.Select(s => new StudentAnswer { StudentQuestionId = s.QuestionId, AnswerValue = s.OptionValue, IsCorrect = s.IsCorrect, }).ToList() }; _context.Add(studentQuestion); } _context.SaveChanges(); return(View()); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?simID) { if (StudentAnswers == null) { return(NotFound()); } if (simID == null) { return(NotFound()); } Questions = await Context.Questions .Include(q => q.QuestionType) .Include(q => q.StudentQuestions) .Where(q => q.SimulationID == simID.Value) .ToListAsync(); int score = 0; for (int i = 0; i < StudentAnswers.Length; i++) { var studentQuestion = new StudentQuestion { Answer = StudentAnswers[i], UserID = UserManager.GetUserId(User), SimulationID = simID.Value, QIndex = i + 1, isCorrect = AnswerType.Incorrect }; var isAuthorized = await AuthorizationService.AuthorizeAsync(User, studentQuestion, Operations.CreateStudentAssignment); if (!isAuthorized.Succeeded) { return(Forbid()); } var question = Questions.Single(q => q.QIndex == studentQuestion.QIndex); var answerList = question.AnswerString.Split(';'); switch (question.QuestionType.Name) { case "Multiple Choice": case "TrueFalse": if (studentQuestion.Answer == answerList[0]) { score++; studentQuestion.isCorrect = AnswerType.Correct; } break; case "Calculation": float answer, lenience, studentAnswer; float?temp; if (!float.TryParse(answerList[0], out answer)) { break; } if (!float.TryParse(studentQuestion.Answer, out studentAnswer)) { break; } if (!float.TryParse(answerList[1], out lenience)) { lenience = 0f; } if (studentAnswer >= answer - lenience && studentAnswer <= answer + lenience) { score++; studentQuestion.isCorrect = AnswerType.Correct; } else if (answerList[2] != null) { temp = EvaluatePostFixExp(answerList[2]); if (temp != null) { answer = temp.Value; if (studentAnswer > answer - lenience && studentAnswer < answer + lenience) { score++; studentQuestion.isCorrect = AnswerType.ErrorCarriedForward; } } } break; } //if (studentQuestion.Answer == Questions.Single(q => q.QIndex == studentQuestion.QIndex).AnswerString.Split('/')[0]) //{ // score++; // studentQuestion.Correct = true; //} var existigSQ = Context.StudentQuestions .SingleOrDefault(m => m.UserID == UserManager.GetUserId(User) && m.SimulationID == simID && m.QIndex == i + 1); if (existigSQ != null) { Context.Remove(existigSQ); await Context.SaveChangesAsync(); } Context.StudentQuestions.Add(studentQuestion); await Context.SaveChangesAsync(); } StudentAssignment s = Context.StudentAssignments .SingleOrDefault(s => s.SimulationID == Questions[0].SimulationID && s.UserID == UserManager.GetUserId(User)); if (s == null) { s = new StudentAssignment { UserID = UserManager.GetUserId(User), SimulationID = simID.Value }; } else { Context.Remove(s); await Context.SaveChangesAsync(); } s.DateCompleted = DateTime.Now.Date; s.Percentage = (int)(100 * score / Questions.Count()); Context.StudentAssignments.Add(s); await Context.SaveChangesAsync(); return(RedirectToPage("./Details", new { simID = simID.Value })); }
public async Task OnGetAsync(string classId, int?simId, bool?showDetails) { ClassroomData = new ClassroomIndexData(); if (User.IsInRole(Constants.StudentRole)) { var userEnrollments = (from e in Context.Enrollments select e) .Where(e => e.NEAUserId == UserManager.GetUserId(User)); var classIDs = await(from e in userEnrollments select e.ClassroomID) .AsNoTracking() .ToListAsync(); ClassroomData.Classrooms = await Context.Classrooms .Include(c => c.Teacher) .Include(c => c.Enrollments) .ThenInclude(c => c.NEAUser) .ThenInclude(c => c.StudentAssignments) .Include(c => c.Enrollments) .ThenInclude(c => c.NEAUser) .ThenInclude(c => c.StudentQuestions) .Include(c => c.ClassAssignments) .ThenInclude(c => c.Simulation) .ThenInclude(c => c.Questions) .Where(c => classIDs.Contains(c.ClassroomID)) .AsNoTracking() .ToListAsync(); } else { ClassroomData.Classrooms = await Context.Classrooms .Include(c => c.Teacher) .Include(c => c.Enrollments) .ThenInclude(c => c.NEAUser) .ThenInclude(c => c.StudentAssignments) .Include(c => c.Enrollments) .ThenInclude(c => c.NEAUser) .ThenInclude(c => c.StudentQuestions) .Include(c => c.ClassAssignments) .ThenInclude(c => c.Simulation) .ThenInclude(c => c.Questions) .Where(c => c.UserID == UserManager.GetUserId(User)) .AsNoTracking() .ToListAsync(); } /*foreach (var item in classroom) * { * List<Enrollment> students = await Context.Enrollments * .Where(e => e.ClassroomID == item.ClassroomID) * .AsNoTracking() * .ToListAsync(); * * ClassroomVM.Add( * new ClassroomVM * { * ID = item.ClassroomID, * Name = item.Name, * Teacher = item.Teacher.LastName + ", " + item.Teacher.FirstName, * StudentCount = students.Count() * } * ) ; * }*/ if (classId != null) { ClassID = classId; Classroom classroom = ClassroomData.Classrooms.Where(c => c.ClassroomID == ClassID).SingleOrDefault(); if (classroom == null) { return; } ClassroomData.ClassAssignments = classroom.ClassAssignments.Select(c => c); } if (simId != null) { SimulatorID = simId.Value; ClassAssignment assignment = ClassroomData.ClassAssignments.Where(c => c.SimulationID == SimulatorID).SingleOrDefault(); if (assignment == null) { return; } if (User.IsInRole(Constants.StudentRole)) { NEAUser thisuser = ClassroomData.Classrooms .Single(c => c.ClassroomID == ClassID) .Enrollments .Single(e => e.NEAUserId == UserManager.GetUserId(User)) .NEAUser; StudentAssignment?studentAssignment = thisuser .StudentAssignments .SingleOrDefault(s => s.SimulationID == SimulatorID); ClassroomData.StudentAssignments = new List <StudentAssignmentIndexVM> { new StudentAssignmentIndexVM { StudentName = thisuser.LastName + ", " + thisuser.FirstName, Percentage = studentAssignment?.Percentage, DateCompleted = studentAssignment?.DateCompleted, SimulationID = SimulatorID } }; } else { var enrollments = ClassroomData.Classrooms .Single(c => c.ClassroomID == ClassID) .Enrollments; var students = from e in enrollments select e.NEAUser; var studentAssignments = new List <StudentAssignmentIndexVM>(); foreach (var item in students) { var studentAssignment = item.StudentAssignments.SingleOrDefault(s => s.SimulationID == SimulatorID); studentAssignments.Add(new StudentAssignmentIndexVM() { StudentName = item.LastName + ", " + item.FirstName, Percentage = studentAssignment?.Percentage, DateCompleted = studentAssignment?.DateCompleted, SimulationID = SimulatorID }); } ClassroomData.StudentAssignments = studentAssignments; } if (showDetails != null && showDetails == true) { ShowDetails = showDetails.Value; ClassroomData.Questions = await Context.Questions .Where(s => s.SimulationID == SimulatorID) .AsNoTracking() .ToListAsync(); List <NEAUser> students = await(from e in Context.Enrollments where e.ClassroomID == ClassID select e.NEAUser) .AsNoTracking() .ToListAsync(); List <StudentQuestion> studentQuestions = await Context.StudentQuestions .Include(s => s.NEAUser) .ThenInclude(s => s.Enrollments) .Where(s => s.SimulationID == SimulatorID) .AsNoTracking() .ToListAsync(); var studentQuestionsVM = new List <StudentQuestionVM>(); foreach (var student in students) { var thisStudentsQuestions = studentQuestions.Where(s => s.UserID == student.Id); foreach (var question in ClassroomData.Questions) { var studentQuestion = thisStudentsQuestions.SingleOrDefault(s => s.QIndex == question.QIndex); if (studentQuestion == null) { studentQuestion = new StudentQuestion { QIndex = question.QIndex, SimulationID = SimulatorID, Answer = "", isCorrect = AnswerType.Unanswered }; } var isAuthorized = await AuthorizationService.AuthorizeAsync(User, studentQuestion, Operations.ViewStudentAssignment); if (!isAuthorized.Succeeded) { var enrollment = Context.Enrollments .Include(e => e.Classroom) .ThenInclude(e => e.Teacher) .AsNoTracking() .SingleOrDefault(e => e.NEAUserId == student.Id && e.ClassroomID == ClassID); isAuthorized = await AuthorizationService.AuthorizeAsync(User, enrollment, Operations.ViewStudentAssignment); if (!isAuthorized.Succeeded) { continue; } } studentQuestionsVM.Add( new StudentQuestionVM { UserId = student.Id, QIndex = studentQuestion.QIndex, StudentName = student.FirstName + " " + student.LastName, Answer = studentQuestion.Answer, isCorrect = studentQuestion.isCorrect, SimID = SimulatorID }); } } ClassroomData.StudentQuestions = studentQuestionsVM; } } }
public async Task <IActionResult> OnPostOverride(string classid) { //if(String.IsNullOrEmpty(id) || qindex == null || simid == null|| classid == null) StudentQuestion studentQuestion = Context.StudentQuestions .SingleOrDefault(s => s.UserID == StudentQuestion.UserID && s.QIndex == StudentQuestion.QIndex && s.SimulationID == StudentQuestion.SimulationID); if (studentQuestion == null || studentQuestion.isCorrect == AnswerType.Unanswered) { return(RedirectToPage("./index", new { classId = classid, simId = studentQuestion.SimulationID, showDetails = true })); } // var id = stuquest.AuthorizeAccessToID(Context.Users.Single(u => u.Id == UserManager.GetUserId(User)), Context, classid); /*if (String.IsNullOrEmpty(id)) * { * return Page(); * }*/ /*StudentQuestion studentQuestion = Context.StudentQuestions * .Where(s => s.UserID == id) * .Where(s => s.QIndex == qindex) * .SingleOrDefault(s => s.SimulationID == simid); * * if (studentQuestion == null) * { * return RedirectToPage("./index"); * }*/ var enrollment = Context.Enrollments .Include(e => e.Classroom) .SingleOrDefault(e => e.NEAUserId == studentQuestion.UserID && e.ClassroomID == classid); if (enrollment == null) { return(RedirectToPage("./index", new { classId = classid, simId = studentQuestion.SimulationID, showDetails = true })); } var isAuthorized = await AuthorizationService.AuthorizeAsync(User, enrollment, Operations.OverrideStudentAssignment); if (!isAuthorized.Succeeded) { return(Forbid()); } Context.StudentQuestions.Remove(studentQuestion); Context.SaveChanges(); if (studentQuestion.isCorrect == AnswerType.Incorrect || studentQuestion.isCorrect == AnswerType.OverriddenIncorrect) { studentQuestion.isCorrect = AnswerType.OverriddenCorrect; } else { studentQuestion.isCorrect = AnswerType.OverriddenIncorrect; } Context.StudentQuestions.Add(studentQuestion); Context.SaveChanges(); return(RedirectToPage("./index", new { classId = classid, simId = studentQuestion.SimulationID, showDetails = true })); }
public async Task <IActionResult> StartExam(int?examId) { if (examId == null) { return(NotFound()); } var exam = await _context.Exams.Where(m => m.Id == examId) .Include(m => m.Questions) .Include(m => m.ClassRoom) .Include(m => m.ClassRoom.ClassRoomStudents) .FirstOrDefaultAsync(); if (exam == null) { return(NotFound()); } else { foreach (var student in exam.ClassRoom.ClassRoomStudents) { var shuffleQuestions = exam.Questions.OrderBy(a => Guid.NewGuid()).ToList(); if (exam.HasSets) { var firstNQuestions = shuffleQuestions.Take((int)exam.TotalQuestions); foreach (var question in firstNQuestions) { StudentQuestion studentQuestion = new StudentQuestion(); studentQuestion.StudentId = student.StudentId; studentQuestion.QuestionId = question.Id; if (question.QuestionTypeId == 2) { Random rnd = new Random(); List <int> temp = new List <int>(); while (temp.Count != 4) { int tempVar = rnd.Next(1, 5); if (temp.Contains(tempVar)) { continue; } else { temp.Add(tempVar); } } studentQuestion.First = temp[0]; studentQuestion.Second = temp[1]; studentQuestion.Third = temp[2]; studentQuestion.Fourth = temp[3]; } _context.Add(studentQuestion); await _context.SaveChangesAsync(); } } else { foreach (var question in shuffleQuestions) { StudentQuestion studentQuestion = new StudentQuestion(); studentQuestion.StudentId = student.StudentId; studentQuestion.QuestionId = question.Id; if (question.QuestionTypeId == 2) { Random rnd = new Random(); List <int> temp = new List <int>(); while (temp.Count != 4) { int tempVar = rnd.Next(1, 5); if (temp.Contains(tempVar)) { continue; } else { temp.Add(tempVar); } } studentQuestion.First = temp[0]; studentQuestion.Second = temp[1]; studentQuestion.Third = temp[2]; studentQuestion.Fourth = temp[3]; } _context.Add(studentQuestion); await _context.SaveChangesAsync(); } } } if (exam.HasSets) { double tempTotalMark = (double)(exam.TotalQuestions * exam.EachQuestionMark); foreach (var question in exam.Questions) { question.ActualMark = (question.Mark * exam.TotalMark) / tempTotalMark; _context.Update(question); await _context.SaveChangesAsync(); } } else { double tempTotalMark = (double)exam.Questions.Sum(m => m.Mark); foreach (var question in exam.Questions) { question.ActualMark = (question.Mark * exam.TotalMark) / tempTotalMark; _context.Update(question); await _context.SaveChangesAsync(); } } exam.Status = 1; _context.Update(exam); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { classRoomId = exam.ClassRoomId })); } }