Commit 18c74afa by Valera Rozuvan

Attached callback to questions answer text. Fixed empty really hidden…

Attached callback to questions answer text. Fixed empty really hidden conditional. Removed tests from JS part.
parent ce7280bd
...@@ -142,6 +142,7 @@ section.poll_question { ...@@ -142,6 +142,7 @@ section.poll_question {
margin-left: 20px; margin-left: 20px;
height: auto; height: auto;
margin-bottom: 20px; margin-bottom: 20px;
cursor: pointer;
&.short { &.short {
width: 100px; width: 100px;
......
...@@ -23,9 +23,18 @@ class @Conditional ...@@ -23,9 +23,18 @@ class @Conditional
@el.html '' @el.html ''
@el.append(i) for i in response.html @el.append(i) for i in response.html
parentEl = $(element).parent()
parentId = parentEl.attr 'id'
if @el.html().length < 5 if @el.html().length < 5
$(element).hide() if parentId.indexOf('vert') is 0
parentEl.hide()
else
$(element).hide()
else else
$(element).show() if parentId.indexOf('vert') is 0
parentEl.show()
else
$(element).show()
XModule.loadModules @el XModule.loadModules @el
(function (requirejs, require, define) { (function (requirejs, require, define) {
define('PollMain', ['logme'], function (logme) { define('PollMain', ['logme'], function (logme) {
var debugMode;
debugMode = false;
if (debugMode === true) {
logme('MESSAGE: We are in debug mode.');
}
PollMain.prototype = { PollMain.prototype = {
...@@ -50,37 +44,20 @@ PollMain.prototype = { ...@@ -50,37 +44,20 @@ PollMain.prototype = {
answerObj.buttonEl.addClass('answered'); answerObj.buttonEl.addClass('answered');
if (debugMode === true) { // Send the data to the server as an AJAX request. Attach a callback that will
(function () { // be fired on server's response.
var response; $.postWithPrefix(
_this.ajax_url + '/' + answer, {},
response = { function (response) {
'poll_answers': {
'Yes': '1',
'No': '1',
'Dont_know': '8'
},
'total': '10'
};
_this.showAnswerGraph(response.poll_answers, response.total); _this.showAnswerGraph(response.poll_answers, response.total);
}());
} else {
// Send the data to the server as an AJAX request. Attach a callback that will
// be fired on server's response.
$.postWithPrefix(
_this.ajax_url + '/' + answer, {},
function (response) {
_this.showAnswerGraph(response.poll_answers, response.total);
if (_this.wrapperSectionEl !== null) { if (_this.wrapperSectionEl !== null) {
$(_this.wrapperSectionEl).find('.xmodule_ConditionalModule').each(function (index, value) { $(_this.wrapperSectionEl).find('.xmodule_ConditionalModule').each(function (index, value) {
new window.Conditional(value); new window.Conditional(value);
}); });
}
} }
); }
} );
}, // End-of: 'submitAnswer': function (answer, answerEl) { }, // End-of: 'submitAnswer': function (answer, answerEl) {
'postInit': function () { 'postInit': function () {
...@@ -163,6 +140,10 @@ PollMain.prototype = { ...@@ -163,6 +140,10 @@ PollMain.prototype = {
answer.el.appendTo(_this.questionEl); answer.el.appendTo(_this.questionEl);
answer.textEl.on('click', function () {
_this.submitAnswer(index, answer);
});
answer.buttonEl.on('click', function () { answer.buttonEl.on('click', function () {
_this.submitAnswer(index, answer); _this.submitAnswer(index, answer);
}); });
...@@ -239,92 +220,32 @@ function PollMain(el) { ...@@ -239,92 +220,32 @@ function PollMain(el) {
} }
}($(el)[0], 0)); }($(el)[0], 0));
// Test case for when the server part is still not ready. Change to 'false' so you can test actual server try {
// generated JSON config. this.jsonConfig = JSON.parse(this.questionEl.children('.poll_question_div').html());
if (debugMode === true) {
(function () {
var testNum;
// Test for when the user has already answered.
// testNum = 1;
// Test for when the user did not answer yet.
testNum = 2;
if (testNum === 1) {
_this.jsonConfig = {
'poll_answers': {
'Dont_know': '2',
'No': '1',
'Yes': '4'
},
'total': '7',
'poll_answer': 'No',
'answers': {
'Dont_know':
'Don\'t know. What does it mean to not know? Well, the student must be able to ' +
'answer this question for himself. In the case when difficulties arise, he should' +
'consult a TA.',
'No': 'No',
'Yes': 'Yes'
},
'question':
"&lt;h3&gt;What's the Right Thing to Do?&lt;/h3&gt;&lt;p&gt;Suppose four shipwrecked " +
"sailors are stranded at sea in a lifeboat, without food or water. Would it be wrong for " +
"three of them to kill and eat the cabin boy, in order to save their own lives?&lt;/p&gt;"
};
} else if (testNum === 2) {
_this.jsonConfig = {
'poll_answers': {},
'total': '',
'poll_answer': '',
'answers': {
'Dont_know':
'Don\'t know. What does it mean to not know? Well, the student must be able to ' +
'answer this question for himself. In the case when difficulties arise, he should' +
'consult a TA.',
'No': 'No',
'Yes': 'Yes'
},
'question':
"&lt;h3&gt;What's the Right Thing to Do?&lt;/h3&gt;&lt;p&gt;Suppose four shipwrecked " +
"sailors are stranded at sea in a lifeboat, without food or water. Would it be wrong for " +
"three of them to kill and eat the cabin boy, in order to save their own lives?&lt;/p&gt;"
};
}
_this.postInit(); $.postWithPrefix(
}()); '' + this.questionEl.data('ajax-url') + '/' + 'get_state', {},
function (response) {
_this.jsonConfig.poll_answer = response.poll_answer;
_this.jsonConfig.total = response.total;
return; $.each(response.poll_answers, function (index, value) {
} else { _this.jsonConfig.poll_answers[index] = value;
try { });
this.jsonConfig = JSON.parse(this.questionEl.children('.poll_question_div').html());
$.postWithPrefix(
'' + this.questionEl.data('ajax-url') + '/' + 'get_state', {},
function (response) {
_this.jsonConfig.poll_answer = response.poll_answer;
_this.jsonConfig.total = response.total;
$.each(response.poll_answers, function (index, value) {
_this.jsonConfig.poll_answers[index] = value;
});
_this.questionEl.children('.poll_question_div').html(JSON.stringify(_this.jsonConfig));
_this.postInit();
}
);
return; _this.questionEl.children('.poll_question_div').html(JSON.stringify(_this.jsonConfig));
} catch (err) { _this.postInit();
logme( }
'ERROR: Invalid JSON config for poll ID "' + this.id + '".', );
'Error messsage: "' + err.message + '".'
);
return; return;
} } catch (err) {
logme(
'ERROR: Invalid JSON config for poll ID "' + this.id + '".',
'Error messsage: "' + err.message + '".'
);
return;
} }
} // End-of: function PollMain(el) { } // End-of: function PollMain(el) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment