Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Calculated question validating the datasets.

Development:Calculated question validating the datasets: Difference between revisions

From MoodleDocs
Line 43: Line 43:
##define a new function  find_all_question_dataset_names($question, $mandatorydatasets, $possibledatasets) ;
##define a new function  find_all_question_dataset_names($question, $mandatorydatasets, $possibledatasets) ;
#Get all the datasets {a..} present in the mdl_question_datasetdef using
#Get all the datasets {a..} present in the mdl_question_datasetdef using
#Examine and validate all those question_dataset_names that are not already in the database using the following rules
#Examine and validate all those question_dataset_names  
##if there is no category dataset with the same name generate a local datasetdef and a link to it
# if the name IS NOT ALREADY in the list, generate a local dataset (category = 0) and a link to it.
##if there is a category dataset with the same name  
# if the name IS ALREADY in the list use the following rules
###and the same options  
## if it is a local dataset (category = 0) use it without modifications
####check if the category is the same as the question category (has been moved)
## if it is a category dataset (category > 0)
##### yes use it
### if the $dataset->category == $question->category use it without modifications
######add items if less than the already existing category
### if the $dataset->category != $question->category
##### no test if copy to the new category with the already  
####if there is no category dataset with the same name in the $question->category
### and not the same options create a local one
##### test if other questions are using this category dataset in the initial $dataset->category
######if NO question using it just change the category
######if YES other questions using it generate a new category dataset in the$question->category , a link to it and add a copy of the data items already defined in $dataset->category 
####if there is a category dataset with the same name  
#####and the same options use it make a link to it  and add a copy of the data items already defined in $dataset->category  that outnumber those already in $question->category (>itemnumber) 
#####and not the same option, copy as local dataset (category = 0) and a link to it add a copy of the data items already defined in $dataset->category  
 
###### yes use it
#######add items if less than the already existing category
###### no test if copy to the new category with the already  
#### and not the same options create a local one


(to be continued)
(to be continued)

Revision as of 18:33, 17 October 2006

This page contains working notes related to modifying the calculated question code for solving 1.7 bugs related to the handling of datasets and dataitems. They will be "cleaned" when the code proposals will be put in the Moodle code.

Selecting the right dataitem for a question

Actually the code select the dataitem number using the MAX value from the question_dataset_definitions->itemcount. It should choose the MIN so that there is a valid data item for each datasets. so in datasetdependent/abstractype.php line 29

       if(!$maxnumber = (int)get_field_sql(
                           "SELECT MAX(a.itemcount)
                           FROM {$CFG->prefix}question_dataset_definitions a,
                                {$CFG->prefix}question_datasets b
                           WHERE b.question = $question->id
                           AND   a.id = b.datasetdefinition")) {
           error("Couldn't get the specified dataset for a calculated " .
                 "question! (question: {$question->id}");
       }

should be changed to

       if(!$maxnumber = (int)get_field_sql(
                           "SELECT MIN(a.itemcount)
                           FROM {$CFG->prefix}question_dataset_definitions a,
                                {$CFG->prefix}question_datasets b
                           WHERE b.question = $question->id
                           AND   a.id = b.datasetdefinition")) {
           error("Couldn't get the specified dataset for a calculated " .
                 "question! (question: {$question->id}");
       }

But there is a difficulty if there is one question_dataset_definitions->itemcount == 0 there is an error and the question is not working properly. This could happen if question has been moved or the 3 steps of creation or edition process not completed. So I propose that we first validate the datasets BEFORE asking for $maxnumber

Validation of the datasets where?

If there is a problem when creating a calculated question for a quiz or a preview, this is because the test had not been done before i.e. when quiz or preview call calculated->get_question_options(); If calculated question validate its question options before there will be no problems. But what options should be extracted from calculated->get_question_options();

  • the answers (already done)
  • the units (already done)
  • the datasetdefs not done but can simplify the validation, there are some other functions to do this
  • the dataitems this could be large but useful, there are some other functions to do this

We have to choose. I will come back later A new function should be created to validate the datasets.

function datasets_validation()

Validate the datasets: How to

  1. Get all the datasets {a..} present in the question text (possible) and in the answers (mandatory)
    1. define a new function find_all_question_dataset_names($question, $mandatorydatasets, $possibledatasets) ;
  2. Get all the datasets {a..} present in the mdl_question_datasetdef using
  3. Examine and validate all those question_dataset_names
  4. if the name IS NOT ALREADY in the list, generate a local dataset (category = 0) and a link to it.
  5. if the name IS ALREADY in the list use the following rules
    1. if it is a local dataset (category = 0) use it without modifications
    2. if it is a category dataset (category > 0)
      1. if the $dataset->category == $question->category use it without modifications
      2. if the $dataset->category != $question->category
        1. if there is no category dataset with the same name in the $question->category
          1. test if other questions are using this category dataset in the initial $dataset->category
            1. if NO question using it just change the category
            2. if YES other questions using it generate a new category dataset in the$question->category , a link to it and add a copy of the data items already defined in $dataset->category
        2. if there is a category dataset with the same name
          1. and the same options use it make a link to it and add a copy of the data items already defined in $dataset->category that outnumber those already in $question->category (>itemnumber)
          2. and not the same option, copy as local dataset (category = 0) and a link to it add a copy of the data items already defined in $dataset->category
            1. yes use it
              1. add items if less than the already existing category
            2. no test if copy to the new category with the already
        1. and not the same options create a local one

(to be continued)

Are all the necessary datasets defined

First checking if all the necessary datasets are defined using the code in datasetdefinitions.php.

// Determine possible and mandatory datasets...
$possibledatasets = $this->find_dataset_names($form->questiontext);
$mandatorydatasets = array();
foreach ($form->answers as $answer) {
    $mandatorydatasets += $this
            ->find_dataset_names($answer);
}

but this is a good occasion to create a new function

$this->find_all_dataset_names