Exemplo n.º 1
0
        public ActionResult SaveSession(CompletedSessionAjaxViewModel completedSession)
        {
            var reply = new AjaxResponse();
            reply.Success = false;
            reply.Message = "Nothing happened";

            var entities = new Models.db38bab79d27554b96b50aa57c010cd149Entities3();

            var sessionRecord = entities.Sessions.Where(x => x.Id == completedSession.SessionId).SingleOrDefault();

            if (sessionRecord == null)
            {
                reply.Message = "Session ID not found";
                return Json(reply);
            }

            return Json(ProcessSessionRequest(completedSession));
        }
Exemplo n.º 2
0
        private AjaxResponse ProcessSessionRequest(CompletedSessionAjaxViewModel completedSession)
        {
            var entities = new Models.db38bab79d27554b96b50aa57c010cd149Entities3();
            var reply = new AjaxResponse();
            reply.Success = false;
            reply.Message = "Failed to save to database";

            Dictionary<string,Word> WordsByEnglish = new Dictionary<string,Word>();
            foreach(var w in entities.Words)
            {
                if (!WordsByEnglish.ContainsKey(w.English))
                {
                    WordsByEnglish.Add(w.English, w);
                }
            }

            var sessionToUpdate = entities.Sessions.Where(x => x.Id == completedSession.SessionId).SingleOrDefault();
            if(sessionToUpdate != null)
            {
                int tbi = 0;
                foreach(var tb in sessionToUpdate.TrialBlocks)
                {
                    int TrialCount = completedSession.TrialBlocks[tbi].Latencies.Length;
                    tb.Duration = completedSession.TrialBlocks[tbi].Duration;

                    for(int ti = 0; ti < TrialCount; ti++)
                    {
                        var newTrial = new Trial();
                        newTrial.Answer = WordsByEnglish[completedSession.TrialBlocks[tbi].TranslationsClicked[ti]];
                        newTrial.IndexInTrialBlock = ti;
                        newTrial.Latency = completedSession.TrialBlocks[tbi].Latencies[ti];
                        newTrial.Word = completedSession.TrialBlocks[tbi].WordsDisplayed[ti];

                        newTrial.Option1 = completedSession.TrialBlocks[tbi].OptionsDisplayed[ti][0];
                        newTrial.Option2 = completedSession.TrialBlocks[tbi].OptionsDisplayed[ti][1];
                        newTrial.Option3 = completedSession.TrialBlocks[tbi].OptionsDisplayed[ti][2];

                        tb.Trials.Add(newTrial);
                    }
                    tbi++;
                }
                sessionToUpdate.Completed = true;
                entities.SaveChanges();
                reply.Success = true;
                reply.Message = "Session saved";
            }
            return reply;
        }