From e57f1fb172483e8ac194c56e82829e5491b7a256 Mon Sep 17 00:00:00 2001 From: Trond Schertel Date: Wed, 10 Jun 2020 16:15:53 +0200 Subject: [PATCH] Quiz angefangen --- .../Controllers/QuizController.cs | 78 +++++++++++++++++++ MultipleChoiceTrainer/Models/QuizViewModel.cs | 25 ++++++ .../Views/Home/_homeLogInPartial.cshtml | 4 +- MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml | 25 ++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 MultipleChoiceTrainer/Controllers/QuizController.cs create mode 100644 MultipleChoiceTrainer/Models/QuizViewModel.cs create mode 100644 MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml diff --git a/MultipleChoiceTrainer/Controllers/QuizController.cs b/MultipleChoiceTrainer/Controllers/QuizController.cs new file mode 100644 index 0000000..690bec9 --- /dev/null +++ b/MultipleChoiceTrainer/Controllers/QuizController.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MultipleChoiceTrainer.Data; +using MultipleChoiceTrainer.Models; +using MultipleChoiceTrainer.Models.DataModels; + +namespace MultipleChoiceTrainer.Controllers +{ + public class QuizController : Controller + { + private readonly ApplicationDbContext _context; + + public QuizController(ApplicationDbContext context) + { + _context = context; + } + + public IActionResult Section(int id) + { + var vm = new QuizViewModel() + { + QuizType = QuizType.Section, + CurrentTypeId = id + }; + + GetQuestion(vm); + + return Quiz(vm); + } + + public IActionResult Category(int id) + { + var vm = new QuizViewModel() + { + QuizType = QuizType.Category, + CurrentTypeId = id + }; + GetQuestion(vm); + + return Quiz(vm); + } + + private void GetQuestion(QuizViewModel vm) + { + var questions = _context.Questions.Include(e => e.Section).ThenInclude(e => e.Category).Include(e => e.Choices).Include(e => e.Answers).AsQueryable(); + + switch(vm.QuizType) + { + case QuizType.Category: + questions = questions.Where(e => e.Section.CategoryId == vm.CurrentTypeId); + break; + case QuizType.Section: + questions = questions.Where(e => e.SectionId == vm.CurrentTypeId); + break; + } + + questions = questions.ToList().AsQueryable(); + questions = questions.OrderByDescending(e => e.Answers.Count()); + questions = questions.Take(10); + + var rnd = new Random(); + vm.CurrentQuestion = questions.ElementAt(rnd.Next(0, questions.Count() - 1)); + + vm.Choices = vm.CurrentQuestion.Choices.Select(oc => new Choice() { Id = oc.Id, Text = oc.Text }).ToList(); + } + + public IActionResult Quiz(QuizViewModel viewModel) + { + + + return View("quiz", viewModel); + } + } +} \ No newline at end of file diff --git a/MultipleChoiceTrainer/Models/QuizViewModel.cs b/MultipleChoiceTrainer/Models/QuizViewModel.cs new file mode 100644 index 0000000..335b46a --- /dev/null +++ b/MultipleChoiceTrainer/Models/QuizViewModel.cs @@ -0,0 +1,25 @@ +using MultipleChoiceTrainer.Models.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MultipleChoiceTrainer.Models +{ + public enum QuizType + { + Section, + Category + } + + public class QuizViewModel + { + public QuizType QuizType { get; set; } + + public int CurrentTypeId { get; set; } + + public Question CurrentQuestion { get; set; } + + public IList Choices { get; set; } + } +} diff --git a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml index c56bff2..f3bfae6 100644 --- a/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml +++ b/MultipleChoiceTrainer/Views/Home/_homeLogInPartial.cshtml @@ -20,7 +20,7 @@

Kurs bearbeiten     Lektion hinzufügen     - Quiz starten + Quiz starten

@@ -34,7 +34,7 @@

Lektion bearbeiten     Frage hinzufügen     - Quiz starten + Quiz starten

} diff --git a/MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml b/MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml new file mode 100644 index 0000000..16d61de --- /dev/null +++ b/MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml @@ -0,0 +1,25 @@ +@model MultipleChoiceTrainer.Models.QuizViewModel +@{ + ViewData["Title"] = "Quiz"; +} + +@using (Html.BeginForm("quiz", "quiz", FormMethod.Post)) +{ + + +

Frage beantworten

+

aus Lektion @Model.CurrentQuestion.Section.Name, Kurs @Model.CurrentQuestion.Section.Category.Name

+
+

+ @Model.CurrentQuestion.Text +

+ @for(int i = 0; i < Model.Choices.Count(); i++) + { + +
+ + +
+ } + +}