Files
edx-platform/course_data_formats/custom_response.html
2016-02-08 14:32:32 -05:00

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 &mdash; 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> &raquo;</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">&lt;answer&gt;</span></tt> tag:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;problem&gt;</span>
<span class="nt">&lt;p&gt;</span>What is the sum of 2 and 3?<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;customresponse</span> <span class="na">expect=</span><span class="s">&quot;5&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;textline</span> <span class="na">math=</span><span class="s">&quot;1&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/customresponse&gt;</span>
<span class="nt">&lt;answer&gt;</span>
# Python script goes here
<span class="nt">&lt;/answer&gt;</span>
<span class="nt">&lt;/problem&gt;</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">&lt;customresponse&gt;</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">&quot;correct&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;incorrect&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;unknown&quot;</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">&quot;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&quot;</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">&#39;correct&#39;</span>
<span class="n">overall_message</span> <span class="o">=</span> <span class="s">&#39;Good job!&#39;</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">&#39;incorrect&#39;</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">&#39;This answer is incorrect&#39;</span>
<span class="n">overall_message</span> <span class="o">=</span> <span class="s">&#39;Please try again&#39;</span>
</pre></div>
</div>
<p><strong>Important</strong>: Python is picky about indentation. Within the <tt class="docutils literal"><span class="pre">&lt;answer&gt;</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 &#8220;checking function&#8221;
in a <tt class="docutils literal"><span class="pre">&lt;script&gt;</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">&lt;customresponse&gt;</span></tt> tag:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;problem&gt;</span>
<span class="nt">&lt;p&gt;</span>What is the sum of 2 and 3?<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;customresponse</span> <span class="na">cfn=</span><span class="s">&quot;check_func&quot;</span> <span class="na">expect=</span><span class="s">&quot;5&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;textline</span> <span class="na">math=</span><span class="s">&quot;1&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/customresponse&gt;</span>
<span class="nt">&lt;script</span> <span class="na">type=</span><span class="s">&quot;loncapa/python&quot;</span><span class="nt">&gt;</span>
def check_func(expect, ans):
# Python script goes here
<span class="nt">&lt;/script&gt;</span>
<span class="nt">&lt;/problem&gt;</span>
</pre></div>
</div>
<p><strong>Important</strong>: Python is picky about indentation. Within the <tt class="docutils literal"><span class="pre">&lt;script&gt;</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">&lt;customresponse&gt;</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&#8217;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>{ &#39;overall_message&#39;: &#39;Overall message&#39;,
&#39;input_list&#39;: [
{ &#39;ok&#39;: True, &#39;msg&#39;: &#39;Feedback for input 1&#39;},
{ &#39;ok&#39;: False, &#39;msg&#39;: &#39;Feedback for input 2&#39;},
... ] }
</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">&#39;overall_message&#39;</span><span class="p">:</span> <span class="s">&#39;Overall message&#39;</span><span class="p">,</span>
<span class="s">&#39;input_list&#39;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span> <span class="s">&#39;ok&#39;</span><span class="p">:</span> <span class="n">check1</span><span class="p">,</span> <span class="s">&#39;msg&#39;</span><span class="p">:</span> <span class="s">&#39;Feedback 1&#39;</span><span class="p">},</span>
<span class="p">{</span> <span class="s">&#39;ok&#39;</span><span class="p">:</span> <span class="n">check2</span><span class="p">,</span> <span class="s">&#39;msg&#39;</span><span class="p">:</span> <span class="s">&#39;Feedback 2&#39;</span><span class="p">},</span>
<span class="p">{</span> <span class="s">&#39;ok&#39;</span><span class="p">:</span> <span class="n">check3</span><span class="p">,</span> <span class="s">&#39;msg&#39;</span><span class="p">:</span> <span class="s">&#39;Feedback 3&#39;</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> &raquo;</li>
</ul>
</div>
<div class="footer">
&copy; Copyright 2013, edX Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>