public async Task <IActionResult> Details(int id) { var user = await _userManager.FindByIdAsync(User.GetUserId()); var thisQuiz = _db.Quizzes.Include(quizzes => quizzes.Questions).FirstOrDefault(quizzes => quizzes.QuizId == id); var questions = thisQuiz.Questions.ToList(); bool exists = _db.CompletedQuizzes.Any(row => row.UserId == User.GetUserId() && row.QuizId == id); if (exists == false) { CompletedQuiz newCompletedQuiz = new CompletedQuiz(id, user.Id); _db.CompletedQuizzes.Add(newCompletedQuiz); _db.SaveChanges(); Console.WriteLine("it thinks it does not exist yet"); return(View(questions)); } else { var startedQuiz = _db.CompletedQuizzes.Include(quizzes => quizzes.CompletedQuestions).Include(quizzes => quizzes.Quiz).FirstOrDefault(quizzes => quizzes.UserId == User.GetUserId()); if (startedQuiz.InProgress == true) { Console.WriteLine("it thinks it exists but is in progress"); return(View(questions)); } else { Console.WriteLine("it thinks it exists and is not in progress"); return(View("Score", startedQuiz)); } } }
public void SaveComplitedQuiz(CompletedQuiz _quiz) { _quiz.MultipleAnswers = _quiz.MultipleAnswers.Where(q => q.Answer != null).ToArray(); _quiz.QuizTextboxes = _quiz.QuizTextboxes?.Where(q => q.Answer != null).ToArray(); context.CompletedQuizzes.Add(_quiz); context.SaveChanges(); }
public event Action <StartQuizForm, StudentLogin, StoredQuizzes, List <StoredQuestions>, List <StoredQuizQuestions>, List <CompletedQuestion> > CompletedQuiz; //This event is used to return the values of the completed quiz to the stats form when this form is closed public StartQuizForm(StudentLogin student, StoredQuizzes sQuiz, List <StoredQuestions> sQuestions, List <StoredQuizQuestions> storedQQuestions, CompletedQuiz cQuiz, List <CompletedQuestion> compQuestion) { InitializeComponent(); //Parameters are assigned to the global variables Student = student; SQuiz = sQuiz; storedQuestions = sQuestions; storedQuizQuestions = storedQQuestions; completedQuiz = cQuiz; completedQuestion = compQuestion; }
public ActionResult GetResult(CompletedQuiz _quiz) { if (!ModelState.IsValid) { ModelState.AddModelError("", "You need to answer all of questions"); QuizConstructor.ResetIndexator(); return(View("Index", repository.GetQuizById(_quiz.QuizId))); } repository.SaveComplitedQuiz(_quiz); return(View("Statistic", GetStatistic(_quiz.QuizId))); }
public async Task <IActionResult> Score(int id) { var userId = User.GetUserId(); var user = await _userManager.FindByIdAsync(User.GetUserId()); var thisCompletedQuiz = _db.CompletedQuizzes.Include(questions => questions.CompletedQuestions).FirstOrDefault(quiz => quiz.QuizId == id && quiz.UserId == userId); thisCompletedQuiz.InProgress = false; thisCompletedQuiz.Score = CompletedQuiz.ScoreQuiz(thisCompletedQuiz); user.Points = user.Points + (thisCompletedQuiz.Score) / 10; _db.SaveChanges(); return(View(thisCompletedQuiz)); }
private void ReturnButton_Click(object sender, EventArgs e) { //The scores are returned to the previous form in order to be refreshed in the scores table CompletedQuiz?.Invoke(this, Student, SQuiz, storedQuestions, storedQuizQuestions, completedQuestion); this.Close(); }
public ActionResult NewQuizStart(QuizTestViewModel quiz) { // Create a list what is used for counting the total score per User per Quiz List <Question> questionList = db.Question.Include(q => q.Answer).AsEnumerable().Where(x => x.QuizId == quiz.Question[0].QuizId).ToList(); var quizId = quiz.Question.FirstOrDefault().QuizId; var quizName = db.Quiz.Where(x => x.Id == quizId).Select(x => x.Name).FirstOrDefault(); var userId = quiz.Question.FirstOrDefault().UserId; var userNickName = db.User.Where(x => x.Id == userId).Select(x => x.NickName).FirstOrDefault(); // Create unique model instance CompletedQuiz completedQuiz = new CompletedQuiz(); // Create database list to save User's answers var dbList = db.FinishedQuizByUser.ToList(); int totalScore = 0; for (int i = 0; i < quiz.Question.Count; i++) { completedQuiz = new CompletedQuiz { UserId = userId, UserNickName = userNickName, QuizId = quizId, QuizName = quizName, QuestionId = quiz.Question[i].QuestionId, Date = quiz.Question[i].Date, }; if (quiz.Question[i].QuestionType == 1) { if (quiz.Question[i].SelectedAnswer == questionList[i].Answer.Where(x => x.AnswerCheck == true).FirstOrDefault().Id) { totalScore += questionList[i].Point; completedQuiz.Score = questionList[i].Point; } } if (quiz.Question[i].QuestionType == 2) { int trueAnswerCounterInDatabase = 0; int correctAnswerCounterFromUser = 0; foreach (var item in questionList[i].Answer) { if (item.AnswerCheck == true) { trueAnswerCounterInDatabase++; } } for (int j = 0; j < quiz.Question[i].AnswerList.Count; j++) { if (quiz.Question[i].AnswerList[j].AnswerCheck == questionList[i].Answer.ElementAt(j).AnswerCheck&& questionList[i].Answer.ElementAt(j).AnswerCheck == true) { correctAnswerCounterFromUser++; if (correctAnswerCounterFromUser == trueAnswerCounterInDatabase) { completedQuiz.Score = questionList[i].Point; totalScore += questionList[i].Point; break; } } } } FinishedQuizByUser result = new FinishedQuizByUser(); result.UserId = completedQuiz.UserId; result.QuizId = completedQuiz.QuizId; result.QuestionId = completedQuiz.QuestionId; result.Score = completedQuiz.Score; result.Date = completedQuiz.Date; var finishedQuiz = db.FinishedQuizByUser?.Where(q => q.QuestionId == completedQuiz.QuestionId && q.UserId == completedQuiz.UserId)?.FirstOrDefault(); if (finishedQuiz == null) { db.FinishedQuizByUser.Add(result); db.SaveChanges(); } else { finishedQuiz.QuestionId = completedQuiz.QuestionId; finishedQuiz.Score = completedQuiz.Score; finishedQuiz.Date = completedQuiz.Date; db.Entry(finishedQuiz).State = EntityState.Modified; db.SaveChanges(); } } completedQuiz.TotalScore = totalScore; return(View("FinishedQuizView", completedQuiz)); }
private void setup(StudentLogin student, StoredQuizzes SQuiz, List <StoredQuestions> storedQuestions, List <StoredQuizQuestions> storedQuizQuestions, List <CompletedQuestion> cquestion) { //In a seperate sub as the page may need to be refreshed when the student has answered the quiz again if (cquestion == null) { StudentInputNameLabel.Text = ($"{student.FirstName} {student.SecondName}"); InputClassIdLabel.Text = student.ClassId.ToString(); QuestionClass qc = new QuestionClass(); //If the CompletedQuiz has not be loaded from the data base it will load it here if (CQuiz == null) { CQuiz.Id = SQuiz.QuizId; CQuiz.StudentId = student.StudentId; CQuiz.Length = SQuiz.Length; CQuiz = qc.CreateCompletedQuiz(CQuiz); } completedQuestion = qc.GetCompletedQuestion(CQuiz, storedQuizQuestions); } else { //If the questions have already been answered and the user wishes to return to their stats then the page needs to be refreshed but we don`t need to //query the database again listView1.Items.Clear(); listView1.Refresh(); completedQuestion = cquestion; } foreach (StoredQuestions sq in storedQuestions) { //Assigns the relevant stats the the rows for each question on the table ListViewItem b = new ListViewItem(sq.Question); if (sq.Area == 1) { b.SubItems.Add("Recall"); } else { b.SubItems.Add("Calculations"); } if (sq.TopicId == 1) { b.SubItems.Add("Particles"); } else if (sq.TopicId == 2) { b.SubItems.Add("Waves"); } else if (sq.TopicId == 3) { b.SubItems.Add("Mechanics"); } else if (sq.TopicId == 4) { b.SubItems.Add("Materials"); } else if (sq.TopicId == 5) { b.SubItems.Add("Electricity"); } //For each question answered in completed questions, the loop checks to see if it is equal to the current stored question ID. foreach (CompletedQuestion cq in completedQuestion) { if (cq.QuestionId == sq.QuestionId) { //If the question is the same then the code adds the question`s stats to the table containing their scores (times answered, times correct, difficulty score, etc) b.SubItems.Add(cq.XCompleted.ToString()); b.SubItems.Add(cq.XCorrect.ToString()); string score = DifficultyScore(cq.CalculatedDifficulty); b.SubItems.Add(score); b.SubItems.Add(cq.CalculatedDifficulty.ToString()); break; //braks the loop so that there is no more wasted loops } } listView1.Items.Add(b); } Student = student; ListOfStoredQuestions = storedQuestions; SelectedQuiz = SQuiz; QuizQuestionsId = storedQuizQuestions; //saves varaibles to the program so that if the user starts a quiz then they don`t need to be retrived and are ready to be called }