コード例 #1
0
ファイル: JsonScheme.cs プロジェクト: bhargavaganti/Chatbot-
        public IForm <JObject> BuildJsonForm()
        {
            OnCompletionAsyncDelegate <JObject> processResult = async(context, state) =>
            {
                state.Remove("Confirmation");
                //List<int> IDquestions = new List<int>();
                List <HelperClass> list = new List <HelperClass>();

                int j = 0;
                // Iterate the responses and do what you like here
                foreach (JProperty item in (JToken)state)
                {
                    int    questionID = Int32.Parse(item.Name.ToString()) - 100;
                    string answer     = item.Value.ToString();

                    int questionTypeID = _userService.GetQuestionTypeID(questionID);

                    // int questionTypeID = questionType.questionTypeID;
                    if (questionTypeID == 3)               //"wielokrotnego wyboru")
                    {
                        string an   = answer.Remove(0, 6); //.Remove(answer.Length - 1, 1);
                        string answ = an.Remove(an.Length - 4, 4);

                        string[] answers = answ.Split(',');
                        if (answers.Count() != 1)
                        {
                            for (int i = 0; i < answers.Count(); i++)
                            {
                                string oneAnswer = answers[i];
                                if (i == 0)
                                {
                                    an         = oneAnswer.Remove(oneAnswer.Length - 1, 1);
                                    answers[i] = an;
                                    HelperClass helper = new HelperClass(questionID, an);
                                    list.Add(helper);
                                }

                                else if (i == answers.Count() - 1)
                                {
                                    an         = oneAnswer.Remove(0, 5);
                                    answers[i] = an;
                                    HelperClass helper = new HelperClass(questionID, an);
                                    list.Add(helper);
                                }
                                else
                                {
                                    an         = oneAnswer.Remove(0, 5);
                                    oneAnswer  = an;
                                    an         = oneAnswer.Remove(oneAnswer.Length - 1, 1);
                                    answers[i] = an;
                                    HelperClass helper = new HelperClass(questionID, an);
                                    list.Add(helper);
                                }
                            }
                        }
                        else
                        {
                            HelperClass helper = new HelperClass(questionID, answ);
                            list.Add(helper);
                        }
                    }
                    else if (questionTypeID == 2)//jednokrotnego wyboru
                    {
                        HelperClass helper = new HelperClass(questionID, answer);
                        list.Add(helper);
                    }
                    else if (questionTypeID == 1)//Pytanie otwarte
                    {
                        HelperClass helper = new HelperClass(questionID, answer);
                        list.Add(helper);
                    }
                    else//Pytanie otwarte i jednokrotnego wyboru
                    {
                        await context.PostAsync("Coś się zjebało się");
                    }
                }
                foreach (HelperClass item in list)
                {
                    GivenAnswer givenAnswer = _userService.CreateGivenAnswer(testID, item.questionID, item.answer);
                    _userService.AddGivenAnswer(givenAnswer);
                }
                _userService.FinalizingTest(testID);//, TimeSpan.MinValue);
                await context.PostAsync("xd");

                // await context.PostAsync(msg);
                await context.PostAsync("Thanks for completing our test");
            };


            var schema = JObject.Parse(_jsonProvider.GetJsonForTest(testID));

            return(new FormBuilderJson(schema)
                   .AddRemainingFields()
                   .OnCompletion(processResult)
                   .Build());


            //using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BotChat.schema.json"))
            //{
            //    JsonProvider.GetJsonForTest();
            //    var schema = JObject.Parse(new StreamReader(stream).ReadToEnd());
            //    return new FormBuilderJson(schema)
            //        .AddRemainingFields()
            //        .Build();
            //}
        }