Commit 5ef584a3 by Valera Rozuvan

Added client side check for current state on Poll initialization.

parent d95052e1
...@@ -125,6 +125,17 @@ PollMain.prototype = { ...@@ -125,6 +125,17 @@ PollMain.prototype = {
_this.showAnswerGraph(response.poll_answers, response.total); _this.showAnswerGraph(response.poll_answers, response.total);
_this.jsonConfig.poll_answer = answer;
_this.jsonConfig.total = response.total;
$.each(response.poll_answers, function (index, value) {
_this.jsonConfig.poll_answers[index] = value;
});
logme('Current "jsonConfig": ');
logme(_this.jsonConfig);
_this.questionEl.children('.poll_question_div').html(JSON.stringify(_this.jsonConfig));
/* /*
_this.vertModEl.find('.xmodule_ConditionalModule').each( _this.vertModEl.find('.xmodule_ConditionalModule').each(
function (index, value) { function (index, value) {
...@@ -135,7 +146,56 @@ PollMain.prototype = { ...@@ -135,7 +146,56 @@ PollMain.prototype = {
} }
); );
} }
} // End-of: 'submitAnswer': function (answer, answerEl) { }, // End-of: 'submitAnswer': function (answer, answerEl) {
'postInit': function () {
var _this;
// Access this object inside inner functions.
_this = this;
// Get the DOM id of the question.
this.id = this.questionEl.attr('id');
// Get the URL to which we will post the users answer to the question.
this.ajax_url = this.questionEl.data('ajax-url');
this.questionHtmlMarkup = $('<div />').html(this.jsonConfig.question).text();
this.questionEl.append(this.questionHtmlMarkup);
// When the user selects and answer, we will set this flag to true.
this.questionAnswered = false;
logme('this.jsonConfig.answers: ', this.jsonConfig.answers);
logme('this.jsonConfig.poll_answer: ', this.jsonConfig.poll_answer);
$.each(this.jsonConfig.answers, function (index, value) {
var answerEl;
answerEl = $('<div class="poll_answer">' + value + '</li>');
answerEl.on('click', function () {
_this.submitAnswer(index, answerEl);
});
if (index === _this.jsonConfig.poll_answer) {
answerEl.addClass('answered');
_this.questionAnswered = true;
}
answerEl.appendTo(_this.questionEl);
});
this.graphAnswerEl = $('<div class="graph_answer"></div>');
this.graphAnswerEl.hide();
this.graphAnswerEl.appendTo(this.questionEl);
logme('PollMain object: ', this);
// If it turns out that the user already answered the question, show the answers graph.
if (this.questionAnswered === true) {
this.showAnswerGraph(this.jsonConfig.poll_answers, this.jsonConfig.total);
}
} // End-of: 'postInit': function () {
}; // End-of: PollMain.prototype = { }; // End-of: PollMain.prototype = {
return PollMain; return PollMain;
...@@ -227,6 +287,8 @@ function PollMain(el) { ...@@ -227,6 +287,8 @@ function PollMain(el) {
"three of them to kill and eat the cabin boy, in order to save their own lives?&lt;/p&gt;" "three of them to kill and eat the cabin boy, in order to save their own lives?&lt;/p&gt;"
}; };
} }
_this.postInit();
}()); }());
} else { } else {
try { try {
...@@ -253,47 +315,15 @@ function PollMain(el) { ...@@ -253,47 +315,15 @@ function PollMain(el) {
return; return;
} }
// Get the DOM id of the question. $.postWithPrefix(
this.id = this.questionEl.attr('id'); _this.ajax_url + '/' + 'get_state', {},
function (response) {
// Get the URL to which we will post the users answer to the question. logme('Get pre init state.');
this.ajax_url = this.questionEl.data('ajax-url'); logme('response:', response);
this.questionHtmlMarkup = $('<div />').html(this.jsonConfig.question).text(); _this.postInit();
this.questionEl.append(this.questionHtmlMarkup);
// When the user selects and answer, we will set this flag to true.
this.questionAnswered = false;
logme('this.jsonConfig.answers: ', this.jsonConfig.answers);
logme('this.jsonConfig.poll_answer: ', this.jsonConfig.poll_answer);
$.each(this.jsonConfig.answers, function (index, value) {
var answerEl;
answerEl = $('<div class="poll_answer">' + value + '</li>');
answerEl.on('click', function () {
_this.submitAnswer(index, answerEl);
});
if (index === _this.jsonConfig.poll_answer) {
answerEl.addClass('answered');
_this.questionAnswered = true;
} }
);
answerEl.appendTo(_this.questionEl);
});
this.graphAnswerEl = $('<div class="graph_answer"></div>');
this.graphAnswerEl.hide();
this.graphAnswerEl.appendTo(this.questionEl);
logme('PollMain object: ', this);
// If it turns out that the user already answered the question, show the answers graph.
if (this.questionAnswered === true) {
this.showAnswerGraph(this.jsonConfig.poll_answers, this.jsonConfig.total);
}
} // End-of: function PollMain(el) { } // End-of: function PollMain(el) {
}); // End-of: define('PollMain', ['logme'], function (logme) { }); // End-of: define('PollMain', ['logme'], function (logme) {
......
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