Commit 5a4e103d by Piotr Mitros

Starting to make data structures a little bit more sensible

parent 5650a335
...@@ -26,8 +26,7 @@ class CrowdsourceHinter(XBlock): ...@@ -26,8 +26,7 @@ class CrowdsourceHinter(XBlock):
# Each key (incorrect answer) has a corresponding dictionary (in # Each key (incorrect answer) has a corresponding dictionary (in
# which hints are keys and the hints' ratings are the values). # which hints are keys and the hints' ratings are the values).
# For example: # For example:
# {"computerr": {"You misspelled computer, remove the last r.": 5}} # {"computerr": {"You misspelled computer, remove the last r.": {'upvotes':5, 'downvotes':3}}}
# TODO: We should store upvotes and downvotes independently.
hint_database = Dict(default={}, scope=Scope.user_state_summary) hint_database = Dict(default={}, scope=Scope.user_state_summary)
# Database of initial hints, set by the course # Database of initial hints, set by the course
...@@ -112,10 +111,14 @@ class CrowdsourceHinter(XBlock): ...@@ -112,10 +111,14 @@ class CrowdsourceHinter(XBlock):
studio view. studio view.
The Studio view is not yet complete. The Studio view is not yet complete.
TODO: How do we make this handler Studio-specific? We don't
want students being able to call this.
""" """
initial_hints = json.loads(data['initial_hints']) initial_hints = json.loads(data['initial_hints'])
generic_hints = json.loads(data['generic_hints']) generic_hints = json.loads(data['generic_hints'])
# Validate input
if not isinstance(generic_hints, list): if not isinstance(generic_hints, list):
return {'success': False, return {'success': False,
'error': 'Generic hints should be a list.'} 'error': 'Generic hints should be a list.'}
...@@ -341,10 +344,10 @@ class CrowdsourceHinter(XBlock): ...@@ -341,10 +344,10 @@ class CrowdsourceHinter(XBlock):
if any(data_hint in generic_hints for generic_hints in self.generic_hints): if any(data_hint in generic_hints for generic_hints in self.generic_hints):
return return
if data_rating == 'upvote': if data_rating == 'upvote':
self.hint_database[str(answer_data)][str(data_hint)] += 1 self.hint_database[str(answer_data)][str(data_hint)]['upvotes'] += 1
return self.hint_database[str(answer_data)][str(data_hint)] return self.hint_database[str(answer_data)][str(data_hint)]
else: else:
self.hint_database[str(answer_data)][str(data_hint)] -= 1 self.hint_database[str(answer_data)][str(data_hint)]['downvotes'] += 1
return self.hint_database[str(answer_data)][str(data_hint)] return self.hint_database[str(answer_data)][str(data_hint)]
@XBlock.json_handler @XBlock.json_handler
...@@ -352,21 +355,21 @@ class CrowdsourceHinter(XBlock): ...@@ -352,21 +355,21 @@ class CrowdsourceHinter(XBlock):
""" """
This function adds a new hint submitted by the student into the hint_database. This function adds a new hint submitted by the student into the hint_database.
Args: Args:
data['submission']: This is the text of the new hint that the student has submitted. data['new_hint_submission']: This is the text of the new hint that the student has submitted.
data['answer']: This is the incorrect answer for which the student is submitting a new hint. data['answer']: This is the incorrect answer for which the student is submitting a new hint.
""" """
submission = data['submission'] submission = data['new_hint_submission']
answer = data['answer'] answer = data['answer']
# If we don't have the hint already, add it
if str(submission) not in self.hint_database[str(answer)]: if str(submission) not in self.hint_database[str(answer)]:
self.hint_database[str(answer)].update({submission: 0}) self.hint_database[str(answer)].update({submission: {'upvotes':0, 'downvotes':0}})
return return {'success':True,
else: 'result': 'Hint added'}
# if the hint exists already, simply upvote the previously entered hint
if str(submission) in self.generic_hints: self.hint_database[str(answer)][str(submission)]['upvotes'] += 1
return return {'success':True,
else: 'result': 'We already had this hint. We gave it an upvote'}
self.hint_database[str(answer)][str(submission)] += 1
return
@XBlock.json_handler @XBlock.json_handler
def studiodata(self, data, suffix=''): def studiodata(self, data, suffix=''):
......
...@@ -219,7 +219,7 @@ function CrowdsourceHinter(runtime, element, data){ ...@@ -219,7 +219,7 @@ function CrowdsourceHinter(runtime, element, data){
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: runtime.handlerUrl(element, 'add_new_hint'), url: runtime.handlerUrl(element, 'add_new_hint'),
data: JSON.stringify({"submission": newHint, "answer": studentAnswer}), data: JSON.stringify({"new_hint_submission": newHint, "answer": studentAnswer}),
success: function() { success: function() {
$('.csh_student_text_input', element).attr('style', 'display: none;'); $('.csh_student_text_input', element).attr('style', 'display: none;');
$(submitHintButtonHTML.currentTarget).attr('style', 'display: none;'); $(submitHintButtonHTML.currentTarget).attr('style', 'display: none;');
......
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