Example #1
0
        public static void CheckQuestion(QuestionViewModel SolvedQvm, Controller context, ApplicationUser user, eQuizContext db)
        {
            var QuestionToCheck = db.Questions.Where(q => q.QuestionId == SolvedQvm.Question.QuestionId).SingleOrDefault();
            var CorrectAnswer = QuestionToCheck.Answers.SingleOrDefault(a => a.IsCorrect);

            var QuestionUser = db.QuestionUsers.SingleOrDefault(
                    qu => qu.QuestionId.Equals(SolvedQvm.Question.QuestionId) &&
                        qu.ApplicationUserId.Equals(user.Id));

            if (SolvedQvm.IsLastQuestion)
            {
                string EasternStandardTimeId = "Eastern Standard Time";
                TimeZoneInfo ESTTimeZone = TimeZoneInfo.FindSystemTimeZoneById(EasternStandardTimeId);
                DateTime ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), ESTTimeZone);
                QuestionUser.EndTime = ESTDateTime;
            }

            if (SolvedQvm.SelectedAnswerId == CorrectAnswer.AnswerId)
            {
                if (context.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("TotalScore"))
                {
                    HttpCookie cookie = context.ControllerContext.HttpContext.Request.Cookies["TotalScore"];
                    //cookie.Expires = DateTime.Now.AddDays(-1); // remove the cookie
                    int TotalScore = int.Parse(cookie.Value);
                    TotalScore++;
                    cookie.Value = TotalScore.ToString();
                    context.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
                }
                else
                {
                    HttpCookie cookie = new HttpCookie("TotalScore");
                    cookie.Value = "1";
                    context.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
                }
                QuestionUser.IsCorrect = true;
            }
            else
            {
                // answer is incorrect
                QuestionUser.IsCorrect = false;
            }

            try
            {
                QuestionUser.IsSolved = true;
                db.SaveChanges();

                user.SolvedQuestions.Add(QuestionToCheck);
                db.Questions.Attach(QuestionToCheck);
                db.SaveChanges();
            }
            catch (Exception exc) { }
        }
Example #2
0
        public ActionResult Result(QuestionViewModel SolvedQvm)
        {
            var QSTime = DateTime.Parse(db.Settings.SingleOrDefault(s => s.Name == "Quiz Start Time").Value);
            ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ESTTimeZone);
            var TimeDiff = QSTime.Subtract(ESTDateTime);
            if (TimeDiff.TotalSeconds > 0)
            {
                return RedirectToAction("Index", "Home");
            }

            ResultViewModel rvm = new ResultViewModel();

            try
            {
                var UserId = User.Identity.GetUserId();
                user = db.Users.Where(u => u.Id == UserId).SingleOrDefault();

                if (SolvedQvm.Question != null)
                {
                    SolvedQvm.IsLastQuestion = true;
                    QuestionHelper.CheckQuestion(SolvedQvm, this, user, db);
                    
                    var QuizInfo = user.QuizInfo;
                    QuizInfo.HasCompletedQuiz = true;
                    db.Entry(QuizInfo).State = EntityState.Modified;
                    db.SaveChanges();
                }
                var CorrectAnswersCount = db.QuestionUsers.Where(qu => qu.ApplicationUserId.Equals(UserId) && qu.IsCorrect.Equals(true)).Count();

                //var QuizStartTime = db.QuestionUsers.OrderBy(qu => qu.StartTime).FirstOrDefault().StartTime;
                var QuizStartTime = user.QuizInfo.QuizStartDateTime;
                var QuizEndTime = user.QuestionUsers.OrderByDescending(qu => qu.EndTime).FirstOrDefault().EndTime;

                TimeSpan TimeSpan = QuizEndTime.Subtract(QuizStartTime);

                rvm.QuizTime = TimeSpan;
                rvm.CorrectAnswersCount = CorrectAnswersCount;
                rvm.TotalQuestions = db.Questions.ToList().Count;
                rvm.TotalScore = GetScoreFromCookie();
            }
            catch (NullReferenceException exc)
            {
                rvm.TotalScore = GetScoreFromCookie();
            }
            catch (Exception exc)
            {
                rvm.TotalScore = GetScoreFromCookie();
            }

            return View(rvm);
        }
Example #3
0
        public ActionResult Details(int? id, QuestionViewModel SolvedQvm)
        {
            var UserId = User.Identity.GetUserId();
            var userTask = db.Users.Where(u => u.Id == UserId).SingleOrDefault();
            var QuizStartTimeTask = db.Settings.SingleOrDefault(s => s.Name == "Quiz Start Time");
            var QuestionToSolveTask = db.Questions.SingleOrDefault(q => q.QuestionId == id);

            //await Task.WhenAll(userTask, QuizStartTimeTask, QuestionToSolveTask);

            var user = userTask;//.Result;
            if (user.QuizInfo != null && user.QuizInfo.HasCompletedQuiz) return RedirectToAction("Result", "Home");
            var QuizStartTime = DateTime.Parse(QuizStartTimeTask.Value);
            
            DateTime ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ESTTimeZone);
            var TimeDiff = QuizStartTime.Subtract(ESTDateTime);

            if (TimeDiff.TotalSeconds > 0)
            {
                return RedirectToAction("Index", "Home");
            }
            
            Question QuestionToSolve = QuestionToSolveTask;//.Result;
            
            var UserMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            //var user = UserMngr.FindById(User.Identity.GetUserId());

            if (user.QuizInfo == null)
            {
                QuizInfo QuizInfo = new QuizInfo();
                QuizInfo.QuizStartDateTime = QuizStartTime;
                QuizInfo.ApplicationUser = user;
                db.QuizInfo.Add(QuizInfo);
                db.SaveChanges();
            }

            try
            {
                var QuestionUser = db.QuestionUsers.SingleOrDefault(
                    qu => qu.QuestionId.Equals(QuestionToSolve.QuestionId) &&
                        qu.ApplicationUserId.Equals(user.Id));
                if (QuestionUser == null)
                {
                    QuestionUser = new QuestionUser();
                    QuestionUser.ApplicationUserId = user.Id;
                    QuestionUser.QuestionId = QuestionToSolve.QuestionId;
                    ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ESTTimeZone);                    
                    QuestionUser.StartTime = ESTDateTime;
                    QuestionUser.EndTime = ESTDateTime;
                    db.QuestionUsers.Add(QuestionUser);
                    db.SaveChanges();
                } else if (!QuestionUser.IsSolved)
                {
                    ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), ESTTimeZone);
                    QuestionUser.StartTime = ESTDateTime;
                    db.SaveChanges();
                }
            }
            catch (Exception exc) { }

            var UnsolvedQuestions = user.GetUnsolvedQuestions();
            if (user.QuizInfo != null && user.QuizInfo.HasCompletedQuiz)
            {
                HttpCookie cookie = new HttpCookie("QuizSolved", true.ToString());
                Response.Cookies.Add(cookie);
                return RedirectToAction("Solved", "Home");
            }

            if (user.HasSolvedQuestion(QuestionToSolve))
            {
                // redirect to next unsolved question
                var QuestionsInDb = db.Questions.OrderBy(q => q.QuestionId);
                foreach (Question Question in QuestionsInDb)
                {
                    if (!user.HasSolvedQuestion(Question))
                    {
                        HttpCookie cookie = new HttpCookie("QuestionId", Question.QuestionId.ToString());
                        Response.Cookies.Add(cookie);
                        return RedirectToAction("Solved", "Home");
                    }
                }
            }

            if (SolvedQvm != null && SolvedQvm.SelectedAnswerId != 0)
            {
                QuestionHelper.CheckQuestion(SolvedQvm, this, user, db);
            }

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (user.IsAheadOfNextUnsolvedQuestion(QuestionToSolve))
            {
                // redirect to next unsolved question
                var QuestionsInDb = db.Questions.OrderBy(q => q.QuestionId);
                foreach (Question Question in QuestionsInDb)
                {
                    if (!user.HasSolvedQuestion(Question))
                    {
                        HttpCookie cookie = new HttpCookie("QuestionId", Question.QuestionId.ToString());
                        Response.Cookies.Add(cookie);
                        return RedirectToAction("Solved", "Home");
                    }
                }
            }

            List<Answer> Answers = QuestionToSolve.Answers;
            var qvm = new QuestionViewModel();
            qvm.Question = QuestionToSolve;
            Answers.Shuffle();
            qvm.Answers = Answers;
            var NextQuestion = db.Questions.OrderBy(q => q.QuestionId).FirstOrDefault(q => q.QuestionId > id);
            if (NextQuestion != null)
            {
                qvm.NextId = NextQuestion.QuestionId;
                qvm.IsLastQuestion = false;
            }
            else
            {
                qvm.IsLastQuestion = true;
            }

            if (QuestionToSolve == null)
            {
                return HttpNotFound();
            }
            
            return View(qvm);
        }
Example #4
0
        public ActionResult Edit(QuestionViewModel qvm) //[Bind(Include = "Id,Text,AnswerId")]
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var ModifiedQuestion = qvm.Question;
                    var Question = db.Questions.Find(ModifiedQuestion.QuestionId);
                    Question.Text = ModifiedQuestion.Text;

                    db.Entry(Question).State = EntityState.Modified;
                    db.SaveChanges();

                    foreach (var ModifiedAnswer in qvm.Answers)
                    {
                        var Answer = db.Answers.Find(ModifiedAnswer.AnswerId);
                        Answer.Text = ModifiedAnswer.Text;
                        Answer.IsCorrect = ModifiedAnswer.IsCorrect;

                        db.Entry(Answer).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    //db.Entry(qvm.Question).State = EntityState.Modified;
                    //db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch(Exception exc) {}
            }
            return View(qvm);
        }
Example #5
0
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Question question = db.Questions.Find(id);
            List<Answer> Answers = question.Answers;

            var qvm = new QuestionViewModel();
            qvm.Question = question;
            qvm.Answers = Answers;

            if (question == null)
            {
                return HttpNotFound();
            }
            return View(qvm);
        }
Example #6
0
        public ActionResult Create(QuestionViewModel qvm) //[Bind(Include = "Id,Text,AnswerId")]
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Questions.Add(qvm.Question);
                    db.SaveChanges();

                    foreach (var Answer in qvm.Answers)
                    {
                        if (Answer.AnswerId.Equals(0))
                        {
                            Answer.QuestionId = qvm.Question.QuestionId;
                            db.Answers.Add(Answer);
                            db.SaveChanges();
                        }
                    }

                    return RedirectToAction("Index");
                }
                catch(Exception exc) { }
            }

            return View(qvm.Question);
        }