コード例 #1
0
ファイル: AstBuilderVisitor.cs プロジェクト: diepvriezer/ql
        public override AstNode VisitQuestionReference([NotNull] QLParser.QuestionReferenceContext context)
        {
            var questionRef = new QuestionReference
            {
                Id = context.ID().GetText()
            };

            return(questionRef);
        }
        public virtual T VisitQuestionReference(QuestionReference questionRef)
        {
            questionRef.Id.Accept(this);

            foreach (var styleAttr in questionRef.StyleAttributes)
            {
                styleAttr.Accept(this);
            }
            return(default(T));
        }
コード例 #3
0
        /// <summary>
        /// Edit a question reference
        /// </summary>
        /// <param name="answerID">answer where you want to update the referece</param>
        /// <param name="newReference">the edited reference</param>
        /// <param name="connection">database connection</param>
        internal static void editAnswerReference(int answerID, QuestionReference newReference, SqlConnection connection)
        {
            try
            {
                SqlCommand updateQuestionReferenceCommand = new SqlCommand(null, connection)
                {
                    CommandText = "UPDATE ANSWER_QUESTIONS SET QUESTION_REFERENCE_ID = @newReferenceID, " +
                                  "PROBABILITY = @probability WHERE ANSWER_ID = @answerID " +
                                  "AND QUESTION_REFERENCE_ID = @prevReferenceID;"
                };
                var paramAnswerID = new SqlParameter("@answerID", SqlDbType.Int)
                {
                    Value = answerID
                };
                var paramPrevReferenceID = new SqlParameter("@prevReferenceID", SqlDbType.Int)
                {
                    Value = newReference.prevQuestionReference
                };
                var paramNewReferenceID = new SqlParameter("@newReferenceID", SqlDbType.Int)
                {
                    Value = newReference.questionReference
                };
                var paramProbability = new SqlParameter("@probability", SqlDbType.Int)
                {
                    Value = newReference.questionReferenceProbability
                };


                updateQuestionReferenceCommand.Parameters.Add(paramAnswerID);
                updateQuestionReferenceCommand.Parameters.Add(paramPrevReferenceID);
                updateQuestionReferenceCommand.Parameters.Add(paramNewReferenceID);
                updateQuestionReferenceCommand.Parameters.Add(paramProbability);

                connection.Close(); connection.Open();
                updateQuestionReferenceCommand.Prepare();
                updateQuestionReferenceCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Singleton.errorCode = "ANS08";
                Singleton.writeErrorToFile(Singleton.errorCode, ex.Message, ex.StackTrace);
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #4
0
        public override object VisitQuestionReference(QuestionReference questionRef)
        {
            // Look up the referred QL question.
            var question = _questions.Where(q => q.Name == questionRef.Name).SingleOrDefault();

            if (question != null)
            {
                _currentDataType = question.DataType;

                foreach (var styleAttr in questionRef.StyleAttributes)
                {
                    styleAttr.Accept(this);
                }
            }
            return(null);
        }
コード例 #5
0
        public override object VisitQuestionReference(QuestionReference questionRef)
        {
            // Look up the referred QL question.
            var question = _questions.Where(q => q.Name == questionRef.Name).SingleOrDefault();

            if (question != null)
            {
                _currentDataType = question.DataType;

                foreach (var styleAttr in questionRef.StyleAttributes)
                {
                    styleAttr.Accept(this);
                }
            }
            return null;
        }
コード例 #6
0
        /// <summary>
        ///  add reference to a question from an answer (an answer points to a question or multiple questions)
        /// </summary>
        /// <param name="answerID">answer you want to attach the reference to</param>
        /// <param name="questionReference">reference you want to add to the answer</param>
        /// <param name="connection">database connection</param>
        public static void addAnswerReference(int answerID, QuestionReference questionReference, SqlConnection connection)
        {
            try
            {
                SqlCommand addQuestionReferenceCommand = new SqlCommand(null, connection)
                {
                    CommandText = "INSERT INTO ANSWER_QUESTIONS (ANSWER_ID, QUESTION_REFERENCE_ID, PROBABILITY) " +
                                  "VALUES (@answerID, @questionID, @probability);"
                };

                var paramID = new SqlParameter("@answerID", SqlDbType.Int)
                {
                    Value = answerID
                };
                var paramQuestion = new SqlParameter("@questionID", SqlDbType.Int)
                {
                    Value = questionReference.questionReference
                };
                var paramProbability = new SqlParameter("@probability", SqlDbType.Int)
                {
                    Value = questionReference.questionReferenceProbability
                };

                addQuestionReferenceCommand.Parameters.Add(paramID);
                addQuestionReferenceCommand.Parameters.Add(paramQuestion);
                addQuestionReferenceCommand.Parameters.Add(paramProbability);

                connection.Close(); connection.Open();
                addQuestionReferenceCommand.Prepare();
                addQuestionReferenceCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Singleton.errorCode = "ANS09";
                Singleton.writeErrorToFile(Singleton.errorCode, ex.Message, ex.StackTrace);
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }
コード例 #7
0
 public override object VisitQuestionReference(QuestionReference questionRef)
 {
     if (_unrefferedQuestions.Contains(questionRef.Name))
     {
         _unrefferedQuestions.Remove(questionRef.Name);
         _referredQuestions.Add(questionRef.Name);
     }
     else
     {
         if (_referredQuestions.Contains(questionRef.Name))
         {
             Report.AddError(questionRef.Position, "The style sheet already contains a reference to question '{0}'.", questionRef.Name);
         }
         else
         {
             Report.AddError(questionRef.Position, "Question '{0}' is not defined in the QL.", questionRef.Name);
         }
     }
     return null;
 }
 public override object VisitQuestionReference(QuestionReference questionRef)
 {
     if (_unrefferedQuestions.Contains(questionRef.Name))
     {
         _unrefferedQuestions.Remove(questionRef.Name);
         _referredQuestions.Add(questionRef.Name);
     }
     else
     {
         if (_referredQuestions.Contains(questionRef.Name))
         {
             Report.AddError(questionRef.Position, "The style sheet already contains a reference to question '{0}'.", questionRef.Name);
         }
         else
         {
             Report.AddError(questionRef.Position, "Question '{0}' is not defined in the QL.", questionRef.Name);
         }
     }
     return(null);
 }
コード例 #9
0
        public override object VisitQuestionReference(QuestionReference questionRef)
        {
            var question = _questions.Where(q => q.Name == questionRef.Name).SingleOrDefault();

            if (question != null)
            {
                StyleSet dataTypeStyleSet = _currentDataTypeStyles.GetStyleSet(question.DataType);

                StyleSet questionStyleSet = dataTypeStyleSet.GetCopy();
                questionStyleSet.OverrideStyles(questionRef.StyleAttributes);

                _questionStyles.AddStyleSet(question.Name, questionStyleSet);

                return(null);
            }
            else
            {
                throw new ApplicationException("Question not found.");
            }
        }
コード例 #10
0
        public override Control VisitQuestionReference(QuestionReference questionRef)
        {
            var question = _questionForm.GetAllQuestions().Where(q => q.Name == questionRef.Name).SingleOrDefault();

            if (question != null)
            {
                StyleSet questionStyles = _questionStyles.GetStyleSet(question.Name);

                QuestionWidget questionWidget = (QuestionWidget)questionStyles.WidgetStyle.CreateWidgetControl(new WidgetFactory(question));

                questionWidget.ApplyStyles(questionStyles);

                _questionWidgets.Add(questionWidget);

                return(questionWidget);
            }
            else
            {
                throw new ApplicationException("Question not found in the questionnaire AST.");
            }
        }
コード例 #11
0
 public ActionResult AddQuestionReference(int answerID)
 {
     if (String.IsNullOrEmpty((string)Session["userId"]))
     {
         return(RedirectToAction("Login", "Account"));
     }
     try
     {
         Session["selectedAnswer"] = answerID;
         ((EditScenarioModel)Session["editScenario"]).newAnswer = ((EditScenarioModel)Session["editScenario"]).answers[answerID];
         QuestionReference newReference = new QuestionReference();
         newReference.questionReference            = 0;
         newReference.questionReferenceProbability = 100;
         ((EditScenarioModel)Session["editScenario"]).newQuestionReference = newReference;
         CheckForError();
         return(View("_PartialAddAnswerReferences", ((EditScenarioModel)Session["editScenario"])));
     }
     catch (Exception ex)
     {
         Singleton.errorCode = "SENCON22";
         Singleton.writeErrorToFile(Singleton.errorCode, ex.Message, ex.StackTrace);
         throw ex;
     }
 }
コード例 #12
0
 public override string Visit(QuestionReference node)
 {
     return(node.Id);
 }
コード例 #13
0
 public override QuestionInventoryResult Visit(QuestionReference node)
 {
     _result.References.Add(node);
     return(_result);
 }
コード例 #14
0
 public static void editAnswerReference(int answerID, QuestionReference newReference)
 {
     AnswerDatabaseUtils.editAnswerReference(answerID, newReference, connection);
 }
コード例 #15
0
 public static void addAnswerReference(int answerID, QuestionReference questionReference)
 {
     AnswerDatabaseUtils.addAnswerReference(answerID, questionReference, connection);
 }
コード例 #16
0
 public override BaseType Visit(QuestionReference node)
 {
     return(_questionTypeMap[node.Id]);
 }
コード例 #17
0
    public ActionResult SubmitQuestionReference(string saveType, bool isNew) //isNew (reference)
    {
        if (String.IsNullOrEmpty((string)Session["userId"]))
        {
            return(RedirectToAction("Login", "Account"));
        }
        try
        {
            int answerID = (int)Session["selectedAnswer"];
            if (!saveType.Equals("Cancel"))
            {
                QuestionReference newReference = new QuestionReference();
                newReference.prevQuestionReference        = Convert.ToInt32(Request["newQuestionReference.prevQuestionReference"]);
                newReference.questionReference            = Convert.ToInt32(Request["newQuestionReference.questionReference"]);
                newReference.questionReferenceProbability = Convert.ToInt32(Request["newQuestionReference.questionReferenceProbability"]);

                ((EditScenarioModel)Session["editScenario"]).currentQuestionReferencesInAnswer = new List <int>();

                if (((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences == null)
                {
                    ((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences = new List <QuestionReference>();
                }

                foreach (QuestionReference reference in ((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences)
                {
                    ((EditScenarioModel)Session["editScenario"]).currentQuestionReferencesInAnswer.Add(reference.questionReference);
                }

                if (isNew)
                {
                    if (((EditScenarioModel)Session["editScenario"]).currentQuestionReferencesInAnswer.Contains(newReference.questionReference))
                    {
                        Console.Out.WriteLine("DIRTY ADD");
                        ((EditScenarioModel)Session["editScenario"]).errorMsg = "That question is already referenced by this answer. Please select a different question.";
                        return(View("_PartialAddAnswerReferences", ((EditScenarioModel)Session["editScenario"])));
                    }
                    else
                    {
                        Console.Out.WriteLine("CLEAN ADD");
                        ((EditScenarioModel)Session["editScenario"]).errorMsg = "";
                        DatabaseUtils.addAnswerReference(answerID, newReference);
                    }
                }
                else
                {
                    ((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences[((EditScenarioModel)Session["editScenario"]).editReferenceId].questionReference            = newReference.questionReference;
                    ((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences[((EditScenarioModel)Session["editScenario"]).editReferenceId].questionReferenceProbability = newReference.questionReferenceProbability;
                    int count = 0;
                    foreach (QuestionReference qr in ((EditScenarioModel)Session["editScenario"]).newAnswer.questionReferences)
                    {
                        if (qr.questionReference.Equals(newReference.questionReference))
                        {
                            count++;
                        }
                    }
                    if (count > 1)
                    {
                        Console.Out.WriteLine("DIRTY UPDATE");
                        ((EditScenarioModel)Session["editScenario"]).errorMsg = "An answer already references that question";
                        return(View("_PartialEditAnswerReferences", ((EditScenarioModel)Session["editScenario"])));
                    }
                    else
                    {
                        Console.Out.WriteLine("CLEAN UPDATE");
                        ((EditScenarioModel)Session["editScenario"]).errorMsg = "";
                        DatabaseUtils.editAnswerReference(answerID, newReference);
                    }
                }
                Session["currentScenario"] = DatabaseUtils.getScenario(((EditScenarioModel)Session["editScenario"]).ScenarioId);
                //sets the ((EditScenarioModel)Session["editScenario"])
            }
            getEditQuestionsAndAnswers(((Scenario)Session["currentScenario"]));
            CheckForError();
            return(EditAnswer(answerID));
        }
        catch (Exception ex)
        {
            Singleton.errorCode = "SENCON24";
            Singleton.writeErrorToFile(Singleton.errorCode, ex.Message, ex.StackTrace);
            throw ex;
        }
    }
コード例 #18
0
ファイル: DefaultVisitor.cs プロジェクト: diepvriezer/ql
 public virtual T Visit(QuestionReference node)
 {
     return(default(T));
 }
コード例 #19
0
        /*
         * This function takes in a scenario and sets the Singleton.currentScenario to this scenario.
         * It pulls out and populates all the questions and answers associated with that scenario and stores
         * them in the respective data elements. When looping through potential question references, it will
         * select one of them randomly according to the probabalities listed and set that reference as the next question.
         */
        public static void getQuestionsAndAnswers(Scenario scenario, SqlConnection connection)
        {
            try
            {
                var questionCommand = new SqlCommand(null, connection);

                // Create and prepare an SQL statement.

                questionCommand.CommandText =
                    "SELECT * FROM QUESTIONS LEFT OUTER JOIN ANSWERS ON QUESTIONS.QUESTION_ID = ANSWERS.ANSWER_FOR_QUESTION " +
                    "LEFT OUTER JOIN ANSWER_QUESTIONS ON ANSWERS.ANSWER_ID = ANSWER_QUESTIONS.ANSWER_ID " +
                    "WHERE QUESTION_SCENARIO = @id AND QUESTIONS.QUESTION_ID != 0 AND QUESTIONS.QUESTION_ID != -1";

                var param2 = new SqlParameter("@id", SqlDbType.Int);
                param2.Value = scenario.scenarioID;
                questionCommand.Parameters.Add(param2);
                // Call Prepare after setting the Commandtext and Parameters.

                int previousAnswerID   = -1;
                int previousQuestionID = -1;

                scenario.questions       = new Dictionary <int, Question>();
                scenario.questionsActive = new Dictionary <int, Question>();
                connection.Close(); connection.Open();
                questionCommand.Prepare();
                using (var reader = questionCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // each row contains a question, answer, and answer reference

                        // if the question in a given row is new, record the values and store that question in the
                        // list of questions that belongs to the scenario
                        var questionLocation = Convert.ToInt32($"{reader["QUESTION_ID"]}");
                        if (!scenario.questions.ContainsKey(questionLocation))
                        {
                            if (previousQuestionID != -1)
                            {
                                if (scenario.questions[previousQuestionID].isActive)
                                {
                                    scenario.questionsActive.Add(previousQuestionID, scenario.questions[previousQuestionID]);
                                }
                            }
                            var question = new Question();
                            question.questionTitle       = $"{reader["QUESTION_TITLE"]}";
                            question.questionScenario    = Convert.ToInt32($"{reader["QUESTION_SCENARIO"]}");
                            question.questionDescription = $"{reader["QUESTION_DESCRIPTION"]}";
                            question.questionPicture     = $"{reader["QUESTION_PICTURE_URL"]}";
                            question.questionId          = questionLocation;
                            question.isActive            = Convert.ToBoolean($"{reader["QUESTION_IS_ACTIVE"]}");
                            question.answerList          = new Dictionary <int, Answer>();
                            scenario.questions.Add(questionLocation, question);
                        }

                        int    answerID       = 0;
                        string answerIdString = $"{reader["ANSWER_ID"]}";
                        if (!answerIdString.Equals(""))
                        {
                            answerID = Convert.ToInt32(answerIdString);

                            // if the answer in a given row is new, record the values and store it as an answer for the current question
                            if (!scenario.questions[questionLocation].answerList.ContainsKey(answerID))
                            {
                                // if this is a new answer but not the first one, go through the question references for the previous answer
                                // and set the value for one of them as the selected question reference.
                                if (previousAnswerID != -1 &&
                                    scenario.questions[previousQuestionID].answerList.Count != 0)
                                {
                                    if (scenario.questions[previousQuestionID].answerList[previousAnswerID]
                                        .questionReferences.Count != 0)
                                    {
                                        List <int> cdf = new List <int>();
                                        int        totalProbability = 0;

                                        foreach (QuestionReference reference in scenario.questions[previousQuestionID]
                                                 .answerList[previousAnswerID].questionReferences)
                                        {
                                            totalProbability += reference.questionReferenceProbability;
                                            cdf.Add(totalProbability);
                                        }
                                        Random r = new Random();
                                        int    questionProbabilityNumber = r.Next(0, totalProbability);

                                        for (int i = 0;
                                             i < scenario.questions[previousQuestionID].answerList[previousAnswerID]
                                             .questionReferences.Count;
                                             i++)
                                        {
                                            if (questionProbabilityNumber <= cdf[i])
                                            {
                                                scenario.questions[previousQuestionID].answerList[previousAnswerID]
                                                .nextQuestion = scenario.questions[previousQuestionID]
                                                                .answerList[previousAnswerID].questionReferences[i].questionReference;
                                                break;
                                            }
                                        }
                                    }
                                }

                                var answer = new Answer();
                                answer.answerID   = answerID;
                                answer.answerText = $"{reader["ANSWER_TEXT"]}";
                                string answerForQuestionString = $"{reader["ANSWER_FOR_QUESTION"]}";
                                var    isActive = $"{reader["ANSWER_IS_ACTIVE"]}";
                                if (!String.IsNullOrEmpty(isActive))
                                {
                                    answer.isActive = Convert.ToBoolean(isActive.ToLower());
                                }
                                if (!answerForQuestionString.Equals(""))
                                {
                                    answer.answerForQuestion = Convert.ToInt32(answerForQuestionString);
                                }


                                var requiresComment = $"{reader["ANSWER_REQUIRES_COMMENT"]}";
                                if (!String.IsNullOrEmpty(requiresComment))
                                {
                                    answer.requiresComment = Convert.ToBoolean(requiresComment.ToLower());
                                }

                                answer.questionReferences = new List <QuestionReference>();

                                if (scenario.questions.ContainsKey(questionLocation))
                                {
                                    scenario.questions[questionLocation].answerList.Add(answer.answerID, answer);
                                }
                                previousAnswerID = answerID;
                            }


                            // create a new question reference and add it to the current answer
                            QuestionReference questionReference = new QuestionReference();

                            var questionReferenceString = $"{reader["QUESTION_REFERENCE_ID"]}";
                            if (!questionReferenceString.Equals(""))
                            {
                                string questionReferenceID          = $"{reader["QUESTION_REFERENCE_ID"]}";
                                string questionReferenceProbability = $"{reader["PROBABILITY"]}";
                                if (!questionReferenceID.Equals(""))
                                {
                                    questionReference.questionReference = Convert.ToInt32(questionReferenceID);
                                }
                                if (!questionReferenceProbability.Equals(""))
                                {
                                    questionReference.questionReferenceProbability =
                                        Convert.ToInt32(questionReferenceProbability);
                                }

                                scenario.questions[questionLocation].answerList[answerID].questionReferences
                                .Add(questionReference);
                            }
                        }
                        previousQuestionID = questionLocation;
                    }
                    if (previousQuestionID != -1)
                    {
                        if (scenario.questions[previousQuestionID].isActive)
                        {
                            scenario.questionsActive.Add(previousQuestionID, scenario.questions[previousQuestionID]);
                        }
                    }
                    HttpContext.Current.Session["currentScenario"] = scenario;
                }
            }
            catch (Exception ex)
            {
                Singleton.errorCode = "SEN09";
                Singleton.writeErrorToFile(Singleton.errorCode, ex.Message, ex.StackTrace);
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }