Commit b6ec41ca by Victor Shnayder

Merge pull request #1432 from MITx/7012x-clean

7012x clean
parents 965e3b4c 0141f31d
......@@ -834,3 +834,108 @@ class DragAndDropInput(InputTypeBase):
registry.register(DragAndDropInput)
#--------------------------------------------------------------------------------------------------------------------
class EditAMoleculeInput(InputTypeBase):
"""
An input type for edit-a-molecule. Integrates with the molecule editor java applet.
Example:
<editamolecule size="50"/>
options: size -- width of the textbox.
"""
template = "editamolecule.html"
tags = ['editamoleculeinput']
@classmethod
def get_attributes(cls):
"""
Can set size of text field.
"""
return [Attribute('file'),
Attribute('missing', None)]
def _extra_context(self):
"""
"""
context = {
'applet_loader': '/static/js/capa/editamolecule.js',
}
return context
registry.register(EditAMoleculeInput)
#-----------------------------------------------------------------------------
class DesignProtein2dInput(InputTypeBase):
"""
An input type for design of a protein in 2D. Integrates with the Protex java applet.
Example:
<designprotein2d width="800" hight="500" target_shape="E;NE;NW;W;SW;E;none" />
"""
template = "designprotein2dinput.html"
tags = ['designprotein2dinput']
@classmethod
def get_attributes(cls):
"""
Note: width, hight, and target_shape are required.
"""
return [Attribute('width'),
Attribute('height'),
Attribute('target_shape')
]
def _extra_context(self):
"""
"""
context = {
'applet_loader': '/static/js/capa/design-protein-2d.js',
}
return context
registry.register(DesignProtein2dInput)
#-----------------------------------------------------------------------------
class EditAGeneInput(InputTypeBase):
"""
An input type for editing a gene. Integrates with the genex java applet.
Example:
<editagene width="800" hight="500" dna_sequence="ETAAGGCTATAACCGA" />
"""
template = "editageneinput.html"
tags = ['editageneinput']
@classmethod
def get_attributes(cls):
"""
Note: width, hight, and dna_sequencee are required.
"""
return [Attribute('width'),
Attribute('height'),
Attribute('dna_sequence')
]
def _extra_context(self):
"""
"""
context = {
'applet_loader': '/static/js/capa/edit-a-gene.js',
}
return context
registry.register(EditAGeneInput)
......@@ -875,7 +875,8 @@ def sympy_check2():
allowed_inputfields = ['textline', 'textbox', 'crystallography',
'chemicalequationinput', 'vsepr_input',
'drag_and_drop_input']
'drag_and_drop_input', 'editamoleculeinput',
'designprotein2dinput', 'editageneinput']
def setup_response(self):
xml = self.xml
......
......@@ -31,7 +31,6 @@
<div id="input_${id}_preview" class="equation">
</div>
<p id="answer_${id}" class="answer"></p>
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']:
......
<section id="designprotein2dinput_${id}" class="designprotein2dinput">
<div class="script_placeholder" data-src="${applet_loader}"/>
% if status == 'unsubmitted':
<div class="unanswered" id="status_${id}">
% elif status == 'correct':
<div class="correct" id="status_${id}">
% elif status == 'incorrect':
<div class="incorrect" id="status_${id}">
% elif status == 'incomplete':
<div class="incorrect" id="status_${id}">
% endif
<object type="application/x-java-applet" id="applet_${id}" class="applet" width="${width}" height="${height}">
<param name="archive" value="/static/applets/capa/Protex.jar" />
<param name="code" value="protex.ProtexApplet.class" />
<param name="TARGET_SHAPE" value="${target_shape}" />
Applet failed to run. No Java plug-in was found.
</object>
<input type="hidden" name="input_${id}" id="input_${id}" value="${value|h}"/>
<p class="status">
% if status == 'unsubmitted':
unanswered
% elif status == 'correct':
correct
% elif status == 'incorrect':
incorrect
% elif status == 'incomplete':
incomplete
% endif
</p>
<p id="answer_${id}" class="answer"></p>
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']:
</div>
% endif
</section>
<section id="editageneinput_${id}" class="editageneinput">
<div class="script_placeholder" data-src="${applet_loader}"/>
% if status == 'unsubmitted':
<div class="unanswered" id="status_${id}">
% elif status == 'correct':
<div class="correct" id="status_${id}">
% elif status == 'incorrect':
<div class="incorrect" id="status_${id}">
% elif status == 'incomplete':
<div class="incorrect" id="status_${id}">
% endif
<object type="application/x-java-applet" id="applet_${id}" class="applet" width="${width}" height="${height}">
<param name="archive" value="/static/applets/capa/genex.jar" />
<param name="code" value="GX.GenexApplet.class" />
<param name="DNA_SEQUENCE" value="${dna_sequence}" />
Applet failed to run. No Java plug-in was found.
</object>
<input type="hidden" name="input_${id}" id="input_${id}" value="${value|h}"/>
<p class="status">
% if status == 'unsubmitted':
unanswered
% elif status == 'correct':
correct
% elif status == 'incorrect':
incorrect
% elif status == 'incomplete':
incomplete
% endif
</p>
<p id="answer_${id}" class="answer"></p>
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']:
</div>
% endif
</section>
<section id="editamoleculeinput_${id}" class="editamoleculeinput">
<div class="script_placeholder" data-src="${applet_loader}"/>
% if status == 'unsubmitted':
<div class="unanswered" id="status_${id}">
% elif status == 'correct':
<div class="correct" id="status_${id}">
% elif status == 'incorrect':
<div class="incorrect" id="status_${id}">
% elif status == 'incomplete':
<div class="incorrect" id="status_${id}">
% endif
<div id="applet_${id}" class="applet" data-molfile-src="${file}" style="display:block;width:500px;height:400px">
</div>
<br/>
<input type="hidden" name="input_${id}" id="input_${id}" value="${value|h}"/>
<button id="reset_${id}" class="reset">Reset</button>
<p id="answer_${id}" class="answer"></p>
<p class="status">
% if status == 'unsubmitted':
unanswered
% elif status == 'correct':
correct
% elif status == 'incorrect':
incorrect
% elif status == 'incomplete':
incomplete
% endif
</p>
<br/> <br/>
<div class="error_message" style="padding: 5px 5px 5px 5px; background-color:#FA6666; height:60px;width:400px; display: none"></div>
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']:
</div>
% endif
</section>
# A simple script demonstrating how to have an external program post problem
# responses to an edx server.
#
# ***** NOTE *****
# This is not intended as a stable public API. In fact, it is almost certainly
# going to change. If you use this for some reason, be prepared to change your
# code.
#
# We will be working to define a stable public API for external programs. We
# don't have have one yet (Feb 2013).
import requests
import sys
import getpass
def prompt(msg, default=None, safe=False):
d = ' [{0}]'.format(default) if default is not None else ''
prompt = 'Enter {msg}{default}: '.format(msg=msg, default=d)
if not safe:
print prompt
x = sys.stdin.readline().strip()
else:
x = getpass.getpass(prompt=prompt)
if x == '' and default is not None:
return default
return x
server = 'https://www.edx.org'
course_id = 'HarvardX/PH207x/2012_Fall'
location = 'i4x://HarvardX/PH207x/problem/ex_practice_2'
#server = prompt('Server (no trailing slash)', 'http://127.0.0.1:8000')
#course_id = prompt('Course id', 'MITx/7012x/2013_Spring')
#location = prompt('problem location', 'i4x://MITx/7012x/problem/example_upload_answer')
value = prompt('value to upload')
username = prompt('username on server', 'victor@edx.org')
password = prompt('password', 'abc123', safe=True)
print "get csrf cookie"
session = requests.session()
r = session.get(server + '/')
r.raise_for_status()
# print session.cookies
# for some reason, the server expects a header containing the csrf cookie, not just the
# cookie itself.
session.headers['X-CSRFToken'] = session.cookies['csrftoken']
# for https, need a referer header
session.headers['Referer'] = server + '/'
login_url = '/'.join([server, 'login'])
print "log in"
r = session.post(login_url, {'email': 'victor@edx.org', 'password': 'Secret!', 'remember': 'false'})
#print "request headers: ", r.request.headers
#print "response headers: ", r.headers
r.raise_for_status()
url = '/'.join([server, 'courses', course_id, 'modx', location, 'problem_check'])
data = {'input_{0}_2_1'.format(location.replace('/','-').replace(':','').replace('--','-')): value}
#data = {'input_i4x-MITx-7012x-problem-example_upload_answer_2_1': value}
print "Posting to '{0}': {1}".format(url, data)
r = session.post(url, data)
r.raise_for_status()
print ("To see the uploaded answer, go to {server}/courses/{course_id}/jump_to/{location}"
.format(server=server, course_id=course_id, location=location))
......@@ -11,8 +11,9 @@ class @Problem
$(selector, @el)
bind: =>
@el.find('.problem > div').each (index, element) =>
MathJax.Hub.Queue ["Typeset", MathJax.Hub, element]
if MathJax?
@el.find('.problem > div').each (index, element) =>
MathJax.Hub.Queue ["Typeset", MathJax.Hub, element]
window.update_schematics()
......@@ -31,8 +32,9 @@ class @Problem
# Dynamath
@$('input.math').keyup(@refreshMath)
@$('input.math').each (index, element) =>
MathJax.Hub.Queue [@refreshMath, null, element]
if MathJax?
@$('input.math').each (index, element) =>
MathJax.Hub.Queue [@refreshMath, null, element]
updateProgress: (response) =>
if response.progress_changed
......@@ -230,8 +232,9 @@ class @Problem
showMethod = @inputtypeShowAnswerMethods[cls]
showMethod(inputtype, display, answers) if showMethod?
@el.find('.problem > div').each (index, element) =>
MathJax.Hub.Queue ["Typeset", MathJax.Hub, element]
if MathJax?
@el.find('.problem > div').each (index, element) =>
MathJax.Hub.Queue ["Typeset", MathJax.Hub, element]
@$('.show').val 'Hide Answer'
@el.addClass 'showed'
......@@ -273,7 +276,7 @@ class @Problem
preprocessor_tag = "inputtype_" + elid
mathjax_preprocessor = @inputtypeDisplays[preprocessor_tag]
if jax = MathJax.Hub.getAllJax(target)[0]
if MathJax? and jax = MathJax.Hub.getAllJax(target)[0]
eqn = $(element).val()
if mathjax_preprocessor
eqn = mathjax_preprocessor(eqn)
......@@ -286,7 +289,8 @@ class @Problem
$("##{element.id}_dynamath").val(jax.root.toMathML '')
catch exception
throw exception unless exception.restart
MathJax.Callback.After [@refreshMath, jax], exception.restart
if MathJax?
MathJax.Callback.After [@refreshMath, jax], exception.restart
refreshAnswers: =>
@$('input.schematic').each (index, element) ->
......
(function () {
var timeout = 1000;
function initializeApplet(applet) {
console.log("Initializing " + applet);
waitForApplet(applet);
}
function waitForApplet(applet) {
if (applet.isActive && applet.isActive()) {
console.log("Applet is ready.");
// FIXME: [rocha] This is a hack to capture the click on the check
// button and update the hidden field with the applet values
var input_field = $('.designprotein2dinput input');
var problem = $(applet).parents('.problem');
var check_button = problem.find('input.check');
check_button.on('click', function() {
var answerStr = applet.checkAnswer();
console.log(answerStr);
input_field.val(answerStr);
});
} else {
console.log("Waiting for applet...");
setTimeout(function() { waitForApplet(applet); }, timeout);
}
}
var applets = $('.designprotein2dinput object');
applets.each(function(i, el) { initializeApplet(el); });
}).call(this);
(function () {
var timeout = 1000;
function initializeApplet(applet) {
console.log("Initializing " + applet);
waitForApplet(applet);
}
function waitForApplet(applet) {
if (applet.isActive && applet.isActive()) {
console.log("Applet is ready.");
var answerStr = applet.checkAnswer();
console.log(answerStr);
var input = $('.editageneinput input');
console.log(input);
input.val(answerStr);
} else if (timeout > 30 * 1000) {
console.error("Applet did not load on time.");
} else {
console.log("Waiting for applet...");
setTimeout(function() { waitForApplet(applet); }, timeout);
}
}
var applets = $('.editageneinput object');
applets.each(function(i, el) { initializeApplet(el); });
}).call(this);
(function () {
var timeout = 100;
// Simple "lock" to prevent applets from being initialized more than once
if (typeof(_editamolecule_loaded) == 'undefined' || _editamolecule_loaded == false) {
_editamolecule_loaded = true;
loadGWTScripts();
waitForGWT();
} else {
return;
}
function loadScript(src) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', src);
$('head')[0].appendChild(script);
}
function loadGWTScripts() {
// The names of the script are split to prevent them from
// being rewritten by LMS. GWT uses the filename of the script
// to find the URL path in which the script lives. If the name
// of the file is changed, GWT won't load correctly
var jsmolcalc_src = '/sta' + 'tic/js/capa/jsmolcalc/jsmolcalc.nocache.js';
var jsme_src = '/sta' + 'tic/js/capa/jsme/jsme_export.nocache.js';
// Make sure we don't request the scripts twice
if (typeof (_jsmolcalc) == 'undefined') {
_jsmolcalc = true;
loadScript(jsmolcalc_src);
}
if (typeof (_jsme) == 'undefined') {
_jsme = true;
loadScript(jsme_src);
}
}
function waitForGWT() {
// jsme and jsmolcalc are not initialized automatically by the GWT
// script loader. To fix this, wait for the scripts to load,
// initialize them manually and wait until they are ready
if (typeof(jsmolcalc) != 'undefined' && jsmolcalc)
{
jsmolcalc.onInjectionDone('jsmolcalc');
}
if (typeof(jsme_export) != 'undefined' && jsme_export)
{
// dummy function called by jsme_export
window.jsmeOnLoad = function() {};
jsme_export.onInjectionDone('jsme_export');
}
// jsmol is defined my jsmolcalc and JavaScriptApplet is defined by jsme
if (typeof(jsmol) != 'undefined' && typeof(JavaScriptApplet) != 'undefined') {
// ready, initialize applets
initializeApplets();
_editamolecule_loaded = false; // for reloading when checking is pressed
} else {
setTimeout(waitForGWT, timeout);
}
}
function initializeApplets() {
var applets = $('.editamoleculeinput div.applet');
applets.each(function(i, element) {
if (!$(element).hasClass('loaded')) {
var applet = new JavaScriptApplet.JSME(
element.id,
$(element).width(),
$(element).height(),
{
"options" : "query, hydrogens"
});
$(element).addClass('loaded');
configureApplet(element, applet);
}
});
}
function configureApplet(element, applet) {
// Traverse up the DOM tree and get the other relevant elements
var parent = $(element).parent();
var input_field = parent.find('input[type=hidden]');
var reset_button = parent.find('button.reset');
var message_field = parent.find('.error_message');
// Applet options
applet.setAntialias(true);
// Load initial data
var value = input_field.val();
if (value) {
var data = JSON.parse(value)["mol"];
loadAppletData(applet, data, input_field);
} else {
requestAppletData(element, applet, input_field);
}
reset_button.on('click', function() {
requestAppletData(element, applet, input_field);
message_field.html('').hide(); // clear messages
});
// Update the input element everytime the is an interaction
// with the applet (click, drag, etc)
$(element).on('mouseup', function() {
var values = updateInput(applet, input_field);
updateMessages(message_field, values);
});
}
function requestAppletData(element, applet, input_field) {
var molFile = $(element).data('molfile-src');
jQuery.ajax({
url: molFile,
dataType: "text",
success: function(data) {
loadAppletData(applet, data, input_field);
},
error: function() {
console.error("Cannot load mol data from: " + molFile);
}
});
}
function loadAppletData(applet, data, input_field) {
applet.readMolFile(data);
updateInput(applet, input_field);
}
function updateInput(applet, input_field) {
var mol = applet.molFile();
var smiles = applet.smiles();
var jme = applet.jmeFile();
var raw_info = jsmol.API.getInfo(mol, smiles, jme).toString();
var info = formatInfo(raw_info);
var error = formatError(raw_info);
var value = { mol: mol, info: info, error: error };
input_field.val(JSON.stringify(value));
return value;
}
function formatInfo(raw_info) {
var results = [];
if (raw_info.search("It is not possible") == -1) {
var fragment = $('<div>').append(raw_info);
fragment.find('font').each(function () {
results.push($(this).html());
});
}
return results;
}
function formatError(raw_info) {
var error = '';
if (raw_info.search("It is not possible") != -1) {
var tags = /<((\/)?\w{1,7})>/g;
error = raw_info.replace(tags, ' ');
}
return error;
}
function updateMessages(message_field, values) {
var error = values['error'];
if (error) {
message_field.html(error).show();
} else {
// Clear messages
message_field.html('').hide();
}
}
}).call(this);
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
(Originally written by Ike.)
At a high level, the main challenges of checking symbolic math expressions are (1) making sure the expression is mathematically legal, and (2) simplifying the expression for comparison with what is expected.
(1) Generation (and testing) of legal input is done by using MathJax to provide input math in an XML format known as Presentation MathML (PMathML). Such expressions typeset correctly, but may not be mathematically legal, like "5 / (1 = 2)". The PMathML is converted into "Content MathML" (CMathML), which is by definition mathematically legal, using an XSLT 2.0 stylesheet, via a module in SnuggleTeX. CMathML is then converted into a sympy expression. This work is all done in `lms/lib/symmath/formula.py`
(2) Simplifying the expression and checking against what is expected is done by using sympy, and a set of heuristics based on options flags provided by the problem author. For example, the problem author may specify that the expected expression is a matrix, in which case the dimensionality of the input expression is checked. Other options include specifying that the comparison be checked numerically in addition to symbolically. The checking is done in stages, first with no simplification, then with increasing levels of testing; if a match is found at any stage, then an "ok" is returned. Helpful messages are also returned, eg if the input expression is of a different type than the expected. This work is all done in `lms/lib/symmath/symmath_check.py`
Links:
SnuggleTex: http://www2.ph.ed.ac.uk/snuggletex/documentation/overview-and-features.html
MathML: http://www.w3.org/TR/MathML2/overview.html
SymPy: http://sympy.org/en/index.html
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