public void SaveVoteMembre(AnswerByMembre answer)
        {
            IDbCommand command = new SqlCommand("INSERT INTO AnswerUsers (IdSurvey, IdMembre, IdAnswer) VALUES (@IdSurvey, @IdMembre, @IdAnswer)", (SqlConnection)ConnectionSurveys.Instance);

            command.Parameters.Add(new SqlParameter("@IdSurvey", SqlDbType.Int)
            {
                Value = answer.IdSurvey
            });
            command.Parameters.Add(new SqlParameter("@IdMembre", SqlDbType.Int)
            {
                Value = answer.IdMembre
            });
            command.Parameters.Add(new SqlParameter("@IdAnswer", SqlDbType.Int)
            {
                Value = answer.IdAnswer
            });
            ConnectionSurveys.Instance.Open();
            command.ExecuteNonQuery();
            command.Dispose();
            ConnectionSurveys.Instance.Close();
        }
        public IActionResult Vote(int?vote, int idSurvey)
        {
            ViewBag.NbreVisitUnique = GetVisitIP();
            ViewBag.NbrePagesVues   = GetPageVues();
            UserConnect(ViewBag);
            ConsentCookie(ViewBag);


            List <string> errors = new List <string>();

            if (vote == null)
            {
                errors.Add("Vous devez cocher une case pour voter!");
                ViewBag.errors = errors;
                Survey survey = new Survey {
                    Id = idSurvey
                };
                survey = survey.GetSurvey();

                Membres membre = new Membres(); //Récupérer le membre auteur du sondage
                membre = membre.GetMembreById(survey.IdCreateur);
                SurveyViewModel viewModel = new SurveyViewModel {
                    Survey = survey, Membre = membre
                };

                return(View("ViewSurvey", viewModel));
            }

            if (ViewBag.Logged)
            {
                AnswerByMembre answer = new AnswerByMembre {
                    IdMembre = Convert.ToInt32(ViewBag.Id), IdSurvey = idSurvey, IdAnswer = (int)vote
                };
                answer.SaveVoteMembre();
            }
            else
            {
                Visiteur   v = new Visiteur();
                string     remoteIpAddress = Convert.ToString(Request.HttpContext.Connection.RemoteIpAddress);
                AnswerByIp answer          = new AnswerByIp {
                    IdIp = v.GetIdIp(remoteIpAddress), IdAnswer = (int)vote, IdSurvey = idSurvey
                };
                answer.SaveVoteIdIp();

                CookieOptions option = new CookieOptions
                {
                    Expires  = DateTime.Now.AddDays(365),
                    HttpOnly = true
                };

                Response.Cookies.Append("Survey" + idSurvey, Convert.ToString(vote), option);
            }

            Survey s = new Survey {
                Id = idSurvey
            };

            s = s.GetSurvey();

            Membres m = new Membres(); //Récupérer le membre auteur du sondage

            m = m.GetMembreById(s.IdCreateur);
            List <Survey>         surveys = s.GetAllSurveys(true);
            SurveyResultViewModel result  = new SurveyResultViewModel {
                Survey = s, Surveys = surveys, Membre = m
            };

            return(View("ResultSurvey", result));
        }
        public IActionResult DisplaySurvey(int id)
        {
            ViewBag.NbreVisitUnique = GetVisitIP();
            ViewBag.NbrePagesVues   = GetPageVues();
            UserConnect(ViewBag);
            ConsentCookie(ViewBag);


            Survey s = new Survey {
                Id = id
            };

            s = s.GetSurvey();

            Membres m = new Membres(); //Récupérer le membre auteur du sondage

            m = m.GetMembreById(s.IdCreateur);

            SurveyViewModel viewModel = new SurveyViewModel {
                Survey = s, Membre = m
            };


            bool DejaVote; // Vérifier si déjà voté

            if (ViewBag.Logged)
            {
                AnswerByMembre answer   = new AnswerByMembre();
                int            IdMembre = Convert.ToInt32(ViewBag.Id);
                DejaVote = answer.VerifVoteMembre(IdMembre, id);
            }
            else
            {
                Visiteur   v = new Visiteur();
                string     remoteIpAddress = Convert.ToString(Request.HttpContext.Connection.RemoteIpAddress);
                AnswerByIp answer          = new AnswerByIp();

                bool VerifByIp = answer.VerifVoteIp(v.GetIdIp(remoteIpAddress), id);
                bool VerifByCookie;

                if (Request.Cookies["Survey" + id] == null)
                {
                    VerifByCookie = false;
                }
                else
                {
                    VerifByCookie = true;
                }

                if (VerifByIp && VerifByCookie)
                {
                    DejaVote = true;
                }
                else
                {
                    DejaVote = false;
                }
            }

            if (DejaVote)
            {
                List <Survey>         surveys = s.GetAllSurveys(true);
                SurveyResultViewModel result  = new SurveyResultViewModel {
                    Survey = s, Surveys = surveys, Membre = m
                };
                return(View("ResultSurvey", result));
            }

            return(View("ViewSurvey", viewModel));
        }