From dac9b5c038783ec7ff3ae76bdeee54193bcea9ca Mon Sep 17 00:00:00 2001 From: Vasyl Nakvasiuk Date: Mon, 8 Apr 2013 15:48:30 +0300 Subject: [PATCH] Word Cloud: add full cycle Python + HTML + JS --- .../xmodule/css/word_cloud/display.scss | 3 + .../js/src/word_cloud/word_cloud_main.js | 55 ++++++++++++------ .../lib/xmodule/xmodule/tests/test_import.py | 2 +- .../lib/xmodule/xmodule/word_cloud_module.py | 57 ++++++++++--------- .../word_cloud/sequential/Problem_Demos.xml | 6 +- lms/templates/word_cloud.html | 14 +++++ 6 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 common/lib/xmodule/xmodule/css/word_cloud/display.scss diff --git a/common/lib/xmodule/xmodule/css/word_cloud/display.scss b/common/lib/xmodule/xmodule/css/word_cloud/display.scss new file mode 100644 index 0000000000..0957c98fd2 --- /dev/null +++ b/common/lib/xmodule/xmodule/css/word_cloud/display.scss @@ -0,0 +1,3 @@ +.input-cloud { + margin: 5px; +} \ No newline at end of file diff --git a/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js b/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js index f917badab4..d10f1a4c98 100644 --- a/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js +++ b/common/lib/xmodule/xmodule/js/src/word_cloud/word_cloud_main.js @@ -3,33 +3,49 @@ define('WordCloudMain', ['logme'], function (logme) { WordCloudMain.prototype = { -'submitAnswer': function (answer, answerObj) { - var _this; +'submitAnswer': function () { + var _this, sendData; + + sendData = { + 'data': [] + }; _this = this; console.log('submit answer'); - - answerObj.buttonEl.addClass('answered'); + this.wordCloudEl.find('input.input-cloud').each(function(index, value){ + sendData.data.push($(value).val()); + }); // 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, {}, + _this.ajax_url + '/' + 'submit', JSON.stringify(sendData), 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(response); - - - _this.showWordCloud(response.poll_answers, response.total); - + _this.showWordCloud(); } ); }, // End-of: 'submitAnswer': function (answer, answerEl) { '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 = { @@ -38,19 +54,24 @@ return WordCloudMain; function WordCloudMain(el) { var _this; - - this.questionEl = $(el).find('.poll_question'); - if (this.questionEl.length !== 1) { + this.wordCloudEl = $(el).find('.word_cloud'); + if (this.wordCloudEl.length !== 1) { // 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; } - // Access this object inside inner functions. - _this = this; + 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.inputSaveEl.on('click', function () { + _this.submitAnswer(); + }); - this.submitAnswer(this.questionEl) } // End-of: function WordCloudMain(el) { }); // End-of: define('WordCloudMain', ['logme'], function (logme) { diff --git a/common/lib/xmodule/xmodule/tests/test_import.py b/common/lib/xmodule/xmodule/tests/test_import.py index a838c6e3fd..c73f1675c4 100644 --- a/common/lib/xmodule/xmodule/tests/test_import.py +++ b/common/lib/xmodule/xmodule/tests/test_import.py @@ -437,7 +437,7 @@ class ImportTestCase(BaseCourseTestCase): location = Location(location.tag, location.org, location.course, 'sequential', 'Problem_Demos') module = modulestore.get_instance(course.id, location) - self.assertEqual(len(module.children), 2) + self.assertEqual(len(module.children), 1) def test_cohort_config(self): """ diff --git a/common/lib/xmodule/xmodule/word_cloud_module.py b/common/lib/xmodule/xmodule/word_cloud_module.py index 63c8d40f4a..d2f2eef869 100644 --- a/common/lib/xmodule/xmodule/word_cloud_module.py +++ b/common/lib/xmodule/xmodule/word_cloud_module.py @@ -31,8 +31,9 @@ class WordCloudFields(object): display_name = String(help="Display name for this module", 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=[]) + 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_low_border = Integer(help="Number to distinguish top from all words", scope=Scope.content) @@ -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_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" - Number_of_top_words = 250 + number_of_top_words = 250 def handle_ajax(self, dispatch, get): """Ajax handler. @@ -61,6 +62,7 @@ class WordCloudModule(WordCloudFields, XModule): json string """ if dispatch == 'submit': + student_words_from_client = json.loads(get.lists()[0][0])['data'] # self.all_words[word] -= 1 # FIXME: fix this, when xblock will support mutable types. @@ -69,42 +71,44 @@ class WordCloudModule(WordCloudFields, XModule): temp_all_words = self.all_words temp_top_words = self.top_words - if self.submitted: + # if self.submitted: - for word in self.student_words: - temp_all_words[word] -= 1 + # for word in self.student_words: + # temp_all_words[word] -= 1 - if word in temp_top_words: - temp_top_words -= 1 + # if word in temp_top_words: + # temp_top_words -= 1 - else: - self.submitted = True + # else: + # 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: - temp_all_words[word] += 1 + # for word in self.student_words: + # temp_all_words[word] += 1 - if word in temp_top_words: - temp_top_words += 1 - else: - if temp_all_words[word] > top_low_border: - question_words[word] = temp_all_words[word] + # if word in temp_top_words: + # temp_top_words += 1 + # else: + # if temp_all_words[word] > top_low_border: + # question_words[word] = temp_all_words[word] - self.all_words = temp_all_words - - self.top_words = self.update_top_words(question_words, temp_top_words) + # self.all_words = temp_all_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, + # 'top_words': self.top_words, + # }) + + return json.dumps({'student_words': ['aa', 'bb'], 'top_words': ['aa', 'bb', 'RRR'], 'status': 'success'}) elif dispatch == 'get_state': return json.dumps({'student_answers': self.student_answers, - 'top_words': self.top_words + 'top_words': self.top_words, + 'status': 'success' }) else: # return error message return json.dumps({'error': 'Unknown Command!'}) @@ -128,6 +132,7 @@ class WordCloudModule(WordCloudFields, XModule): 'element_class': self.location.category, 'ajax_url': self.system.ajax_url, 'configuration_json': json.dumps({}), + 'num_inputs': int(self.num_inputs), } self.content = self.system.render_template('word_cloud.html', params) return self.content diff --git a/common/test/data/word_cloud/sequential/Problem_Demos.xml b/common/test/data/word_cloud/sequential/Problem_Demos.xml index 24ef73b84e..f08d828f5c 100644 --- a/common/test/data/word_cloud/sequential/Problem_Demos.xml +++ b/common/test/data/word_cloud/sequential/Problem_Demos.xml @@ -1,9 +1,5 @@ - - - Some text - - + diff --git a/lms/templates/word_cloud.html b/lms/templates/word_cloud.html index ffa73bff53..c2d1459063 100644 --- a/lms/templates/word_cloud.html +++ b/lms/templates/word_cloud.html @@ -3,6 +3,20 @@ class="${element_class}" data-ajax-url="${ajax_url}" > + +
+ % for row in range(num_inputs): + + % endfor + +
+ +
+
+ + +