Skip to content

Commit

Permalink
Merge pull request openedx-unsupported#237 from edx/dsjen/answer-dist…
Browse files Browse the repository at this point in the history
…ribution-value

Updated to use answer_value field.
  • Loading branch information
dsjen committed Jan 22, 2015
2 parents ed3039f + 41dd72d commit f510e5a
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 39 deletions.
9 changes: 2 additions & 7 deletions acceptance_tests/test_course_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ def _test_table(self):

rows = self.page.browser.find_elements_by_css_selector('{} tbody tr'.format(table_section_selector))

value_field = 'answer_value_text'
if self.answer_distribution[0]['answer_value_text'] is None:
value_field = 'answer_value_numeric'
value_field = 'answer_value'

for i, row in enumerate(rows):
answer = self.answer_distribution[i]
Expand All @@ -81,10 +79,7 @@ def _test_table(self):
for col in columns:
actual.append(col.text)

answer_value = answer[value_field]
if value_field == 'answer_value_numeric':
answer_value = str(('%f' % answer_value).rstrip('0').rstrip('.'))
expected = [answer_value]
expected = [answer[value_field]]
correct = '-'
if answer['correct']:
correct = 'Correct'
Expand Down
23 changes: 8 additions & 15 deletions analytics_dashboard/courses/presenters/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,15 @@ def _build_problem_description(self, problem_part_id, questions):
return problem['question']

def _get_answer_type(self, answer_distributions):
""" Returns either answer_value_text or answer_value_numeric. """

# answer field for numeric values returned from API
numeric_field = 'answer_value_numeric'
# answer field for text values returned from API
text_field = 'answer_value_text'

# returns the first field found to be filled
"""
Returns either 'text' or 'numeric' to describe the answer and used in the JS table to format
and sort the dataset.
"""
field = 'answer_value'
for ad in answer_distributions:
if ad[numeric_field] is not None:
return numeric_field
elif ad[text_field] is not None:
return text_field

# default to text answer if both fields were null/none
return text_field
if ad[field] is not None and not utils.number.is_number(ad[field]):
return 'text'
return 'numeric'

def _is_answer_distribution_random(self, answer_distributions):
"""
Expand Down
8 changes: 4 additions & 4 deletions analytics_dashboard/courses/tests/test_presenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def test_multiple_answer_distribution(self, mock_answer_distribution):
'active_question': 'Submissions for Part 1: Is this a text problem?',
'problem_part_description': 'Example problem - Submissions for Part 1: Is this a text problem?',
'is_random': False,
'answer_type': 'answer_value_text'
'answer_type': 'text'
}
},
{
Expand All @@ -272,7 +272,7 @@ def test_multiple_answer_distribution(self, mock_answer_distribution):
'active_question': 'Submissions for Part 2: Is this a numeric problem?',
'problem_part_description': 'Example problem - Submissions for Part 2: Is this a numeric problem?',
'is_random': False,
'answer_type': 'answer_value_numeric'
'answer_type': 'numeric'
}
},
{
Expand All @@ -282,7 +282,7 @@ def test_multiple_answer_distribution(self, mock_answer_distribution):
'problem_part_description': 'Example problem - Submissions for Part 3: Is this a '
'randomized problem?',
'is_random': True,
'answer_type': 'answer_value_numeric'
'answer_type': 'numeric'
}
}
]
Expand All @@ -302,7 +302,7 @@ def test_single_answer_distribution(self, mock_answer_distribution):
'active_question': 'Submissions: Is this a text problem?',
'problem_part_description': 'Example problem - Submissions: Is this a text problem?',
'is_random': False,
'answer_type': 'answer_value_text'
'answer_type': 'text'
}
}
]
Expand Down
8 changes: 8 additions & 0 deletions analytics_dashboard/courses/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ def test_is_feature_enabled(self):
switch.active = True
switch.save()
self.assertTrue(utils.is_feature_enabled(item))


class NumberTests(TestCase):
def test_is_number(self):
self.assertTrue(utils.number.is_number('-123'))
self.assertTrue(utils.number.is_number('12678.123'))
self.assertFalse(utils.number.is_number('45Test'))
self.assertFalse(utils.number.is_number('edx'))
17 changes: 8 additions & 9 deletions analytics_dashboard/courses/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ def get_mock_api_answer_distribution_multiple_questions_data(course_id):

for text_response in ['Asia', 'Europe', 'Africa']:
answers.append({
'answer_value_numeric': None,
'answer_value_text': text_response,
'answer_value': text_response,
'correct': False,
'count': total_count,
'course_id': course_id,
Expand All @@ -536,8 +535,7 @@ def get_mock_api_answer_distribution_multiple_questions_data(course_id):

for numeric_value in range(20):
answers.append({
'answer_value_numeric': numeric_value,
'answer_value_text': None,
'answer_value': numeric_value,
'correct': False,
'count': total_count,
'course_id': course_id,
Expand All @@ -554,8 +552,7 @@ def get_mock_api_answer_distribution_multiple_questions_data(course_id):

for randomized in range(5):
answers.append({
'answer_value_numeric': 0,
'answer_value_text': None,
'answer_value': 0,
'correct': True,
'count': total_count,
'course_id': course_id,
Expand Down Expand Up @@ -618,9 +615,11 @@ def get_presenter_answer_distribution(course_id, problem_part_id):
answer_distributions = get_filtered_answer_distribution(course_id, problem_part_id)
answer_distribution_limited = answer_distributions[:12]
is_random = answer_distribution_limited[0]['variant'] is not None
answer_type = 'answer_value_text'
if answer_distribution_limited[0]['answer_value_text'] is None:
answer_type = 'answer_value_numeric'
try:
float(answer_distribution_limited[0]['answer_value'])
answer_type = 'numeric'
except ValueError:
answer_type = 'text'
problem_part_description = 'Example problem - Submissions for Part 1: Is this a text problem?'

return AnswerDistributionEntry(CREATED_DATETIME, questions, active_question, answer_distributions,
Expand Down
10 changes: 10 additions & 0 deletions analytics_dashboard/courses/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ def is_feature_enabled(item):
return True


class number(object):
@staticmethod
def is_number(word):
try:
float(word)
return True
except ValueError:
return False


class math(object):
@staticmethod
def calculate_percent(count, total):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
function (_, DataTableView, DiscreteBarView) {

var courseModel = page.models.courseModel,
answerColumn = {key: courseModel.get('answerType'), title: gettext('Answer'), type:'hasNull'},
answerField = 'answer_value',
answerColumn = {key: answerField, title: gettext('Answer'), type:'hasNull'},
tableColumns = [
answerColumn,
{key: 'correct', title: gettext('Correct'), type: 'bool'},
{key: 'count', title: gettext('Submission Count'), type: 'number', className: 'text-right'}
];

// answers are stored either the numeric or string fields
if (courseModel.get('answerType') === 'answer_value_numeric') {
if (courseModel.get('answerType') === 'numeric') {
answerColumn.type = 'number';
}

Expand Down Expand Up @@ -51,7 +52,7 @@ require(['vendor/domReady!', 'load/init-page'], function(doc, page) {
}
}
}],
x: { key: courseModel.get('answerType') },
x: { key: answerField },
y: { key: 'count' },
// Translators: <%=value%> will be replaced by a student response to a question asked in a course.
interactiveTooltipHeaderTemplate: _.template(gettext('Answer: <%=value%>'))
Expand Down
2 changes: 1 addition & 1 deletion analytics_dashboard/static/js/views/data-table-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ define(['dataTablesBootstrap', 'jquery', 'naturalSort', 'underscore', 'utils/uti
display = value;
if (type === 'display') {
if (!_(value).isUndefined() && !_(value).isNull()){
display = Utils.localizeNumber(value);
display = Utils.localizeNumber(Number(value));
}
}
return display;
Expand Down

0 comments on commit f510e5a

Please sign in to comment.