Quiz angefangen
This commit is contained in:
78
MultipleChoiceTrainer/Controllers/QuizController.cs
Normal file
78
MultipleChoiceTrainer/Controllers/QuizController.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
MultipleChoiceTrainer/Models/QuizViewModel.cs
Normal file
25
MultipleChoiceTrainer/Models/QuizViewModel.cs
Normal file
@@ -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<Choice> Choices { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
<p class="text-black-50">
|
<p class="text-black-50">
|
||||||
<a asp-action="edit" asp-controller="Categories" asp-route-id="@category.Id" class="text-black-50"><i class="far fa-edit"></i> Kurs bearbeiten</a>
|
<a asp-action="edit" asp-controller="Categories" asp-route-id="@category.Id" class="text-black-50"><i class="far fa-edit"></i> Kurs bearbeiten</a>
|
||||||
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-plus"></i> Lektion hinzufügen</a>
|
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-plus"></i> Lektion hinzufügen</a>
|
||||||
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-map-marker-question"></i> Quiz starten</a>
|
<a asp-action="category" asp-controller="Quiz" asp-route-id="@category.Id" class="text-black-50"><i class="far fa-map-marker-question"></i> Quiz starten</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div class="list-group">
|
<div class="list-group">
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
<p class="text-black-50">
|
<p class="text-black-50">
|
||||||
<a asp-action="edit" asp-controller="Sections" asp-route-id="@sub.Id" class="text-black-50"><i class="far fa-edit"></i> Lektion bearbeiten</a>
|
<a asp-action="edit" asp-controller="Sections" asp-route-id="@sub.Id" class="text-black-50"><i class="far fa-edit"></i> Lektion bearbeiten</a>
|
||||||
<a asp-action="create" asp-controller="Questions" asp-route-sectionId="@sub.Id" class="text-black-50"><i class="far fa-plus"></i> Frage hinzufügen</a>
|
<a asp-action="create" asp-controller="Questions" asp-route-sectionId="@sub.Id" class="text-black-50"><i class="far fa-plus"></i> Frage hinzufügen</a>
|
||||||
<a asp-action="create" asp-controller="Sections" asp-route-categoryId="@category.Id" class="text-black-50"><i class="far fa-map-marker-question"></i> Quiz starten</a>
|
<a asp-action="section" asp-controller="Quiz" asp-route-id="@sub.Id" class="text-black-50"><i class="far fa-map-marker-question"></i> Quiz starten</a>
|
||||||
</p>
|
</p>
|
||||||
</ul>
|
</ul>
|
||||||
}
|
}
|
||||||
|
|||||||
25
MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml
Normal file
25
MultipleChoiceTrainer/Views/Quiz/Quiz.cshtml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
@model MultipleChoiceTrainer.Models.QuizViewModel
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Quiz";
|
||||||
|
}
|
||||||
|
|
||||||
|
@using (Html.BeginForm("quiz", "quiz", FormMethod.Post))
|
||||||
|
{
|
||||||
|
<input asp-for="QuizType" type="hidden" />
|
||||||
|
|
||||||
|
<h4>Frage beantworten</h4>
|
||||||
|
<p class="text-black-50">aus Lektion <i>@Model.CurrentQuestion.Section.Name</i>, Kurs <i>@Model.CurrentQuestion.Section.Category.Name</i></p>
|
||||||
|
<hr />
|
||||||
|
<p class="lead">
|
||||||
|
@Model.CurrentQuestion.Text
|
||||||
|
</p>
|
||||||
|
@for(int i = 0; i < Model.Choices.Count(); i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
<div class="form-check">
|
||||||
|
<input asp-for="@Model.Choices[i].IsTrue" class="form-check-input" value="true" />
|
||||||
|
<label asp-for="@Model.Choices[i].IsTrue" class="form-check-label">@Model.Choices[i].Text</label>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user