Commit 0c6d3d68 by solashirai Committed by Piotr Mitros

removed dict->string->dict patterns

parent d5d549d9
...@@ -4,7 +4,8 @@ import operator ...@@ -4,7 +4,8 @@ import operator
import pkg_resources import pkg_resources
import random import random
import json import json
import hashlib import copy
from copy import deepcopy
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, Dict, List, Boolean from xblock.fields import Scope, Dict, List, Boolean
...@@ -128,15 +129,15 @@ class CrowdsourceHinter(XBlock): ...@@ -128,15 +129,15 @@ class CrowdsourceHinter(XBlock):
print(str(self.hint_database)) print(str(self.hint_database))
print(str(self.initial_hints)) print(str(self.initial_hints))
print(str(self.generic_hints)) print(str(self.generic_hints))
print(str(self.Flagged))
# populate hint_database with hints from initial_hints if there are no hints in hint_database. # populate hint_database with hints from initial_hints if there are no hints in hint_database.
# this probably will occur only on the very first run of a unit containing this block. # this probably will occur only on the very first run of a unit containing this block.
if not bool(self.hint_database): if not bool(self.hint_database):
# this temporary dictionary is set to equal the initial_hints to allow hint_database to #temporarydict = self.initial_hints
# set initial hints. temporarydict is set to a string then uses ast.literal_eval due to #temporarydict = ast.literal_eval(temporarydict)
# a scope error that occured when simply setting temporarydict = self.initial_hints #TODO: Figure out why temporarydict = self.initial_hints doesn't work.
temporarydict = str(self.initial_hints)
temporarydict = ast.literal_eval(temporarydict) self.hint_database = copy.copy(self.initial_hints)
self.hint_database = temporarydict
answer = str(data["submittedanswer"]) answer = str(data["submittedanswer"])
answer = answer.lower() # for analyzing the student input string I make it lower case. answer = answer.lower() # for analyzing the student input string I make it lower case.
found_equal_sign = 0 found_equal_sign = 0
...@@ -229,6 +230,14 @@ class CrowdsourceHinter(XBlock): ...@@ -229,6 +230,14 @@ class CrowdsourceHinter(XBlock):
# that were not used. The keys are the used hints, the values are the # that were not used. The keys are the used hints, the values are the
# corresponding incorrect answer # corresponding incorrect answer
feedback_data = {} feedback_data = {}
if data['isStaff'] == 'true':
if len(self.Flagged) != 0:
for answer_keys in self.hint_database:
if str(len(self.hint_database[str(answer_keys)])) != str(0):
for hints in self.hint_database[str(answer_keys)]:
for flagged_hints in self.Flagged:
if str(hints) == flagged_hints:
feedback_data[str(hints)] = str("Flagged")
if len(self.WrongAnswers) == 0: if len(self.WrongAnswers) == 0:
return return
else: else:
...@@ -275,12 +284,10 @@ class CrowdsourceHinter(XBlock): ...@@ -275,12 +284,10 @@ class CrowdsourceHinter(XBlock):
hint_rating['student_ansxwer'] = 'Flagged' hint_rating['student_ansxwer'] = 'Flagged'
hint_rating['hint'] = data['hint'] hint_rating['hint'] = data['hint']
return hint_rating return hint_rating
temporary_dictionary = str(self.hint_database[data['student_answer']]) hint_rating['rating'] = self.hint_database[data['student_answer']][data['hint']]
temporary_dictionary = (ast.literal_eval(temporary_dictionary))
hint_rating['rating'] = temporary_dictionary[data['hint']]
hint_rating['student_answer'] = data['student_answer'] hint_rating['student_answer'] = data['student_answer']
hint_rating['hint'] = data['hint'] hint_rating['hint'] = data['hint']
return hint_rating return hint_rating
@XBlock.json_handler @XBlock.json_handler
def rate_hint(self, data, suffix=''): def rate_hint(self, data, suffix=''):
...@@ -300,6 +307,9 @@ class CrowdsourceHinter(XBlock): ...@@ -300,6 +307,9 @@ class CrowdsourceHinter(XBlock):
answer_data = data['student_answer'] answer_data = data['student_answer']
data_rating = data['student_rating'] data_rating = data['student_rating']
data_hint = data['hint'] data_hint = data['hint']
print answer_data, data_rating, data_hint
print (str(self.hint_database))
print (str(self.Flagged))
if data['student_rating'] == 'unflag': if data['student_rating'] == 'unflag':
for flagged_hints in self.Flagged: for flagged_hints in self.Flagged:
if flagged_hints == data_hint: if flagged_hints == data_hint:
...@@ -308,10 +318,7 @@ class CrowdsourceHinter(XBlock): ...@@ -308,10 +318,7 @@ class CrowdsourceHinter(XBlock):
if data['student_rating'] == 'remove': if data['student_rating'] == 'remove':
for flagged_hints in self.Flagged: for flagged_hints in self.Flagged:
if data_hint == flagged_hints: if data_hint == flagged_hints:
temporary_dict = str(self.hint_database[self.Flagged[data_hint]]) self.hint_database[self.Flagged[data_hint]].pop(data_hint, None)
temporary_dict = (ast.literal_eval(temporary_dict))
temporary_dict.pop(data_hint, None)
self.hint_database[self.Flagged[data_hint]] = temporary_dict
self.Flagged.pop(data_hint, None) self.Flagged.pop(data_hint, None)
return {'rating': 'removed'} return {'rating': 'removed'}
if data['student_rating'] == 'flag': if data['student_rating'] == 'flag':
...@@ -343,15 +350,11 @@ class CrowdsourceHinter(XBlock): ...@@ -343,15 +350,11 @@ class CrowdsourceHinter(XBlock):
The rating associated with the hint is returned. This rating is identical The rating associated with the hint is returned. This rating is identical
to what would be found under self.hint_database[answer_string[hint_string]] to what would be found under self.hint_database[answer_string[hint_string]]
""" """
temporary_dictionary = str(self.hint_database[str(answer_data)])
temporary_dictionary = (ast.literal_eval(temporary_dictionary))
if data_rating == 'upvote': if data_rating == 'upvote':
temporary_dictionary[str(data_hint)] += 1 self.hint_database[str(answer_data)][str(data_hint)] += 1
else: else:
temporary_dictionary[str(data_hint)] -= 1 self.hint_database[str(answer_data)][str(data_hint)] -= 1
self.hint_database[str(answer_data)] = temporary_dictionary
print("Ratings changed : " + str(self.hint_database)) print("Ratings changed : " + str(self.hint_database))
return str(temporary_dictionary[str(data_hint)])
@XBlock.json_handler @XBlock.json_handler
def give_hint(self, data, suffix=''): def give_hint(self, data, suffix=''):
...@@ -365,23 +368,14 @@ class CrowdsourceHinter(XBlock): ...@@ -365,23 +368,14 @@ 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 str(submission) not in self.hint_database[str(answer)]:
temporary_dictionary = str(self.hint_database[str(answer)]) self.hint_database[str(answer)].update({submission: 0})
temporary_dictionary = (ast.literal_eval(temporary_dictionary))
temporary_dictionary.update({submission: 0})
# once again, manipulating temporary_dictionary and setting
# self.hint_database equal to it due to being unable to directly
# edit self.hint_databse. Most likely scope error
self.hint_database[str(answer)] = temporary_dictionary
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 str(submission) in self.generic_hints:
return return
else: else:
temporary_dictionary = str(self.hint_database[str(answer)]) self.hint_database[str(answer)][str(submission)] += 1
temporary_dictionary = (ast.literal_eval(temporary_dictionary))
temporary_dictionary[str(submission)] += 1
self.hint_database[str(answer)] = temporary_dictionary
return return
@XBlock.json_handler @XBlock.json_handler
...@@ -415,6 +409,7 @@ class CrowdsourceHinter(XBlock): ...@@ -415,6 +409,7 @@ class CrowdsourceHinter(XBlock):
block = runtime.construct_xblock_from_class(cls, keys) block = runtime.construct_xblock_from_class(cls, keys)
print(node) print(node)
print(node.text) print(node.text)
print type(node)
block.generic_hints = ["Make sure to check your answer for basic mistakes like spelling!"] block.generic_hints = ["Make sure to check your answer for basic mistakes like spelling!"]
block.initial_hints = {"michigann": {"You have an extra N in your answer": 1}} block.initial_hints = {"michigann": {"You have an extra N in your answer": 1}}
return block return block
...@@ -101,7 +101,7 @@ function CrowdsourceHinter(runtime, element){ ...@@ -101,7 +101,7 @@ function CrowdsourceHinter(runtime, element){
$(function(){ $(function(){
var template = $('#show_flagged_feedback').html(); var template = $('#show_flagged_feedback').html();
var data = { var data = {
hint: result.hint hint: result
}; };
html = Mustache.render(template, data); html = Mustache.render(template, data);
}); });
...@@ -125,9 +125,16 @@ function CrowdsourceHinter(runtime, element){ ...@@ -125,9 +125,16 @@ function CrowdsourceHinter(runtime, element){
//to the student, as well as an option to create a new hint for an answer. //to the student, as well as an option to create a new hint for an answer.
if(isStaff){ if(isStaff){
$('.crowdsourcehinter_block').attr('class', 'crowdsourcehinter_block_is_staff'); $('.crowdsourcehinter_block').attr('class', 'crowdsourcehinter_block_is_staff');
$.each(result, function(index, value) {
if(value == "Flagged") {
//index represents the flagged hint's text
showFlaggedFeedback(index);
}
});
} }
if(!isShowingHintFeedback){ if(!isShowingHintFeedback){
$.each(result, function(index, value) { $.each(result, function(index, value) {
if(value != "Flagged"){
setStudentAnswers(value); setStudentAnswers(value);
student_answer = value; student_answer = value;
hint = index; hint = index;
...@@ -152,6 +159,7 @@ function CrowdsourceHinter(runtime, element){ ...@@ -152,6 +159,7 @@ function CrowdsourceHinter(runtime, element){
else{ else{
showHintFeedback(hint, student_answer); showHintFeedback(hint, student_answer);
} }
}
}); });
isShowingHintFeedback = true; isShowingHintFeedback = true;
} }
...@@ -223,7 +231,7 @@ function CrowdsourceHinter(runtime, element){ ...@@ -223,7 +231,7 @@ function CrowdsourceHinter(runtime, element){
//Staff ratings are the removal or unflagging of flagged hints from the database. The attribute 'data-rate' is used //Staff ratings are the removal or unflagging of flagged hints from the database. The attribute 'data-rate' is used
//to determine whether to unflag or delete the hint. //to determine whether to unflag or delete the hint.
hint = $(this).parent().find(".csh_hint").text(); hint = $(this).parent().find(".csh_hint").text();
student_answer = $(this).parent().parent().find('.csh_answer_text').attr('answer'); student_answer = "Flagged";
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: runtime.handlerUrl(element, 'rate_hint'), url: runtime.handlerUrl(element, 'rate_hint'),
......
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