Commit 97fbd02c by Victor Shnayder

Merge pull request #1090 from MITx/vik/modify_open_ended

Vik/modify open ended
parents 32d8a6e9 bcd30223
......@@ -1927,8 +1927,6 @@ class OpenEndedResponse(LoncapaResponse):
else:
self.max_score = 1
log.debug(self.max_score)
def get_score(self, student_answers):
try:
......@@ -2032,6 +2030,68 @@ class OpenEndedResponse(LoncapaResponse):
def get_initial_display(self):
return {self.answer_id: self.initial_display}
def _convert_longform_feedback_to_html(self,response_items):
"""
Take in a dictionary, and return html formatted strings appropriate for sending via xqueue.
Input:
Dictionary with keys success, feedback, and errors
Output:
String
"""
feedback_item_start='<div class="{feedback_key}">'
feedback_item_end='</div>'
for tag in ['status', 'feedback']:
if tag not in response_items:
feedback_long=feedback_item_start.format(feedback_key="errors") + "Error getting feedback." + feedback_item_end
feedback_items=response_items['feedback']
try:
feedback_items=json.loads(feedback_items)
except:
pass
success=response_items['success']
if success:
feedback_long=""
for k,v in feedback_items.items():
feedback_long+=feedback_item_start.format(feedback_key=k)
feedback_long+=str(v)
feedback_long+=feedback_item_end
if len(feedback_items)==0:
feedback_long=feedback_item_start.format(feedback_key="feedback") + "No feedback available." + feedback_item_end
else:
feedback_long=feedback_item_start.format(feedback_key="errors") + response_items['feedback'] + feedback_item_end
return feedback_long
def _format_feedback(self, response_items):
"""
Input:
Dictionary called feedback. Must contain keys seen below.
Output:
Return error message or feedback template
"""
feedback=self._convert_longform_feedback_to_html(response_items)
if not response_items['success']:
return self.system.render_template("open_ended_error.html", {'errors' : feedback})
feedback_template=self.system.render_template("open_ended_feedback.html",{
'grader_type' : response_items['grader_type'],
'score' : response_items['score'],
'feedback' : feedback,
})
return feedback_template
def _parse_score_msg(self, score_msg):
"""
Grader reply is a JSON-dump of the following dict
......@@ -2057,17 +2117,17 @@ class OpenEndedResponse(LoncapaResponse):
log.error("External grader message should be a JSON-serialized dict."
" Received score_result = %s" % score_result)
return fail
for tag in ['score','feedback']:
for tag in ['score', 'feedback', 'grader_type', 'success']:
if tag not in score_result:
log.error("External grader message is missing one or more required"
" tags: 'score', 'feedback")
log.error("External grader message is missing required tag: {0}".format(tag))
return fail
# Next, we need to check that the contents of the external grader message
# is safe for the LMS.
# 1) Make sure that the message is valid XML (proper opening/closing tags)
# 2) TODO: Is the message actually HTML?
feedback = score_result['feedback']
feedback = self._format_feedback(score_result)
score_ratio=int(score_result['score'])/self.max_score
......@@ -2075,6 +2135,7 @@ class OpenEndedResponse(LoncapaResponse):
if score_ratio>=.66:
correct=True
log.debug(feedback)
try:
etree.fromstring(feedback)
except etree.XMLSyntaxError as err:
......
......@@ -12,6 +12,7 @@ from django.http import HttpResponse
from django_future.csrf import ensure_csrf_cookie
from django.views.decorators.cache import cache_control
from mitxmako.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from courseware import grades
from courseware.access import has_access, get_access_group_name
......
<section>
<div class="shortform">
<div class="result-errors">
There was an error with your submission. Please contact course staff.
</div>
</div>
<div class="longform">
<div class="result-errors">
${errors}
</div>
</div>
</section>
\ No newline at end of file
<section>
<header>Feedback</header>
<div class="shortform">
<div class="result-output">
<p>Score: ${score}</p>
% if grader_type == "ML":
<p>Check below for full feedback:</p>
% endif
</div>
</div>
<div class="longform">
<div class="result-output">
${ feedback | n}
</div>
</div>
</section>
\ No newline at end of file
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