Commit 85a06859 by solashirai Committed by Piotr Mitros

fixed minor issues. answers are not being stored correctly

parent c678412a
...@@ -139,7 +139,7 @@ class CrowdsourceHinter(XBlock): ...@@ -139,7 +139,7 @@ class CrowdsourceHinter(XBlock):
frag.add_javascript_url('//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js') frag.add_javascript_url('//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js')
frag.add_css(self.resource_string("static/css/crowdsourcehinter.css")) frag.add_css(self.resource_string("static/css/crowdsourcehinter.css"))
frag.add_javascript(self.resource_string("static/js/src/crowdsourcehinter.js")) frag.add_javascript(self.resource_string("static/js/src/crowdsourcehinter.js"))
frag.initialize_js('CrowdsourceHinter', {'hinting_element': self.Element, 'isStaff': self.xmodule_runtime.user_is_staff}) frag.initialize_js('CrowdsourceHinter', {'hinting_element': self.Element, 'isStaff': get_user_is_staff})
return frag return frag
def extract_student_answers(self, answers): def extract_student_answers(self, answers):
...@@ -152,7 +152,7 @@ class CrowdsourceHinter(XBlock): ...@@ -152,7 +152,7 @@ class CrowdsourceHinter(XBlock):
# First, we split this into the submission # First, we split this into the submission
answers = [a.split('=') for a in answers.split("&")] answers = [a.split('=') for a in answers.split("&")]
# Next, we decode the HTML escapes # Next, we decode the HTML escapes
answers = [(a[0], html_parser.unescape(a[1])) for a in answers] answers = [(a[0], HTMLparser.unescape(a[1])) for a in answers]
return dict(answers) return dict(answers)
@XBlock.json_handler @XBlock.json_handler
...@@ -175,17 +175,11 @@ class CrowdsourceHinter(XBlock): ...@@ -175,17 +175,11 @@ class CrowdsourceHinter(XBlock):
# there are no hints in hint_database. this probably will # there are no hints in hint_database. this probably will
# occur only on the very first run of a unit containing this # occur only on the very first run of a unit containing this
# block. # block.
# TODO: This more complex than it needs to be. Why isn't as if not self.hint_database:
# simple as: self.hints_database = self.initial_hints
# if not self.hint_database:
# self.hints_database = self.initial_hints
for answers in self.initial_hints:
if answers not in self.hint_database:
self.hint_database[answers] = {}
if self.initial_hints[answers] not in self.hint_database[answers]:
self.hint_database[answers].update({self.initial_hints[answers]: 0})
answer = self.extract_student_answers(data["submittedanswer"]) answer = self.extract_student_answers(data["submittedanswer"])
# HACK: For now, we assume just one submission, a string, and # HACK: For now, we assume just one submission, a string, and
# case insensitive # case insensitive
# #
...@@ -196,25 +190,25 @@ class CrowdsourceHinter(XBlock): ...@@ -196,25 +190,25 @@ class CrowdsourceHinter(XBlock):
# Put the student's answer to lower case so that differences # Put the student's answer to lower case so that differences
# in capitalization don't make different groups of # in capitalization don't make different groups of
# hints. TODO: We should replace this with a . # hints. TODO: We should replace this with a .
remaining_hints = int(0) # TODO: This is confused remaining_hints = 0 # TODO: This is confused
best_hint = "" # TODO: What is this? best_hint = "" # TODO: What is this?
remaining_hints = str(self.find_hints(answer)) remaining_hints = self.find_hints(answer)
if remaining_hints != str(0): if remaining_hints != 0:
for hint in self.hint_database[str(answer)]: for hint in self.hint_database[answer]:
if hint not in self.reported_hints.keys(): if hint not in self.reported_hints.keys():
# if best_hint hasn't been set yet or the rating of hints is greater than the rating of best_hint # if best_hint hasn't been set yet or the rating of hints is greater than the rating of best_hint
if best_hint == "" or self.hint_database[str(answer)][hint] > self.hint_database[str(answer)][str(best_hint)]: if best_hint == "" or self.hint_database[answer][hint] > self.hint_database[answer][best_hint]:
best_hint = hint best_hint = hint
self.used.append(best_hint) self.used.append(best_hint)
return {'BestHint': best_hint, "StudentAnswer": answer} return {'BestHint': best_hint, "StudentAnswer": answer}
# find generic hints for the student if no specific hints exist # find generic hints for the student if no specific hints exist
if len(self.generic_hints) != 0: if self.generic_hints:
generic_hint = random.choice(self.generic_hints) generic_hint = random.choice(self.generic_hints)
self.used.append(generic_hint) self.used.append(generic_hint)
return {'BestHint': generic_hint, "StudentAnswer": answer} return {'BestHint': generic_hint, "StudentAnswer": answer}
else: else:
# if there are no hints in either the database or generic hints # if there are no hints in either the database or generic hints
self.used.append(str("There are no hints for" + " " + answer)) self.used.append("There are no hints for" + " " + answer)
return {'BestHint': "Sorry, there are no hints for this answer.", "StudentAnswer": answer} return {'BestHint': "Sorry, there are no hints for this answer.", "StudentAnswer": answer}
def find_hints(self, answer): def find_hints(self, answer):
...@@ -229,19 +223,19 @@ class CrowdsourceHinter(XBlock): ...@@ -229,19 +223,19 @@ class CrowdsourceHinter(XBlock):
""" """
isreported = [] isreported = []
self.incorrect_answers.append(str(answer)) self.incorrect_answers.append(answer)
if str(answer) not in self.hint_database: if answer not in self.hint_database:
# add incorrect answer to hint_database if no precedent exists # add incorrect answer to hint_database if no precedent exists
self.hint_database[str(answer)] = {} self.hint_database[answer] = {}
return str(0) return 0
for hint_keys in self.hint_database[str(answer)]: for hint_key in self.hint_database[answer]:
for reported_keys in self.reported_hints: for reported_keys in self.reported_hints:
if hint_keys == reported_keys: if hint_key in self.reported_hints:
isreported.append(hint_keys) isreported.append(hint_key)
if (len(self.hint_database[str(answer)]) - len(isreported)) > 0: if (len(self.hint_database[answer]) - len(isreported)) > 0:
return str(1) return 1
else: else:
return str(0) return 0
@XBlock.json_handler @XBlock.json_handler
def get_used_hint_answer_data(self, data, suffix=''): def get_used_hint_answer_data(self, data, suffix=''):
...@@ -264,24 +258,18 @@ class CrowdsourceHinter(XBlock): ...@@ -264,24 +258,18 @@ class CrowdsourceHinter(XBlock):
used_hint_answer_text = {} used_hint_answer_text = {}
if self.get_user_is_staff(): if self.get_user_is_staff():
for key in self.reported_hints: for key in self.reported_hints:
used_hint_answer_text[key] = str("Reported") used_hint_answer_text[key] = "Reported"
if len(self.incorrect_answers) == 0: if len(self.incorrect_answers) == 0:
return used_hint_answer_text return used_hint_answer_text
else: else:
for index in range(0, len(self.used)): for index in range(0, len(self.used)):
# each index is a hint that was used, in order of usage # each index is a hint that was used, in order of usage
if str(self.used[index]) in self.hint_database[self.incorrect_answers[index]]: if self.used[index] in self.hint_database[self.incorrect_answers[index]]:
# add new key (hint) to used_hint_answer_text with a value (incorrect answer) # add new key (hint) to used_hint_answer_text with a value (incorrect answer)
used_hint_answer_text[str(self.used[index])] = str(self.incorrect_answers[index]) used_hint_answer_text[self.used[index]] = self.incorrect_answers[index]
self.incorrect_answers = []
self.used = []
return used_hint_answer_text
else: else:
# if the student's answer had no hints (or all the hints were reported and unavailable) return None # if the student's answer had no hints (or all the hints were reported and unavailable) return None
used_hint_answer_text[None] = str(self.incorrect_answers[index]) used_hint_answer_text[None] = self.incorrect_answers[index]
self.incorrect_answers = []
self.used = []
return used_hint_answer_text
self.incorrect_answers = [] self.incorrect_answers = []
self.used = [] self.used = []
return used_hint_answer_text return used_hint_answer_text
...@@ -309,21 +297,21 @@ class CrowdsourceHinter(XBlock): ...@@ -309,21 +297,21 @@ class CrowdsourceHinter(XBlock):
return {"rating": None, 'hint': data_hint} return {"rating": None, 'hint': data_hint}
if data['student_rating'] == 'unreport': if data['student_rating'] == 'unreport':
for reported_hints in self.reported_hints: for reported_hints in self.reported_hints:
if reported_hints == data_hint: if data_hint in self.reported_hints:
self.reported_hints.pop(data_hint, None) self.reported_hints.pop(data_hint, None)
return {'rating': 'unreported'} return {'rating': 'unreported'}
if data['student_rating'] == 'remove': if data['student_rating'] == 'remove':
for reported_hints in self.reported_hints: for reported_hints in self.reported_hints:
if data_hint == reported_hints: if data_hint in self.reported_hints:
self.hint_database[self.reported_hints[data_hint]].pop(data_hint, None) self.hint_database[self.reported_hints[data_hint]].pop(data_hint, None)
self.reported_hints.pop(data_hint, None) self.reported_hints.pop(data_hint, None)
return {'rating': 'removed'} return {'rating': 'removed'}
if data['student_rating'] == 'report': if data['student_rating'] == 'report':
# add hint to Reported dictionary # add hint to Reported dictionary
self.reported_hints[str(data_hint)] = answer_data self.reported_hints[data_hint] = answer_data
return {"rating": 'reported', 'hint': data_hint} return {"rating": 'reported', 'hint': data_hint}
rating = self.change_rating(data_hint, data_rating, answer_data) rating = self.change_rating(data_hint, data_rating, answer_data)
return {"rating": str(rating), 'hint': data_hint} return {"rating": rating, 'hint': data_hint}
def change_rating(self, data_hint, data_rating, answer_data): def change_rating(self, data_hint, data_rating, answer_data):
""" """
...@@ -341,11 +329,11 @@ class CrowdsourceHinter(XBlock): ...@@ -341,11 +329,11 @@ 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 delta_rating = 1
return self.hint_database[str(answer_data)][str(data_hint)]
else: else:
self.hint_database[str(answer_data)][str(data_hint)] -= 1 delta_rating = -1
return self.hint_database[str(answer_data)][str(data_hint)] self.hint_database[answer_data][data_hint] += delta_rating
return self.hint_database[answer_data)][data_hint]
@XBlock.json_handler @XBlock.json_handler
def add_new_hint(self, data, suffix=''): def add_new_hint(self, data, suffix=''):
...@@ -357,15 +345,15 @@ class CrowdsourceHinter(XBlock): ...@@ -357,15 +345,15 @@ class CrowdsourceHinter(XBlock):
""" """
submission = data['submission'] submission = data['submission']
answer = data['answer'] answer = data['answer']
if str(submission) not in self.hint_database[str(answer)]: if submission not in self.hint_database[answer]:
self.hint_database[str(answer)].update({submission: 0}) self.hint_database[answer].update({submission: 0})
return return
else: else:
# if the hint exists already, simply upvote the previously entered hint # if the hint exists already, simply upvote the previously entered hint
if str(submission) in self.generic_hints: if submission in self.generic_hints:
return return
else: else:
self.hint_database[str(answer)][str(submission)] += 1 self.hint_database[answer][submission] += 1
return return
@XBlock.json_handler @XBlock.json_handler
......
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