From a8d2a373a184ee2c05efed0ca4738e2b5676f9a4 Mon Sep 17 00:00:00 2001
From: jamiesensei <jamiesensei>
Date: Tue, 30 Jan 2007 15:31:50 +0000
Subject: [PATCH] some fixes for problem reported by Pierre Pichet.

Data set items form is now the opposite way round as the original was. This means that the add item section appears at the top of the form. Also if you press return while editing fields in the add item section the item will now be added. Default action for most browsers when a submit button is pressed is to act as if the first submit button in the form had been pressed.

Removed cancel buttons second and third pages of form too as they may not work as expected.
---
 question/type/calculated/questiontype.php     |  9 +-
 .../datasetdefinitions_form.php               |  2 +-
 .../datasetdependent/datasetitems_form.php    | 83 +++++++++----------
 3 files changed, 44 insertions(+), 50 deletions(-)

diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php
index e2e7eb8efb..a390e25d95 100644
--- a/question/type/calculated/questiontype.php
+++ b/question/type/calculated/questiontype.php
@@ -331,6 +331,8 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
         $minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_datasetdependent'), 'size="3"');
         $minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_datasetdependent'), 'size="3"');
         $mform->addGroup($minmaxgrp, 'minmaxgrp', get_string('minmax', 'qtype_datasetdependent'), ' - ', false);
+        $mform->setType('calcmin', PARAM_NUMBER);
+        $mform->setType('calcmax', PARAM_NUMBER);
 
         $precisionoptions = range(0, 10);
         $mform->addElement('select', "calclength[$idx]", get_string('calclength', 'qtype_datasetdependent'), $precisionoptions);
@@ -339,10 +341,6 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
         $mform->addElement('select', "calcdistribution[$idx]", get_string('calcdistribution', 'qtype_datasetdependent'), $distriboptions);
 
 
-        $mform->addElement('hidden', "definition[$j]");
-        $mform->addElement('hidden', "itemid[$j]");
-
-
     }
 
     function custom_generator_set_data($datasetdefs, $formdata){
@@ -394,7 +392,7 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
         if (empty($form->definition) || empty($form->calcmin)
                 || empty($form->calcmax) || empty($form->calclength)
                 || empty($form->calcdistribution)) {
-            // I gues not:
+            // I guess not
 
         } else {
             // Looks like we just could have some new information here
@@ -452,6 +450,7 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
         }
         // Handle adding and removing of dataset items
         $i = 1;
+        ksort($fromform->definition);
         foreach ($fromform->definition as $key => $defid) {
             //if the delete button has not been pressed then skip the datasetitems
             //in the 'add item' part of the form.
diff --git a/question/type/datasetdependent/datasetdefinitions_form.php b/question/type/datasetdependent/datasetdefinitions_form.php
index 2c45304cdf..b383d1c35f 100644
--- a/question/type/datasetdependent/datasetdefinitions_form.php
+++ b/question/type/datasetdependent/datasetdefinitions_form.php
@@ -66,7 +66,7 @@ class question_dataset_dependent_definitions_form extends moodleform {
                 $key++;
             }
         }
-        $this->add_action_buttons(true, get_string('nextpage', 'qtype_calculated'));
+        $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
 
 
         //hidden elements
diff --git a/question/type/datasetdependent/datasetitems_form.php b/question/type/datasetdependent/datasetitems_form.php
index b25bad7546..5da7c2b7a0 100644
--- a/question/type/datasetdependent/datasetitems_form.php
+++ b/question/type/datasetdependent/datasetitems_form.php
@@ -62,74 +62,69 @@ class question_dataset_dependent_items_form extends moodleform {
     function definition() {
         $mform =& $this->_form;
         $strquestionlabel = $this->qtypeobj->comment_header($this->question);
-
-//------------------------------------------------------------------------------------------------------------------------------
         if ($this->maxnumber != -1){
             $this->noofitems = $this->maxnumber;
         } else {
             $this->noofitems = 0;
         }
-        $j = 1;
-        for ($i=1; $i <= $this->noofitems; $i++){
-            $mform->addElement('header', 'itemhdr', get_string('itemno', 'qtype_datasetdependent', $i));
-            foreach ($this->datasetdefs as $defkey => $datasetdef){
-                $mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
-                $mform->setType("number[$j]", PARAM_NUMBER);
-
-                $mform->addElement('hidden', "itemid[$j]");
-
-                $mform->addElement('hidden', "definition[$j]");
-
-                $j++;
-            }
-            if ('' != $strquestionlabel){
-                $repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
-            }
-            if ($i == $this->noofitems) {//last item
-                $mform->addElement('submit', 'deletebutton', get_string('deletelastitem', 'qtype_datasetdependent'));
-
-            }
-        }
-        $mform->setType("itemid", PARAM_INT);
-        $mform->setType("definition", PARAM_NOTAGS);
-
-        $mform->addElement('submit', 'addbutton', get_string('additem', 'qtype_datasetdependent'));
-        $mform->closeHeaderBefore('addbutton');
-
-
 //------------------------------------------------------------------------------------------------------------------------------
         $mform->addElement('header', 'additemhdr', get_string('itemtoadd', 'qtype_datasetdependent'));
         $idx = 1;
+        $j = (($this->noofitems) * count($this->datasetdefs))+1;
         foreach ($this->datasetdefs as $defkey => $datasetdef){
             $mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
             $this->qtypeobj->custom_generator_tools_part(&$mform, $idx, $j);
             $idx++;
-            $j++;
+            $mform->addElement('hidden', "definition[$j]");
+            $mform->addElement('hidden', "itemid[$j]");
             $mform->addElement('static', "divider[$j]", '', '<hr />');
-
+            $j++;
         }
 
         if ('' != $strquestionlabel){
             $mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel);
         }
-
-        $mform->closeHeaderBefore('forceregenerationgrp');
+        $mform->closeHeaderBefore('addbutton');
+        $mform->addElement('submit', 'addbutton', get_string('additem', 'qtype_datasetdependent'));
 
         if ($this->qtypeobj->supports_dataset_item_generation()){
             $radiogrp = array();
             $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_datasetdependent'), 0);
             $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration', 'qtype_datasetdependent'), 1);
             $mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), null, false);
-            $mform->setDefault('nextpageparam[forceregeneration]', 0);
         }
 
         $mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_datasetdependent'));
 
+//------------------------------------------------------------------------------------------------------------------------------
+        $j = $this->noofitems * count($this->datasetdefs);
+        for ($i = $this->noofitems; $i >= 1 ; $i--){
+            $mform->addElement('header', '', get_string('itemno', 'qtype_datasetdependent', $i));
+            foreach ($this->datasetdefs as $defkey => $datasetdef){
+                $mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
+
+                $mform->addElement('hidden', "itemid[$j]");
+
+                $mform->addElement('hidden', "definition[$j]");
+
+                $j--;
+            }
+            if ('' != $strquestionlabel){
+                $repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
+            }
+            if ($i == $this->noofitems) {//last item
+                $mform->addElement('submit', 'deletebutton', get_string('deletelastitem', 'qtype_datasetdependent'));
+            }
+        }
+        $mform->setType('number', PARAM_NUMBER);
+        $mform->setType('itemid', PARAM_INT);
+        $mform->setType('definition', PARAM_NOTAGS);
+
+
+//------------------------------------------------------------------------------------------------------------------------------
         //non standard name for button element needed so not using add_action_buttons
-        $buttonarray=array();
-        $buttonarray[] = &$mform->createElement('submit', 'backtoquiz', get_string('savechanges'));
-        $buttonarray[] = &$mform->createElement('cancel');
-        $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
+        $mform->addElement('submit', 'backtoquiz', get_string('savechanges'));
+        $mform->closeHeaderBefore('backtoquiz');
 
         //hidden elements
         $mform->addElement('hidden', 'returnurl');
@@ -148,8 +143,8 @@ class question_dataset_dependent_items_form extends moodleform {
         $formdata = array();
 
         //fill out all data sets and also the fields for the next item to add.
-        $j = 1;
-        for ($itemnumber = 1; $itemnumber <= $this->noofitems; $itemnumber++){
+        $j = $this->noofitems * count($this->datasetdefs);
+        for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){
             $data = array();
             foreach ($this->datasetdefs as $defid => $datasetdef){
                 if (isset($datasetdef->items[$itemnumber])){
@@ -158,14 +153,14 @@ class question_dataset_dependent_items_form extends moodleform {
                     $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id;
                     $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value;
                 }
-                $j++;
+                $j--;
             }
             $formdata['answercomment['.$itemnumber.']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, $itemnumber);
         }
 
         $formdata['nextpageparam[forceregeneration]'] = $this->regenerate;
 
-        $savej = $j;
+        $j = $this->noofitems * count($this->datasetdefs)+1;
         $data = array(); // data for comment_on_datasetitems later
         //dataset generation dafaults
         if ($this->qtypeobj->supports_dataset_item_generation()) {
@@ -181,7 +176,7 @@ class question_dataset_dependent_items_form extends moodleform {
         }
 
         //existing records override generated data depending on radio element
-        $j = $savej;
+        $j = $this->noofitems * count($this->datasetdefs)+1;
         if (!$this->regenerate){
             $idx = 1;
             $itemnumber = $this->noofitems+1;
-- 
2.39.5