예제 #1
0
        public ActionResult UpdateSurvey(SurveyViewModel model)
        {
            string[]      listQuestion     = Request.Form.GetValues("question");
            string[]      listAnwser       = Request.Form.GetValues("anwser1");
            string[]      listCountAnwser  = Request.Form.GetValues("count");
            List <string> listCountAnwsers = new List <string>(listCountAnwser);
            List <string> listQuestions    = new List <string>(listQuestion);
            List <string> listAnwsers      = new List <string>(listAnwser);

            string[]   member    = Request.Form.GetValues("people");
            string[]   privacy   = Request.Form.GetValues("privacy");
            string[]   priority  = Request.Form.GetValues("priority");
            string[]   status    = Request.Form.GetValues("status");
            List <int> listCount = new List <int>();

            List <List <string> > totalAnsertGroup = new List <List <string> >();

            for (int i = 0; i < (listCountAnwsers.Count) - 1; i++)
            {
                if (Int32.Parse(listCountAnwsers[i]) > Int32.Parse(listCountAnwsers[i + 1]))
                {
                    listCount.Add(Int32.Parse(listCountAnwsers[i]));
                }
            }
            listCount.Add(Int32.Parse((listCountAnwsers[(listCountAnwsers.Count) - 1])));
            for (int i = 0; i < listCount.Count; i++)
            {
                List <string> listAnwserGroup = new List <string>();
                int           countAneser     = 0;
                for (int j = 0; j < listAnwsers.Count; j++)
                {
                    if (countAneser != listCount[i] && countAneser < listCount[i])
                    {
                        listAnwserGroup.Add(listAnwsers[j]);
                        countAneser++;
                        listAnwsers.Remove(listAnwsers[j]);
                        j = j - 1;
                    }
                }
                totalAnsertGroup.Add(listAnwserGroup);
            }
            //
            int  a   = 0;
            Poll obj = PollService.FindById(model.Id);

            if (listAnwser.Length == 2)
            {
                obj.Answer1 = listAnwser[0];
                obj.Answer2 = listAnwser[1];
            }
            else if (listAnwser.Length == 3)
            {
                obj.Answer1 = listAnwser[0];
                obj.Answer2 = listAnwser[1];
                obj.Answer3 = listAnwser[2];
            }
            else if (listAnwser.Length == 4)
            {
                obj.Answer1 = listAnwser[0];
                obj.Answer2 = listAnwser[1];
                obj.Answer3 = listAnwser[2];
                obj.Answer4 = listAnwser[3];
            }
            else if (listAnwser.Length == 5)
            {
                obj.Answer1 = listAnwser[0];
                obj.Answer2 = listAnwser[1];
                obj.Answer3 = listAnwser[2];
                obj.Answer4 = listAnwser[3];
                if (listAnwser.Length == 5)
                {
                    obj.Answer5 = listAnwser[(listAnwser.Length) - 1];
                }
            }



            obj.Description = model.Title;
            obj.Question    = listQuestion[0];

            obj.EndDate     = (model.EndDate);
            obj.PublishDate = (model.PublishDate);
            obj.Status      = int.Parse(status[0]);
            obj.Mode        = int.Parse(member[0]);

            obj.Priority = int.Parse(priority[0]);
            PollService.UpdatePoll(obj);
            string[] listBlock = Request.Form.GetValues("block");
            if (listBlock != null)
            {
                List <string> listBlockrs = new List <string>(listBlock);

                List <BlockPoll> listBlockPollsDb = BlockPollService.FindByPollId(model.Id);

                List <int> listEditBlock = new List <int>();
                List <int> listLoad      = new List <int>();

                foreach (var VARIABLE in listBlockrs)
                {
                    listEditBlock.Add(blockService.FindBlockByName(VARIABLE).Id);
                }
                foreach (var aa in listBlockPollsDb)
                {
                    listLoad.Add(aa.BlockId);
                }
                if (listEditBlock.Count < listLoad.Count)
                {
                    List <int> list3 = listLoad.Except(listEditBlock).ToList();
                    // number of check less than before, delete
                    List <int> list5 = listEditBlock.Intersect(listLoad).ToList();
                    if (list5.Count == 0)
                    {
                        // uncheck A, check B
                        foreach (var object1 in listLoad)
                        {
                            BlockPoll BlockPoll = BlockPollService.FIndBlockPollByBlockIdPollId(object1, model.Id);
                            BlockPollService.DeleteBlockPoll(BlockPoll);
                        }
                        foreach (var obj1 in listEditBlock)
                        {
                            BlockPoll blockPoll = new BlockPoll();
                            blockPoll.BlockId = obj1;
                            blockPoll.PollId  = model.Id;
                            BlockPollService.AddBlockPoll(blockPoll);
                        }
                    }
                    else if (list5.Count != 0)
                    {
                        foreach (var object1 in list3)
                        {
                            BlockPoll BlockPoll = BlockPollService.FIndBlockPollByBlockIdPollId(object1, model.Id);
                            BlockPollService.DeleteBlockPoll(BlockPoll);
                        }
                    }
                }
                else if (listEditBlock.Count >= listLoad.Count)
                {
                    List <int> list4 = listEditBlock.Except(listLoad).ToList();
                    // number of check more than before,
                    List <int> list5 = listEditBlock.Intersect(listLoad).ToList();
                    if (list5.Count == 0)
                    {
                        // uncheck A, check B
                        foreach (var object1 in listLoad)
                        {
                            BlockPoll BlockPoll = BlockPollService.FIndBlockPollByBlockIdPollId(object1, model.Id);
                            BlockPollService.DeleteBlockPoll(BlockPoll);
                        }
                        foreach (var obj1 in listEditBlock)
                        {
                            BlockPoll blockPoll = new BlockPoll();
                            blockPoll.BlockId = obj1;
                            blockPoll.PollId  = model.Id;
                            BlockPollService.AddBlockPoll(blockPoll);
                        }
                    }
                    else if (list5.Count != 0)
                    {
                        foreach (var obj1 in list4)
                        {
                            BlockPoll blockPoll = new BlockPoll();
                            blockPoll.BlockId = obj1;
                            blockPoll.PollId  = model.Id;
                            BlockPollService.AddBlockPoll(blockPoll);
                        }
                    }
                }
            }
            else
            {
                List <BlockPoll> listBlockPollsDb = BlockPollService.FindByPollId(model.Id);


                List <int> listLoad = new List <int>();


                foreach (var aa in listBlockPollsDb)
                {
                    listLoad.Add(aa.BlockId);
                }

                List <int> list3 = listLoad.ToList();
                // number of check less than before, delete
                foreach (var object1 in list3)
                {
                    BlockPoll BlockPoll = BlockPollService.FIndBlockPollByBlockIdPollId(object1, model.Id);
                    BlockPollService.DeleteBlockPoll(BlockPoll);
                }
            }

            List <int> aaa = new List <int>();

            aaa.Add(1);
            aaa.Add(2);
            List <int> aaa1 = new List <int>();

            aaa1.Add(2);
            aaa1.Add(3);
            aaa1.Add(4);
            List <int> asd = aaa1.Except(aaa).ToList();

            return(RedirectToAction("DetailSurvey", new { PollId = obj.Id, alert = "Cập nhật thành công!" }));
        }