public CompletedSurvey BuildCompletedSurvey(string answersJson, int studentID) { CompletedSurvey completedSurvey = new CompletedSurvey(); JObject jObject = JObject.Parse(answersJson); JToken jModule = jObject["ModuleID"]; completedSurvey.ModuleID = (string)jObject["ModuleID"]; completedSurvey.DateCompleted = DateTime.Now; completedSurvey.StudentID = studentID; JToken[] jTokenAnswers = jObject["Answers"].ToArray(); List <Answer> answers = new List <Answer>(); foreach (JToken jAnswer in jTokenAnswers) { Answer answer = new Answer(); answer.QuestionID = Convert.ToDouble(jAnswer["QuestionID"]); answer.Rating = Convert.ToInt32(jAnswer["Rating"]); answer.Comment = Convert.ToString(jAnswer["Comment"]); answers.Add(answer); } completedSurvey.Answers = answers; return(completedSurvey); }
public void SaveAnswers(List <Answer> answers, string userID) { var survey = GetActiveSurvey(userID); var questions = survey.Questions.ToList(); for (int i = 0; i < questions.Count; i++) { answers[i].QuestionId = questions[i].Id; } var completedSurvey = new CompletedSurvey() { Answers = answers, DateOfComplete = DateTime.Now, SurveyId = survey.Id }; using (ApplicationDbContext db = new ApplicationDbContext()) { var user = db.Students.FirstOrDefault(x => x.Id == userID); if (user != null) { user.CompletedSurveys.Add(completedSurvey); user.SurveyIsStarted = true; db.SaveChanges(); } } }
public IHttpActionResult Get(string content) { AuthenticatedUser user = authenticate.confirmToken(); if (user.UserID != 0) { ObjectBuilder builder = new ObjectBuilder(); SurveyQueries query = new SurveyQueries(); CompletedSurvey completedSurvey = builder.BuildCompletedSurvey(content, user.UserID); query.PostAnswers(completedSurvey); return(Ok("success")); } else { return(Unauthorized()); } }
public void PostAnswers(CompletedSurvey completedSurvey) { HttpContext context = HttpContext.Current; int completedSurveyID = PostCompletedSurvey(completedSurvey.ModuleID, completedSurvey.StudentID); string valueString = ""; foreach (Answer answer in completedSurvey.Answers) { string addValue = string.Format("({0}, {1}, {2}, '{3}')", completedSurveyID, answer.QuestionID, answer.Rating, answer.Comment); if (!string.IsNullOrEmpty(valueString)) { valueString += ","; } valueString += addValue; } if (!string.IsNullOrEmpty(valueString)) { con.Open(); SqlCommand cmd = new SqlCommand(string.Format("INSERT INTO Answer(CompletedSurveyID, QuestionID, Rating, Comment) " + "VALUES {0}", valueString), con); SqlDataReader reader = cmd.ExecuteReader(); con.Close(); } }
public CompletedQuestion(Question question, string answer, CompletedSurvey completedSurvey = null) { Question = question; Answer = answer; CompletedSurvey = completedSurvey; }
public void Add(CompletedSurvey survey) { _context.CompletedSurveys.Add(survey); }