Difference between revisions of "Add answer options of the close questions to the open questions"

From Catglobe Wiki
Jump to: navigation, search
 
(7 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
To add answer options of the close questions to the open questions (open/number/text/textgrid question type), please follow below steps:
 
To add answer options of the close questions to the open questions (open/number/text/textgrid question type), please follow below steps:
  
1. Copy the Javascript function '''addCloseAO() '''into the Javascript area of the open question. Using guide about the function '''addCloseAO()''' are comments of below javascript code snippet. You also need to know about the '''[http://wiki.catglobe.com/index.php/Replacement_tags Catglobe Replacement Tags]''' to be able to use above function.
+
1. Copy the Javascript function '''addCloseAO() '''into the Javascript area of the open question. Using guide about the function '''addCloseAO()''' are comments of below javascript code snippet. You also need to know about the '''Catglobe [[Replacement tags]]''' to be able to use above function.
  
 
2. Add the answer options of the close questions to the open question by using above function '''addCloseAO() '''and put this part into the body of function '''quest.onInit()'''. This '''quest.onInit()''' code also must be put into the Javascript area of the open question.
 
2. Add the answer options of the close questions to the open question by using above function '''addCloseAO() '''and put this part into the body of function '''quest.onInit()'''. This '''quest.onInit()''' code also must be put into the Javascript area of the open question.
Line 13: Line 13:
 
[[File:AddAOofCloseQuestionsToOpenQuestions1.jpg]]
 
[[File:AddAOofCloseQuestionsToOpenQuestions1.jpg]]
  
 
+
<br/>To implement this, i created 2 questions:
To implement this, i created 2 questions:
 
  
 
*A text grid question which will contain all below Javascript codes. Name of this question is: '''Case1_Textgrid'''
 
*A text grid question which will contain all below Javascript codes. Name of this question is: '''Case1_Textgrid'''
 
*A multi question which contain the answer option will be added to above text grid question. Name of this question is:'''Case1_Multi'''
 
*A multi question which contain the answer option will be added to above text grid question. Name of this question is:'''Case1_Multi'''
  
[[File:AddAOofCloseQuestionsToOpenQuestions2.jpg]]
+
<br/>[[File:AddAOofCloseQuestionsToOpenQuestions2.jpg]]
  
[[File:AddAOofCloseQuestionsToOpenQuestions3.jpg]]
+
<br/>[[File:AddAOofCloseQuestionsToOpenQuestions3.jpg]]
  
 
+
<br/><br/><source lang="javascript">
<br/><source lang="javascript">
 
 
/**************************************************************************
 
/**************************************************************************
  * ADD RADIO OR CHECKBOX OF ANSWER OPTIONS OF CLOSE QUESTIONS
+
  * GENERATE WIDGET OF AN ANSWER OPTION OF A CLOSE QUESTION
  * @param object parameterObj The structure of this object  
+
  * @param object p The structure of this object  
{
+
{
containerSelector: string type, the location which this radio/checkbox will be appended into. Ex: '#page_question_text',
+
type: string type, there are 2 available values: 'radio' and 'checkbox',  
inputType: string type, there are 2 available values: 'radio' and 'checkbox',  
+
qname: string type, the name of the single/multi question. Ex: 'ChooseQnaireByCountry',  
questionName: string type, the name of the single/multi question. Ex: 'ChooseQnaireByCountry',  
+
aov: string type or number type. Ex: '1',  
answerOptionValue: string type. Ex: '1',  
+
ansv: string type, must be the replacement tags. See this wiki link for more info: http://wiki.catglobe.com/index.php/Replacement_tags,
answerValue: string type, must be the replacement tags. See this wiki link for more info: http://wiki.catglobe.com/index.php/Replacement_tags,
+
aotxt: string type, the text of the answer option. Ex: 'this is answer option 1',
answerOptionText: string type, the text of the answer option. Ex: 'this is answer option 1',  
+
isHidden: bool type, whether shown or hide
isHidden: boolean type, if true then the added radio/checkbox will be shown, else it will be hidden,
+
}  
disableOpenQuestionsIfChecked: string type, the name of open question which this close question will added into. If this option is set then the text boxes of open question will be disabled when this radio/checkbox is checked. Ex: 'Case1_Textgrid'
+
  * @return jqueryObject jquery widget element
}  
 
  * @return jqueryObject jquery radio/checkbox element
 
 
  **************************************************************************/
 
  **************************************************************************/
function addCloseAO(parameterObj) {
+
function closeQuestionAOWidget(p) {
var input = $('<input type="' + parameterObj.inputType + '"/>')
+
 
.attr({
+
var input = $('<input type="' + p.type + '"/>').attr({
'class': 'ao_InputElement ' + parameterObj.questionName + ' value_' + parameterObj.answerOptionValue,
+
'class': 'ao_input_element ' + p.qname + ' value_' + p.aov,
'name': 'QUESTION.' + parameterObj.questionName,
+
'name': 'QUESTION.' + p.qname,
'value': parameterObj.answerOptionValue,
+
'value': p.aov,
'checked': parameterObj.answerValue === parameterObj.answerOptionValue ? true : false
+
'checked': p.ansv == p.aov ? true : false
});
+
});
if(typeof parameterObj.disableOpenQuestionsIfChecked === 'string' && parameterObj.disableOpenQuestionsIfChecked !== '') {
+
$(function() {
+
var span = $('<span>')
$('input:submit[name=next]').click(function() {
+
.addClass('ao_text ' + p.qname + ' value_' + p.aov)
if(input.attr('checked'))
+
.html(p.aotxt)
$('input:text[name^="QUESTION.' + parameterObj.disableOpenQuestionsIfChecked + '"]').attr('disabled', false).val('');
+
.click(function(e) {  
});
+
            //input.attr('checked') ? input.attr('checked', false) : input.attr('checked', true);  
});
+
            input.click();
input.click(function() {
+
        });
if( $(this).attr('checked') )
+
 
$('input:text[name^="QUESTION.' + parameterObj.disableOpenQuestionsIfChecked + '"]').attr('disabled', true).val('');
+
var hiddenInput = $('<input type="hidden"/>').attr({
else
+
'class': 'ao_hidden_input ' + p.qname + ' value_' + p.aov,
$('input:text[name^="QUESTION.' + parameterObj.disableOpenQuestionsIfChecked + '"]').attr('disabled', false);
+
'name': 'QUESTION.' + p.qname,
});
+
'value': ''
}
+
});
if(input.attr('checked'))
+
 
$('input:text[name^="QUESTION.' + parameterObj.disableOpenQuestionsIfChecked + '"]').attr('disabled', true);
+
var div = $('<div>')
var span = $('<span>')
+
  .attr({
.addClass('ao_Text ' + parameterObj.questionName + ' value_' + parameterObj.answerOptionValue)
+
'class': 'ao_container ' + p.qname + ' value_' + p.aov,
.text(parameterObj.answerOptionText);
+
'name': 'QUESTION.' + p.qname,
var div = $('<div>')
+
'value': p.aov
.attr({
+
  })
'class': 'ao_Container ' + parameterObj.questionName + ' value_' + parameterObj.answerOptionValue,
+
  .css('display', p.isHidden ? 'none' : 'block');
'name': 'QUESTION.' + parameterObj.questionName,
+
'value': parameterObj.answerOptionValue
+
div.append(input, span, hiddenInput);
})
+
.css('display', parameterObj.isHidden ? 'none' : 'block');  
+
return div;
div.append(input, span);
 
$(parameterObj.containerSelector).append(div);
 
if($('input[type="hidden"][name="QUESTION.' + parameterObj.questionName + '"]').length === 0)
 
$(parameterObj.containerSelector).append(
 
$('<input type="hidden"/>').attr( {
 
'name': 'QUESTION.' + parameterObj.questionName,
 
'value': ''
 
} )
 
);
 
return input;
 
 
}
 
}
 +
  
 
// DO THE JOB
 
// DO THE JOB
 
quest.onInit = function() {
 
quest.onInit = function() {
  // Add the close question
+
  // Generate the widget of the answer option of the specified close question element (but not yet added to DOM)
  window.checkboxInputElement = addCloseAO({
+
  var $widget = closeQuestionAOWidget({
  containerSelector: '.QuestionSpace p:first',
+
  type: 'checkbox',  
inputType: 'checkbox',  
+
  qname: 'Case1_Multi',  
  questionName: 'Case1_Multi',  
+
  aov: '1',  
  answerOptionValue: '1',  
+
  ansv: '{{Case1_Multi[0].value}}',  
  answerValue: '{{Case1_Multi[0].value}}',  
+
  aotxt: 'Don’t know / Do not wish to answer',  
  answerOptionText: 'Don’t know / Do not wish to answer',  
+
  isHidden: false
  isHidden: false,
 
disableOpenQuestionsIfChecked: 'Case1_Textgrid'
 
 
  });
 
  });
 +
 +
// Then add above $widget into the specified location
 +
$('.QuestionSpace p:first').append($widget);
 
};
 
};
 
</source>
 
</source>

Latest revision as of 08:52, 8 December 2014

Add answer options of the close questions to the open questions (open/number/text/textgrid question type)

To add answer options of the close questions to the open questions (open/number/text/textgrid question type), please follow below steps:

1. Copy the Javascript function addCloseAO() into the Javascript area of the open question. Using guide about the function addCloseAO() are comments of below javascript code snippet. You also need to know about the Catglobe Replacement tags to be able to use above function.

2. Add the answer options of the close questions to the open question by using above function addCloseAO() and put this part into the body of function quest.onInit(). This quest.onInit() code also must be put into the Javascript area of the open question.

3. The close question must be set to dummy. If not then the questionnaire will always stay at the open question when you click the next button, in other words the questionnaire can not move to the next question.


Below is an example about adding an answer option of a multi question into a text grid question. The final result of this text grid question will look like below:

AddAOofCloseQuestionsToOpenQuestions1.jpg


To implement this, i created 2 questions:

  • A text grid question which will contain all below Javascript codes. Name of this question is: Case1_Textgrid
  • A multi question which contain the answer option will be added to above text grid question. Name of this question is:Case1_Multi


AddAOofCloseQuestionsToOpenQuestions2.jpg


AddAOofCloseQuestionsToOpenQuestions3.jpg



/**************************************************************************
 * GENERATE WIDGET OF AN ANSWER OPTION OF A CLOSE QUESTION
 * @param object p 	The structure of this object 
								{									
									type: string type, there are 2 available values: 'radio' and 'checkbox', 
									qname: string type, the name of the single/multi question. Ex: 'ChooseQnaireByCountry', 
									aov: string type or number type. Ex: '1', 
									ansv: string type, must be the replacement tags. See this wiki link for more info: http://wiki.catglobe.com/index.php/Replacement_tags,
									aotxt: string type, the text of the answer option. Ex: 'this is answer option 1',						
									isHidden: bool type, whether shown or hide
								} 
 * @return  jqueryObject 	jquery widget element
 **************************************************************************/
function closeQuestionAOWidget(p) {

	var input = $('<input type="' + p.type + '"/>').attr({
					'class': 'ao_input_element ' + p.qname + ' value_' + p.aov,
					'name': 'QUESTION.' + p.qname,
					'value': p.aov,
					'checked': p.ansv == p.aov ? true : false
				});
														
	var span = $('<span>')
					.addClass('ao_text ' + p.qname + ' value_' + p.aov)
					.html(p.aotxt)
					.click(function(e) { 
				            //input.attr('checked') ? input.attr('checked', false) : input.attr('checked', true); 
				            input.click();
			         });

	var hiddenInput = $('<input type="hidden"/>').attr({
						'class': 'ao_hidden_input ' + p.qname + ' value_' + p.aov,
						'name': 'QUESTION.' + p.qname, 					
						'value': ''
					});

	var div = $('<div>')
			  .attr({
					'class': 'ao_container ' + p.qname + ' value_' + p.aov,
					'name': 'QUESTION.' + p.qname,
					'value': p.aov
			  })
			  .css('display', p.isHidden ? 'none' : 'block');
											
	div.append(input, span, hiddenInput);
	
	return div;
}


// DO THE JOB
quest.onInit = function() {
 // Generate the widget of the answer option of the specified close question element (but not yet added to DOM)
 var $widget = closeQuestionAOWidget({
 type: 'checkbox', 
 qname: 'Case1_Multi', 
 aov: '1', 
 ansv: '{{Case1_Multi[0].value}}', 
 aotxt: 'Don’t know / Do not wish to answer', 
 isHidden: false
 });

 // Then add above $widget into the specified location
 $('.QuestionSpace p:first').append($widget);
};