Commit b378af87 by Victor Shnayder

Merge pull request #1011 from MITx/vik/add_self_assessment

Vik/add self assessment
parents 1e956d44 6a313fd5
......@@ -28,3 +28,4 @@ cms/static/sass/*.css
lms/lib/comment_client/python
nosetests.xml
cover_html/
.idea/
......@@ -28,6 +28,7 @@ setup(
"problem = xmodule.capa_module:CapaDescriptor",
"problemset = xmodule.seq_module:SequenceDescriptor",
"section = xmodule.backcompat_module:SemanticSectionDescriptor",
"selfassessment = xmodule.self_assessment_module:SelfAssessmentDescriptor",
"sequential = xmodule.seq_module:SequenceDescriptor",
"slides = xmodule.backcompat_module:TranslateCustomTagDescriptor",
"vertical = xmodule.vertical_module:VerticalDescriptor",
......
......@@ -30,15 +30,17 @@ TIMEDELTA_REGEX = re.compile(r'^((?P<days>\d+?) day(?:s?))?(\s)?((?P<hours>\d+?)
def only_one(lst, default="", process=lambda x: x):
"""
If lst is empty, returns default
If lst has a single element, applies process to that element and returns it
Otherwise, raises an exeception
If lst has a single element, applies process to that element and returns it.
Otherwise, raises an exception.
"""
if len(lst) == 0:
return default
elif len(lst) == 1:
return process(lst[0])
else:
raise Exception('Malformed XML')
raise Exception('Malformed XML: expected at most one element in list.')
def parse_timedelta(time_str):
......@@ -292,11 +294,11 @@ class CapaModule(XModule):
# check button is context-specific.
# Put a "Check" button if unlimited attempts or still some left
if self.max_attempts is None or self.attempts < self.max_attempts-1:
if self.max_attempts is None or self.attempts < self.max_attempts-1:
check_button = "Check"
else:
# Will be final check so let user know that
check_button = "Final Check"
check_button = "Final Check"
reset_button = True
save_button = True
......@@ -527,11 +529,11 @@ class CapaModule(XModule):
# Problem queued. Students must wait a specified waittime before they are allowed to submit
if self.lcp.is_queued():
current_time = datetime.datetime.now()
prev_submit_time = self.lcp.get_recentmost_queuetime()
prev_submit_time = self.lcp.get_recentmost_queuetime()
waittime_between_requests = self.system.xqueue['waittime']
if (current_time-prev_submit_time).total_seconds() < waittime_between_requests:
msg = 'You must wait at least %d seconds between submissions' % waittime_between_requests
return {'success': msg, 'html': ''} # Prompts a modal dialog in ajax callback
return {'success': msg, 'html': ''} # Prompts a modal dialog in ajax callback
try:
old_state = self.lcp.get_state()
......@@ -672,10 +674,10 @@ class CapaDescriptor(RawDescriptor):
'problems/' + path[8:],
path[8:],
]
def __init__(self, *args, **kwargs):
super(CapaDescriptor, self).__init__(*args, **kwargs)
weight_string = self.metadata.get('weight', None)
if weight_string:
self.weight = float(weight_string)
......
class @SelfAssessment
constructor: (element) ->
@el = $(element).find('section.self-assessment')
@id = @el.data('id')
@ajax_url = @el.data('ajax-url')
@state = @el.data('state')
@allow_reset = @el.data('allow_reset')
# valid states: 'initial', 'assessing', 'request_hint', 'done'
# Where to put the rubric once we load it
@errors_area = @$('.error')
@answer_area = @$('textarea.answer')
@rubric_wrapper = @$('.rubric-wrapper')
@hint_wrapper = @$('.hint-wrapper')
@message_wrapper = @$('.message-wrapper')
@submit_button = @$('.submit-button')
@reset_button = @$('.reset-button')
@reset_button.click @reset
@find_assessment_elements()
@find_hint_elements()
@rebind()
# locally scoped jquery.
$: (selector) ->
$(selector, @el)
rebind: () =>
# rebind to the appropriate function for the current state
@submit_button.unbind('click')
@submit_button.show()
@reset_button.hide()
if @state == 'initial'
@submit_button.prop('value', 'Submit')
@submit_button.click @save_answer
else if @state == 'assessing'
@submit_button.prop('value', 'Submit assessment')
@submit_button.click @save_assessment
else if @state == 'request_hint'
@submit_button.prop('value', 'Submit hint')
@submit_button.click @save_hint
else if @state == 'done'
@submit_button.hide()
if @allow_reset
@reset_button.show()
else
@reset_button.hide()
find_assessment_elements: ->
@assessment = @$('select.assessment')
find_hint_elements: ->
@hint_area = @$('textarea.hint')
save_answer: (event) =>
event.preventDefault()
if @state == 'initial'
data = {'student_answer' : @answer_area.val()}
$.postWithPrefix "#{@ajax_url}/save_answer", data, (response) =>
if response.success
@rubric_wrapper.html(response.rubric_html)
@state = 'assessing'
@find_assessment_elements()
@rebind()
else
@errors_area.html(response.message)
else
@errors_area.html('Problem state got out of sync. Try reloading the page.')
save_assessment: (event) =>
event.preventDefault()
if @state == 'assessing'
data = {'assessment' : @assessment.find(':selected').text()}
$.postWithPrefix "#{@ajax_url}/save_assessment", data, (response) =>
if response.success
@hint_wrapper.html(response.hint_html)
@state = 'request_hint'
@find_hint_elements()
@rebind()
else
@errors_area.html(response.message)
else
@errors_area.html('Problem state got out of sync. Try reloading the page.')
save_hint: (event) =>
event.preventDefault()
if @state == 'request_hint'
data = {'hint' : @hint_area.val()}
$.postWithPrefix "#{@ajax_url}/save_hint", data, (response) =>
if response.success
@message_wrapper.html(response.message_html)
@state = 'done'
@allow_reset = response.allow_reset
@rebind()
else
@errors_area.html(response.message)
else
@errors_area.html('Problem state got out of sync. Try reloading the page.')
reset: (event) =>
event.preventDefault()
if @state == 'done'
$.postWithPrefix "#{@ajax_url}/reset", {}, (response) =>
if response.success
@rubric_wrapper.html('')
@hint_wrapper.html('')
@message_wrapper.html('')
@state = 'initial'
@rebind()
@reset_button.hide()
else
@errors_area.html(response.message)
else
@errors_area.html('Problem state got out of sync. Try reloading the page.')
"""
A Self Assessment module that allows students to write open-ended responses,
submit, then see a rubric and rate themselves. Persists student supplied
hints, answers, and assessment judgment (currently only correct/incorrect).
Parses xml definition file--see below for exact format.
"""
import copy
from fs.errors import ResourceNotFoundError
import logging
import os
import sys
from lxml import etree
from lxml.html import rewrite_links
from path import path
import json
from progress import Progress
from pkg_resources import resource_string
from .capa_module import only_one, ComplexEncoder
from .editing_module import EditingDescriptor
from .html_checker import check_html
from .stringify import stringify_children
from .x_module import XModule
from .xml_module import XmlDescriptor
from xmodule.modulestore import Location
log = logging.getLogger("mitx.courseware")
# Set the default number of max attempts. Should be 1 for production
# Set higher for debugging/testing
# attempts specified in xml definition overrides this.
MAX_ATTEMPTS = 1
# Set maximum available number of points. Should be set to 1 for now due to assessment handling,
# which only allows for correct/incorrect.
MAX_SCORE = 1
class SelfAssessmentModule(XModule):
"""
States:
initial (prompt, textbox shown)
|
assessing (read-only textbox, rubric + assessment input shown)
|
request_hint (read-only textbox, read-only rubric and assessment, hint input box shown)
|
done (submitted msg, green checkmark, everything else read-only. If attempts < max, shows
a reset button that goes back to initial state. Saves previous
submissions too.)
"""
# states
INITIAL = 'initial'
ASSESSING = 'assessing'
REQUEST_HINT = 'request_hint'
DONE = 'done'
js = {'coffee': [resource_string(__name__, 'js/src/selfassessment/display.coffee')]}
js_module_name = "SelfAssessment"
def __init__(self, system, location, definition, descriptor,
instance_state=None, shared_state=None, **kwargs):
XModule.__init__(self, system, location, definition, descriptor,
instance_state, shared_state, **kwargs)
"""
Definition file should have 4 blocks -- prompt, rubric, submitmessage, hintprompt,
and one optional attribute, attempts, which should be an integer that defaults to 1.
If it's > 1, the student will be able to re-submit after they see
the rubric. Note: all the submissions are stored.
Sample file:
<selfassessment attempts="1">
<prompt>
Insert prompt text here. (arbitrary html)
</prompt>
<rubric>
Insert grading rubric here. (arbitrary html)
</rubric>
<hintprompt>
Please enter a hint below: (arbitrary html)
</hintprompt>
<submitmessage>
Thanks for submitting! (arbitrary html)
</submitmessage>
</selfassessment>
"""
# Load instance state
if instance_state is not None:
instance_state = json.loads(instance_state)
else:
instance_state = {}
# Note: assessment responses are 'incorrect'/'correct'
self.student_answers = instance_state.get('student_answers', [])
self.assessment = instance_state.get('assessment', [])
self.hints = instance_state.get('hints', [])
self.state = instance_state.get('state', 'initial')
# Used for progress / grading. Currently get credit just for
# completion (doesn't matter if you self-assessed correct/incorrect).
self.score = instance_state.get('score', 0)
self._max_score = instance_state.get('max_score', MAX_SCORE)
self.attempts = instance_state.get('attempts', 0)
self.max_attempts = int(self.metadata.get('attempts', MAX_ATTEMPTS))
self.rubric = definition['rubric']
self.prompt = definition['prompt']
self.submit_message = definition['submitmessage']
self.hint_prompt = definition['hintprompt']
def get_html(self):
#set context variables and render template
previous_answer = self.student_answers[-1] if self.student_answers else ''
allow_reset = self.state == self.DONE and self.attempts < self.max_attempts
context = {
'prompt': self.prompt,
'previous_answer': previous_answer,
'ajax_url': self.system.ajax_url,
'initial_rubric': self.get_rubric_html(),
'initial_hint': self.get_hint_html(),
'initial_message': self.get_message_html(),
'state': self.state,
'allow_reset': allow_reset,
}
html = self.system.render_template('self_assessment_prompt.html', context)
# cdodge: perform link substitutions for any references to course static content (e.g. images)
return rewrite_links(html, self.rewrite_content_links)
def get_score(self):
"""
Returns dict with 'score' key
"""
return {'score': self.score}
def max_score(self):
"""
Return max_score
"""
return self._max_score
def get_progress(self):
'''
For now, just return score / max_score
'''
if self._max_score > 0:
try:
return Progress(self.score, self._max_score)
except Exception as err:
log.exception("Got bad progress")
return None
return None
def handle_ajax(self, dispatch, get):
"""
This is called by courseware.module_render, to handle an AJAX call.
"get" is request.POST.
Returns a json dictionary:
{ 'progress_changed' : True/False,
'progress': 'none'/'in_progress'/'done',
<other request-specific values here > }
"""
handlers = {
'save_answer': self.save_answer,
'save_assessment': self.save_assessment,
'save_hint': self.save_hint,
'reset': self.reset,
}
if dispatch not in handlers:
return 'Error'
before = self.get_progress()
d = handlers[dispatch](get)
after = self.get_progress()
d.update({
'progress_changed': after != before,
'progress_status': Progress.to_js_status_str(after),
})
return json.dumps(d, cls=ComplexEncoder)
def out_of_sync_error(self, get):
"""
return dict out-of-sync error message, and also log.
"""
log.warning("Assessment module state out sync. state: %r, get: %r",
self.state, get)
return {'success': False,
'error': 'The problem state got out-of-sync'}
def get_rubric_html(self):
"""
Return the appropriate version of the rubric, based on the state.
"""
if self.state == self.INITIAL:
return ''
# we'll render it
context = {'rubric': self.rubric}
if self.state == self.ASSESSING:
context['read_only'] = False
elif self.state in (self.REQUEST_HINT, self.DONE):
context['read_only'] = True
else:
raise ValueError("Illegal state '%r'" % self.state)
return self.system.render_template('self_assessment_rubric.html', context)
def get_hint_html(self):
"""
Return the appropriate version of the hint view, based on state.
"""
if self.state in (self.INITIAL, self.ASSESSING):
return ''
# else we'll render it
hint = self.hints[-1] if len(self.hints) > 0 else ''
context = {'hint_prompt': self.hint_prompt,
'hint': hint}
if self.state == self.REQUEST_HINT:
context['read_only'] = False
elif self.state == self.DONE:
context['read_only'] = True
else:
raise ValueError("Illegal state '%r'" % self.state)
return self.system.render_template('self_assessment_hint.html', context)
def get_message_html(self):
"""
Return the appropriate version of the message view, based on state.
"""
if self.state != self.DONE:
return ""
return """<div class="save_message">{0}</div>""".format(self.submit_message)
def save_answer(self, get):
"""
After the answer is submitted, show the rubric.
"""
# Check to see if attempts are less than max
if self.attempts > self.max_attempts:
# If too many attempts, prevent student from saving answer and
# seeing rubric. In normal use, students shouldn't see this because
# they won't see the reset button once they're out of attempts.
return {
'success': False,
'message': 'Too many attempts.'
}
if self.state != self.INITIAL:
return self.out_of_sync_error(get)
self.student_answers.append(get['student_answer'])
self.state = self.ASSESSING
return {
'success': True,
'rubric_html': self.get_rubric_html()
}
def save_assessment(self, get):
"""
Save the assessment.
Returns a dict { 'success': bool, 'hint_html': hint_html 'error': error-msg},
with 'error' only present if 'success' is False, and 'hint_html' only if success is true
"""
if (self.state != self.ASSESSING or
len(self.student_answers) != len(self.assessment) + 1):
return self.out_of_sync_error(get)
self.assessment.append(get['assessment'].lower())
self.state = self.REQUEST_HINT
# TODO: return different hint based on assessment value...
return {'success': True, 'hint_html': self.get_hint_html()}
def save_hint(self, get):
'''
Save the hint.
Returns a dict { 'success': bool,
'message_html': message_html,
'error': error-msg,
'allow_reset': bool},
with the error key only present if success is False and message_html
only if True.
'''
if self.state != self.REQUEST_HINT or len(self.assessment) != len(self.hints) + 1:
return self.out_of_sync_error(get)
self.hints.append(get['hint'].lower())
self.state = self.DONE
# Points are assigned for completion, so always set to 1
points = 1
# increment attempts
self.attempts = self.attempts + 1
# To the tracking logs!
event_info = {
'selfassessment_id': self.location.url(),
'state': {
'student_answers': self.student_answers,
'assessment': self.assessment,
'hints': self.hints,
'score': points,
}
}
self.system.track_function('save_hint', event_info)
return {'success': True,
'message_html': self.get_message_html(),
'allow_reset': self.attempts < self.max_attempts}
def reset(self, get):
"""
If resetting is allowed, reset the state.
Returns {'success': bool, 'error': msg}
(error only present if not success)
"""
if self.state != self.DONE:
return self.out_of_sync_error(get)
if self.attempts > self.max_attempts:
return {
'success': False,
'error': 'Too many attempts.'
}
self.state = self.INITIAL
return {'success': True}
def get_instance_state(self):
"""
Get the current assessment, points, and state
"""
#Assign points based on completion. May want to change to assessment-based down the road.
points = 1
state = {
'student_answers': self.student_answers,
'assessment': self.assessment,
'hints': self.hints,
'state': self.state,
'score': points,
'max_score': self._max_score,
'attempts': self.attempts
}
return json.dumps(state)
class SelfAssessmentDescriptor(XmlDescriptor, EditingDescriptor):
"""
Module for adding self assessment questions to courses
"""
mako_template = "widgets/html-edit.html"
module_class = SelfAssessmentModule
filename_extension = "xml"
stores_state = True
has_score = True
template_dir_name = "selfassessment"
js = {'coffee': [resource_string(__name__, 'js/src/html/edit.coffee')]}
js_module_name = "HTMLEditingDescriptor"
@classmethod
def definition_from_xml(cls, xml_object, system):
"""
Pull out the rubric, prompt, and submitmessage into a dictionary.
Returns:
{
'rubric': 'some-html',
'prompt': 'some-html',
'submitmessage': 'some-html'
'hintprompt': 'some-html'
}
"""
expected_children = ['rubric', 'prompt', 'submitmessage', 'hintprompt']
for child in expected_children:
if len(xml_object.xpath(child)) != 1:
raise ValueError("Self assessment definition must include exactly one '{0}' tag".format(child))
def parse(k):
"""Assumes that xml_object has child k"""
return stringify_children(xml_object.xpath(k)[0])
return {'rubric': parse('rubric'),
'prompt': parse('prompt'),
'submitmessage': parse('submitmessage'),
'hintprompt': parse('hintprompt'),
}
def definition_to_xml(self, resource_fs):
'''Return an xml element representing this definition.'''
elt = etree.Element('selfassessment')
def add_child(k):
child_str = '<{tag}>{body}</{tag}>'.format(tag=k, body=self.definition[k])
child_node = etree.fromstring(child_str)
elt.append(child_node)
for child in ['rubric', 'prompt', 'submitmessage', 'hintprompt']:
add_child(child)
return elt
......@@ -113,3 +113,7 @@ class RoundTripTestCase(unittest.TestCase):
def test_full_roundtrip(self):
self.check_export_roundtrip(DATA_DIR, "full")
def test_selfassessment_roundtrip(self):
#Test selfassessment xmodule to see if it exports correctly
self.check_export_roundtrip(DATA_DIR,"self_assessment")
......@@ -339,4 +339,16 @@ class ImportTestCase(unittest.TestCase):
self.assertRaises(etree.XMLSyntaxError, system.process_xml, bad_xml)
def test_selfassessment_import(self):
'''
Check to see if definition_from_xml in self_assessment_module.py
works properly. Pulls data from the self_assessment directory in the test data directory.
'''
modulestore = XMLModuleStore(DATA_DIR, course_dirs=['self_assessment'])
sa_id = "edX/sa_test/2012_Fall"
location = Location(["i4x", "edX", "sa_test", "selfassessment", "SampleQuestion"])
sa_sample = modulestore.get_instance(sa_id, location)
#10 attempts is hard coded into SampleQuestion, which is the url_name of a selfassessment xml tag
self.assertEqual(sa_sample.metadata['attempts'], '10')
This is a very very simple course, useful for debugging self assessment code.
roots/2012_Fall.xml
\ No newline at end of file
<course>
<chapter url_name="Overview">
<selfassessment url_name="SampleQuestion"/>
</chapter>
</course>
{
"course/2012_Fall": {
"graceperiod": "2 days 5 hours 59 minutes 59 seconds",
"start": "2015-07-17T12:00",
"display_name": "Self Assessment Test",
"graded": "true"
},
"chapter/Overview": {
"display_name": "Overview"
},
"selfassessment/SampleQuestion": {
"display_name": "Sample Question",
},
}
<course org="edX" course="sa_test" url_name="2012_Fall"/>
<selfassessment attempts='10'>
<prompt>
What is the meaning of life?
</prompt>
<rubric>
This is a rubric.
</rubric>
<submitmessage>
Thanks for your submission!
</submitmessage>
<hintprompt>
Enter a hint below:
</hintprompt>
</selfassessment>
......@@ -242,4 +242,25 @@ div.course-wrapper {
}
section.self-assessment {
textarea.answer {
height: 200px;
padding: 5px;
margin-top: 5px;
margin-bottom: 5px;
}
textarea.hint {
height: 100px;
padding: 5px;
margin-top: 5px;
margin-bottom: 5px;
}
div {
margin-top: 5px;
margin-bottom: 5px;
}
}
<div class="hint">
<div class="hint-prompt">
${hint_prompt}
</div>
<textarea name="hint" class="hint" cols="70" rows="5"
${'readonly="true"' if read_only else ''}>${hint}</textarea>
</div>
<section id="self_assessment_${id}" class="self-assessment" data-ajax-url="${ajax_url}"
data-id="${id}" data-state="${state}" data-allow_reset="${allow_reset}">
<div class="error"></div>
<div class="prompt">
${prompt}
</div>
<div>
<textarea name="answer" class="answer" cols="70" rows="20">${previous_answer|h}</textarea>
</div>
<div class="rubric-wrapper">${initial_rubric}</div>
<div class="hint-wrapper">${initial_hint}</div>
<div class="message-wrapper">${initial_message}</div>
<input type="button" value="Submit" class="submit-button" name="show"/>
<input type="button" value="Reset" class="reset-button" name="reset"/>
</section>
<div class="assessment">
<div class="rubric">
<h3>Self-assess your answer with this rubric:</h3>
${rubric}
</div>
% if not read_only:
<select name="assessment" class="assessment">
<option value="incorrect">Incorrect</option>
<option value="correct">Correct</option>
</select>
% endif
</div>
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