public bool UpdateResponse(ParticipantAnswer response) { //the code below is in regards to the session id thingy in the controller //var e = entities.Participants.Where(x => x.phoneNumber == response.phoneNumber).FirstOrDefault(x => x.session_id == response.Participant.session_id); var e = entities.Participants.FirstOrDefault(x => x.phoneNumber == response.phoneNumber); if (e != null) { //e.answer = response.answer; //entities.Entry(e).CurrentValues.SetValues(response); //e.answer = e.answer == null || e.answer.Equals("") ? e.answer + response.answer : e.answer + "," + response.answer; var ans = entities.ParticipantAnswers.FirstOrDefault(m => m.participant_id == e.participant_id); //e.ParticipantAnswer.answer = e.ParticipantAnswer.answer == null || e.ParticipantAnswer.answer.Equals("") ? e.ParticipantAnswer.answer + response.answer : e.ParticipantAnswer.answer + "," + response.answer; if (ans != null) { ans.answer = ans.answer == null || ans.answer.Equals("") ? ans.answer + response.answer : ans.answer + "," + response.answer; ans.Date = DateTime.Now; entities.Entry(ans).State = System.Data.Entity.EntityState.Modified; } else { ans = response; ans.participant_id = e.participant_id; ans.Date = DateTime.Now; entities.ParticipantAnswers.Add(ans); } entities.SaveChanges(); return(true); } else { return(false); } }
public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb) { context.Configuration.AutoDetectChangesEnabled = false; var participants = oldDb.Participants.Select(x => new { x.Id, x.Contest, OldUserId = x.User1.Id, x.RegisteredOn, x.IsOfficial, x.Answer, }).ToList(); var contests = context.Contests.Select(x => new { x.OldId, x.Id, Question = x.Questions.FirstOrDefault() }).ToDictionary(x => x.OldId); var users = context.Users.Select(x => new { x.OldId, x.Id }).ToDictionary(x => x.OldId); foreach (var oldParticipant in participants) { if (!contests.ContainsKey(oldParticipant.Contest)) { continue; } var contest = contests[oldParticipant.Contest]; if (!users.ContainsKey(oldParticipant.OldUserId)) { continue; } var participant = new Participant { OldId = oldParticipant.Id, PreserveCreatedOn = true, CreatedOn = oldParticipant.RegisteredOn, IsOfficial = oldParticipant.IsOfficial, ContestId = contest.Id, UserId = users[oldParticipant.OldUserId].Id, }; if (contest.Question != null) { var answer = new ParticipantAnswer { ContestQuestionId = contest.Question.Id, Answer = oldParticipant.Answer, Participant = participant, }; context.ParticipantAnswers.Add(answer); } context.Participants.Add(participant); } context.SaveChanges(); context.Configuration.AutoDetectChangesEnabled = true; }
public ActionResult SendAnswer([FromBody] ParticipantAnswer model, string token) { if (token.Length != TokenManagement.TokenLength) { token = WebUtility.UrlDecode(token); } using TrackerContext db = new(); var ut = db.UserToken.FirstOrDefault(t => t.Token.Equals(token)); if (ut is null) { return(Unauthorized("You're not authorized")); } var participant = db.Participant.FirstOrDefault(p => p.ID == ut.ParticipantID); if (participant is null) { return(BadRequest("Cannot find participant with current id")); } var project = db.Project.FirstOrDefault(p => p.ID == participant.ProjectID); if (project is null) { return(BadRequest("Cannot find project for current participant")); } var question = db.Question.FirstOrDefault(q => q.ID == model.QuestionID); if (question is null) { return(BadRequest("Question not found")); } model.AnswerDate = DateTime.Now; try { db.ParticipantAnswer.Add(model); db.SaveChanges(); } catch (Exception ex) { return(BadRequest(ex)); } return(Ok()); }
public void AddParticipantAnswer(ParticipantAnswer ParticipantAnswer) { DataTable dtParticipantAnswer = null; DataRow drNew = null; try { LoadXML(); dtParticipantAnswer = oeEntity.Tables[EntityConstents.TBL_PARTICIPANTANSWER]; drNew = dtParticipantAnswer.NewRow(); drNew[EntityConstents.COL_PARTICIPANTANSWER_ID] = Guid.NewGuid().ToString(); drNew[EntityConstents.COL_PARTICIPANTANSWER_ANSWER] = ParticipantAnswer.Answer; drNew[EntityConstents.COL_PARTICIPANTANSWER_PARTICIPANTASSESMENT] = ParticipantAnswer.ParticipantAssesment; //drNew[EntityConstents.COL_PARTICIPANTANSWER_QUESTION] = ParticipantAnswer.Question; dtParticipantAnswer.Rows.Add(drNew); saveXML(oeEntity); } catch { } }
public HttpResponseMessage httpResponseMessage([FromBody] PaceResponse paceResponse) { HttpResponseMessage responseMessage; string response; var participant = new Participant(); var participantanswer = new ParticipantAnswer(); var answer = new Answer(); var resquest = Request.Content.ReadAsStringAsync(); if (paceResponse.text == null) { paceResponse.text = "Successfull!"; } if (paceResponse.text.Equals("", StringComparison.Ordinal)) { response = "CON Welcome to ECHO Survey\n"; response += "1.Register\n"; response += "2.Answer Question\n"; } else if (paceResponse.text.Equals("1", StringComparison.Ordinal)) { response = $"CON Enter your \n NAME, FACILITY : {paceResponse.ParticipantName}, { paceResponse.facility} \n"; //String vresponse = ussdResponse.text; } else if (paceResponse.text.StartsWith("1*")) { var details = paceResponse.text.Split('*')[1].Split(','); participant.participant_id = Guid.NewGuid(); participant.session_id = Guid.NewGuid().ToString();//paceResponse.session_id participant.ParticipantName = details[0]; participant.facility = details[1]; participant.phoneNumber = paceResponse.phoneNumber; participantanswer.answer = paceResponse.answer; participantanswer.Date = DateTime.Now; answer.correctAnswer = paceResponse.correctAnswer; _ParticipantServices.AddParticipant(participant); response = "Successful!"; } else if (paceResponse.text.Equals("2", StringComparison.Ordinal)) { response = "CON 1.Nigeria is in:\nA. West Africa\nB. East Africa\nC. North Africa\nD. South Africa\nE. All of the above";; } else if (paceResponse.text.StartsWith("2*", StringComparison.Ordinal)) { string[] array = new string[4]; array[0] = "CON 2.My name is:\nA.Ibrahim\nB.Bola\nC.Chidi\nD.Dooshima\nE.None of the Above"; array[1] = "CON 3.My age is within the age ranges:\nA. 21-30 Years\nB. 31-40 Years\nC. 41-50 Years\nD. 51-60 Years"; array[2] = "CON 4.I am:\nA. Single\nB. Married\nC. Divorced\nD. Widow/Widower\nE. None of the above"; array[3] = "CON 5.I am:\nA. Christian\nB. Muslim\nC. Traditionalist\nD. None of the Above\nE. All of the above"; String[] youranswer = paceResponse.text.Split('*'); var index = youranswer.Length - 2; if (index <= array.Length) { Regex r = new Regex(@"^[ABCDE]$", RegexOptions.IgnoreCase); var c = youranswer[youranswer.Length - 1]; if (r.Match(c).Success) { response = index == array.Length ? "You have finshed the questions successfully" : array[index]; //participantanswer.answer = paceResponse.answer; participantanswer.answer = c; participantanswer.phoneNumber = paceResponse.phoneNumber; //the code below is for checking session id when the users phone number would appear twice //participantanswer.Participant.session_id = paceResponse.session_id; //participant.ParticipantId = Guid.Parse("5D03D687-2639-4239-998F-03116303D8EF"); //participant.SessionId = Guid.Parse("D6D75B72ED7F4DC78D654B6754B76944"); bool status = _ParticipantServices.UpdateResponse(participantanswer); if (!status) { response = "You need to Regiter"; } } else { response = "CON You entered an inValid Option"; } } else { response = "END The End"; } } else { response = "CON Invalid Option\n"; } responseMessage = Request.CreateResponse(HttpStatusCode.Created, response); responseMessage.Content = new StringContent(response, Encoding.UTF8, "text/plain"); return(responseMessage); }