// GET: Quiz
        public ActionResult Index(bool isFromPost = false)
        {
            QuestionOptions questions = new QuestionOptions();

            if (!isFromPost)
            {
                var quiz = GetQuizRecords();

                questions = GetQuizQuestion(quiz.Id);

                if (questions == null)
                {
                    ViewBag.ErrorMessage = "No questions available in the quiz.";
                    return(View());
                }


                var user = (User)Session["user"];

                if (user == null)
                {
                    return(RedirectToAction("index", "home"));
                }


                user.QuizId     = quiz.Id;
                Session["user"] = user;


                questions.ElapseTime = quiz.ElapseTime;
                ViewBag.FromPost     = false;
            }
            else
            {
                var quizList = (List <QuestionOptions>)Session["Questions"];

                //ObjectCache cache = MemoryCache.Default;
                //var quizList = (List<QuestionOptions>)cache.Get("Questions");

                questions        = quizList.Where(x => x.IsAnswered == false).FirstOrDefault();
                ViewBag.FromPost = true;
            }

            ViewBag.IsCompletedQuestions = false;

            if (questions == null)
            {
                var user = (User)Session["user"];

                UpdateScores(user.Id, user.Score, user.QuizId);

                ViewBag.Score    = user.Score;
                ViewBag.UserName = user.Email;

                ViewBag.IsCompletedQuestions = true;
            }


            return(View(questions));
        }
        public ActionResult Index(QuestionOptions options, bool chkCorrectAnswer = false)
        {
            if (chkCorrectAnswer)
            {
                var user = (User)Session["user"];
                user.Score      = user.Score + 5;
                Session["user"] = user;
            }

            //ObjectCache cache = MemoryCache.Default;
            //var quizList = (List<QuestionOptions>)cache.Get("Questions");
            var quizList = (List <QuestionOptions>)Session["Questions"];
            int index    = quizList.FindIndex(a => a.Id == options.Id);
            var quiz     = quizList[index];

            quiz.IsAnswered      = true;
            quizList[index]      = quiz;
            Session["Questions"] = quizList;

            return(RedirectToAction("Index", new { isFromPost = true }));
        }
        private QuestionOptions GetQuizQuestion(Guid quizId)
        {
            List <QuestionOptions> quizList   = new List <QuestionOptions>();
            QuestionOptions        quiz       = new QuestionOptions();
            SqlConnection          connection = new SqlConnection(CONNECTION_STRING);
            var date = DateTime.Now.ToString("dd/MM/yyyy hh:mm");

            SqlCommand command = new SqlCommand(
                @"select question.Id,question.Description,questionoptions.Description as OptionDescription,IsAnswer from question 
inner join quizquestions  on question.id = quizquestions.QuestionId
INNER JOIN questionoptions on questionoptions.QuestionId = quizquestions.QuestionId  WHERE QuizId='" + quizId + "' AND ISNULL(QuizQuestions.IsPublished,0)=0" +
                "Order by question.Id",
                connection);

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            //Guid prevId = Guid.Empty;
            //List<Options> options = new List<Options>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    var guid = new Guid(Convert.ToString(reader["Id"]));

                    if (quizList.Where(x => x.Id == guid).FirstOrDefault() == null)
                    {
                        quiz = new QuestionOptions();
                    }

                    //if (prevId != guid)
                    //{
                    //    if (prevId != Guid.Empty)
                    //    {
                    //        quizList.Add(quiz);
                    //    }
                    //    quiz = new QuestionOptions();
                    //}
                    //prevId = guid;
                    quiz.Id          = guid;
                    quiz.Description = Convert.ToString(reader["Description"]);
                    if (quiz.OptionsList == null)
                    {
                        quiz.OptionsList = new List <Options>();
                    }
                    quiz.OptionsList.Add(new Options {
                        Description = Convert.ToString(reader["OptionDescription"]), IsAnswer = Convert.ToBoolean(reader["IsAnswer"])
                    });

                    if (quizList.Where(x => x.Id == guid).FirstOrDefault() == null)
                    {
                        quizList.Add(quiz);
                    }
                }
            }

            reader.Close();

            if (quiz.Id != Guid.Empty)
            {
                //  UpdateQuestionPublish(quiz.Id, quizId);
            }

            // quiz.OptionsList = options;
            Session["Questions"] = quizList;
            //ObjectCache cache = MemoryCache.Default;
            // Store data in the cache
            //CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
            //cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);
            //cache.Add("Questions", quizList, cacheItemPolicy);
            return(quizList.FirstOrDefault());
        }