MDL-71205 Default options for qtype_numerical
authorMahmoud Kassaei <mk4359@open.ac.uk>
Wed, 7 Apr 2021 11:39:44 +0000 (12:39 +0100)
committerMahmoud Kassaei <mk4359@open.ac.uk>
Fri, 9 Apr 2021 17:39:37 +0000 (18:39 +0100)
question/type/numerical/edit_numerical_form.php
question/type/numerical/questiontype.php
question/type/numerical/tests/behat/add.feature

index c54e06d..fa93e6e 100644 (file)
@@ -88,11 +88,12 @@ class qtype_numerical_edit_form extends question_edit_form {
         );
         $mform->addElement('select', 'unitrole',
                 get_string('unithandling', 'qtype_numerical'), $unitoptions);
+        $mform->setDefault('unitrole', $this->get_default_value('unitrole', qtype_numerical::UNITNONE));
 
         $penaltygrp = array();
         $penaltygrp[] = $mform->createElement('float', 'unitpenalty',
                 get_string('unitpenalty', 'qtype_numerical'), array('size' => 6));
-        $mform->setDefault('unitpenalty', 0.1000000);
+        $mform->setDefault('unitpenalty', $this->get_default_value('unitpenalty', 0.1000000));
 
         $unitgradingtypes = array(
             qtype_numerical::UNITGRADEDOUTOFMARK =>
@@ -101,7 +102,8 @@ class qtype_numerical_edit_form extends question_edit_form {
                     get_string('decfractionofquestiongrade', 'qtype_numerical'),
         );
         $penaltygrp[] = $mform->createElement('select', 'unitgradingtypes', '', $unitgradingtypes);
-        $mform->setDefault('unitgradingtypes', 1);
+        $mform->setDefault('unitgradingtypes',
+                $this->get_default_value('unitgradingtypes', qtype_numerical::UNITGRADEDOUTOFMARK));
 
         $mform->addGroup($penaltygrp, 'penaltygrp',
                 get_string('unitpenalty', 'qtype_numerical'), ' ', false);
@@ -114,6 +116,8 @@ class qtype_numerical_edit_form extends question_edit_form {
         );
         $mform->addElement('select', 'multichoicedisplay',
                 get_string('studentunitanswer', 'qtype_numerical'), $unitinputoptions);
+        $mform->setDefault('multichoicedisplay',
+                $this->get_default_value('multichoicedisplay', qtype_numerical::UNITINPUT));
 
         $unitsleftoptions = array(
             0 => get_string('rightexample', 'qtype_numerical'),
@@ -121,7 +125,7 @@ class qtype_numerical_edit_form extends question_edit_form {
         );
         $mform->addElement('select', 'unitsleft',
                 get_string('unitposition', 'qtype_numerical'), $unitsleftoptions);
-        $mform->setDefault('unitsleft', 0);
+        $mform->setDefault('unitsleft', $this->get_default_value('unitsleft', 0));
 
         $mform->disabledIf('penaltygrp', 'unitrole', 'eq', qtype_numerical::UNITNONE);
         $mform->disabledIf('penaltygrp', 'unitrole', 'eq', qtype_numerical::UNITOPTIONAL);
index 25754a9..ccf62b1 100644 (file)
@@ -157,6 +157,15 @@ class qtype_numerical extends question_type {
         return true;
     }
 
+    public function save_defaults_for_new_questions(stdClass $fromform): void {
+        parent::save_defaults_for_new_questions($fromform);
+        $this->set_default_value('unitrole', $fromform->unitrole);
+        $this->set_default_value('unitpenalty', $fromform->unitpenalty);
+        $this->set_default_value('unitgradingtypes', $fromform->unitgradingtypes);
+        $this->set_default_value('multichoicedisplay', $fromform->multichoicedisplay);
+        $this->set_default_value('unitsleft', $fromform->unitsleft);
+    }
+
     /**
      * Save the units and the answers associated with this question.
      */
index 8133a89..83da6e3 100644 (file)
@@ -37,3 +37,37 @@ Feature: Test creating a Numerical question
       | id_tolerance_2                     | 0                                      |
       | id_fraction_2                      | 100%                                   |
     Then I should see "Numerical-001"
+
+  @javascript
+  Scenario: Create a Numerical question with units
+    Given I add a "Numerical" question filling the form with:
+      | Question name                      | Numerical-002                               |
+      | Question text                      | How many meter is 1m + 20cm + 50mm?         |
+      | Default mark                       | 1                                           |
+      | General feedback                   | The correct answer is 1.25m                 |
+      | id_answer_0                        | 1.25                                        |
+      | id_tolerance_0                     | 0                                           |
+      | id_fraction_0                      | 100%                                        |
+      | id_answer_1                        | 125                                         |
+      | id_tolerance_1                     | 0                                           |
+      | id_fraction_1                      | 0%                                          |
+      | id_answer_2                        | 1250                                        |
+      | id_tolerance_2                     | 0                                           |
+      | id_fraction_2                      | 0%                                          |
+      | id_unitrole                        | The unit must be given, and will be graded. |
+      | id_unitpenalty                     | 0.15                                        |
+      | id_unitgradingtypes                | as a fraction (0-1) of the question grade   |
+      | id_unitsleft                       | on the right, for example 1.00cm or 1.00km  |
+      | id_multichoicedisplay              | a drop-down menu                            |
+      | id_unit_0                          | m                                           |
+    And I should see "Numerical-002"
+    # Checking that the next new question form displays user preferences settings.
+    When I press "Create a new question ..."
+    And I set the field "item_qtype_numerical" to "1"
+    And I click on "Add" "button" in the "Choose a question type to add" "dialogue"
+    Then the following fields match these values:
+      | id_unitrole                        | The unit must be given, and will be graded. |
+      | id_unitpenalty                     | 0#15                                        |
+      | id_unitgradingtypes                | as a fraction (0-1) of the question grade   |
+      | id_multichoicedisplay              | a drop-down menu                            |
+      | id_unitsleft                       | on the right, for example 1.00cm or 1.00km  |