public JsonResult Create([DataSourceRequest] DataSourceRequest request, GameQuestionDTO gameQuestionDTO)
        {
            try
            {
                //check to ensure the user owns the resources she is trying to access. if not; we get out of here.
                //Somebody is trying to do bad stuff.
                if (!ProbeValidate.IsGameForLoggedInUser((long)gameQuestionDTO.GameId))
                {
                    ModelState.AddModelError("", "The attempt to add a game question was not successful");
                    return(Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()));
                }
                if (!ProbeValidate.IsQuestionForLoggedInUser((long)gameQuestionDTO.QuestionId))
                {
                    ModelState.AddModelError("", "The attempt to add a game question was not successful");
                    return(Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()));
                }

                //transform DTO to business object (GameQuestion)
                GameQuestion gameQuestion = new GameQuestion
                {
                    Id         = 0,
                    GameId     = gameQuestionDTO.GameId,
                    QuestionId = gameQuestionDTO.QuestionId,
                    OrderNbr   = gameQuestionDTO.OrderNbr,
                    Weight     = gameQuestionDTO.Weight
                };

                //We need to clone the question and set the UsedInGame field to true. The cloned questions
                //is what will be associated with the game
                ChoiceQuestion clonedQuestion = ProbeQuestion.CloneQuestion(this, ref db, true, gameQuestion.QuestionId);
                gameQuestion.Question = clonedQuestion; //we do a switch to the cloned question


                db.GameQuestion.Add(gameQuestion);
                db.SaveChanges(Request != null ? Request.LogonUserIdentity.Name : null);
                gameQuestionDTO.Id = gameQuestion.Id; //pass back the new Id to the client

                return(Json(new[] { gameQuestionDTO }.ToDataSourceResult(request, ModelState)));
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex); //log to elmah
                ModelState.AddModelError("", ProbeConstants.MSG_UnsuccessfulOperation_STR);
                return(Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()));
            }
        }
        }//public ActionResult Update([DataSourceRequest] DataSourceRequest dsRequest, QuestionDTO questionDTO)

        // GET: ChoiceQuestions/Clone
        public JsonResult Clone(long id)
        {
            try
            {
                //limit the questions to only what the user possesses
                string loggedInUserId = (User.Identity.GetUserId() != null ? User.Identity.GetUserId() : "-1");

                ChoiceQuestion clonedQuestion = ProbeQuestion.CloneQuestion(this, ref db, false, id);
                db.Question.Add(clonedQuestion);
                db.SaveChanges(Request != null ? Request.LogonUserIdentity.Name : null);

                //The message that the calling RAZOR can use
                ResultMessage resultMessage = new ResultMessage
                {
                    MessageId   = ProbeConstants.MSG_QuestionCloneSuccessful,
                    MessageType = Helpers.Mics.MessageType.Informational,
                    Message     = @"The question <span style=""font-style:italic;font-weight:bold"">" +
                                  db.ChoiceQuestion.Find(id).Name + @"</span> has been cloned successfully to question <span style=""font-style:italic;font-weight:bold"">" +
                                  clonedQuestion.Name + @"</span>"
                };

                NotifyProbe.NotifyQuestionChanged(User.Identity.Name); //let all clients know where was a game change.

                return(Json(resultMessage, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex); //log to elmah

                //The message return via an AJAX call
                ResultMessage resultMessage = new ResultMessage
                {
                    MessageId   = ProbeConstants.MSG_UnsuccessfulOperation,
                    MessageType = Helpers.Mics.MessageType.Error,
                    Message     = ProbeConstants.MSG_UnsuccessfulOperation_STR
                };

                return(Json(resultMessage, JsonRequestBehavior.AllowGet));
            }
        } //public JsonResult Clone(long id)
Esempio n. 3
0
        /*
         * Will clone the game and all artifacts associated with that game. gameconfiguration, gamequestion, question/choicequestion,
         * choice and game records. The game to clone is defined by the sourceGameId. Where the game is clone to (what user) is
         * determined by the destAspNetUsersId
         */
        public static Game CloneGame(Controller controller, ProbeDataContext db, long sourceGameId
                                     , string destAspNetUsersId, bool cloneCrossUsers, bool gamePlayInd)
        {
            //clone game - always in suspend mode so its not playable yet and it's editable
            Game   gSource = db.Game.Find(sourceGameId);
            string newName = GetClonedGameName(gSource.Name, destAspNetUsersId);
            string newCode = GetClonedCode(gSource.Code);

            Game gNew = new Game
            {
                AspNetUsersId      = destAspNetUsersId,
                GameTypeId         = gSource.GameTypeId,
                Name               = newName,
                Description        = gSource.Description,
                Code               = newCode,
                TestMode           = gSource.TestMode,
                Published          = (cloneCrossUsers) ? gSource.Published : false,
                SuspendMode        = (cloneCrossUsers) ? gSource.SuspendMode : false,
                ClientReportAccess = gSource.ClientReportAccess,
                StartDate          = gSource.StartDate,
                EndDate            = gSource.EndDate,
                GameUrl            = gSource.GameUrl,
                ACLId              = gSource.ACLId
            };

            db.Game.Add(gNew);

            //clone all gameconfigurations. should only pull the gameconfiguration records that exist. Any configuration that is still
            //using the default ConfigurationG record will not be pulled here. No need to clone this record.
            IList <GameConfigurationDTO> gameConfigurations = db.ConfigurationG
                                                              .Where(c => c.ConfigurationType != ConfigurationG.ProbeConfigurationType.GLOBAL)
                                                              .SelectMany(c => c.GameConfigurations.Where(gc => gc.Game.Id == sourceGameId),
                                                                          (c, gc) =>
                                                                          new GameConfigurationDTO
            {
                ConfigurationGId = gc.ConfigurationGId,
                Value            = gc.Value
            }
                                                                          ).ToList();

            foreach (GameConfigurationDTO gameConfiguration in gameConfigurations)
            {
                GameConfiguration clonedGameConfiguration = new GameConfiguration
                {
                    Game             = gNew,
                    ConfigurationGId = gameConfiguration.ConfigurationGId,
                    Value            = gameConfiguration.Value
                };

                db.GameConfiguration.Add(clonedGameConfiguration);
            }//foreach (GameConfiguration gameConfiguration in gameConfigurations)

            //clone gamequestions and question/choices for each gamequestion
            IList <GameQuestion> gameQuestions = db.GameQuestion.Where(gq => gq.GameId == sourceGameId).ToList();

            Dictionary <long, Dictionary <long, Choice> > questionXreference = new Dictionary <long, Dictionary <long, Choice> >();

            foreach (GameQuestion gameQuestion in gameQuestions)
            {
                //attach questions to game
                long           sourceQuestionId = gameQuestion.QuestionId;
                ChoiceQuestion clonedQuestion   = ProbeQuestion.CloneQuestion(controller, ref db, true, sourceQuestionId);

                GameQuestion clonedGameQuestion = new GameQuestion
                {
                    Game     = gNew,
                    Question = clonedQuestion,
                    OrderNbr = gameQuestion.OrderNbr,
                    Weight   = gameQuestion.Weight
                };

                db.GameQuestion.Add(clonedGameQuestion);

                //We are building a question - choice cross reference table for down the road. old question id -> (old choice id -> new choice)
                ChoiceQuestion            cqOrg            = db.ChoiceQuestion.Find(sourceQuestionId);
                Dictionary <long, Choice> choiceXreference = new Dictionary <long, Choice>();
                //Here we populate the choice X reference table. Associate the old choice with the new choice. Somebody might need this down the road.
                for (int i = 0; i < cqOrg.Choices.Count(); i++)
                {
                    //NEED A DATASTRUCTURE THAT ASSOCIATES ONE CHOICE WITH ANOTHER CHOICE
                    choiceXreference.Add(cqOrg.Choices.ElementAt(i).Id, clonedQuestion.Choices.ElementAt(i));
                }

                //DATASTRUCTURE that does hold old question id -> (old choice id -> new choice). This is to record the choices for the
                //new questions of the game associated with the new gamequestions
                questionXreference.Add(gameQuestion.QuestionId, choiceXreference);
            }//foreach (GameQuestion gameQuestion in gameQuestions)

            //if directed, then the game that is played will be cloned. Players, GameAnswers
            if (gamePlayInd)
            {
                //Get all the players for the game played
                IList <Player> players = db.Player.Where(p => p.GameId == sourceGameId).ToList();
                foreach (Player player in players)
                {
                    Player clonedPlayer = new Player
                    {
                        LastName         = player.LastName,
                        FirstName        = player.FirstName,
                        MiddleName       = player.MiddleName,
                        NickName         = player.NickName,
                        EmailAddr        = player.EmailAddr,
                        MobileNbr        = player.MobileNbr,
                        Sex              = player.Sex,
                        SubmitDate       = player.SubmitDate,
                        SubmitTime       = player.SubmitTime,
                        GameId           = gNew.Id,
                        Active           = player.Active,
                        PlayerGameReason = player.PlayerGameReason
                    };
                    db.Player.Add(clonedPlayer);

                    //Get all the OLD answers for the player in this iteration
                    IList <GameAnswer> gameanswers = db.GameAnswer.Where(ga => ga.PlayerId == player.Id).ToList();
                    foreach (GameAnswer gameAnswer in gameanswers)
                    {
                        GameAnswer clonedGameAnswer = new GameAnswer
                        {
                            Player = clonedPlayer,
                            Choice = questionXreference[gameAnswer.Choice.ChoiceQuestionId][gameAnswer.ChoiceId]
                        };

                        db.GameAnswer.Add(clonedGameAnswer);
                    } //foreach (GameAnswer gameAnswer in gameanswers)
                }     //foreach (Player player in players)
            }         //if (gamePlayInd)

            //THIS WILL RECORD NEW Game, GameConfiguration, GameQuestions, ChoiceQuestion/Question, Choice
            db.SaveChanges(controller.Request != null ? controller.Request.LogonUserIdentity.Name : null); //record all gameanswers for the new player

            return(gNew);
        }//CloneGame