Development:Question type code flow
From MoodleDocs
This page is a proposal to set the rules for the question types code so that it manage correctly either
- the creation and edition process from a user interface
- the import of question from files of different formats.
RULE 1: The creation by the user and the import process should use the same
[QTYPE]->save_question() function Actually, there is one general save_question() that save the question general parameters as they are defined in the database question Table. then call the specific [QTYPE]->save_options() ==RULE 1.1==The [QTYPE]->save_question() function should accept either a possible multipage user input or a one step import. Actually the the only standar multipage question_type is the calculated which implies a three page user inputs.
TO-DO: modify the ['calculated']->save_question() so that it save the data from an import processed file
RULE 2: The [QTYPE]->save_question() accept only validated data.
All the validation should be done before submitting the question to the saving process. The only errors should be related to database problems excluding ones related to badly defined items (numeric instead of text etc.)
RULE 3: The question_types items should be standardized so to be directly loadable in the database
The question_types "standard" and "options" items should be well documented and given a specific name so that either the user interface or import code developpers have the necessary information
RULE 4: A general validate function for the general question parameters should be available
This should be useable by either
- the user interface edit_question_form
- than by format.php.
RULE 5: Each [QTYPES] should have a general validate options parameter function
There can be a general validate options for common elements to all question types like at least a 100% value answer. This should validate elements that are common in the user interface and the import porcess like number of answers, there property (text, numerical etc.)
RULE 5.1: The validation of specific elements necessary for the user interface should be put in a validate function of edit_qtype_form.php
RULE 5.2: The validation of specific elements necessary for the import should be put in the format code
I Disagree with the following two sections
There should only be one validation function: edit_question_xxx_form->validate data. This should, of course, call edit_question_form->validate_data via parent::validate_data, to do the stuff common to all question types. Question editing already uses this. Import should use it too.
Import needs to do addslashes before any validation is done, becuase that is what happens when saving the question. Tim Hunt 02:43, 15 March 2007 (CDT)
I will use the calculated question more complex case to study if your proposition can be used as you suggested. Also, if there is a need to do changes in the edit_question_forms and this reflect in the data validating, this would affect the imports. For example, recently we had to adapt the valiadation to the fact that the Quickform define null values differently than the preceding code. I will prefer to address directly the validate function through the question_type as I understand that the Quickform is not a question_type class. It this right? Pierre Pichet 07:05, 15 March 2007 (CDT)
Typical code flow from user creating and editing interface
- the edit_question_form->validate data
- transform html form objects (input, select) in general question parameters
- call the question_type->validate_general_question_parameter()
- the actual validation done in save_question()
default name, question text, general feedback etc.
- call the [qtype]->validate_options()
answers data, number of valid answers actually empty answerss are detected in save_options function, it should be done here
errors are used to set back the html form with specific message so that the user correct the data If, finally everything is validated OK then question2.php can call the[qtype]->save question that will call the save_options
Typical code flow from import
- the specific/ format->process()
- readquestions()
- transform the import fil data in general question parameters
- call the question_type->validate_general_question_parameter()
- the actual validation done in save_question()
default name, question text, general feedback etc.
- addslahes to all text question data
or this has been done by a modifed validate_general_question_parameter() with an additional paramter to tell the functn to addslashes.
- call the [qtype]->validate_options()
the specific format.php put the values in the expected parameters answers data, number of valid answers perhaps some specific format.php use similar code to do this and common functions could be created...
errors from the validation are used to alert the user of problems with the input file
If, finally everything is validated OK then call the[qtype]->save question that will call the save_options.
errors on return are treated specially because the last question should have been saved but not the options the user could want or not to keep the already saved correct questions
(to be continued Pierre Pichet 21:37, 14 March 2007 (CDT))
