Problem Templates¶
Note
If you copy the templates below be sure to paste them in the advanced (xml) editor in edX Studio (Do NOT use the visual editor)
Dropdown - Inline Display¶
Displays the question text inline with the dropdown menu for selecting answer options. Dropdown problems can only be programmed with one correct answer (trying to add more than one correct answer will result in only the last programmed answer being scored as correct).
<problem>
<p style="display:inline;">Question text</p>
<optionresponse inline="1">
<optioninput inline="1" label="Question text" options="('Option 1','Option 2','Option 3','Option 4','etc.')" correct="Option 1"></optioninput>
</optionresponse>
<solution>
<div class="detailed-solution">
<p>Explanation</p>
<p>Solution Text</p>
</div>
</solution>
</problem>
Long Answer¶
Creates multiline textboxes for questions that require larger amounts of text.
<problem>
<script type="loncapa/python">
import re
#the check answer functions below will mark any entries in the textboxes as correct
def checkAnswer1(expect, ans):
response = re.search('', ans)
if response:
return 1
else:
return 0
def checkAnswer2(expect, ans):
response = re.search('', ans)
if response:
return 1
else:
return 0
</script>
<!--Every textbox needs 2 unique identifier tags, edit the ids in the span tags and in the javascript below to add a 3rd textbox-->
<!--unique_identifier_1-->
<script type="text/javascript">
/* Replace 1 single line textinput with multiline textbox*/
(function() {
var elem = $("#unique_identifier_1")
.closest("div.problem #unique_identifier_1_refine_1")
.find(":text");
/* Adjust the number of rows and cols below as needed*/
var textarea = $('<textarea style="height:150px" rows="20" cols="70" />');
console.log(elem);
console.log(textarea);
for (attrib in {'id':null, 'name':null}) {
textarea.attr(attrib, elem.attr(attrib));
}
textarea.val(elem.val())
elem.replaceWith(textarea);
})();
</script>
<!--unique_identifier_2-->
<script type="text/javascript">
/* Replace 1 single line textinput with multiline textbox*/
(function() {
var elem = $("#unique_identifier_2")
.closest("div.problem #unique_identifier_2_refine_2")
.find(":text");
/* Adjust the number of rows and cols below as needed*/
var textarea = $('<textarea style="height:150px" rows="20" cols="70" />');
console.log(elem);
console.log(textarea);
for (attrib in {'id':null, 'name':null}) {
textarea.attr(attrib, elem.attr(attrib));
}
textarea.val(elem.val())
elem.replaceWith(textarea);
})();
</script>
<span id="unique_identifier_1_refine_1">
<span id="unique_identifier_1">
<p>Question Text</p>
<customresponse cfn="checkAnswer1">
<textline size="40" correct_answer="Correct Answer Displayed to Students" label="Question Text"/>
</customresponse>
<br/>
<br/>
</span>
</span>
<span id="unique_identifier_2_refine_2">
<span id="unique_identifier_2">
<p>Question Text</p>
<customresponse cfn="checkAnswer2">
<textline size="40" correct_answer="Correct Answer Displayed to Students" label="Question Text"/>
</customresponse>
</span>
</span>
<br/>
<br/>
</problem>
Custom Numerical Response¶
The template below removes alphabetical characters, percent signs and converts commas to decimal places. This avoids the error message resulting from standard edX numerical problems.
<problem>
<script type="loncapa/python">
import re
import string
def answerConverter(answer):
try:
answer = str(answer)
#Removes alpha numerical characters and % sign from the answer
answer= re.sub("[A-Za-z]|%","", answer)
#Converts commas to decimal points
answer = re.sub(',',".",answer)
answer = float(answer)
return answer
except ValueError:
return False
def checkAnswer(expect, ans):
try:
#Replace 10 with the correct numerical answer
correctAnswer = float(10)
#Replace 1 with the desired numerical tolerance
tolerance = 1
submittedAnswer = answerConverter(ans)
toleratedAnswer = abs(correctAnswer-submittedAnswer)
if tolerance>=toleratedAnswer:
return True
else:
return False
except ValueError:
return False
</script>
<p>Question Text</p>
<customresponse cfn="checkAnswer">
<textline size="40" correct_answer="Answer Displayed to Students" label="Question Text"/><br/>
</customresponse>
<solution>
<div class="detailed-solution">
<p>Explanation</p>
<p> Solution </p>
</div>
</solution>
</problem>
Self Report Completion Button¶
A button that when clicked awards points
<problem>
<script type="text/javascript">
// get the JSinput element //
// its ID is stored as the attribute "inputid" in a div with name "JSinput" //
jsi_div = parent.document.getElementsByName('JSinput')[0];
jsi_id = jsi_div.getAttribute('inputid');
jsi = parent.document.getElementById(jsi_id);
jsi.value = "yes"
</script>
<script type="text/javascript">
(function() {
$( ".check-label" ).text("Text displayed on self report button");
$( ".check.Check" ).css({"text-transform": "none"});
// To center all submit buttons on page uncomment the line below
// $( "div.problem div.action").css({"text-align":"center"});
//Replace the Title of Problem
$("input[value='Title of Problem']").parent("div.action").css({"text-align":"center"});
})();
</script>
<script type="text/python">
import re
def checkAnswer(expect, ans):
try:
a1=str(ans[0])
if a1=="yes":
return True
else:
return True
except ValueError:
return True
</script>
<customresponse cfn="checkAnswer">
<textline hidden="JSinput"/>
</customresponse>
</problem>