public Form CreateForm(FormIngestionViewModel model) { try { model.ColumnHeadings = GetColumnHeadings(model.DataSheet); string lastColName = GetColumnName(model.ColumnHeadings.Count - 1); string dataRange = string.Format("'{0}'!A2:{1}", model.DataSheet, lastColName); var request = SheetsService.Spreadsheets.Values.Get(Spreadsheet.SpreadsheetId, dataRange); var result = request.Execute(); if (string.IsNullOrEmpty(model.ListIdColumn)) { throw new Exception("Please identify the List ID column"); } int listIdCol = model.ColumnHeadings.IndexOf(model.ListIdColumn); if (string.IsNullOrEmpty(model.BlockIdColumn)) { throw new Exception("Please identify the Block ID column"); } int blockIdCol = model.ColumnHeadings.IndexOf(model.BlockIdColumn); List <int> preContextColIndicies = model.PreContextColumns.Select(s => model.ColumnHeadings.IndexOf(s)).ToList(); if (string.IsNullOrEmpty(model.QuestionColumn)) { throw new Exception("Please idenfify the Question column"); } int questionCol = model.ColumnHeadings.IndexOf(model.QuestionColumn); if (string.IsNullOrEmpty(model.AnswerTypeColumn)) { throw new Exception("Please idenfify the Answer Type column"); } int answerTypeCol = model.ColumnHeadings.IndexOf(model.AnswerTypeColumn); if (string.IsNullOrEmpty(model.AnswerOptionsColumn)) { throw new Exception("Please idenfify the Answer Options column"); } int answerOptionsCol = model.ColumnHeadings.IndexOf(model.AnswerOptionsColumn); if (string.IsNullOrEmpty(model.QuestionIdColumn)) { throw new Exception("Please idenfify the Question ID column"); } int questionIdCol = model.ColumnHeadings.IndexOf(model.QuestionIdColumn); if (string.IsNullOrEmpty(model.IsRequiredIndicatorColumn)) { throw new Exception("Please idenfify the Required Question Indicator column"); } int isRequiredCol = model.ColumnHeadings.IndexOf(model.IsRequiredIndicatorColumn); if (string.IsNullOrEmpty(model.AudioFileColumn)) { throw new Exception("Please idenfify the Audio File column"); } int audioFileCol = model.ColumnHeadings.IndexOf(model.AudioFileColumn); string urlBase = string.IsNullOrEmpty(model.MediaFolderUrl) ? "/media/" : model.MediaFolderUrl.TrimEnd(new char[] { '/' }) + "/"; //Iterating through all the data and creating numbered lists and list of blocks, list of headers and list of footers //The key of each dictionary element represents the list where each block, header or foter belongs to. List <CompositeFormField> listFields = new List <CompositeFormField>(); Dictionary <int, List <CompositeFormField> > blockFieldSets = new Dictionary <int, List <CompositeFormField> >(); Dictionary <int, CompositeFormField> headers = new Dictionary <int, CompositeFormField>(); Dictionary <int, CompositeFormField> footers = new Dictionary <int, CompositeFormField>(); var uniqueListIds = result.Values.Select(x => x[listIdCol] as string).Distinct().ToList(); foreach (var listId in uniqueListIds.Where(x => x != "*").Select(x => int.Parse(x))) { listFields.Add(new CompositeFormField() { Name = "Page " + listId, Page = listId }); blockFieldSets.Add(listId, new List <CompositeFormField>()); headers.Add(listId, new CompositeFormField() { Name = "Footer" }); footers.Add(listId, new CompositeFormField() { Name = "Header" }); } //Iterating through all the data and creating blocks, headers, footers and questions for (int index = 0; index < result.Values.Count; ++index) { var row = result.Values[index]; List <string> values = row.Select(s => s.ToString().Trim()).ToList(); //Creating the question List <string> preContexts = preContextColIndicies.Select(i => values[i]).ToList(); string questionText = values[questionCol]; string questionId = values[questionIdCol]; string answerType = values[answerTypeCol]; string answerOptions = answerOptionsCol < values.Count ? values[answerOptionsCol] : ""; if (string.IsNullOrEmpty(answerType)) { if (string.IsNullOrEmpty(answerOptions)) { answerType = "ShortText"; } else { answerType = "RadioButtonSet"; } } /// /// Each precontext and question are represented by a composite field inside the selected block. /// CompositeFormField surveyItem = new CompositeFormField() { Name = "Question " + questionId }; foreach (string pc in preContexts) { if (!string.IsNullOrEmpty(pc.Trim())) { HtmlField html = new HtmlField() { Name = "Description" }; html.SetDescription(pc); surveyItem.InsertChildElement("./fields", html.Data); } } //Adding the question text HtmlField questionTextField = new HtmlField() { Name = "Text" }; questionTextField.SetDescription(questionText); surveyItem.InsertChildElement("./fields", questionTextField.Data); //Adding the media file string mediaFile = audioFileCol < values.Count ? values[audioFileCol].TrimStart(new char[] { '/' }) : null; if (!string.IsNullOrEmpty(mediaFile)) { ExternalMediaField mf = new ExternalMediaField() { Name = "Media", Source = urlBase + mediaFile, MediaType = Models.Data.CFDataFile.MimeType.Audio, PlayOnce = true, PlayOnShow = true }; surveyItem.InsertChildElement("./fields", mf.Data); } FormField question = null; if (answerType == "ShortText") { question = new TextField(); } else if (answerType == "Number") { question = new NumberField(); } else if (answerType == "Scale") { List <string> options = answerOptions.Split(new char[] { '|' }).Select(s => s.Trim()).ToList(); string[] begin = options[0].Split(new char[] { ':' }); string[] end = options[1].Split(new char[] { ':' }); question = new SliderField() { Min = begin.Length > 0 ? int.Parse(begin[0]) : 0, Max = end.Length > 0 ? int.Parse(end[0]) : 1, MinLabel = begin.Length > 1 ? begin[1] : "", MaxLabel = end.Length > 1 ? end[1] : "" }; } else if (answerType == "RadioButtonSet" || answerType == "DropDown" || answerType == "CheckBoxSet") { List <string> optionStrings = answerOptions.Split(new char[] { '|' }).Select(s => s.Trim()).ToList(); List <Option> options = new List <Option>(); foreach (var optVal in optionStrings) { Option opt = new Option(); opt.Value = new List <TextValue>() { new TextValue() { Value = optVal, LanguageCode = "en" } }; options.Add(opt); } if (answerType == "RadioButtonSet") { question = new RadioButtonSet() { Options = options } } ; else if (answerType == "CheckBoxSet") { question = new CheckBoxSet() { Options = options } } ; else { question = new DropDownMenu() { Options = options } }; } else if (answerType == "TextArea") { question = new TextArea(); } else if (answerType == "Attachment") { question = new Attachment(); } ////else if(answerType == "DropDown") ////{ //// List<string> optionStrings = answerOptions.Split(new char[] { '|' }).Select(s => s.Trim()).ToList(); //// List<Option> options = new List<Option>(); //// foreach (var optVal in optionStrings) //// { //// Option opt = new Option(); //// opt.Value = new List<TextValue>() { new TextValue() { Value = optVal, LanguageCode = "en" } }; //// options.Add(opt); //// } //// question = new DropDownMenu() { Options = options }; ////} else { throw new Exception(string.Format("Answer type \"{0}\" is not implemented in survey form ingestion.", answerType)); } question.Name = "Answer"; question.ReferenceLabel = questionId; question.IsRequired = values[isRequiredCol] == "1"; question.SetAttribute("question-id", values[questionIdCol].ToString()); surveyItem.InsertChildElement("./fields", question.Data); //Adding the question to the header, footer or the appropriate block of the //selected list (or all the lists, if list number is *) var listIdStr = values[listIdCol]; string blockIdStr = values[blockIdCol].ToUpper(); var applicableLists = listIdStr == "*" ? listFields : listFields.Where(x => x.Page == int.Parse(listIdStr)); foreach (var list in applicableLists) { if (blockIdStr == "H") { headers[list.Page].InsertChildElement("./fields", surveyItem.Data); } else if (blockIdStr == "F") { footers[list.Page].InsertChildElement("./fields", surveyItem.Data); } else { var blockSet = blockFieldSets[list.Page]; var block = blockSet.Where(x => x.Page == int.Parse(blockIdStr)).FirstOrDefault(); if (block == null) { block = new CompositeFormField() { Name = "Block " + blockIdStr, Page = int.Parse(blockIdStr) }; blockSet.Add(block); } block.InsertChildElement("./fields", surveyItem.Data); } } }//END: foreach (var row in result.Values) /// /// By this point, we have all "lists" in the listFields array and all "blocks" corresponding to /// each of those lists in the blockFieldSets dictionary. /// //Inserting all blocks into each list entry foreach (var list in listFields) { List <CompositeFormField> blocks = blockFieldSets[list.Page]; foreach (var block in blocks) { list.InsertChildElement("./fields", block.Data); } if (headers[list.Page].Fields.Any()) { list.InsertChildElement("./header", headers[list.Page].Data); } if (footers[list.Page].Fields.Any()) { list.InsertChildElement("./footer", footers[list.Page].Data); } } Form form = new Form(); form.SetName(model.FormName); form.SetDescription(model.FormDescription); foreach (var field in listFields) { form.InsertChildElement("./fields", field.Data); } form.Serialize(); return(form); ////// //If the block number is H or F, the question goes to the header or the footer section of a block. ////// if (blockIdCol.HasValue && values[blockIdCol.Value].ToUpper() == "H") ////// { ////// string lsitNum = listIdCol.HasValue ? values[listIdCol.Value] : "0"; ////// if (!headers.ContainsKey(lsitNum)) ////// headers.Add(lsitNum, new List<CompositeFormField>()); ////// } ////// //Otherwise, this is a regular question. ////// string listNum = listIdCol.HasValue ? values[listIdCol.Value] : "0"; ////// string blockNum = blockIdCol.HasValue ? values[blockIdCol.Value] : "0"; ////// List<string> preContexts = preContextColIndicies.Select(i => values[i]).ToList(); ////// string questionText = values[questionCol]; ////// string answerType = answerOptionsCol.HasValue ? values[answerOptionsCol.Value] : ""; ////// string answerOptions = answerOptionsCol.HasValue ? values[answerOptionsCol.Value] : ""; ////// if (string.IsNullOrEmpty(answerType)) ////// { ////// if (string.IsNullOrEmpty(answerOptions)) ////// answerType = "TextField"; ////// else ////// answerType = "RadioButtonSet"; ////// } ////// /// ////// /// Each list is represented by a composite field at the top level of the form. ////// ///The list number is stored in the "page" property of the field. ////// ///Get the composite field representing the given list number, or create a new one if it doesn't exist ////// /// ////// CompositeFormField list = listFields.Where(field => field.Page == int.Parse(listNum)).FirstOrDefault(); ////// if(list == null) ////// { ////// list = new CompositeFormField() { Page = int.Parse(listNum) }; ////// listFields.Add(list); ////// blockFieldSets.Add(listNum, new List<CompositeFormField>()); //Placehoder for blocks of this list. ////// } ////// /// ////// /// Each block is represented by a composite field in the "list". ////// ///The block number is stored in the "page" propoerty of this composite field. ////// ///Get the composite field representinhg the give block number from the selected list. pr create a new one if it doesn't exist ////// /// ////// List<CompositeFormField> blocks = blockFieldSets[listNum]; ////// CompositeFormField block = blocks.Where(field => field.Page == int.Parse(blockNum)).FirstOrDefault(); ////// if(block == null) ////// { ////// block = new CompositeFormField() { Page = int.Parse(blockNum) }; ////// blocks.Add(block); ////// } ////// /// ////// /// Each precontext and question are represented by a composite field inside the selected block. ////// /// ////// CompositeFormField surveyItem = new CompositeFormField(); ////// foreach(string pc in preContexts) ////// { ////// if(!string.IsNullOrEmpty(pc.Trim())) ////// { ////// HtmlField html = new HtmlField(); ////// html.SetDescription(pc); ////// surveyItem.InsertChildElement("./fields", html.Data); ////// } ////// } ////// FormField question = null; ////// if (answerType == "TextField") ////// question = new TextField(); ////// else if (answerOptions == "RadioButtonSet") ////// { ////// List<string> optionStrings = answerOptions.Split(new char[] { '\n' }).Select(s => s.Trim()).ToList(); ////// List<Option> options = new List<Option>(); ////// foreach (var optVal in optionStrings) ////// { ////// Option opt = new Option(); ////// opt.Value = new List<TextValue>() { new TextValue() { Value = optVal } }; ////// options.Add(opt); ////// } ////// question = new RadioButtonSet() ////// { ////// Options = options ////// }; ////// } ////// else ////// throw new Exception(string.Format("Answer type \"{0}\" is not implemented in survey form ingestion.")); ////// question.SetName(questionText); ////// surveyItem.InsertChildElement("./fields", question.Data); ////// block.InsertChildElement("./fields", surveyItem.Data); //////} //////// /// //////// /// By this point, we have all "lists" in the listFields array and all "blocks" corresponding to //////// /// each of those lists in the blockFieldSets dictionary. //////// /// //////// //Inserting all blocks into each list entry //////// foreach(var list in listFields) //////// { //////// List<CompositeFormField> blocks = blockFieldSets[list.Page.ToString()]; //////// foreach (var block in blocks) //////// list.InsertChildElement("./fields", block.Data); //////// } //////// Form form = new Form(); //////// form.SetName(model.FormName); //////// form.SetDescription(model.FormDescription); //////// foreach (var field in listFields) //////// form.InsertChildElement("./fields", field.Data); //////// form.Serialize(); //////// return form; } catch (Exception ex) { model.Error = ex.Message; } return(null); }
public override void Update(GameTime gameTime, Vector2 mousePosition, Vector2 parentPosition) { if (GUIControl.UIElementEngaged && !IsEngaged) { return; } //Break Engagement if (IsEngaged && !GUIControl.IsLMBPressed()) { GUIControl.UIElementEngaged = false; IsEngaged = false; } if (!GUIControl.IsLMBPressed()) { return; } Vector2 bound1 = Position + parentPosition + _textBlock.Dimensions * Vector2.UnitY /*+ SliderIndicatorBorder*Vector2.UnitX*/; Vector2 bound2 = bound1 + SliderDimensions /* - 2*SliderIndicatorBorder * Vector2.UnitX*/; if (mousePosition.X >= bound1.X && mousePosition.Y >= bound1.Y && mousePosition.X < bound2.X && mousePosition.Y < bound2.Y + 1) { GUIControl.UIElementEngaged = true; IsEngaged = true; } if (IsEngaged) { GUIControl.UIWasUsed = true; float lowerx = bound1.X + SliderIndicatorBorder; float upperx = bound2.X - SliderIndicatorBorder; _sliderPercent = MathHelper.Clamp((mousePosition.X - lowerx) / (upperx - lowerx), 0, 1); _sliderValue = (int)Math.Round(_sliderPercent * (float)(MaxValue - MinValue) + MinValue) / StepSize * StepSize; UpdateText(); _sliderPercent = (float)(_sliderValue - MinValueInt) / (MaxValueInt - MinValueInt); if (SliderObject != null) { if (SliderField != null) { SliderField.SetValue(SliderObject, SliderValue, BindingFlags.Public, null, null); } else if (SliderProperty != null) { SliderProperty.SetValue(SliderObject, SliderValue); } } else { if (SliderField != null) { SliderField.SetValue(null, SliderValue, BindingFlags.Static | BindingFlags.Public, null, null); } else if (SliderProperty != null) { SliderProperty.SetValue(null, SliderValue); } } } }
/// <summary> /// Sets the value of the slider belonging to the given field constant. /// </summary> /// <param name="field"></param> /// <param name="value"></param> public void SetSliderData(SliderField field, float value) { sliderData[(int)field % ENUM_INTERVAL] = value; }
public new void SetField(Object obj, string field) { SliderObject = obj; SliderField = obj.GetType().GetField(field); SliderValue = (int)SliderField.GetValue(obj); }
/// <summary> /// Retrieves the value of the slider belonging to the given field constant. /// </summary> /// <param name="field"></param> /// <returns></returns> public float GetSliderData(SliderField field) { return(sliderData[(int)field % ENUM_INTERVAL]); }