Commit c47fed9b by Alan Boudreault

Implemented min_characters for free text answer

parent d1af936a
...@@ -29,7 +29,7 @@ from lazy import lazy ...@@ -29,7 +29,7 @@ from lazy import lazy
from xblock.fragment import Fragment from xblock.fragment import Fragment
from .light_children import LightChild, Boolean, Scope, String from .light_children import LightChild, Boolean, Scope, String, Integer
from .models import Answer from .models import Answer
from .utils import render_template from .utils import render_template
...@@ -51,6 +51,8 @@ class AnswerBlock(LightChild): ...@@ -51,6 +51,8 @@ class AnswerBlock(LightChild):
read_only = Boolean(help="Display as a read-only field", default=False, scope=Scope.content) read_only = Boolean(help="Display as a read-only field", default=False, scope=Scope.content)
default_from = String(help="If specified, the name of the answer to get the default value from", default_from = String(help="If specified, the name of the answer to get the default value from",
default=None, scope=Scope.content) default=None, scope=Scope.content)
min_characters = Integer(help="Minimum number of characters allowed for the answer",
scope=Scope.content)
@lazy @lazy
def student_input(self): def student_input(self):
...@@ -106,7 +108,11 @@ class AnswerBlock(LightChild): ...@@ -106,7 +108,11 @@ class AnswerBlock(LightChild):
@property @property
def completed(self): def completed(self):
return bool(self.read_only or self.student_input) answer_length_ok = self.student_input
if self.min_characters > 0:
answer_length_ok = len(self.student_input) >= self.min_characters
return bool(self.read_only or answer_length_ok)
def save(self): def save(self):
""" """
......
function AnswerBlock(runtime, element) { function AnswerBlock(runtime, element) {
return { return {
submit: function() { submit: function() {
return $(element).find(':input').serializeArray(); var input = $(':input', element),
answer_length = input.val().length,
data = input.data();
if (_.isNumber(data.min_characters) &&
(data.min_characters > 0) &&
(answer_length < data.min_characters)) {
throw "The answer has a minimum of " + data.min_characters + " characters.";
}
return input.serializeArray();
}, },
handleSubmit: function(result) { handleSubmit: function(result) {
$(element).find('.message').text((result || {}).error || ''); $(element).find('.message').text((result || {}).error || '');
......
...@@ -77,16 +77,25 @@ function MentoringBlock(runtime, element) { ...@@ -77,16 +77,25 @@ function MentoringBlock(runtime, element) {
var submit_dom = $(element).find('.submit .input-main'); var submit_dom = $(element).find('.submit .input-main');
submit_dom.bind('click', function() { submit_dom.bind('click', function() {
var success = true;
var data = {}; var data = {};
var children = getChildren(element); var children = getChildren(element);
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
var child = children[i]; var child = children[i];
if (child.name !== undefined) { if (child.name !== undefined) {
try {
data[child.name] = callIfExists(child, 'submit'); data[child.name] = callIfExists(child, 'submit');
} }
catch (error) {
alert(error);
success = false;
} }
}
}
if (success) {
var handlerUrl = runtime.handlerUrl(element, 'submit'); var handlerUrl = runtime.handlerUrl(element, 'submit');
$.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults); $.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults);
}
}); });
// init children (especially mrq blocks) // init children (especially mrq blocks)
......
<textarea class="answer editable" cols="50" rows="10" name="input">{{ self.student_input }}</textarea> <textarea
class="answer editable" cols="50" rows="10" name="input"
data-min_characters="{{ self.min_characters }}"
>{{ self.student_input }}</textarea>
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