From ee0bc9e47d71998774bb8f3755b9b8aab974c921 Mon Sep 17 00:00:00 2001 From: ihor-romaniuk Date: Fri, 3 May 2024 10:34:57 +0200 Subject: [PATCH] fix: aggregated vote value is not updated till refresh --- feedback/feedback.py | 9 +-- feedback/static/js/src/feedback.js | 92 +++++++++++++------------ feedback/templates/html/feedback.html | 2 +- feedback/templates/html/scale_item.html | 4 ++ feedback/templates/html/staff_item.html | 17 ----- 5 files changed, 57 insertions(+), 67 deletions(-) delete mode 100644 feedback/templates/html/staff_item.html diff --git a/feedback/feedback.py b/feedback/feedback.py index e778f21..a2edfcb 100644 --- a/feedback/feedback.py +++ b/feedback/feedback.py @@ -185,11 +185,7 @@ def student_view(self, context=None): # pylint: disable=unused-argument self.prompt_choice = random.randint(0, len(self.prompts) - 1) prompt = self.get_prompt() - # Staff see vote totals, so we have slightly different HTML here. - if self.vote_aggregate and (self.show_aggregate_to_students or self.is_staff()): - item_templates_file = "templates/html/staff_item.html" - else: - item_templates_file = "templates/html/scale_item.html" + item_templates_file = "templates/html/scale_item.html" # We have five Likert fields right now, but we'd like this to # be dynamic @@ -238,6 +234,7 @@ def get_url(icon_type, i): 'vote_cnt': vote_cnt, 'ina_icon': ina_icon, 'act_icon': act_icon, + 'is_display_vote_cnt': self.vote_aggregate and (self.show_aggregate_to_students or self.is_staff()), }, i18n_service=self.runtime.service(self, 'i18n'), ) for @@ -411,7 +408,7 @@ def feedback(self, data, suffix=''): # pylint: disable=unused-argument "vote": self.user_vote }) - if self.is_staff(): + if self.show_aggregate_to_students or self.is_staff(): response['aggregate'] = self.vote_aggregate return response diff --git a/feedback/static/js/src/feedback.js b/feedback/static/js/src/feedback.js index 03d2f4d..5353d5b 100644 --- a/feedback/static/js/src/feedback.js +++ b/feedback/static/js/src/feedback.js @@ -1,59 +1,65 @@ /* Javascript for FeedbackXBlock. */ // Work-around so we can log in edx-platform, but not fail in Workbench if (typeof Logger === 'undefined') { - var Logger = { - log: function(a, b) { - console.log(JSON.stringify(a)+"/"+JSON.stringify(a)); - } - }; + var Logger = { + log: function (a) { + console.log(JSON.stringify(a) + '/' + JSON.stringify(a)); + } + }; } function FeedbackXBlock(runtime, element) { - function likert_vote() { - var vote = 0; - if ($(".feedback_radio:checked", element).length === 0) { - vote = -1; - } else { - vote = parseInt($(".feedback_radio:checked", element).attr("data-id").split("_")[1]); - } - return vote; + function getLikedVote() { + if ($('.feedback_radio:checked', element).length === 0) { + return -1; } - function feedback() { - return $(".feedback_freeform_area", element).val(); + return parseInt($('.feedback_radio:checked', element).attr('data-id').split('_')[1]); + } + + function getFeedbackMessage() { + return $('.feedback_freeform_area', element).val(); + } + + function updateVoteCount(data) { + if (data.success && data.aggregate && $('.feedback_vote_count', element).length) { + $('.feedback_vote_count', element).each(function (idx) { + $(this).text('(' + data.aggregate[idx] + ')'); + }); } + } - function submit_feedback(freeform, vote) { - var feedback = {}; - if(freeform) { - feedback['freeform'] = freeform; - } - if(vote != -1) { - feedback['vote'] = vote; - } - - Logger.log("edx.feedbackxblock.submitted", feedback); - $.ajax({ - type: "POST", - url: runtime.handlerUrl(element, 'feedback'), - data: JSON.stringify(feedback), - success: function(data) { - $('.feedback_thank_you', element).text(""); - $('.feedback_thank_you', element).text(data.response); - } - }); + function submit_feedback(freeform, vote) { + var feedback = {}; + if (freeform) { + feedback['freeform'] = freeform; + } + if (vote !== -1) { + feedback['vote'] = vote; } - $(".feedback_submit_feedback", element).click(function(eventObject) { - submit_feedback(feedback(), -1); + Logger.log('edx.feedbackxblock.submitted', feedback); + $.ajax({ + type: 'POST', + url: runtime.handlerUrl(element, 'feedback'), + data: JSON.stringify(feedback), + success: function (data) { + $('.feedback_thank_you', element).text(data.response || ''); + updateVoteCount(data); + } }); + } - $('.feedback_radio', element).change(function(eventObject) { - Logger.log("edx.feedbackxblock.likert_changed", {"vote":likert_vote()}); - submit_feedback(false, likert_vote()); - }); + $('.feedback_submit_feedback', element).click(function () { + submit_feedback(getFeedbackMessage(), -1); + }); - $('.feedback_freeform_area', element).change(function(eventObject) { - Logger.log("edx.feedbackxblock.freeform_changed", {"freeform":feedback()}); - }); + $('.feedback_radio', element).change(function () { + Logger.log('edx.feedbackxblock.likert_changed', { vote: getLikedVote() }); + submit_feedback(false, getLikedVote()); + }); + + $('.feedback_freeform_area', element).change(function () { + Logger.log('edx.feedbackxblock.freeform_changed', { freeform: getFeedbackMessage() }); + }); } diff --git a/feedback/templates/html/feedback.html b/feedback/templates/html/feedback.html index 8161648..8a3c1fa 100644 --- a/feedback/templates/html/feedback.html +++ b/feedback/templates/html/feedback.html @@ -11,7 +11,7 @@
- +
diff --git a/feedback/templates/html/scale_item.html b/feedback/templates/html/scale_item.html index 75a22cd..aafd399 100644 --- a/feedback/templates/html/scale_item.html +++ b/feedback/templates/html/scale_item.html @@ -11,5 +11,9 @@
{% trans scale_text %} + {% if is_display_vote_cnt %} +
+ ({{vote_cnt}}) + {% endif %} diff --git a/feedback/templates/html/staff_item.html b/feedback/templates/html/staff_item.html deleted file mode 100644 index 31f5566..0000000 --- a/feedback/templates/html/staff_item.html +++ /dev/null @@ -1,17 +0,0 @@ -{% load i18n %} - -
- -