# Lesson - Numerical - Input validation for numerical values

XMLWordPrintable

#### Details

• Type: Bug
• Status: Closed
• Priority: Minor
• Resolution: Fixed
• Affects Version/s: 3.1.3, 3.2
• Fix Version/s:
• Component/s:
• Labels:
• Testing Instructions:
Hide

As a teacher...

1. Create a lesson
2. Add a question page of type "Numerical"

As a student...

1. Start the lesson and navigate to the numerical question type. Successively, try to enter the following (without the quotes)...
• "yes"
• "1 tree"
• "" (empty string!)
1. Each time, expect a warning: "One or more questions have no answer given. Please go back and submit an answer." ... this is perfect.
2. To verify that intended behaviour still works, try entering one of
• "0" (actively entering 0 on purpose should work)
• "1.0" (floating-point numbers as well)
Show
As a teacher... Create a lesson Add a question page of type "Numerical" As a student... Start the lesson and navigate to the numerical question type. Successively, try to enter the following (without the quotes)... " yes " " 1 tree " "" (empty string!) Each time, expect a warning: "One or more questions have no answer given. Please go back and submit an answer." ... this is perfect. To verify that intended behaviour still works, try entering one of " 0 " (actively entering 0 on purpose should work) " 1.0 " (floating-point numbers as well)
• Affected Branches:
MOODLE_31_STABLE, MOODLE_32_STABLE
• Fixed Branches:
MOODLE_31_STABLE, MOODLE_32_STABLE
• Pull from Repository:
• Pull Master Branch:
wip-MDL-57257-master
• Pull Master Diff URL:

#### Description

I can add numerical questions to a lesson. If students submit an answer, it is compared to the range(s) that are defined by a teacher.
However, it seems to me that no validation takes place, checking the kind of answer that a student submits, i.e. the following could happen:

1. if a student submits "yes" or "no", the answer is merely converted to 0 by PHP typecasting (\$result->useranswer = (float)\$data->answer; in https://github.com/moodle/moodle/blob/master/mod/lesson/pagetypes/numerical.php#L92)
2. if a student submits "1 tree", the input is converted to 1, similarly. (This might even be useful if students are unsure whether to submit a unit. Well...)
3. if a student submits "about 2 cars", again, the typecast changes the value to 0 because the string does not start with a number.
4. if a student submits nothing, the mform validates as well, coercing the "answer" to 0.

This is rather bad especially if the correct numerical answer is "0", because arbitrary strings lead to the correct answer by accident. In addition, this results in loss of information because the converted answer is evaluated and stored, and the input string is lost.

Anyway, I think that input validation would be useful here; providing means to report e.g. "Your input is not numerical". This would avoid unwanted loss of information by providing a helpful message.

#### People

Assignee:
Jan Dageförde
Reporter:
Jan Dageförde
Peer reviewer:
Integrator:
Dan Poltawski
Tester:
Frédéric Massart
Participants:
Component watchers:
Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias, Sujith Haridasan