From 16430dcc0d2f91ccda4590ef614e5ee1b806b49b Mon Sep 17 00:00:00 2001 From: Giulio Gratta Date: Tue, 25 Sep 2018 14:05:36 -0700 Subject: [PATCH 1/3] Show Peer Responses on page load if q answered - Hide responses if feature turned off - Hide responses on page load if NOT answered - Display responses is student answer valid --- freetextresponse/freetextresponse.py | 14 ++++++++---- freetextresponse/public/view.css | 4 +++- freetextresponse/public/view.js | 22 ++++++++++++------- .../templates/freetextresponse_view.html | 8 +++++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/freetextresponse/freetextresponse.py b/freetextresponse/freetextresponse.py index a71febe0..82baf92a 100644 --- a/freetextresponse/freetextresponse.py +++ b/freetextresponse/freetextresponse.py @@ -279,7 +279,6 @@ def student_view(self, context={}): (Fragment): The HTML Fragment for this XBlock, which determines the general frame of the FreeTextResponse Question. """ - self.runtime.service(self, 'i18n') context.update( { @@ -294,7 +293,7 @@ def student_view(self, context={}): 'visibility_class': self._get_indicator_visibility_class(), 'word_count_message': self._get_word_count_message(), 'display_other_responses': self.display_other_student_responses, - 'other_responses': [], + 'other_responses': self.get_other_answers(), } ) template = get_template('freetextresponse_view.html') @@ -602,7 +601,7 @@ def submit(self, data, suffix=''): 'user_alert': self._get_user_alert( ignore_attempts=True, ), - 'other_responses': self.get_other_answers(self.get_student_id()), + 'other_responses': self.get_other_answers(), 'display_other_responses': self.display_other_student_responses, 'visibility_class': self._get_indicator_visibility_class(), } @@ -654,12 +653,19 @@ def store_student_response(self): # MAX_RESPONSES answers if their answer is in the pool. self.displayable_answers = self.displayable_answers[-(MAX_RESPONSES+1):] - def get_other_answers(self, student_id): + def get_other_answers(self): """ Returns at most MAX_RESPONSES answers from the pool. Does not return answers the student had submitted. """ + student_id = self.get_student_id() + display_other_responses = self.display_other_student_responses + shouldnt_show_other_responses = not display_other_responses + student_answer_incorrect = self._determine_credit() == Credit.zero + if student_answer_incorrect or shouldnt_show_other_responses: + return [] + return_list = [ response for response in self.displayable_answers diff --git a/freetextresponse/public/view.css b/freetextresponse/public/view.css index fe0c61c5..2ad15442 100644 --- a/freetextresponse/public/view.css +++ b/freetextresponse/public/view.css @@ -104,12 +104,14 @@ } .freetextresponse .responses-box { - display: none; margin-top: 10px; padding: 10px; background: #fbfbfb; border: 1px solid #ddd; } +.freetextresponse .responses-box.hidden { + display: none; +} .freetextresponse .responses-title { margin-left: 15px; diff --git a/freetextresponse/public/view.js b/freetextresponse/public/view.js index a2fba14b..54c1d299 100644 --- a/freetextresponse/public/view.js +++ b/freetextresponse/public/view.js @@ -68,13 +68,7 @@ function FreeTextResponseView(runtime, element) { userAlertMessage.text(response.user_alert); buttonSave.addClass(response.nodisplay_class); setClassForTextAreaParent(response.indicator_class); - if (!response.user_alert && response.display_other_responses) { - var responseHTML = get_student_responses_html(response.other_responses); - if (responseHTML) { - responseList.html(responseHTML); - } - $element.find('.responses-box').show(); - } + displayResponsesIfAnswered(response); $xblocksContainer.data(cachedAnswerId, $element.find('.student_answer').val()); $xblocksContainer.data(problemProgressId, response.problem_progress); @@ -91,17 +85,29 @@ function FreeTextResponseView(runtime, element) { return false; }); - function get_student_responses_html(responses) { + function getStudentResponsesHtml(responses) { /* Convert list of responses to a html string to add to the page */ var html = ''; + var noResponsesText = responseList.data('noresponse'); responses.forEach(function(item) { html += '
  • ' + item.answer + '
  • '; }); + html = html || '
  • ' + noResponsesText + '
  • '; return html; } + function displayResponsesIfAnswered(response) { + if (!response.display_other_responses) { + $element.find('.responses-box').addClass('hidden'); + return; + } + var responseHTML = getStudentResponsesHtml(response.other_responses); + responseList.html(responseHTML); + $element.find('.responses-box').removeClass('hidden'); + } + buttonSave.on('click', function () { buttonSave.text(buttonSave[0].dataset.checking); runtime.notify('save', { diff --git a/freetextresponse/templates/freetextresponse_view.html b/freetextresponse/templates/freetextresponse_view.html index eea4e252..99bb5de4 100644 --- a/freetextresponse/templates/freetextresponse_view.html +++ b/freetextresponse/templates/freetextresponse_view.html @@ -30,7 +30,7 @@

    {{ display_name }}

    {{ user_alert }}
    {% if display_other_responses %} -
    +

    Submissions by others

      -
    • No responses to show at this time
    • + {% for response in other_responses %} +
    • {{ response.answer }}
    • + {% empty %} +
    • No responses to show at this time
    • + {% endfor %}
    {% endif %} From 23914ae02b91b0047ed1908df99f2a760f028f8b Mon Sep 17 00:00:00 2001 From: Giulio Gratta Date: Tue, 4 Dec 2018 10:19:23 -0800 Subject: [PATCH 2/3] Allow for translation of text in HTML template --- .../templates/freetextresponse_view.html | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/freetextresponse/templates/freetextresponse_view.html b/freetextresponse/templates/freetextresponse_view.html index 99bb5de4..19ddcdb6 100644 --- a/freetextresponse/templates/freetextresponse_view.html +++ b/freetextresponse/templates/freetextresponse_view.html @@ -12,8 +12,7 @@

    {{ display_name }}

    {% if display_other_responses %} - Allow my response to possibly be visible by other learners after - submitting their response + {% trans "Allow my response to possibly be visible by other learners after submitting their response" %}
    {% endif %}
    {{ submitted_message }}
    @@ -32,16 +31,16 @@

    {{ display_name }}

    {% if display_other_responses %}
    -

    Submissions by others

    -
    From 66ecc582d392bd95f4090f40730a7688b314df22 Mon Sep 17 00:00:00 2001 From: Giulio Gratta Date: Tue, 4 Dec 2018 10:19:44 -0800 Subject: [PATCH 3/3] Fix pep8 and pylint errors in modified files - define new variables to shorten line lengths - remove unused import - indent arguments - remove empty lines - disable too-few-public-methods where pertinent --- freetextresponse/freetextresponse.py | 21 ++++++++++++--------- freetextresponse/mixins.py | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/freetextresponse/freetextresponse.py b/freetextresponse/freetextresponse.py index 82baf92a..c1f60fa1 100644 --- a/freetextresponse/freetextresponse.py +++ b/freetextresponse/freetextresponse.py @@ -2,7 +2,6 @@ This is the core logic for the Free-text Response XBlock """ from enum import Enum -import pkg_resources from django.db import IntegrityError from django.template.context import Context from django.template.loader import get_template @@ -27,10 +26,10 @@ @XBlock.needs("i18n") class FreeTextResponse( - EnforceDueDates, - MissingDataFetcherMixin, - StudioEditableXBlockMixin, - XBlock, + EnforceDueDates, + MissingDataFetcherMixin, + StudioEditableXBlockMixin, + XBlock, ): # pylint: disable=too-many-ancestors, too-many-instance-attributes """ @@ -279,6 +278,9 @@ def student_view(self, context={}): (Fragment): The HTML Fragment for this XBlock, which determines the general frame of the FreeTextResponse Question. """ + + display_other_responses = self.display_other_student_responses + self.runtime.service(self, 'i18n') context.update( { @@ -292,7 +294,7 @@ def student_view(self, context={}): 'used_attempts_feedback': self._get_used_attempts_feedback(), 'visibility_class': self._get_indicator_visibility_class(), 'word_count_message': self._get_word_count_message(), - 'display_other_responses': self.display_other_student_responses, + 'display_other_responses': display_other_responses, 'other_responses': self.get_other_answers(), } ) @@ -589,7 +591,8 @@ def submit(self, data, suffix=''): # even if word count is invalid. self.count_attempts += 1 self._compute_score() - if self.display_other_student_responses and data.get('can_record_response'): + display_other_responses = self.display_other_student_responses + if display_other_responses and data.get('can_record_response'): self.store_student_response() result = { 'status': 'success', @@ -651,7 +654,8 @@ def store_student_response(self): # Want to store extra response so student can still see # MAX_RESPONSES answers if their answer is in the pool. - self.displayable_answers = self.displayable_answers[-(MAX_RESPONSES+1):] + response_index = -(MAX_RESPONSES+1) + self.displayable_answers = self.displayable_answers[response_index:] def get_other_answers(self): """ @@ -665,7 +669,6 @@ def get_other_answers(self): student_answer_incorrect = self._determine_credit() == Credit.zero if student_answer_incorrect or shouldnt_show_other_responses: return [] - return_list = [ response for response in self.displayable_answers diff --git a/freetextresponse/mixins.py b/freetextresponse/mixins.py index 6517bab9..73c1a3dc 100644 --- a/freetextresponse/mixins.py +++ b/freetextresponse/mixins.py @@ -1,10 +1,11 @@ """ Mixins for the Free Text Response XBlock """ +# pylint: disable=too-few-public-methods import datetime -class EnforceDueDates(object): # pylint: disable=too-few-public-methods +class EnforceDueDates(object): """ xBlock Mixin to allow xblocks to check the due date (taking the graceperiod into account) of the