MDL-8475 - Better handling of blank answer - further changes to make it even more robust. Also, make the same changes to the shortanswer question type that were already made to the numerical question type.

This commit is contained in:
tjhunt
2008-08-28 01:46:47 +00:00
parent f1ea9b43b7
commit f863bed701
6 changed files with 97 additions and 79 deletions
+1
View File
@@ -2,4 +2,5 @@
$string['addmoreanswerblanks'] = 'Blanks for {no} More Answers';
$string['answerno'] = 'Answer $a';
$string['filloutoneanswer'] = 'You must provide at least one possible answer. Answers left blank will not be used. \'*\' can be used as a wildcard to match any characters. The first matching answer will be used to determine the score and feedback.';
$string['answermustbegiven'] = 'You must enter an answer if there is a grade or feedback.';
?>
+12 -1
View File
@@ -600,7 +600,18 @@ function clean_param($param, $type) {
}
}
/**
* This function is useful for testing whether something you got back from
* the HTML editor actually contains anything. Sometimes the HTML editor
* appear to be empty, but actually you get back a <br> tag or something.
*
* @param string $string a string containing HTML.
* @return boolean does the string contain any actual content - that is text,
* images, objcts, etc.
*/
function html_is_blank($string) {
return trim(strip_tags($string, '<img><object><applet><input><select><textarea><hr>')) == '';
}
/**
* Set a key in global configuration
@@ -124,14 +124,9 @@ class question_edit_numerical_form extends question_edit_form {
if ($data['fraction'][$key] == 1) {
$maxgrade = true;
}
} else {
if ($data['fraction'][$key] !=0 || trim($data['feedback'][$key]) != ''){
$errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
$answercount++;
if (trim($data['feedback'][$key]) != ''){
$errors["feedback[$key]"]=get_string('feedback','quiz').'='.htmlspecialchars(trim($data['feedback'][$key]));
}
}
} else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) {
$errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
$answercount++;
}
}
if ($answercount==0){
+51 -47
View File
@@ -107,59 +107,63 @@ class question_numerical_qtype extends question_shortanswer_qtype {
// Insert all the new answers
foreach ($question->answer as $key => $dataanswer) {
if ( !( trim($dataanswer)=='' && $question->fraction[$key]== 0 && trim($question->feedback[$key])=='')) {
$answer = new stdClass;
$answer->question = $question->id;
if (trim($dataanswer) == '*') {
$answer->answer = '*';
} else {
$answer->answer = $this->apply_unit($dataanswer, $units);
if ($answer->answer === false) {
$result->notice = get_string('invalidnumericanswer', 'quiz');
}
}
$answer->fraction = $question->fraction[$key];
$answer->feedback = trim($question->feedback[$key]);
// Check for, and ingore, completely blank answer from the form.
if (trim($dataanswer) == '' && $question->fraction[$key] == 0 &&
html_is_blank($question->feedback[$key])) {
continue;
}
if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
$answer->id = $oldanswer->id;
if (! update_record("question_answers", $answer)) {
$result->error = "Could not update quiz answer! (id=$answer->id)";
return $result;
}
} else { // This is a completely new answer
if (! $answer->id = insert_record("question_answers", $answer)) {
$result->error = "Could not insert quiz answer!";
return $result;
}
$answer = new stdClass;
$answer->question = $question->id;
if (trim($dataanswer) == '*') {
$answer->answer = '*';
} else {
$answer->answer = $this->apply_unit($dataanswer, $units);
if ($answer->answer === false) {
$result->notice = get_string('invalidnumericanswer', 'quiz');
}
}
$answer->fraction = $question->fraction[$key];
$answer->feedback = trim($question->feedback[$key]);
// Set up the options object
if (!$options = array_shift($oldoptions)) {
$options = new stdClass;
if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
$answer->id = $oldanswer->id;
if (! update_record("question_answers", $answer)) {
$result->error = "Could not update quiz answer! (id=$answer->id)";
return $result;
}
$options->question = $question->id;
$options->answer = $answer->id;
if (trim($question->tolerance[$key]) == '') {
$options->tolerance = '';
} else {
$options->tolerance = $this->apply_unit($question->tolerance[$key], $units);
if ($options->tolerance === false) {
$result->notice = get_string('invalidnumerictolerance', 'quiz');
}
} else { // This is a completely new answer
if (! $answer->id = insert_record("question_answers", $answer)) {
$result->error = "Could not insert quiz answer!";
return $result;
}
}
// Save options
if (isset($options->id)) { // reusing existing record
if (! update_record('question_numerical', $options)) {
$result->error = "Could not update quiz numerical options! (id=$options->id)";
return $result;
}
} else { // new options
if (! insert_record('question_numerical', $options)) {
$result->error = "Could not insert quiz numerical options!";
return $result;
}
// Set up the options object
if (!$options = array_shift($oldoptions)) {
$options = new stdClass;
}
$options->question = $question->id;
$options->answer = $answer->id;
if (trim($question->tolerance[$key]) == '') {
$options->tolerance = '';
} else {
$options->tolerance = $this->apply_unit($question->tolerance[$key], $units);
if ($options->tolerance === false) {
$result->notice = get_string('invalidnumerictolerance', 'quiz');
}
}
// Save options
if (isset($options->id)) { // reusing existing record
if (! update_record('question_numerical', $options)) {
$result->error = "Could not update quiz numerical options! (id=$options->id)";
return $result;
}
} else { // new options
if (! insert_record('question_numerical', $options)) {
$result->error = "Could not insert quiz numerical options!";
return $result;
}
}
}
@@ -81,6 +81,9 @@ class question_edit_shortanswer_form extends question_edit_form {
if ($data['fraction'][$key] == 1) {
$maxgrade = true;
}
} else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) {
$errors["answer[$key]"] = get_string('answermustbegiven', 'qtype_shortanswer');
$answercount++;
}
}
if ($answercount==0){
+27 -23
View File
@@ -50,32 +50,36 @@ class question_shortanswer_qtype extends default_questiontype {
// Insert all the new answers
foreach ($question->answer as $key => $dataanswer) {
if ($dataanswer != "") {
if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
$answer = $oldanswer;
$answer->answer = trim($dataanswer);
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!update_record("question_answers", $answer)) {
$result->error = "Could not update quiz answer! (id=$answer->id)";
return $result;
}
} else { // This is a completely new answer
$answer = new stdClass;
$answer->answer = trim($dataanswer);
$answer->question = $question->id;
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!$answer->id = insert_record("question_answers", $answer)) {
$result->error = "Could not insert quiz answer!";
return $result;
}
// Check for, and ingore, completely blank answer from the form.
if (trim($dataanswer) == '' && $question->fraction[$key] == 0 &&
html_is_blank($question->feedback[$key])) {
continue;
}
if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it
$answer = $oldanswer;
$answer->answer = trim($dataanswer);
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!update_record("question_answers", $answer)) {
$result->error = "Could not update quiz answer! (id=$answer->id)";
return $result;
}
$answers[] = $answer->id;
if ($question->fraction[$key] > $maxfraction) {
$maxfraction = $question->fraction[$key];
} else { // This is a completely new answer
$answer = new stdClass;
$answer->answer = trim($dataanswer);
$answer->question = $question->id;
$answer->fraction = $question->fraction[$key];
$answer->feedback = $question->feedback[$key];
if (!$answer->id = insert_record("question_answers", $answer)) {
$result->error = "Could not insert quiz answer!";
return $result;
}
}
$answers[] = $answer->id;
if ($question->fraction[$key] > $maxfraction) {
$maxfraction = $question->fraction[$key];
}
}
if ($options = get_record("question_shortanswer", "question", $question->id)) {