public async Task <string> Initialize(InitializeDTO initializeDTO)
        {
            if (!ModelState.IsValid)
            {
                return(null);
            }
            User user = await this._userManager.GetUserAsync(HttpContext.User);

            string numberWhichUserMustGuess = _gameHandler.GenerateUniqueDigitsNumber();

            user.Games.Add(new Game()
            {
                NumberWhichUserMustGuess = numberWhichUserMustGuess.ToString(),
                NumberWhichAIMustGuess   = initializeDTO.numberToGuess
            });
            await _context.SaveChangesAsync();

            return(initializeDTO.numberToGuess);
        }
Beispiel #2
0
        public ActionResult Initialize(string studyId)
        {
            var messages = new List <ValidationMessages>();
            var dto      = new InitializeDTO {
                IsSuccessful = true, ValidMessages = messages
            };

            studyId = Encoder.HtmlEncode(studyId);

            if (DbUtils.IsStudyIdValid(studyId) != 1)
            {
                dto.IsSuccessful = false;
                messages.Add(new ValidationMessages
                {
                    FieldName   = "invalidStudyId",
                    DisplayName = "Study ID",
                    Message     = "is invalid"
                });
                _logger.LogInfo("InitializeSubject.Initialize - invalid study id: " + studyId);
                return(Json(dto));
            }

            _logger.LogInfo("InitializeSubject.Initialize - Post: " + studyId);

            var siteId     = DbUtils.GetSiteidIdForUser(User.Identity.Name);
            var language   = DbUtils.GetSiteLanguage(siteId);
            var sensorType = int.Parse(Request.Params["sensorType"]);


            SSInsertionData ssInsert;

            _logger.LogInfo("InitializeSubject.Initialize - validation: " + studyId);
            if (!DbInitializeContext.IsValidInitialize(Request.Params, out messages, out ssInsert))
            {
                dto.ValidMessages = messages;
                dto.IsSuccessful  = false;
                dto.Message       = "There are validation errors";
                _logger.LogInfo("InitializeSubject.Initialize - validation errors: " + studyId);
            }

            if (dto.IsSuccessful)
            {
                _logger.LogInfo("InitializeSubject.Initialize - validated");
                _logger.LogInfo("InitializeSubject.Initialize - Add senor data to database: " + studyId + ", sensor: " + sensorType);
                if (sensorType > 0)
                {
                    //save the sensor data
                    if (!SsUtils.AddSenorData(studyId, ssInsert))
                    {
                        dto.IsSuccessful = false;
                        dto.Message      = "Could not add sensor data";

                        _logger.LogInfo("InitializeSubject.Initialize - could not save sensor data: " + studyId);
                    }
                }

                bool   useCafpint     = bool.Parse(Request.Params["cafpint"]);
                bool?  cafpintConsent = null;
                int?   inrGreater3    = null;
                string cafpintId      = null;

                var dateRandomized = DateTime.Parse(Request.Params["randDate"] + " " + Request.Params["randTime"]);

                //set next randomiztion and update database
                if (dto.IsSuccessful)
                {
                    _logger.LogInfo("InitializeSubject.Initialize - sensor data added");
                    _logger.LogInfo("InitializeSubject.Initialize - SetRandomization: " + studyId);

                    if (useCafpint)
                    {
                        if (Request.Params["cafpintYesNo"] == "yes")
                        {
                            cafpintConsent = true;

                            if (Request.Params["cafpintYes"] == "yes")
                            {
                                inrGreater3 = 1;
                            }
                            else if (Request.Params["cafpintYes"] == "no")
                            {
                                inrGreater3 = 2;
                            }
                            else
                            {
                                inrGreater3 = 3;
                            }

                            if (inrGreater3 > 1)
                            {
                                cafpintId = Request.Params["cafPintId"];
                            }
                        }
                        else
                        {
                            cafpintConsent = false;
                        }
                    }

                    var  onInsulinYesNo    = Request.Params["onInsulinYesNo"];
                    bool onInsulinInfusion = onInsulinYesNo == "yes";

                    var iret = SsUtils.SetRandomization(studyId, cafpintConsent, inrGreater3, cafpintId, ref ssInsert, User.Identity.Name, dateRandomized, onInsulinInfusion);
                    if (iret == -1)
                    {
                        dto.IsSuccessful = false;
                        dto.Message      = "Could not set randomization";

                        _logger.LogInfo("InitializeSubject.Initialize - SetRandomization encountered a problem: " + studyId);
                    }
                }

                //insert data into checks template
                if (dto.IsSuccessful)
                {
                    _logger.LogInfo("InitializeSubject.Initialize - Randomization set");
                    _logger.LogInfo("InitializeSubject.InitializeSs - CHECKS inititalization");
                    if (!SsUtils.InitializeSs(Request.PhysicalApplicationPath, studyId, ssInsert, sensorType, language))
                    {
                        dto.IsSuccessful = false;
                        dto.Message      = "Could not initialized CHECKS";

                        _logger.LogInfo("InitializeSubject.InitializeSs - CHECKS inititalization encountered a problem");
                    }
                    _logger.LogInfo("InitializeSubject.InitializeSs - CHECKS initialized");
                }

                //send email notifications
                if (dto.IsSuccessful)
                {
                    _logger.LogInfo("InitializeSubject.Initialize - notifications: " + studyId);
                    TempData["InsertData"] = ssInsert;

                    //var users = ConfigurationManager.AppSettings["InitializeSubject"].Split(new[] { ',' }, StringSplitOptions.None);
                    var staff = NotificationUtils.GetStaffForEvent(1, siteId);

                    string siteName = DbUtils.GetSiteNameForUser(User.Identity.Name);

                    var u = new UrlHelper(Request.RequestContext);
                    if (Request.Url != null)
                    {
                        string url = "http://" + Request.Url.Host +
                                     u.RouteUrl("Default", new { Controller = "Home", Action = "Index" });

                        // don't let notifications error stop initialization process
                        try
                        {
                            if (!url.Contains("hpProd"))
                            {
                                staff.Clear();
                                staff.Add("*****@*****.**");
                                staff.Add("*****@*****.**");
                            }
                            Utility.SendStudyInitializedMail(staff.ToArray(), null, studyId, User.Identity.Name,
                                                             siteName, Server, url, ssInsert.Arm, cafpintId, dateRandomized);
                            _logger.LogInfo("InitializeSubject.Initialize - notifications sent: " + studyId);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError("InitializeSubject.Initialize - error sending notifications: " + ex.Message);
                        }
                    }
                }
            }
            if (dto.IsSuccessful)
            {
                _logger.LogInfo("InitializeSubject.Initialize - is successful: " + dto.IsSuccessful.ToString());
            }
            else
            {
                _logger.LogInfo("InitializeSubject.Initialize - failed: " + dto.Message);
            }
            return(Json(dto));
        }