Commit 3c9ff574 by Mike Chen

MultipleChoice+StringResponse+NumericalResponse in CMS

parent 9b322f68
start
= sections:(StringResponse / NumericalResponse / MultipleChoice / Paragraph / Linebreak+)+
{
var hasKind = function(o) {
return o.hasOwnProperty('type');
}
return sections.filter(hasKind);
}
StudentProducedResponseIdentifier
= '='
NumericalResponse
= StudentProducedResponseIdentifier OptionalSpaces '(' OptionalSpaces value:NumericalValue OptionalSpaces ')' OptionalSpaces Linebreak
{
return {'type': 'numerical', 'answer': value};
}
StringResponse
= StudentProducedResponseIdentifier OptionalSpaces !'(' value:Text Linebreak
{
return {'type': 'string', 'answer': $.trim(value)};
}
/* MultipleChoice */
ChoiceIdentifier
= '(' parenthesized:[x\s]? ')'
{
return parenthesized == 'x';
}
ChoiceTextLine
= !ChoiceIdentifier line:Line
{
return line;
}
Choice
= correct:ChoiceIdentifier lines:(ChoiceTextLine)+
{
return {'type': 'choice', 'correct' : correct, 'text': lines.join("")};
}
MultipleChoice
= choices:Choice+
{
return {'type': 'multiple_choice', 'choices': choices};
}
/* Paragraph */
ParagraphTextLine
= !ChoiceIdentifier !StudentProducedResponseIdentifier line:Line
{
return line;
}
Paragraph
= lines:ParagraphTextLine+
{
return {'type': 'paragraph', 'text': lines.join("")};
}
/* Base symbols */
Line
= text:Text terminator:Linebreak
{
return text + terminator;
}
Text
= chars:([^\n]+)
{
return chars.join("");
}
OptionalSpaces
= ' '*
Linebreak
= ('\n')
NumericalValue
= additive
/* Mathematical rules */
additive
= left:multiplicative "+" right:additive { return left + right; }
/ left:multiplicative "-" right:additive { return left - right; }
/ multiplicative
multiplicative
= left:primary "*" right:multiplicative { return left * right; }
/ left:primary "/" right:multiplicative { return left / right; }
/ primary
primary
= integer
/ "(" additive:additive ")" { return additive; }
integer "integer"
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
<section class="capa-edit">
<div class="parser-message-box" style="display:none;">Input parsed successfully.</div>
<script type="text/javascript" src="/static/js/vendor/peg-0.7.0.min.js"></script>
<textarea name="" class="edit-box source-box" rows="8" cols="40"></textarea>
<textarea name="" class="edit-box capa-box" rows="8" cols="40">${data}</textarea>
</section>
......@@ -563,6 +563,9 @@ class CapaDescriptor(RawDescriptor):
"""
module_class = CapaModule
mako_template = "widgets/capa-edit.html"
js_module_name = "CapaDescriptor"
js = {'coffee': [resource_string(__name__, 'js/src/capa/edit.coffee')]}
# VS[compat]
# TODO (cpennington): Delete this method once all fall 2012 course are being
......
class @CapaDescriptor
constructor: (@element) ->
@edit_box = $(".edit-box.capa-box", @element)
@source_box = $(".edit-box.source-box", @element)
@message_box = $(".parser-message-box", @element)
@buildParser()
@source_box.keyup =>
@parse()
save: -> @edit_box.val()
buildParser: ->
$.get "/static/grammar.jspeg", (data) =>
@grammar = data
@parser = PEG.buildParser @grammar
, "text"
buildErrorMessage: (e) ->
if e.line != undefined && e.column != undefined
"Line " + e.line + ", column " + e.column + ": " + e.message
else e.message
parse: ->
try
source = @source_box.val() + "\n"
result = @parser.parse (source)
@outputXML(result)
@message_box.css {"display":"none"}
catch e
console.log @buildErrorMessage(e)
@message_box.html @buildErrorMessage(e)
@message_box.css {"display":"block"}
outputXML: (parsed) ->
@edit_box.val @buildXML(parsed)
dom2capa: (node) ->
capa = new XMLSerializer().serializeToString(node)
capa = capa + ""
return capa.replace(/<startouttext>/g, '<startouttext />').replace(/<\/startouttext>/g, '<endouttext />')
buildXML: (parsed) ->
dom_parser = new DOMParser()
doc = dom_parser.parseFromString("<problem></problem>", "text/xml");
problem = $(doc.getElementsByTagName('problem')[0])
create_text_element = (content) ->
el = $(doc.createElement('startouttext'))
el.text content
return el
for section in parsed
if section.type == 'paragraph'
newel = create_text_element(section.text)
problem.append(newel)
else if section.type == 'multiple_choice'
newel = $(doc.createElement('choiceresponse'))
# count the number of correct choices
num_correct = 0
for choice in section.choices
if choice.correct
num_correct += 1
if num_correct == 1
group = $(doc.createElement('radiogroup'))
else if num_correct > 1
group = $(doc.createElement('checkboxgroup'))
newel.append(group)
for choice_def in section.choices
choice = $(doc.createElement('choice'))
choice.attr 'correct', choice_def.correct
choice.append create_text_element(choice_def.text)
group.append(choice)
problem.append(newel)
else if section.type == 'numerical'
newel = $(doc.createElement('numericalresponse'))
newel.attr 'answer', section.answer
newel.append doc.createElement('textline')
problem.append(newel)
else if section.type == 'string'
newel = $(doc.createElement('stringresponse'))
newel.attr 'answer', section.answer
newel.append doc.createElement('textline')
problem.append(newel)
capa = @dom2capa(doc)
console.log capa
return capa
\ 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