Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Calculated question validating the datasets: Difference between revisions

From MoodleDocs
Line 39: Line 39:
A new function should be created to validate the datasets.
A new function should be created to validate the datasets.
  function datasets_validation()
  function datasets_validation()
==Validate the datasets: How to==
#Get all the datasets {a..} present in the question text (possible) and in the answers (mandatory)
##define a new function  find_all_question_dataset_names($question, $mandatorydatasets, $possibledatasets) ;
#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
##if there is no category dataset with the same name generate a local datasetdef and a link to it
##if there is a category dataset with the same name
###and the same options
####check if the category is the same as the question category (has been moved)
##### 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)
===Are all the necessary datasets defined===
===Are all the necessary datasets defined===
First checking if all the necessary datasets are defined using the code in datasetdefinitions.php.
First checking if all the necessary datasets are defined using the code in datasetdefinitions.php.
Line 49: Line 64:
  }
  }
but this is a good occasion to create a new function
but this is a good occasion to create a new function
===$this->find_all_dataset_names===
===$this->find_all_dataset_names===

Revision as of 17:45, 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 that are not already in the database using the following rules
    1. if there is no category dataset with the same name generate a local datasetdef and a link to it
    2. if there is a category dataset with the same name
      1. and the same options
        1. check if the category is the same as the question category (has been moved)
          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
      2. 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