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
No edit summary
 
Line 28: Line 28:
So I propose that we first validate the datasets BEFORE asking for $maxnumber
So I propose that we first validate the datasets BEFORE asking for $maxnumber
==Validation of the datasets==
==Validation of the datasets==
A new function should be created to validate the datasets.
function datasets_validation()
===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===

Revision as of 12:53, 13 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

A new function should be created to validate the datasets.

function datasets_validation()

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