public static ChartData Map(Survey Survey, long QuestionId)
        {
            //  Instantiate the analysis service
            var analyser = SurveyAnalysisServiceFactory.Create(Survey);
            //  instantiate the chartdata;
            ChartData data = new ChartData();

            //  Get the question object from the survey
            var question = Survey.Questions.FirstOrDefault(x => x.QuestionId == QuestionId);

            //  Map the data
            data.Title = question.Text;                             //  Load question text
            data.Width = 400;
            //  Set the height of the chart
            data.Height = (question.AvailableResponses.Count() * 30) + 150;

            data.Results = new List<ChartInformation>();

            long totalRespondents = analyser.TotalRespondents();    //  Load total
            foreach (var answer in question.AvailableResponses)
            {
                var result = new ChartInformation();
                result.responses = (analyser.TotalForQuestionResponse(question.QuestionId, answer.LikertScaleNumber) / totalRespondents);
                result.Text = answer.Text;
                data.Results.Add(result);
            }

            return data;
        }
        /// <summary>
        /// Create a survey, Add it to the DbContext, for persisting 
        /// in the underlying database
        /// </summary>
        /// <param name="survey"></param>
        public long CreateSurvey(Survey survey)
        {
            _unitOfWork.Surveys.Add(survey);
            _unitOfWork.SaveChanges();

            return survey.SurveyId;
        }
        public static SurveyResultsViewModel MapAllResults(Survey survey, IQuestionRepository questionRepository)
        {
            //  Instantiate the view model
            SurveyResultsViewModel surveyResults = new SurveyResultsViewModel();
            //  map the Survey information
            surveyResults.SurveyId = survey.SurveyId;
            surveyResults.Title = survey.Title;
            //  Map the results information.
            surveyResults.Results = MapResponses(survey.Respondents, questionRepository);

            return surveyResults;
        }
        public static SurveyResultsViewModel MapLatestResult(Survey survey,IRespondentRepository respondentRepository, IQuestionRepository questionRepository)
        {
            //  Get the latest respondents answers.
            ICollection<Respondent> latestRespondents = new List<Respondent>();
            latestRespondents.Add(respondentRepository.GetLatestResultForSurvey(survey.SurveyId));

            //  Instantiate the view model
            SurveyResultsViewModel surveyResults = new SurveyResultsViewModel();
            //  map the Survey information
            surveyResults.SurveyId = survey.SurveyId;
            surveyResults.Title = survey.Title;
            //  Map the results information.
            surveyResults.Results = MapResponses(latestRespondents, questionRepository);

            return surveyResults;
        }
        public static SurveyAnalysisViewModel Map(Survey survey)
        {
            //  Instantiate the analysis service
            var analyser = SurveyAnalysisServiceFactory.Create(survey);

            //  Map the survey level stuff.
            var viewModel = new SurveyAnalysisViewModel();
            viewModel.SurveyId = survey.SurveyId;
            viewModel.Title = survey.Title;
            viewModel.FirstResponseDate = analyser.DateOfFirstResponse();
            viewModel.LastResponseDate = analyser.DateOfLatestResponse();
            viewModel.NumberOfRespondents = analyser.TotalRespondents();

            //  Map the question level stuff.
            viewModel.Questions = new List<QuestionAnalysisViewModel>();
            foreach (var q in survey.Questions)
            {
                var question = new QuestionAnalysisViewModel();
                question.QuestionId = q.QuestionId;
                question.SequenceNumber = q.SequenceNumber;
                question.Text = q.Text;
                question.NumberOfResponses = analyser.TotalRespondents();

                //  Map the Response stuff.
                question.Analysis = new List<ResponseAnalysisViewModel>();
                foreach (var r in q.AvailableResponses)
                {
                    var response = new ResponseAnalysisViewModel();
                    response.QuestionId = q.QuestionId;
                    response.Text = r.Text;
                    response.LikertScaleNumber = r.LikertScaleNumber;
                    response.NumberOfResponses = analyser.TotalForQuestionResponse(response.QuestionId, response.LikertScaleNumber);
                    response.Percentage = string.Format("{0:f2}", analyser.PercentageForQuestionResponse(response.QuestionId, response.LikertScaleNumber));

                    //  Add the responses to the analysis for the question
                    question.Analysis.Add(response);
                }

                //  Add the question to the viewModel
                viewModel.Questions.Add(question);
            }

            return viewModel;
        }
        private void setQuestionsForSurvey3(Survey survey)
        {
            Question q1 = Core.Factories.QuestionFactory.CreateQuestion();
            q1.QuestionId = 1;
            q1.SequenceNumber = 1;
            q1.Text = "Text for Question 1";
            q1.Survey = survey;
            q1.AvailableResponses = new List<AvailableResponse>();
            //  Add the responses to the question.
            setAvailableResponsesForQuestion(survey.SurveyId, q1);
            //  Add to the Survey and the list of questions
            _questions.Add(q1);
            survey.Questions.Add(q1);

            Question q2 = Core.Factories.QuestionFactory.CreateQuestion();
            q2.QuestionId = 2;
            q2.SequenceNumber = 2;
            q2.Text = "Text for Question 2";
            q2.Survey = survey;
            q2.AvailableResponses = new List<AvailableResponse>();
            //  Add the responses to the question.
            setAvailableResponsesForQuestion(survey.SurveyId, q2);

            //  Add to the Survey and the list of questions.
            _questions.Add(q2);
            survey.Questions.Add(q2);
        }
 public static SurveyAnalysisService Create(Survey survey)
 {
     if (survey == null)
         throw new ArgumentNullException("Survey", "No valid Survey supplied to surveyAnalysisServiceFactory");
     return new ConcreteSurveyAnalysisService(survey);
 }
 public void CreateSurvey(Survey survey)
 {
     _db.Surveys.Add(survey);
     _db.SaveChanges();
 }
 public SurveyAnalysisService(Survey survey)
 {
     if (survey == null)
         throw new ArgumentNullException("Survey", "No survey supplied to analyse.");
     _survey = survey;
 }
 public ActionResult Submit(long id)
 {
     string result = "Failed";
     var survey = _surveyRepository.GetSurvey(id);
     if (survey.SubmitForApproval())
     {
         Survey[] surveys = new Survey[] { survey };
         _surveyRepository.UpdateSurveys(surveys);
         result = "OK";
     }
     return Json(result, JsonRequestBehavior.AllowGet);
 }
 public ConcreteSurveyAnalysisService(Survey survey)
     : base(survey)
 {
 }
        public ActionResult Take(TakeSurveyViewModel Responses)
        {
            if (ModelState.IsValid)
            {
                //  Map the responses to the Survey.
                Survey surveyWithResults = _takeSurveyViewModelMapper.Map(Responses);

                //  Now update the DbContext
                Survey[] surveys = new Survey[] { surveyWithResults };
                _surveyRepository.UpdateSurveys(surveys);

                //  Display the Thank you page.
                return RedirectToAction("ThankYou", new { id = surveyWithResults.SurveyId });
            }

            //  An error, so reconstruct the survey details and return to the view
            TakeSurveyViewModel viewModel = _reinstateTakeWurveyViewModelMapper.Map(Responses);

            return View(viewModel);
        }