Exemple #1
0
  /**
   * Checks if a unique question UID is shown according to the values of the given survey
   *
   * @param uid
   * @param id_survey
   * @return
   */
  public static boolean isHiddenQuestionByUidAndSurvey(String uid, Long id_survey) {
    // get question by uid
    Question question = getQuestionByUid(uid);
    if (question == null) return false;

    Long id_question = question.getId_question();
    long hasParentOptionActivated =
        new Select()
            .count()
            .from(Value.class)
            .as("v")
            .join(QuestionOption.class, Join.JoinType.LEFT)
            .as("qo")
            .on(
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION))
                    .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)),
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION))
                    .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION)))
            .join(Match.class, Join.JoinType.LEFT)
            .as("m")
            .on(
                Condition.column(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH))
                    .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH)))
            .join(QuestionRelation.class, Join.JoinType.LEFT)
            .as("qr")
            .on(
                Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION))
                    .eq(
                        ColumnAlias.columnWithTable(
                            "qr", QuestionRelation$Table.ID_QUESTION_RELATION)))
            // Parent child relationship
            .where(
                Condition.column(
                        ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION))
                    .eq(1))
            // For the given survey
            .and(
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY))
                    .eq(id_survey))
            // The child question in the relationship is 'this'
            .and(
                Condition.column(
                        ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION))
                    .eq(id_question))
            .count();
    return hasParentOptionActivated > 0 ? false : true;
  }
Exemple #2
0
  /**
   * Gets all the children compulsory questions, and returns the number of active children
   *
   * @param
   * @return
   */
  public static int countChildrenCompulsoryBySurvey(Long id_survey) {
    int numActiveChildrens = 0;
    // This query returns a list of children compulsory questions
    // But the id_question is not correct, because is the the parent id_questions from the
    // questionOption relation.
    List<Question> questions =
        new Select()
            .from(Question.class)
            .as("q")
            .join(QuestionRelation.class, Join.JoinType.LEFT)
            .as("qr")
            .on(
                Condition.column(ColumnAlias.columnWithTable("q", Question$Table.ID_QUESTION))
                    .eq(ColumnAlias.columnWithTable("qr", QuestionRelation$Table.ID_QUESTION)))
            .join(Match.class, Join.JoinType.LEFT)
            .as("m")
            .on(
                Condition.column(
                        ColumnAlias.columnWithTable(
                            "qr", QuestionRelation$Table.ID_QUESTION_RELATION))
                    .eq(ColumnAlias.columnWithTable("m", Match$Table.ID_QUESTION_RELATION)))
            .join(QuestionOption.class, Join.JoinType.LEFT)
            .as("qo")
            .on(
                Condition.column(ColumnAlias.columnWithTable("m", Match$Table.ID_MATCH))
                    .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_MATCH)))
            .join(Value.class, Join.JoinType.LEFT)
            .as("v")
            .on(
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_QUESTION))
                    .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_QUESTION)),
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_OPTION))
                    .eq(ColumnAlias.columnWithTable("qo", QuestionOption$Table.ID_OPTION)))
            // Parent Child relationship
            .where(
                Condition.column(
                        ColumnAlias.columnWithTable("qr", QuestionRelation$Table.OPERATION))
                    .eq(1))
            // For the given survey
            .and(
                Condition.column(ColumnAlias.columnWithTable("v", Value$Table.ID_SURVEY))
                    .eq(id_survey))
            // The child question requires an answer
            .and(
                Condition.column(ColumnAlias.columnWithTable("q", Question$Table.OUTPUT))
                    .isNot(Constants.NO_ANSWER))
            .and(
                Condition.column(ColumnAlias.columnWithTable("q", Question$Table.COMPULSORY))
                    .eq(true))
            .queryList();

    // checks if the children questions are active by UID
    // Note: the question id_question is wrong because dbflow query overwrites the children
    // id_question with the parent id_question.
    for (Question question : questions) {
      if (!Question.isHiddenQuestionByUidAndSurvey(question.getUid(), id_survey)) {
        numActiveChildrens++;
      }
    }
    // Return number of active compulsory children
    return (int) (numActiveChildrens);
  }
Exemple #3
0
 public void setQuestion(Question question) {
   this.question = question;
   this.id_parent = (question != null) ? question.getId_question() : null;
 }