public T1 GetView(ApplicationDbContext db)
        {
            SurveyUserData       model = Parameters as SurveyUserData;
            SurveyUserDataReturn surveyUserDataReturn = new SurveyUserDataReturn();

            int id               = StringToValue.ParseInt(model.Id);
            int surveyPartId     = StringToValue.ParseInt(model.SectionId);
            int surveyQuestionId = StringToValue.ParseInt(model.QuestionId);

            SurveyPart             surveyPart             = db.T_SurveyPart.Where(sp => sp.SurveyId == id && sp.Id == surveyPartId).FirstOrDefault();
            SurveyQuestion         surveyQuestion         = db.T_SurveyQuestion.Where(sq => sq.SurveyPartId == surveyPart.Id && sq.Id > surveyQuestionId).FirstOrDefault();
            SurveyPartTemplate     surveyPartTemplate     = db.T_SurveyPartTemplate.Find(surveyPart.SurveyPartTemplateId);
            SurveyQuestionTemplate surveyQuestionTemplate = db.T_SurveyQuestionTemplate.Find(surveyQuestion.SurveyQuestionTemplateId);

            surveyUserDataReturn.TotalSections  = db.T_SurveyPart.Where(sp => sp.SurveyId == id).Count();
            surveyUserDataReturn.TotalQuestions = db.T_SurveyPart
                                                  .Join(db.T_SurveyQuestion,
                                                        sp => sp.Id, sq => sq.SurveyPartId,
                                                        (sp, sq) => sp).Where(sp => sp.SurveyId == id).Count();
            surveyUserDataReturn.TotalSectionQuestions   = db.T_SurveyQuestion.Where(sq => sq.SurveyPartId == surveyPart.Id).Count();
            surveyUserDataReturn.QuestionSectionNo       = db.T_SurveyQuestion.Where(sq => sq.SurveyPartId == surveyPart.Id && sq.Id <= surveyQuestion.Id).Count();
            surveyUserDataReturn.SectionName             = surveyPartTemplate.Name;
            surveyUserDataReturn.SectionTitle            = surveyPartTemplate.SummaryTitle;
            surveyUserDataReturn.SectionId               = surveyPart.Id;
            surveyUserDataReturn.QuestionId              = surveyQuestion.Id;
            surveyUserDataReturn.QuestionName            = surveyQuestionTemplate.Name;
            surveyUserDataReturn.QuestionDescription     = surveyQuestionTemplate.Definition;
            surveyUserDataReturn.QuestionType            = surveyQuestionTemplate.QuestionType;
            surveyUserDataReturn.QuestionEmployeeScore   = surveyQuestion.EmployeeScore;
            surveyUserDataReturn.QuestionEmployeeComment = surveyQuestion.EmployeeComment;
            surveyUserDataReturn.SectionNo               = db.T_SurveyPart.Where(sp => sp.SurveyId == id && sp.Id <= surveyPart.Id).Count();

            return(surveyUserDataReturn as T1);
        }
        public void Save(T model, ApplicationDbContext db)
        {
            SurveyPartData modelData      = model as SurveyPartData;
            int            id             = StringToValue.ParseInt(modelData.Id);
            SurveyTemplate surveyTemplate = db.T_SurveyTemplate.Find(id);

            if (surveyTemplate != null)
            {
                SurveyTemplate newSurveyTemplate = new SurveyTemplate()
                {
                    Name                = modelData.Name,
                    SurveyDate          = DateTime.Now,
                    PublishDate         = new DateTime(1900, 1, 1),
                    SurveyPartTemplates = new List <SurveyPartTemplate>()
                };

                db.Entry(surveyTemplate).Collection(p => p.SurveyPartTemplates).Load();
                if (surveyTemplate.SurveyPartTemplates != null)
                {
                    foreach (SurveyPartTemplate surveyPartTemplate in surveyTemplate.SurveyPartTemplates)
                    {
                        SurveyPartTemplate newSurveyPartTemplate = new SurveyPartTemplate()
                        {
                            Name                    = surveyPartTemplate.Name,
                            SummaryTitle            = surveyPartTemplate.SummaryTitle,
                            SurveyQuestionTemplates = new List <SurveyQuestionTemplate>()
                        };

                        newSurveyTemplate.SurveyPartTemplates.Add(newSurveyPartTemplate);

                        db.Entry(surveyPartTemplate).Collection(p => p.SurveyQuestionTemplates).Load();
                        foreach (SurveyQuestionTemplate surveyQuestionTemplate in surveyPartTemplate.SurveyQuestionTemplates)
                        {
                            SurveyQuestionTemplate newSurveyQuestionTemplate = new SurveyQuestionTemplate()
                            {
                                Name         = surveyQuestionTemplate.Name,
                                Definition   = surveyQuestionTemplate.Definition,
                                QuestionType = surveyQuestionTemplate.QuestionType
                            };
                            newSurveyPartTemplate.SurveyQuestionTemplates.Add(newSurveyQuestionTemplate);
                        }
                    }
                }
                db.T_SurveyTemplate.Add(newSurveyTemplate);
                db.SaveChanges();
            }
        }
Beispiel #3
0
        public void Save(T model, ApplicationDbContext db)
        {
            SurveyPartData modelData = model as SurveyPartData;

            int      id       = StringToValue.ParseInt(modelData.Id);
            DateTime deadline = CalculateDate.StringToDate(modelData.Summary, ".");

            SurveyTemplate surveyTemplate = db.T_SurveyTemplate.Find(id);

            if (surveyTemplate != null)
            {
                surveyTemplate.PublishDate = DateTime.Now;

                List <Survey>   surveys   = new List <Survey>();
                List <Employee> employees = db.T_Employees.ToList();

                db.Entry(surveyTemplate).Collection(p => p.SurveyPartTemplates).Load();
                List <SurveyPartTemplate> surveyPartTemplates = surveyTemplate.SurveyPartTemplates;
                if (surveyPartTemplates == null)
                {
                    surveyPartTemplates = new List <SurveyPartTemplate>();
                }


                foreach (Employee employee in employees)
                {
                    Team   team   = db.T_Teams.Find(employee.TeamId);
                    Survey survey = new Survey()
                    {
                        EmployeeId           = employee.Id,
                        ManagerId            = team.ManagerId,
                        SurveyTemplateId     = id,
                        SurveyStatusId       = 1,
                        Name                 = modelData.Name,
                        SurveyDate           = DateTime.Now,
                        SurveyDadline        = deadline,
                        EmployeeCompleted    = false,
                        ManagerCompleted     = false,
                        CompliteEmployeeDate = new DateTime(1901, 1, 1),
                        CompliteManagerDate  = new DateTime(1901, 1, 1),
                        SurveyParts          = new List <SurveyPart>()
                    };

                    if (surveyTemplate.SurveyPartTemplates != null)
                    {
                        foreach (SurveyPartTemplate surveyPartTemplate in surveyTemplate.SurveyPartTemplates)
                        {
                            SurveyPart surveyPart = new SurveyPart()
                            {
                                SurveyPartTemplateId = surveyPartTemplate.Id,
                                SurveyQuestions      = new List <SurveyQuestion>()
                            };

                            survey.SurveyParts.Add(surveyPart);

                            db.Entry(surveyPartTemplate).Collection(p => p.SurveyQuestionTemplates).Load();
                            foreach (SurveyQuestionTemplate surveyQuestionTemplate in surveyPartTemplate.SurveyQuestionTemplates)
                            {
                                SurveyQuestion surveyQuestion = new SurveyQuestion()
                                {
                                    SurveyQuestionTemplateId = surveyQuestionTemplate.Id
                                };
                                surveyPart.SurveyQuestions.Add(surveyQuestion);
                            }
                        }
                    }
                    surveys.Add(survey);
                }
                db.T_Survey.AddRange(surveys);
                db.SaveChanges();
            }
        }