예제 #1
0
        public ActionResult Start(String id)
        {
            List<Round> rounds = new List<Round>();

            if (id != null)
            {
                GameModel gm = new GameModel();
                Quiz quiz = selectQuiz(id);
                rounds = gm.getAllRounds(quiz);

                // ronden in sessie zetten, zodat een gebruiker niet via javascript (jquery call) een andere quiz kan starten
                Session["roundsManaging"] = rounds;
                ViewBag.quizName = quiz.name;
                ViewBag.status = "success";
            }
            else { ViewBag.status = "error"; }

            return View(rounds);
        }
예제 #2
0
        //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;
            }
        }
예제 #3
0
        private Round selectRound(String id)
        {
            GameModel gm = new GameModel();
            Round round = new Round();
            //kijk of de quiz gegeven is, anders gaan we de status aanpassen naar "no quiz given"
            int roundId;
            String status;
            try
            {
                roundId = int.Parse(id);
                try
                {
                    round.Round_Id = roundId;
                    round = gm.getRound(round);
                    status = "success";
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    status = "no round given";
                }
            }
            catch (Exception e)
            {
                //als we hier terechtkomen is de id een textuele string (bv. "abc")
                try
                {
                    round.Round_name = id;
                    round = gm.getRoundByName(round);
                    status = "success";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    status = "no round given";
                }
            }

            if (status != "success")
            {
                return new Round();
            }
            else
            {
                return round;
            }
        }
예제 #4
0
        public ActionResult Detail(FormCollection f)
        {
            GameModel gm = new GameModel();
            Quiz quiz = new Quiz();

            // the return values
            var status = new Dictionary<string, string> { };

            try
            {
                //to check if the round really exists
                int quizId = int.Parse(f["quizId"]);
                try
                {
                    quiz.Quiz_Id = quizId;
                    quiz = gm.getQuiz(quiz);
                    status.Add("status", "success");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    status.Add("status", "error - no quiz given");
                }
            }
            catch (Exception e)
            {
                //als we hier terechtkomen is de id een textuele string (bv. "abc")
                Console.WriteLine(e);
                status.Add("status", "error - quiz not found");
            }

            //get the rounds
            List<Round> rounds = new List<Round>();
            try { rounds = gm.getAllRounds(quiz); }
            catch (Exception e) { status["status"] = "error - rounds not found"; }

            //get the results
            List<CompleteResult> results = new List<CompleteResult>();
            try
            {
                List<Result> resultsByRound = gm.getAllResultsByRound(rounds.First());
                foreach (Result r in resultsByRound)
                {
                    results.Add(new CompleteResult(r));
                }
                status["status"] = "success";
            }
            catch (Exception e)
            {
                status["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;
                l.round = rounds.First();

                //add the leader to list
                leaders.Add(l);

            }

            status.Add("data", leaders.ToString());

            return Json(status, JsonRequestBehavior.AllowGet);
        }
예제 #5
0
        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);
        }
예제 #6
0
        public ActionResult Index()
        {
            GameModel gm = new GameModel();

            //get the results
            List<CompleteResult> results = new List<CompleteResult>();
            try
            {
                List<Result> res = gm.getAllResults();
                foreach (Result r in res)
                {
                    results.Add(new CompleteResult(r));
                }
                ViewBag.status = "success";
            }
            catch (Exception e)
            {
                ViewBag.status = "error";
            }

            //create a leader per quiz

            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 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>();

                //filter results by quiz
                List<int> quizzes = results.Select(r => r.quiz.Quiz_Id).Distinct().ToList<int>();
                for (int j = 0; j < quizzes.Count(); j++)
                {
                    List<CompleteResult> crByUserQuiz = new List<CompleteResult>();
                    Quiz q = new Quiz();
                    q.Quiz_Id = quizzes[j];
                    q = gm.getQuiz(q);
                    crByUserQuiz = crByUser.Where(x => x.quiz.Quiz_Id == q.Quiz_Id).ToList<CompleteResult>();

                    //calculate the values
                    int rightAnswers = 0;
                    int questions = crByUserQuiz.Select(r => r.question.Question_Id).Distinct().Count();
                    foreach (CompleteResult cr in crByUserQuiz)
                    {
                        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 = q;

                    //add the leader to list
                    leaders.Add(l);
                }
            }

            leaders = leaders.OrderBy(l => l.quiz.Quiz_Id).ThenByDescending(l => l.score).ToList<Leader>();
            return View(leaders);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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();
        }
예제 #9
0
        public ActionResult SaveAnswer(FormCollection f)
        {
            GameModel gm = new GameModel();
            Result result = new Result();

            String status = "error";

            try
            {
                if (f["quizId"] != null)
                {
                    result.FK_Quiz = int.Parse(f["quizId"]);
                }
                else
                {
                    throw new Exception("quizId is not given");
                }
                if (f["roundId"] != null)
                {
                    result.FK_Round = int.Parse(f["roundId"]);
                }
                else
                {
                    throw new Exception("roundId is not given");
                }
                if (f["roundId"] != null)
                {
                    result.FK_Round = int.Parse(f["roundId"]);
                }
                else
                {
                    throw new Exception("roundId is not given");
                }
                if (f["questionId"] != null)
                {
                    result.FK_Question = int.Parse(f["questionId"]);
                }
                else
                {
                    throw new Exception("questionId is nog given");
                }
                if (f["answerId"] != null)
                {
                    result.FK_Answer = int.Parse(f["answerId"]);
                }
                else
                {
                    throw new Exception("answerId is nog given");
                }

                if (Session["userId"] != null)
                {
                    result.FK_User = (int)Session["userId"];
                    //result.FK_User = int.Parse((string)Session["userId"]);
                }
                else
                {
                    //gebruiker is niet ingelogd: doorverwijzen naar account/login
                    Response.Redirect("~/account/login");
                }

                int resultId = gm.InsertResult(result);
                if (resultId > 0)
                {
                    //als we een id terug krijgen, is de post gelukt :)
                    status = "success";
                }
            }
            catch (Exception e)
            {
                status = e.Message;
            }

            return Json(status, JsonRequestBehavior.AllowGet);
        }
예제 #10
0
        public ActionResult Index(String id)
        {
            // Op basis van wat er werd ingegeven achter Game/... wordt er een bepaalde quiz geselecteerd.
            // 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();
            PlayableQuiz playQuiz = new PlayableQuiz();
            //kijk of de quiz gegeven is, anders gaan we de status aanpassen naar "no quiz given"
            int quizId;
            try
            {
                quizId = int.Parse(id);
                try
                {
                    playQuiz = gm.getPlayableQuiz(quizId);
                    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
                {
                    playQuiz = gm.getPlayableQuizByCode(id);
                    ViewBag.status = "success";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    //probeer op naam te zoeken (kleine kans, maar vergroot UX)
                    try
                    {
                        playQuiz = gm.getPlayableQuizByName(id);
                        ViewBag.status = "success";
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine(exc);
                        ViewBag.status = "no quiz given";
                    }
                }
            }

            //check if the user already played the quiz
            User u = new User();
            if (Session["userId"] != null)
            {
                u.User_Id = (int)Session["userId"];
            }
            List<Result> resultsByUser = gm.getResultsByUser(u);
            foreach (Result r in resultsByUser)
            {
                if (r.FK_Quiz == playQuiz.quiz.Quiz_Id)
                {
                    playQuiz.userPlayed = true;
                }
            }

            return View(playQuiz);
        }