263 lines
17 KiB
HTML
263 lines
17 KiB
HTML
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>CustomResponse XML and Python Script — edX Data 0.1 documentation</title>
|
|
|
|
<link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '../',
|
|
VERSION: '0.1',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
<link rel="top" title="edX Data 0.1 documentation" href="../index.html" />
|
|
<link rel="next" title="Student Info and Progress Data" href="../internal_data_formats/sql_schema.html" />
|
|
<link rel="prev" title="Xml format of conditional module [xmodule]" href="conditional_module/conditional_module.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="../internal_data_formats/sql_schema.html" title="Student Info and Progress Data"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="conditional_module/conditional_module.html" title="Xml format of conditional module [xmodule]"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><a href="../index.html">edX Data 0.1 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="../index.html">Table Of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">CustomResponse XML and Python Script</a><ul>
|
|
<li><a class="reference internal" href="#answer-tag-format">Answer tag format</a></li>
|
|
<li><a class="reference internal" href="#script-tag-format">Script tag format</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="conditional_module/conditional_module.html"
|
|
title="previous chapter">Xml format of conditional module [xmodule]</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="../internal_data_formats/sql_schema.html"
|
|
title="next chapter">Student Info and Progress Data</a></p>
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/course_data_formats/custom_response.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="../search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<div class="section" id="customresponse-xml-and-python-script">
|
|
<h1>CustomResponse XML and Python Script<a class="headerlink" href="#customresponse-xml-and-python-script" title="Permalink to this headline">¶</a></h1>
|
|
<p>This document explains how to write a CustomResponse problem. CustomResponse
|
|
problems execute Python script to check student answers and provide hints.</p>
|
|
<p>There are two general ways to create a CustomResponse problem:</p>
|
|
<div class="section" id="answer-tag-format">
|
|
<h2>Answer tag format<a class="headerlink" href="#answer-tag-format" title="Permalink to this headline">¶</a></h2>
|
|
<p>One format puts the Python code in an <tt class="docutils literal"><span class="pre"><answer></span></tt> tag:</p>
|
|
<div class="highlight-xml"><div class="highlight"><pre><span class="nt"><problem></span>
|
|
<span class="nt"><p></span>What is the sum of 2 and 3?<span class="nt"></p></span>
|
|
|
|
<span class="nt"><customresponse</span> <span class="na">expect=</span><span class="s">"5"</span><span class="nt">></span>
|
|
<span class="nt"><textline</span> <span class="na">math=</span><span class="s">"1"</span> <span class="nt">/></span>
|
|
<span class="nt"></customresponse></span>
|
|
|
|
<span class="nt"><answer></span>
|
|
# Python script goes here
|
|
<span class="nt"></answer></span>
|
|
<span class="nt"></problem></span>
|
|
</pre></div>
|
|
</div>
|
|
<dl class="docutils">
|
|
<dt>The Python script interacts with these variables in the global context:</dt>
|
|
<dd><ul class="first last simple">
|
|
<li><tt class="docutils literal"><span class="pre">answers</span></tt>: An ordered list of answers the student provided.
|
|
For example, if the student answered <tt class="docutils literal"><span class="pre">6</span></tt>, then <tt class="docutils literal"><span class="pre">answers[0]</span></tt> would
|
|
equal <tt class="docutils literal"><span class="pre">6</span></tt>.</li>
|
|
<li><tt class="docutils literal"><span class="pre">expect</span></tt>: The value of the <tt class="docutils literal"><span class="pre">expect</span></tt> attribute of <tt class="docutils literal"><span class="pre"><customresponse></span></tt>
|
|
(if provided).</li>
|
|
<li><tt class="docutils literal"><span class="pre">correct</span></tt>: An ordered list of strings indicating whether the
|
|
student answered the question correctly. Valid values are
|
|
<tt class="docutils literal"><span class="pre">"correct"</span></tt>, <tt class="docutils literal"><span class="pre">"incorrect"</span></tt>, and <tt class="docutils literal"><span class="pre">"unknown"</span></tt>. You can set these
|
|
values in the script.</li>
|
|
<li><tt class="docutils literal"><span class="pre">messages</span></tt>: An ordered list of message strings that will be displayed
|
|
beneath each input. You can use this to provide hints to users.
|
|
For example <tt class="docutils literal"><span class="pre">messages[0]</span> <span class="pre">=</span> <span class="pre">"The</span> <span class="pre">capital</span> <span class="pre">of</span> <span class="pre">California</span> <span class="pre">is</span> <span class="pre">Sacramento"</span></tt>
|
|
would display that message beneath the first input of the response.</li>
|
|
<li><tt class="docutils literal"><span class="pre">overall_message</span></tt>: A string that will be displayed beneath the
|
|
entire problem. You can use this to provide a hint that applies
|
|
to the entire problem rather than a particular input.</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<p>Example of a checking script:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">answers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">expect</span><span class="p">:</span>
|
|
<span class="n">correct</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'correct'</span>
|
|
<span class="n">overall_message</span> <span class="o">=</span> <span class="s">'Good job!'</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">correct</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'incorrect'</span>
|
|
<span class="n">messages</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'This answer is incorrect'</span>
|
|
<span class="n">overall_message</span> <span class="o">=</span> <span class="s">'Please try again'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Important</strong>: Python is picky about indentation. Within the <tt class="docutils literal"><span class="pre"><answer></span></tt> tag,
|
|
you must begin your script with no indentation.</p>
|
|
</div>
|
|
<div class="section" id="script-tag-format">
|
|
<h2>Script tag format<a class="headerlink" href="#script-tag-format" title="Permalink to this headline">¶</a></h2>
|
|
<p>The other way to create a CustomResponse is to put a “checking function”
|
|
in a <tt class="docutils literal"><span class="pre"><script></span></tt> tag, then use the <tt class="docutils literal"><span class="pre">cfn</span></tt> attribute of the
|
|
<tt class="docutils literal"><span class="pre"><customresponse></span></tt> tag:</p>
|
|
<div class="highlight-xml"><div class="highlight"><pre><span class="nt"><problem></span>
|
|
<span class="nt"><p></span>What is the sum of 2 and 3?<span class="nt"></p></span>
|
|
|
|
<span class="nt"><customresponse</span> <span class="na">cfn=</span><span class="s">"check_func"</span> <span class="na">expect=</span><span class="s">"5"</span><span class="nt">></span>
|
|
<span class="nt"><textline</span> <span class="na">math=</span><span class="s">"1"</span> <span class="nt">/></span>
|
|
<span class="nt"></customresponse></span>
|
|
|
|
<span class="nt"><script</span> <span class="na">type=</span><span class="s">"loncapa/python"</span><span class="nt">></span>
|
|
def check_func(expect, ans):
|
|
# Python script goes here
|
|
<span class="nt"></script></span>
|
|
<span class="nt"></problem></span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Important</strong>: Python is picky about indentation. Within the <tt class="docutils literal"><span class="pre"><script></span></tt> tag,
|
|
the <tt class="docutils literal"><span class="pre">def</span> <span class="pre">check_func(expect,</span> <span class="pre">ans):</span></tt> line must have no indentation.</p>
|
|
<dl class="docutils">
|
|
<dt>The check function accepts two arguments:</dt>
|
|
<dd><ul class="first last">
|
|
<li><p class="first"><tt class="docutils literal"><span class="pre">expect</span></tt> is the value of the <tt class="docutils literal"><span class="pre">expect</span></tt> attribute of <tt class="docutils literal"><span class="pre"><customresponse></span></tt>
|
|
(if provided)</p>
|
|
</li>
|
|
<li><p class="first"><tt class="docutils literal"><span class="pre">answer</span></tt> is either:</p>
|
|
<blockquote>
|
|
<div><ul class="simple">
|
|
<li>The value of the answer the student provided, if there is only one input.</li>
|
|
<li>An ordered list of answers the student provided, if there
|
|
are multiple inputs.</li>
|
|
</ul>
|
|
</div></blockquote>
|
|
</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<p>There are several ways that the check function can indicate whether the student
|
|
succeeded. The check function can return any of the following:</p>
|
|
<blockquote>
|
|
<div><ul class="simple">
|
|
<li><tt class="docutils literal"><span class="pre">True</span></tt>: Indicates that the student answered correctly for all inputs.</li>
|
|
<li><tt class="docutils literal"><span class="pre">False</span></tt>: Indicates that the student answered incorrectly.
|
|
All inputs will be marked incorrect.</li>
|
|
<li>A dictionary of the form: <tt class="docutils literal"><span class="pre">{</span> <span class="pre">'ok':</span> <span class="pre">True,</span> <span class="pre">'msg':</span> <span class="pre">'Message'</span> <span class="pre">}</span></tt>
|
|
If the dictionary’s value for <tt class="docutils literal"><span class="pre">ok</span></tt> is set to <tt class="docutils literal"><span class="pre">True</span></tt>, all inputs are
|
|
marked correct; if it is set to <tt class="docutils literal"><span class="pre">False</span></tt>, all inputs are marked incorrect.
|
|
The <tt class="docutils literal"><span class="pre">msg</span></tt> is displayed beneath all inputs, and it may contain
|
|
XHTML markup.</li>
|
|
<li>A dictionary of the form</li>
|
|
</ul>
|
|
</div></blockquote>
|
|
<div class="highlight-xml"><div class="highlight"><pre>{ 'overall_message': 'Overall message',
|
|
'input_list': [
|
|
{ 'ok': True, 'msg': 'Feedback for input 1'},
|
|
{ 'ok': False, 'msg': 'Feedback for input 2'},
|
|
... ] }
|
|
</pre></div>
|
|
</div>
|
|
<p>The last form is useful for responses that contain multiple inputs.
|
|
It allows you to provide feedback for each input individually,
|
|
as well as a message that applies to the entire response.</p>
|
|
<p>Example of a checking function:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">check_func</span><span class="p">(</span><span class="n">expect</span><span class="p">,</span> <span class="n">answer_given</span><span class="p">):</span>
|
|
<span class="n">check1</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">answer_given</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
|
|
<span class="n">check2</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">answer_given</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="n">check3</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">answer_given</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">==</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="p">{</span><span class="s">'overall_message'</span><span class="p">:</span> <span class="s">'Overall message'</span><span class="p">,</span>
|
|
<span class="s">'input_list'</span><span class="p">:</span> <span class="p">[</span>
|
|
<span class="p">{</span> <span class="s">'ok'</span><span class="p">:</span> <span class="n">check1</span><span class="p">,</span> <span class="s">'msg'</span><span class="p">:</span> <span class="s">'Feedback 1'</span><span class="p">},</span>
|
|
<span class="p">{</span> <span class="s">'ok'</span><span class="p">:</span> <span class="n">check2</span><span class="p">,</span> <span class="s">'msg'</span><span class="p">:</span> <span class="s">'Feedback 2'</span><span class="p">},</span>
|
|
<span class="p">{</span> <span class="s">'ok'</span><span class="p">:</span> <span class="n">check3</span><span class="p">,</span> <span class="s">'msg'</span><span class="p">:</span> <span class="s">'Feedback 3'</span><span class="p">}</span> <span class="p">]</span> <span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The function checks that the user entered <tt class="docutils literal"><span class="pre">1</span></tt> for the first input,
|
|
<tt class="docutils literal"><span class="pre">2</span></tt> for the second input, and <tt class="docutils literal"><span class="pre">3</span></tt> for the third input.
|
|
It provides feedback messages for each individual input, as well
|
|
as a message displayed beneath the entire problem.</p>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="../internal_data_formats/sql_schema.html" title="Student Info and Progress Data"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="conditional_module/conditional_module.html" title="Xml format of conditional module [xmodule]"
|
|
>previous</a> |</li>
|
|
<li><a href="../index.html">edX Data 0.1 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2013, edX Team.
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
|
|
</div>
|
|
</body>
|
|
</html> |