Data Item Functions

Jump to: navigation, search

This page discuss the actual (sept 2006, moodle 1.7) functions used to create, modify or delete data items.
Calculated parameters i.e {x1} are generated using the dataset parameters ( min, max, distribution) or edited directly by the teacher in the 3rd step of calculated questions creation or modification process. The code for doing this,is mostly in question/type/datasetdependent/datasetitems.php and in question/type/questiontype/generate_dataset_item($options) question/type/questiontype/comment_on_datasetitems($question, $data, $number) and backup and restore functions

In question/type/datasetdependent/datasetitems.php

// Handle adding of dataset items A data item have been either generated or reused and is presented in the form so it can be edited. When the add button is clicked

   // This twisted condition should effectively stop resubmits caused by reloads
   if (isset($form->addbutton) && $maxnumber + 1 == $form->numbertoadd) {
       $addeditem->number = $form->numbertoadd;
       foreach ($form->definition as $key => $defid) {
           $addeditem->definition = $datasetdefs[$defid]->id;
           $addeditem->value = $form->value[$key];
           if ($form->itemid[$key]) {
               // Reuse an previously used record
               $addeditem->id = $form->itemid[$key];
               if (!update_record('question_dataset_items', $addeditem)) {
                   error("Error: Unable to update dataset item");
               }
           } else {
               unset($addeditem->id);
               if (!insert_record('question_dataset_items', $addeditem)) {
                   error("Error: Unable to insert dataset item");
               }
           }
           foreach ($datasetdefs as $key => $newdef) {
               if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) {
                   $newdef->itemcount = $maxnumber+1;
                   // Save the new value for options
                   update_record('question_dataset_definitions', $newdef);
               }
           }
       }
       // else Success:
       $maxnumber = $addeditem->number;

// removing of dataset items

When the remove button is clicked

   } else if (isset($form->deletebutton)
              && $maxnumber == $form->numbertodelete)  {
       // Simply decrease itemcount where == $maxnumber
       foreach ($datasetdefs as $datasetdef) {
           if ($datasetdef->itemcount == $maxnumber) {
               $datasetdef->itemcount--;
               if (!update_record('question_dataset_definitions',
                                  $datasetdef)) {
                    error("Error: Unable to update itemcount");
               }
           }
       }
       --$maxnumber;
   }
   make_upload_directory("$course->id");  // Just in case
   $grosscoursefiles = get_directory_list("$CFG->dataroot/$course->id",
                                      "$CFG->moddata");

The next form is prepared with a new or the last removed item is presented

// Generate a new dataset item (or reuse an old one)

   foreach ($datasetdefs as $defid => $datasetdef) {
       if (isset($datasetdef->id)) {
           $datasetdefs[$defid]->items = get_records_sql( // Use number as key!!
                   " SELECT number, definition, id, value
                     FROM {$CFG->prefix}question_dataset_items
                     WHERE definition = $datasetdef->id ");
       }
       if (isset($datasetdefs[$defid]->items[$maxnumber + 1]) && (
           empty($form->forceregeneration) &&
           isset($form->regenerateddefid) &&
           $form->regenerateddefid != $defid ||
           isset($form->deletebutton))) {
           // Reuse existing datasets
       } else {
           $datasetitem = new stdClass;
           $datasetitem->id =
            isset($datasetdefs[$defid]->items[$maxnumber + 1]->id)
            ? $datasetdefs[$defid]->items[$maxnumber + 1]->id : ;
           $datasetitem->number = $maxnumber + 1;
           if ($this->supports_dataset_item_generation()) {
               if (!empty($form->addbutton) || // If we added an item
                   !isset($form->value) ||     // If we don't have a value
                   $form->regenerateddefid == $defid) { // If we explicitly ask for regeneration
                       $datasetitem->value =
                        $this->generate_dataset_item($datasetdef->options);
               } else {
                   $definition = array_flip($form->definition);
                   $datasetitem->value = $form->value[$definition[$defid]];
               }
           } else {
               $datasetitem->value = ;
           }
           $datasetdefs[$defid]->items[$maxnumber + 1] = clone($datasetitem);
       }
   }

When you remove a data item, you don't delete it , you just decrease a counter in the dataset.

By doing it this way, this removed data item can be reused if you want it.

New functions

We need to create new functions that could be call by question/type/datasetdependent/datasetitems.php and question/format.php different files.

function add_data_items($definition_id,$value,$data_item_id)
           $addeditem->definition = $definition_id;
           $addeditem->value = $value;
           if ($data_item_id) {
               // Reuse an previously used record
               $addeditem->id = $data_item_id;
 // perhaps adding a check that the record exist already just in case...
               if (!update_record('question_dataset_items', $addeditem)) {
                   error("Error: Unable to update dataset item");
               }
           } else {
               unset($addeditem->id);
               if (!insert_record('question_dataset_items', $addeditem)) {
                   error("Error: Unable to insert dataset item");
               }
           }


function remove_data_items()

function get_data_items()

function save_data_items()

(to be continued 1 sept 06)