/// <summary> /// Removes a page from a survey /// </summary> /// <param name="view"></param> /// <param name="pageId"></param> public void RemoveSurveyPage(SurveyView view, int pageId) { List <QuestionPartView> pages = view.QuestionPartViews as List <QuestionPartView>; QuestionPartView toDelete = null; int pageIndex = Int32.MaxValue; for (int i = 0; i < pages.Count; i++) { if (pages[i].Order > pageIndex) { pages[i].Order--; } else if (pages[i].Id == pageId) { toDelete = pages[i]; pageIndex = toDelete.Order; } } //remove children question part views (to trigger question part deletes) var pageData = this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructure(toDelete.Id); var childIds = pageData.QuestionPartViewChildren.Select(q => q.Id).ToList(); childIds.ForEach(id => this.RemoveQuestionPartView(pageData, id, false)); view.QuestionPartViews.Remove(toDelete); if (toDelete.CATIDependent != null) { var catiView = this._unitOfWork.SurveyViews.GetSurveyViewWithPagesStructure(view.SurveyId, "CATI"); this.RemoveSurveyPage(catiView, toDelete.CATIDependent.Id); } }
/// <summary> /// /// </summary> /// <param name="view"></param> /// <param name="part"></param> /// <param name="definition"></param> /// <param name="position"></param> public QuestionPartView AddQuestion(SurveyView view, QuestionTypeDefinition definition, int position = -1) { QuestionPartView qpv = new QuestionPartView(); qpv.QuestionPart = new QuestionPart() { QuestionType = definition.TypeName }; if (position < 0) { view.QuestionPartViews.Add(qpv); } else { ((List <QuestionPartView>)view.QuestionPartViews).Insert(position, qpv); } //add more question part views if (definition.QuestionPartSlots.Count > 0) { foreach (var slot in definition.QuestionPartSlots) { var questionSlot = new QuestionPartView(); questionSlot.ParentView = qpv; questionSlot.QuestionPart = new QuestionPart() { QuestionType = definition.TypeName }; qpv.QuestionPartViewChildren.Add(questionSlot); } } return(qpv); }
/// <summary> /// Reorder question within question part /// </summary> /// <param name="questionPartView"></param> /// <param name="newOrder"></param> public void ReOrderQuestions(QuestionPartView questionPartView, List <QuestionPartView> newOrder) { Dictionary <int, int> newOrderDict = newOrder.ToDictionary(r => r.Id, r => r.Order); foreach (var qpartView in questionPartView.QuestionPartViewChildren) { qpartView.Order = newOrderDict[qpartView.Id]; } }
/// <summary> /// /// </summary> /// <param name="qpv"></param> /// <param name="text"></param> /// <param name="language"></param> public void SetQuestionPartViewLabel(QuestionPartView qpv, string text, string language = null) { qpv.Labels[language] = new QuestionPartViewLabel() { Language = language ?? "en", Value = text, QuestionPartView = qpv }; }
public async Task <IActionResult> AddPage(int surveyId, string surveyViewName, string initialLanguage, [FromBody] SBQuestionPartViewViewModel pageInfo) { //test if (ModelState.IsValid) { var survey = await this._unitOfWork.Surveys.GetAsync(surveyId); if (survey.Owner == this.User.Identity.Name || await HasModifySurveyPermissions(surveyId)) { var surveyView = await this._unitOfWork.SurveyViews.GetSurveyViewWithPagesStructureAsync(surveyId, surveyViewName); QuestionPartView newPage = Mapper.Map <QuestionPartView> (pageInfo); newPage.Labels = new LabelCollection <QuestionPartViewLabel> () { new QuestionPartViewLabel() { Language = initialLanguage, Value = pageInfo.Label.Value } }; this._surveyBuilderService.AddSurveyPage(surveyView, newPage); if (surveyViewName != "CATI") { var catiView = await this._unitOfWork.SurveyViews.GetSurveyViewWithPagesStructureAsync(surveyId, "CATI"); if (catiView != null) { newPage.CATIDependent = new QuestionPartView { Icon = newPage.Icon, Order = newPage.Order, Labels = new LabelCollection <QuestionPartViewLabel> () { new QuestionPartViewLabel() { Language = initialLanguage, Value = pageInfo.Label.Value } } }; this._surveyBuilderService.AddSurveyPage(catiView, newPage.CATIDependent); } } await this._unitOfWork.SaveChangesAsync(); return(new OkResult()); } else { return(BadRequest("Insufficient permissions.")); } } return(BadRequest(ModelState)); }
/// <summary> /// Adds a question to a page/question part /// </summary> /// <param name="view"></param> /// <param name="newPage"></param> public void AddQuestionPartView(QuestionPartView ParentQuestionPartView, QuestionPartView ChildQuestionPartView) { //update orders for existing questions foreach (var question in ParentQuestionPartView.QuestionPartViewChildren) { if (question.Order >= ChildQuestionPartView.Order) { question.Order++; } } ParentQuestionPartView.QuestionPartViewChildren.Add(ChildQuestionPartView); }
/// <summary> /// Removes a question/question part from a survey /// </summary> /// <param name="questionPartView"></param> /// <param name="childQuestionPartViewId"></param> public void RemoveQuestionPartView(QuestionPartView questionPartView, int childQuestionPartViewId, bool transfer) { if (questionPartView != null) { var childQuestions = questionPartView.QuestionPartViewChildren.OrderBy(q => q.Order); QuestionPartView toDelete = null; int questionIndex = Int32.MaxValue; foreach (var childQuestion in childQuestions) { if (childQuestion.Order > questionIndex) { childQuestion.Order--; } else if (childQuestion.Id == childQuestionPartViewId) { toDelete = childQuestion; questionIndex = toDelete.Order; } } questionPartView.QuestionPartViewChildren.Remove(toDelete); //delete question part if no other part and not a transfer if (toDelete.QuestionPart != null && !transfer) { int priorParentViewCount = this._unitOfWork.QuestionParts.GetNumberOfParentViews(toDelete.QuestionPart.Id); if (priorParentViewCount == 1) { this._unitOfWork.QuestionParts.Remove(toDelete.QuestionPart); } } else if (toDelete.QuestionPart == null && !transfer) { //repeat for children (calling recursively) var deleteStructure = this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructure(toDelete.Id); var children = deleteStructure.QuestionPartViewChildren.ToList(); foreach (var child in children) { this.RemoveQuestionPartView(toDelete, child.Id, false); } } if (toDelete.CATIDependent != null) { this.RemoveQuestionPartView(questionPartView.CATIDependent, toDelete.CATIDependent.Id, transfer); } } }
public void UpdateQuestionPartViewOptions(QuestionPartView qpv, bool isOptional, bool isHousehold, string repeatSourceQuestionName, string icon) { if (qpv.IsHousehold && repeatSourceQuestionName != null) { throw new ArgumentException("Section cannot be both marked as household and repeat"); } qpv.IsHousehold = isHousehold; qpv.IsOptional = isOptional; if (repeatSourceQuestionName != null) { int sourceQuestionId = int.Parse(repeatSourceQuestionName.Split('~').Last()); qpv.RepeatSource = this._unitOfWork.QuestionParts.Get(sourceQuestionId); } else { qpv.RepeatSource = null; } qpv.Icon = icon; }
/// <summary> /// Adds a question part to the specified SurveyView - this creates a new QuestionPartView from the part /// </summary> /// <param name="view"></param> /// <param name="part"></param> /// <param name="position">0-index order, position of the qustion</param> /// <returns>A reference to the created QuestionPartView</returns> public QuestionPartView AddQuestionPart(SurveyView view, QuestionPart part, QuestionTypeDefinition definition, int position = -1) { QuestionPartView questionPartView = new QuestionPartView { QuestionPart = part }; if (position >= 0) { questionPartView.Order = position; (view.QuestionPartViews as List <QuestionPartView>)?.Insert(position, questionPartView); } else { (view.QuestionPartViews as List <QuestionPartView>)?.Add(questionPartView); questionPartView.Order = view.QuestionPartViews.Count - 1; } return(questionPartView); }
public void DuplicateSurveyViewStructure(SurveyView sourceView, SurveyView targetView, string language) { bool structureExists = true; bool structureAndLanguageExists = false; //deal with base labels (welcome, t&c, thank you) if (targetView.WelcomePageLabels == null) { structureExists = false; targetView.WelcomePageLabels = new LabelCollection <WelcomePageLabel>(); targetView.TermsAndConditionsLabels = new LabelCollection <TermsAndConditionsPageLabel>(); targetView.ThankYouPageLabels = new LabelCollection <ThankYouPageLabel>(); } else if (targetView.WelcomePageLabels[language] != null) { structureAndLanguageExists = true; } if (!structureAndLanguageExists) { targetView.WelcomePageLabels[language] = new WelcomePageLabel { Value = null }; targetView.TermsAndConditionsLabels[language] = new TermsAndConditionsPageLabel { Value = null }; targetView.ThankYouPageLabels[language] = new ThankYouPageLabel { Value = null }; // if structure exists, just create new labels under the language if (structureExists) { foreach (var sourcePage in sourceView.QuestionPartViews) { var page = sourcePage.CATIDependent; page.Labels[language] = new QuestionPartViewLabel { Value = sourcePage.Labels[language].Value }; foreach (var question in page.QuestionPartViewChildren) { question.Labels[language] = new QuestionPartViewLabel { Value = null }; foreach (var subQuestion in question.QuestionPartViewChildren) { subQuestion.Labels[language] = new QuestionPartViewLabel { Value = null }; } } } } else { foreach (var page in sourceView.QuestionPartViews) { QuestionPartView targetPage = new QuestionPartView { Order = page.Order, Icon = page.Icon }; page.CATIDependent = targetPage; targetView.QuestionPartViews.Add(targetPage); targetPage.Labels[language] = new QuestionPartViewLabel { Value = page.Labels[language].Value }; foreach (var question in page.QuestionPartViewChildren) { QuestionPartView targetQuestion = new QuestionPartView { Order = question.Order, IsOptional = question.IsOptional, IsHousehold = question.IsHousehold, RepeatSource = question.RepeatSource, QuestionPart = question.QuestionPart }; question.CATIDependent = targetQuestion; targetPage.QuestionPartViewChildren.Add(targetQuestion); targetQuestion.Labels[language] = new QuestionPartViewLabel { Value = null }; foreach (var subQuestion in question.QuestionPartViewChildren) { QuestionPartView targetSubQuestion = new QuestionPartView { Order = subQuestion.Order, IsOptional = subQuestion.IsOptional, RepeatSource = subQuestion.RepeatSource, QuestionPart = subQuestion.QuestionPart }; subQuestion.CATIDependent = targetSubQuestion; targetQuestion.QuestionPartViewChildren.Add(targetSubQuestion); targetSubQuestion.Labels[language] = new QuestionPartViewLabel { Value = null }; } } } } } }
/// <summary> /// Adds a page to a survey /// </summary> /// <param name="view"></param> /// <param name="newPage"></param> public void AddSurveyPage(SurveyView view, QuestionPartView newPage) { view.QuestionPartViews.Add(newPage); newPage.Order = view.QuestionPartViews.Count - 1; }