//functies om quizzen of rondes uit db te krijgen private Quiz selectQuiz(String id) { GameModel gm = new GameModel(); Quiz quiz = new Quiz(); //kijk of de quiz gegeven is, anders gaan we de status aanpassen naar "no quiz given" int quizId; String status; try { quizId = int.Parse(id); try { quiz.Quiz_Id = quizId; quiz = gm.getQuiz(quiz); status = "success"; } catch (Exception e) { Console.WriteLine(e); status = "no quiz given"; } } catch (Exception e) { //als we hier terechtkomen is de id een textuele string (bv. "abc") try { quiz.code = id; quiz = gm.getQuizByCode(quiz); status = "success"; } catch (Exception ex) { Console.WriteLine(ex); //probeer op naam te zoeken (kleine kans, maar vergroot UX) try { quiz.name = id; quiz = gm.getQuizByName(quiz); status = "success"; } catch (Exception exc) { Console.WriteLine(exc); status = "error"; } } } if (status != "success") { return new Quiz(); } else { return quiz; } }
public ActionResult Results(String id) { // checken of de gebruiker al is ingelogd. Indien niet -> /login -> if (Session["userId"] == null) { //gebruiker is niet ingelogd: doorverwijzen naar account/login Response.Redirect("~/account/login"); } GameModel gm = new GameModel(); List<CompleteResult> results = new List<CompleteResult>(); //kijk of de quiz gegeven is, anders gaan we de status aanpassen naar "no quiz given" User u = new User(); u.User_Id = (int)Session["userId"]; try { int quizId = int.Parse(id); try { //haal resultaten op Quiz q = new Quiz(); q.Quiz_Id = quizId; List<Result> res = gm.getResultsByUserAndQuiz(u, q); foreach (Result r in res) { results.Add(new CompleteResult(r)); } ViewBag.status = "success"; } catch (Exception e) { Console.WriteLine(e); ViewBag.status = "no quiz given"; } } catch (Exception e) { //als we hier terechtkomen is de id een textuele string (bv. "abc") try { //haal resultaten op Quiz q = new Quiz(); q.code = id; q = gm.getQuizByCode(q); List<Result> res = gm.getResultsByUserAndQuiz(u, q); foreach (Result r in res) { results.Add(new CompleteResult(r)); } ViewBag.status = "success"; } catch (Exception ex) { Console.WriteLine(ex); //probeer op naam te zoeken (kleine kans, maar vergroot UX) try { //haal resultaten op Quiz q = new Quiz(); q.name = id; q = gm.getQuizByName(q); List<Result> res = gm.getResultsByUserAndQuiz(u, q); foreach (Result r in res) { results.Add(new CompleteResult(r)); } ViewBag.status = "success"; } catch (Exception exc) { Console.WriteLine(exc); ViewBag.status = "no quiz given"; } } } //viewbag data: //the number of questions int questions = results.Count(); ViewBag.questions = questions; //count the right answers int rightAnswers = 0; foreach (CompleteResult cr in results) { if (cr.answer.Answer_value == 1) rightAnswers++; } ViewBag.rightAnswers = rightAnswers; ViewBag.percentage = Math.Round(((double)rightAnswers / questions) * 100); return View(results); }
public ActionResult Quiz(String id) { GameModel gm = new GameModel(); Quiz quiz = new Quiz(); //kijk of de quiz gegeven is, anders gaan we de status aanpassen naar "no quiz given" int quizId; try { quizId = int.Parse(id); try { quiz.Quiz_Id = quizId; quiz = gm.getQuiz(quiz); ViewBag.status = "success"; ViewBag.quizName = quiz.name; ViewBag.quizId = quiz.Quiz_Id; } catch (Exception e) { Console.WriteLine(e); ViewBag.status = "no quiz given"; ViewBag.quizName = "No Quiz found..."; } } catch (Exception e) { //als we hier terechtkomen is de id een textuele string (bv. "abc") try { quiz.code = id; quiz = gm.getQuizByCode(quiz); ViewBag.status = "success"; ViewBag.quizName = quiz.name; ViewBag.quizId = quiz.Quiz_Id; } catch (Exception ex) { Console.WriteLine(ex); //probeer op naam te zoeken (kleine kans, maar vergroot UX) try { quiz.name = id; quiz = gm.getQuizByName(quiz); ViewBag.status = "success"; ViewBag.quizName = quiz.name; ViewBag.quizId = quiz.Quiz_Id; } catch (Exception exc) { Console.WriteLine(exc); ViewBag.status = "no quiz given"; ViewBag.quizName = "No Quiz found..."; } } } //get the results List<CompleteResult> results = new List<CompleteResult>(); try { List<Result> resultsByQuiz = gm.getAllResultsByQuiz(quiz); foreach (Result r in resultsByQuiz) { results.Add(new CompleteResult(r)); } ViewBag.status = "success"; } catch (Exception e) { ViewBag.status = "error"; } List<Leader> leaders = new List<Leader>(); //select all unique players List<int> users = results.Select(r => r.user.User_Id).Distinct().ToList<int>(); for (int i = 0; i < users.Count(); i++) { //filter results by current user List<CompleteResult> crByUser = new List<CompleteResult>(); UserModel um = new UserModel(); User u = um.getUser(users[i]); crByUser = results.Where(r => r.user.User_Id == u.User_Id).ToList<CompleteResult>(); //calculate the values int rightAnswers = 0; int questions = crByUser.Select(r => r.question.Question_Id).Distinct().Count(); foreach (CompleteResult cr in crByUser) { if (cr.answer.Answer_value == 1) rightAnswers++; } int score = (int)Math.Round(((double)rightAnswers / questions) * 100); //create the leader Leader l = new Leader(); l.score = score; l.goodAnswers = rightAnswers; l.questions = questions; l.user = u; l.quiz = quiz; //add the leader to list leaders.Add(l); } leaders = leaders.OrderByDescending(l => l.score).ToList<Leader>(); return View(leaders); }
public ActionResult Search(String quizCode) { GameModel gm = new GameModel(); Quiz result = new Quiz(); result.name = quizCode; result = gm.getQuizByName(result); if (result != null) { ViewBag.foundQuiz = result.name; ViewBag.quizUrl = result.code; } else { //we konden de quiz niet op de naam vinden, probeer het op code result = new Quiz(); result.code = quizCode; result = gm.getQuizByCode(result); if (result != null) { ViewBag.foundQuiz = result.name; ViewBag.quizUrl = result.code; } else { ViewBag.foundQuiz = "Die quiz hebben we niet gevonden :("; ViewBag.quizUrl = "#"; } } return View(); }