/// <summary> /// Creates a Moq QuestionTypeManager /// </summary> /// <returns></returns> public static QuestionTypeManager CreateQuestionTypeManager() { var mock = new Mock <QuestionTypeManager>(null, CreateLoggerFactory()); Dictionary <string, QuestionTypeDefinition> definitions = new Dictionary <string, QuestionTypeDefinition>(); var mockSurveyQuestion = new Mock <ISurveyQuestion>(); mockSurveyQuestion.SetupProperty(p => p.TypeName, "TestQuestionType1"); mockSurveyQuestion.SetupProperty(p => p.Icon, "IconType1"); TypeDescriptor.AddAttributes(mockSurveyQuestion.Object, new QuestionConfigurationAttribute(ConfigurationValueType.Integer) { Description = "Description", }); var questionAttribute = new SurveyQuestionAttribute(QuestionResponseType.Integer) { CustomBuilderView = null, }; QuestionTypeDefinition definition = new QuestionTypeDefinition(mockSurveyQuestion.Object, questionAttribute); definitions[definition.TypeName] = definition; mock.SetupProperty(p => p.QuestionTypeDefinitions, definitions); return(mock.Object); }
/// <summary> /// </summary> /// <param name="questionType"></param> /// <param name="attribute"></param> /// <param name="sourceAssembly"></param> private QuestionTypeDefinition CreateQuestionTypeDefinition(Type questionType, SurveyQuestionAttribute attribute, Assembly sourceAssembly, Dictionary <string, Dictionary <string, string> > locales) { var typeDefinition = new QuestionTypeDefinition(Activator.CreateInstance(questionType) as ISurveyQuestion, attribute); var configurations = ReadQuestionConfigurationData(questionType, sourceAssembly); typeDefinition.QuestionConfigurations = configurations; var parameterOptions = ReadQuestionOptionData(questionType, sourceAssembly); typeDefinition.QuestionOptions = parameterOptions; typeDefinition.QuestionPartSlots = ListQuestionSlots(questionType); QuestionTypeDefinitions[typeDefinition.TypeName] = typeDefinition; ReadResponseType(attribute, typeDefinition); ReadQuestionResourceData(typeDefinition, questionType, sourceAssembly); ReadLocaleData(sourceAssembly); GetTypeClientData(typeDefinition, sourceAssembly); foreach (var locale in locales.Keys) { typeDefinition.TypeNameLocales[locale] = locales[locale][typeDefinition.TypeName.ToLower()]; } return(typeDefinition); }
/// <summary> /// </summary> /// <param name="definition"></param> /// <param name="questionType"></param> private void ReadQuestionResourceData(QuestionTypeDefinition definition, Type questionType, Assembly sourceAssembly) { var members = questionType.GetMembers(); foreach (var member in members) { var attributes = member.GetCustomAttributes(); foreach (var attribute in attributes) { if (attribute.GetType() == typeof(HasResourceAttribute)) { var hasResourceAttribute = (HasResourceAttribute)attribute; byte[] data; using (var ms = new MemoryStream()) { sourceAssembly.GetManifestResourceStream(hasResourceAttribute.ResourceName).CopyTo(ms); data = ms.ToArray(); } definition.QuestionResources[hasResourceAttribute.ResourceName] = new QuestionResource { ResourceName = hasResourceAttribute.ResourceName, Data = data, FieldName = member.Name }; } } } }
/// <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> /// </summary> /// <param name="typeDefinition"></param> /// <param name="sourceAssembly"></param> /// <returns></returns> private void GetTypeClientData(QuestionTypeDefinition typeDefinition, Assembly sourceAssembly) { var moduleResourceNames = sourceAssembly.GetManifestResourceNames().Where(r => r.EndsWith(".module.js")); foreach (var resourceName in moduleResourceNames) { using (var ms = new MemoryStream()) { sourceAssembly.GetManifestResourceStream(resourceName).CopyTo(ms); QuestionTypeDefinition.ClientModules[resourceName] = ms.ToArray(); } } }
/// <summary> /// Reads all resource data besides question client (js) data and adds it to the the resource dictionary /// The key is either the predefined key or the file name if it was not present /// </summary> /// <param name="typeDefinition"></param> /// <param name="sourceAssembly"></param> private void ReadResourceData(QuestionTypeDefinition typeDefinition, Assembly sourceAssembly, string[] resources) { var resourceNames = sourceAssembly.GetManifestResourceNames().Where(r => !r.EndsWith(".module.js")).ToArray(); var formatter = new BinaryFormatter(); foreach (var resourceName in resources) { using (var ms = new MemoryStream()) { sourceAssembly.GetManifestResourceStream(resourceName).CopyTo(ms); typeDefinition.ResourceData[resourceName] = ms.ToArray(); } } }
/// <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); }
private async Task <FileContentResult> LoadCodeBundleFile(QuestionTypeDefinition definition, string bundleName) { if (bundleName == null) { return(File(QuestionTypeDefinition.ClientModules.Values.ToList()[0], "application/javascript")); } // check if loading development bundle if (System.IO.File.Exists(Path.Combine("development", bundleName))) { return(File(await System.IO.File.ReadAllBytesAsync(Path.Combine("development", bundleName)), "application/javascript")); } else { return(File( QuestionTypeDefinition.ClientModules[ QuestionTypeDefinition.ClientModules.Keys .FirstOrDefault(k => k.EndsWith(bundleName, StringComparison.InvariantCultureIgnoreCase))], "application/javascript")); } }
/// <summary> /// </summary> /// <param name="attribute"></param> /// <param name="definition"></param> private void ReadResponseType(SurveyQuestionAttribute attribute, QuestionTypeDefinition definition) { definition.ResponseType = attribute.QuestionResponseType; }
/// <summary> /// /// </summary> /// <param name="part"></param> /// <param name="definition"></param> public void AddQuestionPartChild(QuestionPart part, QuestionTypeDefinition definition) { part.QuestionType = definition.TypeName; part.QuestionPartChildren.Add(part); }