Difference between revisions of "Business logic of Require property in Questionnaire Template"

From Catglobe Wiki
Jump to: navigation, search
(New page: == Description == This property existed in both questionnaire template's properties and question's properties . *Setting for questionnaire : Image:QnaireRequired.JPG‎ *Setting for q...)
 
Line 33: Line 33:
 
*If the question is a GRID question ( single grid , multi grid , scale grid , text grid) , users need to define which sub questions will be required . '''The range will be save to the value of correlative property''' (REQUIRED or NOT_REQUIRED).
 
*If the question is a GRID question ( single grid , multi grid , scale grid , text grid) , users need to define which sub questions will be required . '''The range will be save to the value of correlative property''' (REQUIRED or NOT_REQUIRED).
  
 +
== Working with Required property in .NET side ==
 +
=== This example shows how to use the Required property of grid question and apply it to related sub question ===
 +
<source lang="csharp" line="1">
 +
            // Apply the required property if set
 +
            QuestionProperty requiredProperty = q.GetProperty(PropertyType.Required);
 +
            if (requiredProperty != null && !string.IsNullOrEmpty(requiredProperty.Value))
 +
            {
 +
              // Parse the property's value into ranges
 +
              Ranges requiredRanges = new Ranges(requiredProperty.Value);
  
 +
              // For each range run through the integers contained in the range
 +
              // and set the required property to true
 +
              foreach (Range r in requiredRanges)
 +
                  for (int j = r.From; j <= r.To; j++)
 +
                    //Tho : this if expression is to make sure that we only take valid
 +
                    //sub question index .  Some old questionnaire template has invalid property value
 +
                    if ( j>-1 && j < q.subQuestions.Count)
 +
                        q.subQuestions[j].Required = true;
 +
            }
 +
 +
            // Apply the not required property if set
 +
            QuestionProperty notRequiredProperty = q.GetProperty(PropertyType.NotRequired);
 +
            if (notRequiredProperty != null && !string.IsNullOrEmpty(notRequiredProperty.Value))
 +
            {
 +
              // Parse the property's value into ranges
 +
              Ranges notRequiredRanges = new Ranges(notRequiredProperty.Value);
 +
 +
              // For each range run through the integers contained in the range
 +
              // and set the required property to false
 +
              foreach (Range r in notRequiredRanges)
 +
              {
 +
                  if (r.To >= q.SubQuestions.Count)
 +
                    r.To = q.SubQuestions.Count - 1;
 +
                  for (int j = r.From; j <= r.To; j++)
 +
                    //Tho : this if expression is to make sure that we only take valid
 +
                    //sub question index .  Some old questionnaire template has invalid property value
 +
                    if (j > -1 && j < q.subQuestions.Count)
 +
                        q.subQuestions[j].Required = false;
 +
              }
 +
            }
 +
</source>
 +
=== This example show how to apply Required property of questionnaire template to question and register it to client side ===
 +
<source lang="csharp">
 +
foreach (QuestionnaireProperty p in Questionnaire.Properties)
 +
{
 +
switch (p.Type)
 +
//Before inherited from Questionnaire properties , you must make sure that the question does not have both Required and NotRequired property
 +
        case PropertyType.Required:
 +
                  if (Question.IsGrid())
 +
                  {
 +
                    int i = 0;
 +
                    foreach (SubQuestion sq in Question.SubQuestions)
 +
                    {
 +
                        if (sq.Visible)
 +
                        {
 +
                          script.Append("// Require answer for ");
 +
                          script.Append(sq.Question.Label);
 +
                          script.Append("[");
 +
                          script.Append(sq.GridNumber);
 +
                          script.Append("]\n");
 +
                          script.AppendFormat("quest.questions[{0}].required = true;\n", i++);
 +
                        }
 +
                    }
 +
                  }
 +
                  else
 +
                  {
 +
                    script.Append("quest.required = true;\n");
 +
                  }
 +
                  break;
 +
          case PropertyType.NotRequired:
 +
                  if (Question.IsGrid())
 +
                  {
 +
                    int i = 0;
 +
                    foreach (SubQuestion sq in Question.SubQuestions)
 +
                    {
 +
                        if (sq.Visible)
 +
                        {
 +
                          script.Append("// Not require answer for ");
 +
                          script.Append(sq.Question.Label);
 +
                          script.Append("[");
 +
                          script.Append(sq.GridNumber);
 +
                          script.Append("]\n");
 +
                          script.AppendFormat("quest.questions[{0}].required = false;\n", i++);
 +
                        }
 +
                    }
 +
                  }
 +
                  else
 +
                  {
 +
                    script.Append("quest.required = false;\n");
 +
                  }
 +
                  break;
 +
//...
 +
}
 +
</source>
 +
=== This example shows how to apply Required property of question register it to client side ===
 +
<source lang="csharp">
 +
foreach (QuestionProperty p in Question.Properties)
 +
{
 +
// ....
 +
      switch(p.Type)
 +
              case PropertyType.NotRequired:
 +
                  if (Question.IsGrid())
 +
                  {
 +
                    ranges = new Ranges(p.Value);
 +
 +
                    int i = 0;
 +
                    foreach (SubQuestion sq in Question.SubQuestions)
 +
                    {
 +
                        if (sq.Visible && ranges.Contains(sq.GridNumber))
 +
                        {
 +
                          script.Append("// Not require answer for ");
 +
                          script.Append(sq.Question.Label);
 +
                          script.Append("[");
 +
                          script.Append(sq.GridNumber);
 +
                          script.Append("]\n");
 +
 +
                          script.AppendFormat("quest.questions[{0}].required = false;\n", i);
 +
                        }
 +
 +
                        if (sq.Visible)
 +
                        {
 +
                          i++;
 +
                        }
 +
                    }
 +
                  }
 +
                  else
 +
                  {
 +
                    script.Append("quest.required = false;\n");
 +
                  }
 +
                  break;
 +
              case PropertyType.Required:
 +
                  if (Question.IsGrid())
 +
                  {
 +
                    ranges = new Ranges(p.Value);
 +
 +
                    int i = 0;
 +
                    foreach (SubQuestion sq in Question.SubQuestions)
 +
                    {
 +
                        if (sq.Visible && ranges.Contains(sq.GridNumber))
 +
                        {
 +
                          script.Append("// Require answer for ");
 +
                          script.Append(sq.Question.Label);
 +
                          script.Append("[");
 +
                          script.Append(sq.GridNumber);
 +
                          script.Append("]\n");
 +
 +
                          script.AppendFormat("quest.questions[{0}].required = true;\n", i);
 +
                        }
 +
 +
                        if (sq.Visible)
 +
                        {
 +
                          i++;
 +
                        }
 +
                    }
 +
                  }
 +
                  else
 +
                  {
 +
                    script.Append("quest.required = true;\n");
 +
                  }
 +
                  break;
 +
//...
 +
}
 +
</source>
 
  [[category:Technical guidelines]]
 
  [[category:Technical guidelines]]

Revision as of 11:35, 7 April 2009

Description

This property existed in both questionnaire template's properties and question's properties .

  • Setting for questionnaire :

QnaireRequired.JPG

  • Setting for question (non-Grid question)

NonGridRequired.JPG

  • Setting for question (Grid question)

GridRequired.JPG

  • Using in .NET

CatGlobe.Domain.Questionnaires.PropertyType.Required

  • Using in Java

Property.REQUIRED

Business logic

Required as Questionnaire's property

  • If this property is checked , it means that all the question in the questionnaire template , which are not specified Required property explicitly , will require the users answer it and they can't go to another question without answering .

Behind the code : the template will has property REQUIRED with the property's value is "true" .

  • If this property is unchecked , it means that all the question in the questionnaire template , which are not specified Required property explicitly,will not require users answer it.

Behind the code : the template will has property NOT_REQUIRE with the property's value is "true"

Required as Question's property

This property is inherited from the value of property Required in a questionnaire. However, a question can set anew for this property through its Required property, and this value is only applied to this question.

  • Inherited from Required property of questionnaire template :

Inherited.JPG
In this case the there is not both REQUIRED and NOT_REQUIRED property register in question's property (the existed one will be removed). So the question will inherited from the Required property of the questionnaire template]

  • Required property is checked

Checked.JPG
In this case , the question will has REQUIRED property with the property's value is "true"

  • Required property is unchecked

UnChecked.JPG
In this case , the question will has NOT_REQUIRED property with the property's value is "true"

  • If the question is a GRID question ( single grid , multi grid , scale grid , text grid) , users need to define which sub questions will be required . The range will be save to the value of correlative property (REQUIRED or NOT_REQUIRED).

Working with Required property in .NET side

This example shows how to use the Required property of grid question and apply it to related sub question

 1             // Apply the required property if set
 2             QuestionProperty requiredProperty = q.GetProperty(PropertyType.Required);
 3             if (requiredProperty != null && !string.IsNullOrEmpty(requiredProperty.Value))
 4             {
 5                // Parse the property's value into ranges
 6                Ranges requiredRanges = new Ranges(requiredProperty.Value);
 7 
 8                // For each range run through the integers contained in the range
 9                // and set the required property to true
10                foreach (Range r in requiredRanges)
11                   for (int j = r.From; j <= r.To; j++)
12                      //Tho : this if expression is to make sure that we only take valid
13                      //sub question index .  Some old questionnaire template has invalid property value
14                      if ( j>-1 && j < q.subQuestions.Count)
15                         q.subQuestions[j].Required = true;
16             }
17 
18             // Apply the not required property if set
19             QuestionProperty notRequiredProperty = q.GetProperty(PropertyType.NotRequired);
20             if (notRequiredProperty != null && !string.IsNullOrEmpty(notRequiredProperty.Value))
21             {
22                // Parse the property's value into ranges
23                Ranges notRequiredRanges = new Ranges(notRequiredProperty.Value);
24 
25                // For each range run through the integers contained in the range
26                // and set the required property to false
27                foreach (Range r in notRequiredRanges)
28                {
29                   if (r.To >= q.SubQuestions.Count)
30                      r.To = q.SubQuestions.Count - 1;
31                   for (int j = r.From; j <= r.To; j++)
32                      //Tho : this if expression is to make sure that we only take valid
33                      //sub question index .  Some old questionnaire template has invalid property value
34                      if (j > -1 && j < q.subQuestions.Count)
35                         q.subQuestions[j].Required = false;
36                }
37             }

This example show how to apply Required property of questionnaire template to question and register it to client side

foreach (QuestionnaireProperty p in Questionnaire.Properties)
{
switch (p.Type)
//Before inherited from Questionnaire properties , you must make sure that the question does not have both Required and NotRequired property
         case PropertyType.Required:
                  if (Question.IsGrid())
                  {
                     int i = 0;
                     foreach (SubQuestion sq in Question.SubQuestions)
                     {
                        if (sq.Visible)
                        {
                           script.Append("// Require answer for ");
                           script.Append(sq.Question.Label);
                           script.Append("[");
                           script.Append(sq.GridNumber);
                           script.Append("]\n");
                           script.AppendFormat("quest.questions[{0}].required = true;\n", i++);
                        }
                     }
                  }
                  else
                  {
                     script.Append("quest.required = true;\n");
                  }
                  break;
          case PropertyType.NotRequired:
                  if (Question.IsGrid())
                  {
                     int i = 0;
                     foreach (SubQuestion sq in Question.SubQuestions)
                     {
                        if (sq.Visible)
                        {
                           script.Append("// Not require answer for ");
                           script.Append(sq.Question.Label);
                           script.Append("[");
                           script.Append(sq.GridNumber);
                           script.Append("]\n");
                           script.AppendFormat("quest.questions[{0}].required = false;\n", i++);
                        }
                     }
                  }
                  else
                  {
                     script.Append("quest.required = false;\n");
                  }
                  break;
//...
}

This example shows how to apply Required property of question register it to client side

foreach (QuestionProperty p in Question.Properties)
{
// ....
       switch(p.Type)
               case PropertyType.NotRequired:
                  if (Question.IsGrid())
                  {
                     ranges = new Ranges(p.Value);

                     int i = 0;
                     foreach (SubQuestion sq in Question.SubQuestions)
                     {
                        if (sq.Visible && ranges.Contains(sq.GridNumber))
                        {
                           script.Append("// Not require answer for ");
                           script.Append(sq.Question.Label);
                           script.Append("[");
                           script.Append(sq.GridNumber);
                           script.Append("]\n");

                           script.AppendFormat("quest.questions[{0}].required = false;\n", i);
                        }

                        if (sq.Visible)
                        {
                           i++;
                        }
                     }
                  }
                  else
                  {
                     script.Append("quest.required = false;\n");
                  }
                  break;
               case PropertyType.Required:
                  if (Question.IsGrid())
                  {
                     ranges = new Ranges(p.Value);

                     int i = 0;
                     foreach (SubQuestion sq in Question.SubQuestions)
                     {
                        if (sq.Visible && ranges.Contains(sq.GridNumber))
                        {
                           script.Append("// Require answer for ");
                           script.Append(sq.Question.Label);
                           script.Append("[");
                           script.Append(sq.GridNumber);
                           script.Append("]\n");

                           script.AppendFormat("quest.questions[{0}].required = true;\n", i);
                        }

                        if (sq.Visible)
                        {
                           i++;
                        }
                     }
                  }
                  else
                  {
                     script.Append("quest.required = true;\n");
                  }
                  break;
//...
}