Commit dac9b5c0 by Vasyl Nakvasiuk

Word Cloud: add full cycle Python + HTML + JS

parent 5a36b7c3
.input-cloud {
margin: 5px;
}
\ No newline at end of file
...@@ -3,33 +3,49 @@ define('WordCloudMain', ['logme'], function (logme) { ...@@ -3,33 +3,49 @@ define('WordCloudMain', ['logme'], function (logme) {
WordCloudMain.prototype = { WordCloudMain.prototype = {
'submitAnswer': function (answer, answerObj) { 'submitAnswer': function () {
var _this; var _this, sendData;
sendData = {
'data': []
};
_this = this; _this = this;
console.log('submit answer'); console.log('submit answer');
this.wordCloudEl.find('input.input-cloud').each(function(index, value){
answerObj.buttonEl.addClass('answered'); sendData.data.push($(value).val());
});
// Send the data to the server as an AJAX request. Attach a callback that will // Send the data to the server as an AJAX request. Attach a callback that will
// be fired on server's response. // be fired on server's response.
$.postWithPrefix( $.postWithPrefix(
_this.ajax_url + '/' + answer, {}, _this.ajax_url + '/' + 'submit', JSON.stringify(sendData),
function (response) { function (response) {
if (
(response.hasOwnProperty('status') !== true) ||
(typeof response.status !== 'string') ||
(response.status.toLowerCase() !== 'success')) {
console.log('Bad response!');
return;
}
console.log('success! response = '); console.log('success! response = ');
console.log(response); console.log(response);
_this.showWordCloud();
_this.showWordCloud(response.poll_answers, response.total);
} }
); );
}, // End-of: 'submitAnswer': function (answer, answerEl) { }, // End-of: 'submitAnswer': function (answer, answerEl) {
'showWordCloud': function(){ 'showWordCloud': function(){
console.log('TADAM!!!') console.log('Show word cloud.');
inputSection = this.wordCloudEl.find('#input-cloud-section');
resultSection = this.wordCloudEl.find('#result-cloud-section');
resultSection.text('TODO: Word cloud canvas');
inputSection.hide();
resultSection.show();
}, },
}; // End-of: WordCloudMain.prototype = { }; // End-of: WordCloudMain.prototype = {
...@@ -38,19 +54,24 @@ return WordCloudMain; ...@@ -38,19 +54,24 @@ return WordCloudMain;
function WordCloudMain(el) { function WordCloudMain(el) {
var _this; var _this;
this.wordCloudEl = $(el).find('.word_cloud');
this.questionEl = $(el).find('.poll_question'); if (this.wordCloudEl.length !== 1) {
if (this.questionEl.length !== 1) {
// We require one question DOM element. // We require one question DOM element.
logme('ERROR: WordCloudMain constructor requires one question DOM element.'); logme('ERROR: WordCloudMain constructor requires one word cloud DOM element.');
return; return;
} }
// Access this object inside inner functions. this.inputSaveEl = $(el).find('input.save');
// Get the URL to which we will post the users words.
this.ajax_url = this.wordCloudEl.data('ajax-url');
_this = this; _this = this;
this.inputSaveEl.on('click', function () {
_this.submitAnswer();
});
this.submitAnswer(this.questionEl)
} // End-of: function WordCloudMain(el) { } // End-of: function WordCloudMain(el) {
}); // End-of: define('WordCloudMain', ['logme'], function (logme) { }); // End-of: define('WordCloudMain', ['logme'], function (logme) {
......
...@@ -437,7 +437,7 @@ class ImportTestCase(BaseCourseTestCase): ...@@ -437,7 +437,7 @@ class ImportTestCase(BaseCourseTestCase):
location = Location(location.tag, location.org, location.course, location = Location(location.tag, location.org, location.course,
'sequential', 'Problem_Demos') 'sequential', 'Problem_Demos')
module = modulestore.get_instance(course.id, location) module = modulestore.get_instance(course.id, location)
self.assertEqual(len(module.children), 2) self.assertEqual(len(module.children), 1)
def test_cohort_config(self): def test_cohort_config(self):
""" """
......
...@@ -31,8 +31,9 @@ class WordCloudFields(object): ...@@ -31,8 +31,9 @@ class WordCloudFields(object):
display_name = String(help="Display name for this module", scope=Scope.settings) display_name = String(help="Display name for this module", scope=Scope.settings)
num_inputs = Integer(help="Number of inputs", scope=Scope.settings) num_inputs = Integer(help="Number of inputs", scope=Scope.settings)
submitted = Boolean(help="Whether this student has voted on the poll", scope=Scope.student_state, default=False) submitted = Boolean(help="Whether this student has posted words to the cloud", scope=Scope.student_state, default=False)
student_words= List(help="Student answer", scope=Scope.student_state, default=[]) student_words= List(help="Student answer", scope=Scope.student_state, default=[])
all_words = Object(help="All possible words from other students", scope=Scope.content) all_words = Object(help="All possible words from other students", scope=Scope.content)
top_words = Object(help="Top N words for word cloud", scope=Scope.content) top_words = Object(help="Top N words for word cloud", scope=Scope.content)
top_low_border = Integer(help="Number to distinguish top from all words", scope=Scope.content) top_low_border = Integer(help="Number to distinguish top from all words", scope=Scope.content)
...@@ -45,10 +46,10 @@ class WordCloudModule(WordCloudFields, XModule): ...@@ -45,10 +46,10 @@ class WordCloudModule(WordCloudFields, XModule):
resource_string(__name__, 'js/src/word_cloud/word_cloud.js'), resource_string(__name__, 'js/src/word_cloud/word_cloud.js'),
resource_string(__name__, 'js/src/word_cloud/word_cloud_main.js')] resource_string(__name__, 'js/src/word_cloud/word_cloud_main.js')]
} }
# css = {'scss': [resource_string(__name__, 'css/word_cloud/display.scss')]} css = {'scss': [resource_string(__name__, 'css/word_cloud/display.scss')]}
js_module_name = "WordCloud" js_module_name = "WordCloud"
Number_of_top_words = 250 number_of_top_words = 250
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
"""Ajax handler. """Ajax handler.
...@@ -61,6 +62,7 @@ class WordCloudModule(WordCloudFields, XModule): ...@@ -61,6 +62,7 @@ class WordCloudModule(WordCloudFields, XModule):
json string json string
""" """
if dispatch == 'submit': if dispatch == 'submit':
student_words_from_client = json.loads(get.lists()[0][0])['data']
# self.all_words[word] -= 1 # self.all_words[word] -= 1
# FIXME: fix this, when xblock will support mutable types. # FIXME: fix this, when xblock will support mutable types.
...@@ -69,42 +71,44 @@ class WordCloudModule(WordCloudFields, XModule): ...@@ -69,42 +71,44 @@ class WordCloudModule(WordCloudFields, XModule):
temp_all_words = self.all_words temp_all_words = self.all_words
temp_top_words = self.top_words temp_top_words = self.top_words
if self.submitted: # if self.submitted:
for word in self.student_words: # for word in self.student_words:
temp_all_words[word] -= 1 # temp_all_words[word] -= 1
if word in temp_top_words: # if word in temp_top_words:
temp_top_words -= 1 # temp_top_words -= 1
else: # else:
self.submitted = True # self.submitted = True
self.student_words = get['student_words'] # self.student_words = student_words_from_client
question_words = {} # question_words = {}
for word in self.student_words: # for word in self.student_words:
temp_all_words[word] += 1 # temp_all_words[word] += 1
if word in temp_top_words: # if word in temp_top_words:
temp_top_words += 1 # temp_top_words += 1
else: # else:
if temp_all_words[word] > top_low_border: # if temp_all_words[word] > top_low_border:
question_words[word] = temp_all_words[word] # question_words[word] = temp_all_words[word]
self.all_words = temp_all_words # self.all_words = temp_all_words
# self.top_words = self.update_top_words(question_words, temp_top_words)
self.top_words = self.update_top_words(question_words, temp_top_words)
# return json.dumps({'student_words': self.student_words,
# 'top_words': self.top_words,
# })
return json.dumps({'student_words': self.student_words, return json.dumps({'student_words': ['aa', 'bb'], 'top_words': ['aa', 'bb', 'RRR'], 'status': 'success'})
'top_words': self.top_words,
})
elif dispatch == 'get_state': elif dispatch == 'get_state':
return json.dumps({'student_answers': self.student_answers, return json.dumps({'student_answers': self.student_answers,
'top_words': self.top_words 'top_words': self.top_words,
'status': 'success'
}) })
else: # return error message else: # return error message
return json.dumps({'error': 'Unknown Command!'}) return json.dumps({'error': 'Unknown Command!'})
...@@ -128,6 +132,7 @@ class WordCloudModule(WordCloudFields, XModule): ...@@ -128,6 +132,7 @@ class WordCloudModule(WordCloudFields, XModule):
'element_class': self.location.category, 'element_class': self.location.category,
'ajax_url': self.system.ajax_url, 'ajax_url': self.system.ajax_url,
'configuration_json': json.dumps({}), 'configuration_json': json.dumps({}),
'num_inputs': int(self.num_inputs),
} }
self.content = self.system.render_template('word_cloud.html', params) self.content = self.system.render_template('word_cloud.html', params)
return self.content return self.content
......
<sequential> <sequential>
<vertical> <vertical>
<word_cloud display_name="cloud" num_inputs="5"> <word_cloud display_name="cloud" num_inputs="5" />
<html>
Some text
</html>
</word_cloud>
</vertical> </vertical>
</sequential> </sequential>
...@@ -3,6 +3,20 @@ ...@@ -3,6 +3,20 @@
class="${element_class}" class="${element_class}"
data-ajax-url="${ajax_url}" data-ajax-url="${ajax_url}"
> >
<section id="input-cloud-section">
% for row in range(num_inputs):
<input class="input-cloud" type="text" size="40" />
% endfor
<section class="action">
<input class="save" type="button" value="Save" />
</section>
</section>
<section id="result-cloud-section" style="display: none;">
</section>
<!-- Hidden field to read configuration JSON from. --> <!-- Hidden field to read configuration JSON from. -->
<div class="${element_class}_div" id="${element_id}_json" style="display: none;">${configuration_json}</div> <div class="${element_class}_div" id="${element_id}_json" style="display: none;">${configuration_json}</div>
</section> </section>
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