Development:Calculated question js validating forms
Using javascript to correct for errors or non valid values in each steps of the creation or editing before going to the next step. This instant feedback will allow to save the data even when creating a calculated question because on output from the first step the presence of at least a valid calculated parameter ( dataset ) will have be verified. In the actual Moodle 1.7 ( 24 August 2006 (CDT)) when a calculated is created, the saving is postponed until a data item is added in the third form.
a modification in weblib.php to facilitate coding
When HTML edit is used, the editor is created through the \lib\weblib.php as a textarea with name = questiontext and id = edit-questiontext.
\lib\weblib.php(3271): $str .= '<textarea id="edit-'. $name .'" name="'. $name .'" rows="'. $rows .'" cols="'. $cols .'">';
and
\lib\weblib.php(3309): echo "edit_$name = new HTMLArea('edit-$name');\n";
The questiontext data (_textArea) can be accessed by either
if ( document.form['edit-questiontext']._textArea==)
or
if ( document.form['questiontext']._textArea==)
but not by
if ( document.form.edit-questiontext._textArea==)
because js understand document.form.edit - questiontext._textArea and effectively does not find document.form.edit . I propose to modify the id identification by using edit_ instead of edit-. The two lines should be corrected to \lib\weblib.php(3271): $str .= '<textarea id="edit_'. $name .'" name="'. $name .'" rows="'. $rows .'" cols="'. $cols .'">';
and
\lib\weblib.php(3309): echo "edit_$name = new HTMLArea('edit_$name');\n";
Doing this we could use
if ( document.form.edit_questiontext._textArea==)
to access the right value either in Explore, Netscape or Firefox browsers.
There is no side effects because the string edit-??? is nowhere else in all the Moodle code.
The code for validating the input values
A first proposal
function validate_input() {
// This client-side script will validate the input and determine the values of min and max
// based on the input for answer and acceptederror.
with(document.theform) {
var Ereg= /\{[A-Za-z]\w*\}/;
if (formula0.value==) {
alert('<?php print_string("missingformula","quiz") ?>');
return false;
<?php
if (($usehtmleditor && empty($question->id)) || !$usehtmleditor ){
?>
} else if ("edit_questiontext"._textArea==) {
alert('<?php print_string("questiontextempty","quiz") ?>');
return false;
<?php } ; ?>
} else if (!Ereg.test(formula0.value) ) {
alert('<?php print_string("nopossibledatasets","quiz") ?>');
//return false;
} else if (!Ereg.test(formula0.value)
<?php
if (($usehtmleditor && empty($question->id)) || !$usehtmleditor ){
?>
&& !Ereg.test("edit-questiontext".value)
<?php } ; ?>
) {
alert('<?php print_string("nopossibledatasets","quiz") ?>');
return false;
} else if (questionname.value==) {
questionname.value='_';
alert('<?php print_string("questionnamedefault","quiz") ?>');
return false;
} else if (questionname.length > 25) {
questionname=questionname.substring(0,20)+"...";
alert('<?php print_string("questionnametoolong","quiz") ?>');
return false;
} else if (''!=defaultunit.value && !isNaN(defaultunit.value)) {
alert('<?php print_string("unitmustnotbenumeric","quiz") ?>');
return false;
} else if (isNaN(tolerance0.value)) {
alert('<?php print_string("tolerancemustbenumeric","quiz") ?>');
return false;
} else if ('2' == document.theform['correctanswerformat[]'].value
&& '0' == document.theform['correctanswerlength[]'].value) {
alert('<?php print_string("zerosignificantfiguresnotallowed","quiz") ?>');
return false;
} else {
return true;
}
}
}