MDL-27410 Big cleanup of the coding style in qtype_calculated.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 16 May 2011 17:48:50 +0000 (18:48 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 16 May 2011 17:48:50 +0000 (18:48 +0100)
12 files changed:
question/type/calculated/backup/moodle2/backup_qtype_calculated_plugin.class.php
question/type/calculated/backup/moodle2/restore_qtype_calculated_plugin.class.php
question/type/calculated/datasetdefinitions_form.php
question/type/calculated/datasetitems_form.php
question/type/calculated/db/upgrade.php
question/type/calculated/edit_calculated_form.php
question/type/calculated/lang/en/qtype_calculated.php
question/type/calculated/questiontype.php
question/type/calculatedmulti/questiontype.php
question/type/calculatedsimple/questiontype.php
question/type/multianswer/questiontype.php
question/type/numerical/questiontype.php

index 85870df..3a302bc 100644 (file)
@@ -71,8 +71,8 @@ class backup_qtype_calculated_plugin extends backup_qtype_plugin {
         $calculatedoptions = new backup_nested_element('calculated_options');
         $calculatedoption = new backup_nested_element('calculated_option', array('id'), array(
             'synchronize', 'single', 'shuffleanswers', 'correctfeedback',
-            'correctfeedbackformat', 'partiallycorrectfeedback', 'partiallycorrectfeedbackformat', 'incorrectfeedback',
-            'incorrectfeedbackformat', 'answernumbering'));
+            'correctfeedbackformat', 'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
+            'incorrectfeedback', 'incorrectfeedbackformat', 'answernumbering'));
 
         // Now the own qtype tree
         $pluginwrapper->add_child($calculatedrecords);
@@ -82,8 +82,10 @@ class backup_qtype_calculated_plugin extends backup_qtype_plugin {
         $calculatedoptions->add_child($calculatedoption);
 
         // set source to populate the data
-        $calculatedrecord->set_source_table('question_calculated', array('question' => backup::VAR_PARENTID));
-        $calculatedoption->set_source_table('question_calculated_options', array('question' => backup::VAR_PARENTID));
+        $calculatedrecord->set_source_table('question_calculated',
+                array('question' => backup::VAR_PARENTID));
+        $calculatedoption->set_source_table('question_calculated_options',
+                array('question' => backup::VAR_PARENTID));
 
         // don't need to annotate ids nor files
 
index 5469b39..c645a11 100644 (file)
@@ -53,11 +53,11 @@ class restore_qtype_calculated_plugin extends restore_qtype_plugin {
 
         // Add own qtype stuff
         $elename = 'calculated_record';
-        $elepath = $this->get_pathfor('/calculated_records/calculated_record'); // we used get_recommended_name() so this works
+        $elepath = $this->get_pathfor('/calculated_records/calculated_record');
         $paths[] = new restore_path_element($elename, $elepath);
 
         $elename = 'calculated_option';
-        $elepath = $this->get_pathfor('/calculated_options/calculated_option'); // we used get_recommended_name() so this works
+        $elepath = $this->get_pathfor('/calculated_options/calculated_option');
         $paths[] = new restore_path_element($elename, $elepath);
 
         return $paths; // And we return the interesting paths
@@ -75,19 +75,17 @@ class restore_qtype_calculated_plugin extends restore_qtype_plugin {
         // Detect if the question is created or mapped
         $oldquestionid   = $this->get_old_parentid('question');
         $newquestionid   = $this->get_new_parentid('question');
-        $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
+        $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
+                true : false;
 
-        // If the question has been created by restore, we need to create its question_calculated too
+        // If the question has been created by restore, we need to create its
+        // question_calculated too
         if ($questioncreated) {
             // Adjust some columns
             $data->question = $newquestionid;
             $data->answer = $this->get_mappingid('question_answer', $data->answer);
             // Insert record
             $newitemid = $DB->insert_record('question_calculated', $data);
-            // Create mapping (not needed, no files nor childs nor states here)
-            //$this->set_mapping('question_calculated', $oldid, $newitemid);
-        } else {
-            // Nothing to remap if the question already existed
         }
     }
 
@@ -103,18 +101,16 @@ class restore_qtype_calculated_plugin extends restore_qtype_plugin {
         // Detect if the question is created or mapped
         $oldquestionid   = $this->get_old_parentid('question');
         $newquestionid   = $this->get_new_parentid('question');
-        $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
+        $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
+                true : false;
 
-        // If the question has been created by restore, we need to create its question_calculated too
+        // If the question has been created by restore, we need to create its
+        // question_calculated too
         if ($questioncreated) {
             // Adjust some columns
             $data->question = $newquestionid;
             // Insert record
             $newitemid = $DB->insert_record('question_calculated_options', $data);
-            // Create mapping (not needed, no files nor childs nor states here)
-            // $this->set_mapping('question_calculated_option', $oldid, $newitemid);
-        } else {
-            // Nothing to remap if the question already existed
         }
     }
 }
index e567c25..f64cf59 100644 (file)
@@ -41,42 +41,43 @@ class question_dataset_dependent_definitions_form extends moodleform {
      *
      * @var object
      */
-    var $question;
+    protected $question;
     /**
      * Reference to question type object
      *
      * @var question_dataset_dependent_questiontype
      */
-    var $qtypeobj;
+    protected $qtypeobj;
     /**
      * Add question-type specific form fields.
      *
      * @param MoodleQuickForm $mform the form being built.
      */
-    function question_dataset_dependent_definitions_form($submiturl, $question){
-        global $QTYPES, $DB;
+    public function __construct($submiturl, $question) {
+        global $DB;
         $this->question = $question;
-        $this->qtypeobj =& $QTYPES[$this->question->qtype];
+        $this->qtypeobj = $QTYPES[$this->question->qtype];
         // Validate the question category.
-        if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
+        if (!$category = $DB->get_record('question_categories',
+                array('id' => $question->category))) {
             print_error('categorydoesnotexist', 'question', $returnurl);
         }
         $this->category = $category;
         $this->categorycontext = get_context_instance_by_id($category->contextid);
-        parent::moodleform($submiturl);
+        parent::__construct($submiturl);
     }
 
     protected function definition() {
         global $SESSION;
-        $mform =& $this->_form;
+        $mform = $this->_form;
         $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
         $mandatorydatasets = array();
-        if (isset($this->question->options->answers)){
+        if (isset($this->question->options->answers)) {
             foreach ($this->question->options->answers as $answer) {
                 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
             }
-        }else{
-            foreach ($SESSION->calculated->questionform->answers as $answer){
+        } else {
+            foreach ($SESSION->calculated->questionform->answers as $answer) {
                 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
             }
         }
@@ -86,8 +87,10 @@ class question_dataset_dependent_definitions_form extends moodleform {
         $datadefscat  = $this->qtypeobj->get_dataset_definitions_category($this->question);
         $datasetmenus = array();
         $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
-        $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
-        $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', 'qtype_calculated'));
+        // explaining the role of datasets so other strings can be shortened
+        $mform->addElement('html', $label);
+        $mform->addElement('header', 'mandatoryhdr',
+                get_string('mandatoryhdr', 'qtype_calculated'));
         $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
 
         foreach ($mandatorydatasets as $datasetname) {
@@ -95,52 +98,60 @@ class question_dataset_dependent_definitions_form extends moodleform {
                 list($options, $selected) =
                         $this->qtypeobj->dataset_options($this->question, $datasetname);
                 unset($options['0']); // Mandatory...
-                $label = get_string("wildcard", "qtype_calculated"). " <strong>$datasetname</strong> ";
+                $label = get_string('wildcard', 'qtype_calculated').
+                        " <strong>$datasetname</strong> ";
                 $mform->addElement('select', "dataset[$key]", $label, $options);
-             if (isset($datadefscat[$datasetname])){
-                  $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined','qtype_calculated', $datadefscat[$datasetname]));
-            }
+                if (isset($datadefscat[$datasetname])) {
+                    $mform->addElement('static', "there is a category",
+                            get_string('sharedwildcard', 'qtype_calculated', $datasetname),
+                            get_string('dataitemdefined', 'qtype_calculated',
+                            $datadefscat[$datasetname]));
+                }
                 $mform->setDefault("dataset[$key]", $selected);
-                $datasetmenus[$datasetname]='';
+                $datasetmenus[$datasetname] = '';
                 $key++;
             }
         }
-                        $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
-
+        $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
 
         foreach ($possibledatasets as $datasetname) {
             if (!isset($datasetmenus[$datasetname])) {
-                list($options, $selected) =
-                        $this->qtypeobj->dataset_options($this->question, $datasetname,false);
-                $label = get_string("wildcard", "qtype_calculated"). " <strong>$datasetname</strong> ";
+                list($options, $selected) = $this->qtypeobj->dataset_options(
+                        $this->question, $datasetname, false);
+                $label = get_string('wildcard', 'qtype_calculated') .
+                        " <strong>$datasetname</strong> ";
                 $mform->addElement('select', "dataset[$key]", $label, $options);
-                 //       $mform->addRule("dataset[$key]", null, 'required', null, 'client');
-             if (isset($datadefscat[$datasetname])){
-                  $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined',"qtype_calculated", $datadefscat[$datasetname]));
-            }
+                if (isset($datadefscat[$datasetname])) {
+                    $mform->addElement('static', "there is a category",
+                            get_string('sharedwildcard', 'qtype_calculated', $datasetname),
+                            get_string('dataitemdefined', 'qtype_calculated',
+                                    $datadefscat[$datasetname]));
+                }
 
-              //   $selected ="0";
                 $mform->setDefault("dataset[$key]", $selected);
-                $datasetmenus[$datasetname]='';
+                $datasetmenus[$datasetname] = '';
                 $key++;
             }
         }
         // temporary strings
-        // temporary strings
-        $mform->addElement('header', 'synchronizehdr', get_string('synchronize', 'qtype_calculated'));
-        $mform->addElement('radio', 'synchronize', '', get_string('synchronizeno', 'qtype_calculated'),0);
-        $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyes', 'qtype_calculated'),1);
-        $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyesdisplay', 'qtype_calculated'),2);
-        if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
-            $mform->setDefault("synchronize", $this->question->options->synchronize);
+        $mform->addElement('header', 'synchronizehdr',
+                get_string('synchronize', 'qtype_calculated'));
+        $mform->addElement('radio', 'synchronize', '',
+                get_string('synchronizeno', 'qtype_calculated'), 0);
+        $mform->addElement('radio', 'synchronize', '',
+                get_string('synchronizeyes', 'qtype_calculated'), 1);
+        $mform->addElement('radio', 'synchronize', '',
+                get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
+        if (isset($this->question->options) &&
+                isset($this->question->options->synchronize)) {
+            $mform->setDefault('synchronize', $this->question->options->synchronize);
         } else {
-            $mform->setDefault("synchronize", 0 );
+            $mform->setDefault('synchronize', 0);
         }
 
         $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
 
-
-        //hidden elements
+        // Hidden elements
         $mform->addElement('hidden', 'returnurl');
         $mform->setType('returnurl', PARAM_LOCALURL);
         $mform->setDefault('returnurl', 0);
@@ -163,28 +174,22 @@ class question_dataset_dependent_definitions_form extends moodleform {
         $mform->addElement('hidden', 'wizard', 'datasetitems');
         $mform->setType('wizard', PARAM_ALPHA);
     }
-/*    function set_data($question) {
-        $formdata = array();
-        $fromform = new stdClass();
-                  $default_values['synchronize'] = $question->options->synchronize ;
-        $question = (object)((array)$question + $default_values);
-    }*/
-    function validation($data, $files) {
+
+    public function validation($data, $files) {
         $errors = parent::validation($data, $files);
         $datasets = $data['dataset'];
-        $countvalid = 0 ;
-        foreach ($datasets as $key => $dataset){
-            if ($dataset !="0") {
+        $countvalid = 0;
+        foreach ($datasets as $key => $dataset) {
+            if ($dataset != '0') {
                 $countvalid++;
             }
         }
-        if (!$countvalid){
-            foreach ($datasets as $key => $dataset){
-                $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_calculated');
+        if (!$countvalid) {
+            foreach ($datasets as $key => $dataset) {
+                $errors['dataset['.$key.']'] =
+                        get_string('atleastonerealdataset', 'qtype_calculated');
             }
-       }
+        }
         return $errors;
     }
-
 }
-
index 45a8cc0..6dbdd46 100644 (file)
@@ -57,33 +57,37 @@ class question_dataset_dependent_items_form extends moodleform {
 
     public $noofitems;
 
-    public $outsidelimit = false ;
+    public $outsidelimit = false;
 
     public $commentanswers = array();
+
     /**
      * Add question-type specific form fields.
      *
      * @param MoodleQuickForm $mform the form being built.
      */
-    function question_dataset_dependent_items_form($submiturl, $question, $regenerate){
-        global $QTYPES, $SESSION, $CFG, $DB;
+    public function __construct($submiturl, $question, $regenerate) {
+        global $SESSION, $CFG, $DB;
         $this->regenerate = $regenerate;
         $this->question = $question;
         $this->qtypeobj =& $QTYPES[$this->question->qtype];
         // Validate the question category.
-        if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
+        if (!$category = $DB->get_record('question_categories',
+                array('id' => $question->category))) {
             print_error('categorydoesnotexist', 'question', $returnurl);
         }
         $this->category = $category;
         $this->categorycontext = get_context_instance_by_id($category->contextid);
         //get the dataset defintions for this question
         if (empty($question->id)) {
-            $this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, $SESSION->calculated->definitionform->dataset);
+            $this->datasetdefs = $this->qtypeobj->get_dataset_definitions(
+                    $question->id, $SESSION->calculated->definitionform->dataset);
         } else {
             if (empty($question->options)) {
                 $this->get_question_options($question);
             }
-            $this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, array());
+            $this->datasetdefs = $this->qtypeobj->get_dataset_definitions(
+                    $question->id, array());
         }
 
         foreach ($this->datasetdefs as $datasetdef) {
@@ -94,45 +98,45 @@ class question_dataset_dependent_items_form extends moodleform {
         }
         foreach ($this->datasetdefs as $defid => $datasetdef) {
             if (isset($datasetdef->id)) {
-                $this->datasetdefs[$defid]->items = $this->qtypeobj->get_database_dataset_items($datasetdef->id);
+                $this->datasetdefs[$defid]->items =
+                        $this->qtypeobj->get_database_dataset_items($datasetdef->id);
             }
         }
-        parent::moodleform($submiturl);
+        parent::__construct($submiturl);
     }
+
     protected function definition() {
                 $labelsharedwildcard = get_string("sharedwildcard", "qtype_calculated");
 
         $mform =& $this->_form;
         $strquestionlabel = $this->qtypeobj->comment_header($this->question);
-        if ($this->maxnumber != -1){
+        if ($this->maxnumber != -1 ) {
             $this->noofitems = $this->maxnumber;
         } else {
             $this->noofitems = 0;
         }
         $label = get_string("sharedwildcards", "qtype_calculated");
-//
-                $html2 = $this->qtypeobj->print_dataset_definitions_category_shared($this->question,$this->datasetdefs);
-                $mform->addElement('static','listcategory',$label,$html2);
-//------------------------------------------------------------------------------------------------------------------------------
-        $mform->addElement('submit', 'updatedatasets', get_string('updatedatasetparam', 'qtype_calculated'));
+
+        $html2 = $this->qtypeobj->print_dataset_definitions_category_shared(
+                $this->question, $this->datasetdefs);
+        $mform->addElement('static', 'listcategory', $label, $html2);
+        //----------------------------------------------------------------------
+        $mform->addElement('submit', 'updatedatasets',
+                get_string('updatedatasetparam', 'qtype_calculated'));
         $mform->registerNoSubmitButton('updatedatasets');
-        $mform->addElement('header', 'additemhdr', get_string('itemtoadd', 'qtype_calculated'));
+        $mform->addElement('header', 'additemhdr',
+                get_string('itemtoadd', 'qtype_calculated'));
         $idx = 1;
         $data = array();
         $j = (($this->noofitems) * count($this->datasetdefs))+1;
-        foreach ($this->datasetdefs as $defkey => $datasetdef){
-            if($datasetdef->category |= 0 ) {
-                $name = get_string('sharedwildcardname', 'qtype_calculated',$datasetdef->name) ;
-            }else {
+        foreach ($this->datasetdefs as $defkey => $datasetdef) {
+            if ($datasetdef->category |= 0 ) {
+                $name = get_string('sharedwildcardname', 'qtype_calculated', $datasetdef->name);
+            } else {
                 $name = get_string('wildcard', 'qtype_calculated', $datasetdef->name);
             }
             $mform->addElement('text', "number[$j]", $name);
             $mform->setType("number[$j]", PARAM_NUMBER);
-       /*     if($datasetdef->category |= 0 && $datasetdef->category == $this->category){
-                                 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetdef->name </strong>", get_string('dataitemdefined',"qtype_calculated", $datasetdef->itemcount));
-            }else  if($datasetdef->category |= 0 && $datasetdef->category != $this->category){
-                                 $mform->addElement('static', "BAD a category", $labelsharedwildcard." <strong>$datasetdef->name </strong>", get_string('dataitemdefined',"qtype_calculated", $datasetdef->itemcount));
-        }*/
             $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j);
             $idx++;
             $mform->addElement('hidden', "definition[$j]");
@@ -143,124 +147,148 @@ class question_dataset_dependent_items_form extends moodleform {
             $mform->setType("divider[$j]", PARAM_RAW);
             $j++;
         }
-        
-        $mform->addElement('header', 'updateanswershdr', get_string('answerstoleranceparam', 'qtype_calculated'));
-        $mform->addElement('submit', 'updateanswers', get_string('updatetolerancesparam', 'qtype_calculated'));
-        $mform->setAdvanced('updateanswers',true);
+
+        $mform->addElement('header', 'updateanswershdr',
+                get_string('answerstoleranceparam', 'qtype_calculated'));
+        $mform->addElement('submit', 'updateanswers',
+                get_string('updatetolerancesparam', 'qtype_calculated'));
+        $mform->setAdvanced('updateanswers', true);
         $mform->registerNoSubmitButton('updateanswers');
 
         $answers = fullclone($this->question->options->answers);
         $key1 =1;
         foreach ($answers as $key => $answer) {
-            if ('' === $answer->answer){
-            }else if ('*' === $answer->answer){
-                $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
+            if ('' === $answer->answer) {
+                // Do nothing.
+            } else if ('*' === $answer->answer) {
+                $mform->addElement('static',
+                        'answercomment[' . ($this->noofitems+$key1) . ']', $answer->answer);
                 $mform->addElement('hidden', 'tolerance['.$key.']', '');
                 $mform->setType('tolerance['.$key.']', PARAM_RAW);
-                $mform->setAdvanced('tolerance['.$key.']',true);
+                $mform->setAdvanced('tolerance['.$key.']', true);
                 $mform->addElement('hidden', 'tolerancetype['.$key.']', '');
                 $mform->setType('tolerancetype['.$key.']', PARAM_RAW);
-                $mform->setAdvanced('tolerancetype['.$key.']',true);
+                $mform->setAdvanced('tolerancetype['.$key.']', true);
                 $mform->addElement('hidden', 'correctanswerlength['.$key.']', '');
                 $mform->setType('correctanswerlength['.$key.']', PARAM_RAW);
-                $mform->setAdvanced('correctanswerlength['.$key.']',true);
+                $mform->setAdvanced('correctanswerlength['.$key.']', true);
                 $mform->addElement('hidden', 'correctanswerformat['.$key.']', '');
                 $mform->setType('correctanswerformat['.$key.']', PARAM_RAW);
-                $mform->setAdvanced('correctanswerformat['.$key.']',true);
-            }else {
-                $mform->addElement('static', 'answercomment['.($this->noofitems+$key1).']', $answer->answer);
-                $mform->addElement('text', 'tolerance['.$key.']', get_string('tolerance', 'qtype_calculated'));
-                $mform->setAdvanced('tolerance['.$key.']',true);
-                $mform->addElement('select', 'tolerancetype['.$key.']', get_string('tolerancetype', 'qtype_numerical'), $this->qtypeobj->tolerance_types());
-                $mform->setAdvanced('tolerancetype['.$key.']',true);
-
-                $mform->addElement('select', 'correctanswerlength['.$key.']', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
-                $mform->setAdvanced('correctanswerlength['.$key.']',true);
-
-                $answerlengthformats = array('1' => get_string('decimalformat', 'qtype_numerical'), '2' => get_string('significantfiguresformat', 'quiz'));
-                $mform->addElement('select', 'correctanswerformat['.$key.']', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
-                $mform->setAdvanced('correctanswerformat['.$key.']',true);
+                $mform->setAdvanced('correctanswerformat['.$key.']', true);
+            } else {
+                $mform->addElement('static', 'answercomment[' . ($this->noofitems+$key1) . ']',
+                        $answer->answer);
+                $mform->addElement('text', 'tolerance['.$key.']',
+                        get_string('tolerance', 'qtype_calculated'));
+                $mform->setAdvanced('tolerance['.$key.']', true);
+                $mform->addElement('select', 'tolerancetype['.$key.']',
+                        get_string('tolerancetype', 'qtype_numerical'),
+                        $this->qtypeobj->tolerance_types());
+                $mform->setAdvanced('tolerancetype['.$key.']', true);
+
+                $mform->addElement('select', 'correctanswerlength['.$key.']',
+                        get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+                $mform->setAdvanced('correctanswerlength['.$key.']', true);
+
+                $answerlengthformats = array(
+                    '1' => get_string('decimalformat', 'qtype_numerical'),
+                    '2' => get_string('significantfiguresformat', 'quiz')
+                );
+                $mform->addElement('select', 'correctanswerformat['.$key.']',
+                        get_string('correctanswershowsformat', 'qtype_calculated'),
+                        $answerlengthformats);
+                $mform->setAdvanced('correctanswerformat['.$key.']', true);
                 $mform->addElement('static', 'dividertolerance', '', '<hr />');
-                $mform->setAdvanced('dividertolerance',true);
+                $mform->setAdvanced('dividertolerance', true);
             }
             $key1++;
         }
 
         $addremoveoptions = array();
         $addremoveoptions['1']='1';
-        for ($i=10; $i<=100 ; $i+=10){
+        for ($i=10; $i<=100; $i+=10) {
              $addremoveoptions["$i"]="$i";
         }
         $showoptions = Array();
         $showoptions['1']='1';
         $showoptions['2']='2';
         $showoptions['5']='5';
-        for ($i=10; $i<=100 ; $i+=10){
+        for ($i=10; $i<=100; $i+=10) {
              $showoptions["$i"]="$i";
         }
         $mform->addElement('header', 'additemhdr', get_string('add', 'moodle'));
         $mform->closeHeaderBefore('additemhdr');
 
-        if ($this->qtypeobj->supports_dataset_item_generation()){
+        if ($this->qtypeobj->supports_dataset_item_generation()) {
             $radiogrp = array();
-            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_calculated'), 0);
-            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregenerationshared', 'qtype_calculated'), 1);
-            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregenerationall', 'qtype_calculated'), 2);
-            $mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), "<br/>", false);
+            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]',
+                    null, get_string('reuseifpossible', 'qtype_calculated'), 0);
+            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]',
+                    null, get_string('forceregenerationshared', 'qtype_calculated'), 1);
+            $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]',
+                    null, get_string('forceregenerationall', 'qtype_calculated'), 2);
+            $mform->addGroup($radiogrp, 'forceregenerationgrp',
+                    get_string('nextitemtoadd', 'qtype_calculated'), "<br/>", false);
         }
 
         $mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_calculated'));
         $mform->addElement('static', "dividera", '', '<hr />');
         $addgrp = array();
         $addgrp[] =& $mform->createElement('submit', 'addbutton', get_string('add', 'moodle'));
-        $addgrp[] =& $mform->createElement('select', "selectadd", get_string('additem', 'qtype_calculated'), $addremoveoptions);
-        $addgrp[] = & $mform->createElement('static',"stat","Items",get_string('newsetwildcardvalues', 'qtype_calculatedsimple'));
-        $mform->addGroup($addgrp, 'addgrp', get_string('additem', 'qtype_calculated'), '   ', false);
+        $addgrp[] =& $mform->createElement('select', "selectadd",
+                get_string('additem', 'qtype_calculated'), $addremoveoptions);
+        $addgrp[] = & $mform->createElement('static', "stat", "Items",
+                get_string('newsetwildcardvalues', 'qtype_calculatedsimple'));
+        $mform->addGroup($addgrp, 'addgrp', get_string('additem', 'qtype_calculated'), ' ', false);
         $mform->addElement('static', "divideradd", '', '');
-    //     $mform->closeHeaderBefore('divideradd');
         if ($this->noofitems > 0) {
             $mform->addElement('header', 'additemhdr', get_string('delete', 'moodle'));
             $deletegrp = array();
-            $deletegrp[] =& $mform->createElement('submit', 'deletebutton', get_string('delete', 'moodle'));
-            $deletegrp[] =& $mform->createElement('select', 'selectdelete', get_string('deleteitem', 'qtype_calculated')."1", $addremoveoptions);
-            $deletegrp[] = & $mform->createElement('static',"stat","Items",get_string('setwildcardvalues', 'qtype_calculatedsimple'));
+            $deletegrp[] = $mform->createElement('submit', 'deletebutton',
+                    get_string('delete', 'moodle'));
+            $deletegrp[] = $mform->createElement('select', 'selectdelete',
+                    get_string('deleteitem', 'qtype_calculated')."1", $addremoveoptions);
+            $deletegrp[] = $mform->createElement('static', "stat", "Items",
+                    get_string('setwildcardvalues', 'qtype_calculatedsimple'));
             $mform->addGroup($deletegrp, 'deletegrp', '', '   ', false);
-   //      $mform->addElement('static', "dividerdelete", '', '<hr />');
-   //      $mform->closeHeaderBefore('dividerdelete');
         } else {
-            $mform->addElement('static','warning','','<span class="error">'.get_string('youmustaddatleastoneitem', 'qtype_calculated').'</span>');
+            $mform->addElement('static', 'warning', '', '<span class="error">' .
+                    get_string('youmustaddatleastoneitem', 'qtype_calculated').'</span>');
         }
 
-      //  $mform->addElement('header', 'additemhdr', get_string('showitems', 'moodle'));
         $addgrp1 = array();
-        $addgrp1[] =& $mform->createElement('submit', 'showbutton', get_string('showitems', 'qtype_calculated'));
-        $addgrp1[] =& $mform->createElement('select', "selectshow",'' , $showoptions);
-        $addgrp1[] = & $mform->createElement('static',"stat",'',get_string('setwildcardvalues', 'qtype_calculated'));
+        $addgrp1[] = $mform->createElement('submit', 'showbutton',
+                get_string('showitems', 'qtype_calculated'));
+        $addgrp1[] = $mform->createElement('select', "selectshow", '' , $showoptions);
+        $addgrp1[] = $mform->createElement('static', "stat", '',
+                get_string('setwildcardvalues', 'qtype_calculated'));
         $mform->addGroup($addgrp1, 'addgrp1', '', '   ', false);
         $mform->registerNoSubmitButton('showbutton');
         $mform->closeHeaderBefore('addgrp1');
-//------------------------------------------------------------------------------------------------------------------------------
+        //----------------------------------------------------------------------
         $j = $this->noofitems * count($this->datasetdefs);
-        $k = 1 ;
-        if ("" != optional_param('selectshow', '', PARAM_INT)){
+        $k = 1;
+        if ("" != optional_param('selectshow', '', PARAM_INT)) {
             $k = optional_param('selectshow', '', PARAM_INT);
         }
-        for ($i = $this->noofitems; $i >= 1 ; $i--){
-                if($k > 0  ){ //||  $this->outsidelimit || !empty($this->numbererrors )
-            $mform->addElement('header', '',"<b>".get_string('setno', 'qtype_calculated', $i)."</b>&nbsp;&nbsp;");
-           // $mform->addElement('header', '', get_string('itemno', 'qtype_calculated', $i));
-        }
-            foreach ($this->datasetdefs as $defkey => $datasetdef){
-                if($k > 0  ){ //||  $this->outsidelimit || !empty($this->numbererrors )
-                    if($datasetdef->category == 0 ){
-                        $mform->addElement('text', "number[$j]", get_string('wildcard', 'qtype_calculated', $datasetdef->name));
-                    }else {
-                        $mform->addElement('text', "number[$j]", get_string('sharedwildcardname', 'qtype_calculated', $datasetdef->name));
+        for ($i = $this->noofitems; $i >= 1; $i--) {
+            if ($k > 0) {
+                $mform->addElement('header', '', "<b>" .
+                        get_string('setno', 'qtype_calculated', $i)."</b>&nbsp;&nbsp;");
+            }
+            foreach ($this->datasetdefs as $defkey => $datasetdef) {
+                if ($k > 0) {
+                    if ($datasetdef->category == 0 ) {
+                        $mform->addElement('text', "number[$j]",
+                                get_string('wildcard', 'qtype_calculated', $datasetdef->name));
+                    } else {
+                        $mform->addElement('text', "number[$j]", get_string(
+                                'sharedwildcardname', 'qtype_calculated', $datasetdef->name));
                     }
-                        
-            }else {
-                $mform->addElement('hidden',"number[$j]" , '');
-            }    
+
+                } else {
+                    $mform->addElement('hidden', "number[$j]" , '');
+                }
                 $mform->setType("number[$j]", PARAM_NUMBER);
                 $mform->addElement('hidden', "itemid[$j]");
                 $mform->setType("itemid[$j]", PARAM_INT);
@@ -271,36 +299,37 @@ class question_dataset_dependent_items_form extends moodleform {
 
                 $j--;
             }
-              if('' != $strquestionlabel && ($k > 0 )){ //||  $this->outsidelimit || !empty($this->numbererrors )
-                $repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
+            if ('' != $strquestionlabel && ($k > 0 )) {
+                //||  $this->outsidelimit || !empty($this->numbererrors )
+                $repeated[] = $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
                 // decode equations in question text
-                $qtext = $this->qtypeobj->substitute_variables($this->question->questiontext, $data);
+                $qtext = $this->qtypeobj->substitute_variables(
+                        $this->question->questiontext, $data);
                 $textequations = $this->qtypeobj->find_math_equations($qtext);
-                if($textequations != '' && count($textequations) > 0 ){
-                $mform->addElement('static', "divider1[$j]", '', 'Formulas {=..} in question text');
-                    foreach($textequations as $key=>$equation){
-                            if( $formulaerrors = qtype_calculated_find_formula_errors($equation)){
-                                $str=$formulaerrors ;
-                            }else {
-                                eval('$str = '.$equation.';');
-                            }
-
-                        $mform->addElement('static', "textequation","{=$equation}","=".$str);    
+                if ($textequations != '' && count($textequations) > 0 ) {
+                    $mform->addElement('static', "divider1[$j]", '',
+                            'Formulas {=..} in question text');
+                    foreach ($textequations as $key => $equation) {
+                        if ($formulaerrors = qtype_calculated_find_formula_errors($equation)) {
+                            $str=$formulaerrors;
+                        } else {
+                            eval('$str = '.$equation.';');
                         }
-                } 
 
-               }
-                        $k-- ;
+                        $mform->addElement('static', "textequation", "{=$equation}", "=".$str);
+                    }
+                }
+
+            }
+            $k--;
 
         }
-      //  if ($this->outsidelimit){
-            $mform->addElement('static','outsidelimit','','');
-      //  }
-//------------------------------------------------------------------------------------------------------------------------------
-        //non standard name for button element needed so not using add_action_buttons
-        if ( !($this->noofitems==0) ){
-        $mform->addElement('submit', 'savechanges', get_string('savechanges'));
-        $mform->closeHeaderBefore('savechanges');
+        $mform->addElement('static', 'outsidelimit', '', '');
+        //----------------------------------------------------------------------
+        // Non standard name for button element needed so not using add_action_buttons
+        if (!($this->noofitems==0) ) {
+            $mform->addElement('submit', 'savechanges', get_string('savechanges'));
+            $mform->closeHeaderBefore('savechanges');
         }
         //hidden elements
         $mform->addElement('hidden', 'id');
@@ -326,46 +355,57 @@ class question_dataset_dependent_items_form extends moodleform {
         $mform->setDefault('returnurl', 0);
     }
 
-    function set_data($question){
+    public function set_data($question) {
         $formdata = array();
         $fromform = new stdClass();
-        if (isset($question->options)){
+        if (isset($question->options)) {
             $answers = $question->options->answers;
             if (count($answers)) {
-                if ( optional_param('updateanswers', '', PARAM_RAW) != '' || optional_param('updatedatasets', '', PARAM_RAW) != ''){
-                    foreach ($answers as $key => $answer){
-                        $fromform->tolerance[$key]= $this->_form->getElementValue('tolerance['.$key.']');
+                if (optional_param('updateanswers', '', PARAM_RAW) != '' ||
+                        optional_param('updatedatasets', '', PARAM_RAW) != '') {
+                    foreach ($answers as $key => $answer) {
+                        $fromform->tolerance[$key]= $this->_form->getElementValue(
+                                'tolerance['.$key.']');
                         $answer->tolerance = $fromform->tolerance[$key];
-                        $fromform->tolerancetype[$key]= $this->_form->getElementValue('tolerancetype['.$key.']');
-                        if( is_array($fromform->tolerancetype[$key])) $fromform->tolerancetype[$key]= $fromform->tolerancetype[$key][0];
+                        $fromform->tolerancetype[$key]= $this->_form->getElementValue(
+                                'tolerancetype['.$key.']');
+                        if (is_array($fromform->tolerancetype[$key])) {
+                            $fromform->tolerancetype[$key]= $fromform->tolerancetype[$key][0];
+                        }
                         $answer->tolerancetype = $fromform->tolerancetype[$key];
-                        $fromform->correctanswerlength[$key]= $this->_form->getElementValue('correctanswerlength['.$key.']');
-                        if( is_array($fromform->correctanswerlength[$key])) $fromform->correctanswerlength[$key]= $fromform->correctanswerlength[$key][0];
+                        $fromform->correctanswerlength[$key]= $this->_form->getElementValue(
+                                'correctanswerlength['.$key.']');
+                        if (is_array($fromform->correctanswerlength[$key])) {
+                            $fromform->correctanswerlength[$key] =
+                                    $fromform->correctanswerlength[$key][0];
+                        }
                         $answer->correctanswerlength = $fromform->correctanswerlength[$key];
-                        $fromform->correctanswerformat[$key]= $this->_form->getElementValue('correctanswerformat['.$key.']');
-                        if( is_array($fromform->correctanswerformat[$key])) $fromform->correctanswerformat[$key]= $fromform->correctanswerformat[$key][0];
+                        $fromform->correctanswerformat[$key] = $this->_form->getElementValue(
+                                'correctanswerformat['.$key.']');
+                        if (is_array($fromform->correctanswerformat[$key])) {
+                            $fromform->correctanswerformat[$key] =
+                                    $fromform->correctanswerformat[$key][0];
+                        }
                         $answer->correctanswerformat = $fromform->correctanswerformat[$key];
                     }
-                    $this->qtypeobj->save_question_calculated($question,$fromform);
-                }else {
-                foreach ($answers as $key => $answer){
-
-                    $formdata['tolerance['.$key.']'] = $answer->tolerance;
-                    $formdata['tolerancetype['.$key.']'] = $answer->tolerancetype;
-                    $formdata['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
-                    $formdata['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
-               }
+                    $this->qtypeobj->save_question_calculated($question, $fromform);
+
+                } else {
+                    foreach ($answers as $key => $answer) {
+                        $formdata['tolerance['.$key.']'] = $answer->tolerance;
+                        $formdata['tolerancetype['.$key.']'] = $answer->tolerancetype;
+                        $formdata['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
+                        $formdata['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
+                    }
+                }
             }
         }
-    }
         //fill out all data sets and also the fields for the next item to add.
-   //     echo "<p> qequation <pre>";print_r($question);echo "</pre></p>";
-   //     echo "<p> datasetdefs <pre>";print_r($this->datasetdefs);echo "</pre></p>";
         $j = $this->noofitems * count($this->datasetdefs);
-         for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){
+        for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--) {
             $data = array();
-            foreach ($this->datasetdefs as $defid => $datasetdef){
-                if (isset($datasetdef->items[$itemnumber])){
+            foreach ($this->datasetdefs as $defid => $datasetdef) {
+                if (isset($datasetdef->items[$itemnumber])) {
                     $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value;
                     $formdata["definition[$j]"] = $defid;
                     $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id;
@@ -373,15 +413,16 @@ class question_dataset_dependent_items_form extends moodleform {
                 }
                 $j--;
             }
-            $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj,$question->id,$question->questiontext,$answers, $data, $itemnumber);
-             if ($comment->outsidelimit) {
-                 $this->outsidelimit=$comment->outsidelimit ;
+            $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj, $question->id,
+                    $question->questiontext, $answers, $data, $itemnumber);
+            if ($comment->outsidelimit) {
+                $this->outsidelimit=$comment->outsidelimit;
             }
             $totalcomment='';
             foreach ($question->options->answers as $key => $answer) {
                 $totalcomment .= $comment->stranswers[$key].'<br/>';
             }
-            $formdata['answercomment['.$itemnumber.']'] = $totalcomment ;
+            $formdata['answercomment['.$itemnumber.']'] = $totalcomment;
         }
 
         $formdata['nextpageparam[forceregeneration]'] = $this->regenerate;
@@ -392,15 +433,17 @@ class question_dataset_dependent_items_form extends moodleform {
         //dataset generation dafaults
         if ($this->qtypeobj->supports_dataset_item_generation()) {
             $itemnumber = $this->noofitems+1;
-            foreach ($this->datasetdefs as $defid => $datasetdef){
-                if( optional_param('updatedatasets', '', PARAM_RAW) == '' && optional_param('updateanswers', '', PARAM_RAW)== ''){
-                $formdata["number[$j]"] = $this->qtypeobj->generate_dataset_item($datasetdef->options);
-            }else {
-                $formdata["number[$j]"] = $this->_form->getElementValue("number[$j]") ;
-            }
+            foreach ($this->datasetdefs as $defid => $datasetdef) {
+                if (optional_param('updatedatasets', '', PARAM_RAW) == '' &&
+                        optional_param('updateanswers', '', PARAM_RAW)== '') {
+                    $formdata["number[$j]"] = $this->qtypeobj->generate_dataset_item(
+                            $datasetdef->options);
+                } else {
+                    $formdata["number[$j]"] = $this->_form->getElementValue("number[$j]");
+                }
                 $formdata["definition[$j]"] = $defid;
-                $formdata["itemid[$j]"] =
-                        isset($datasetdef->items[$itemnumber])?$datasetdef->items[$itemnumber]->id:0;
+                $formdata["itemid[$j]"] = isset($datasetdef->items[$itemnumber]) ?
+                        $datasetdef->items[$itemnumber]->id : 0;
                 $data[$datasetdef->name] = $formdata["number[$j]"];
                 $j++;
             }
@@ -408,11 +451,12 @@ class question_dataset_dependent_items_form extends moodleform {
 
         //existing records override generated data depending on radio element
         $j = $this->noofitems * count($this->datasetdefs)+1;
-        if (!$this->regenerate && (optional_param('updatedatasets', '', PARAM_RAW) == '' && optional_param('updateanswers', '', PARAM_RAW)== '')){
+        if (!$this->regenerate && (optional_param('updatedatasets', '', PARAM_RAW) == '' &&
+                optional_param('updateanswers', '', PARAM_RAW)== '')) {
             $idx = 1;
             $itemnumber = $this->noofitems+1;
-            foreach ($this->datasetdefs as $defid => $datasetdef){
-                if (isset($datasetdef->items[$itemnumber])){
+            foreach ($this->datasetdefs as $defid => $datasetdef) {
+                if (isset($datasetdef->items[$itemnumber])) {
                     $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value;
                     $formdata["definition[$j]"] = $defid;
                     $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id;
@@ -420,52 +464,55 @@ class question_dataset_dependent_items_form extends moodleform {
                 }
                 $j++;
             }
-
         }
 
-            $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj,$question->id,$question->questiontext,$answers, $data, ($this->noofitems+1));
-            if (isset($comment->outsidelimit)&&$comment->outsidelimit)  {
-                 $this->outsidelimit=$comment->outsidelimit ;
-            }
-            $key1 = 1;
-            foreach ($question->options->answers as $key => $answer) {
-                $formdata['answercomment['.($this->noofitems+$key1).']'] = $comment->stranswers[$key];
-                $key1++;
-            }
+        $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj, $question->id,
+                $question->questiontext, $answers, $data, ($this->noofitems + 1));
+        if (isset($comment->outsidelimit)&&$comment->outsidelimit) {
+            $this->outsidelimit=$comment->outsidelimit;
+        }
+        $key1 = 1;
+        foreach ($question->options->answers as $key => $answer) {
+            $formdata['answercomment['.($this->noofitems+$key1).']'] = $comment->stranswers[$key];
+            $key1++;
+        }
 
-        if ($this->outsidelimit){
-            $formdata['outsidelimit']= '<span class="error">'.get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated').'</span>';
+        if ($this->outsidelimit) {
+            $formdata['outsidelimit']= '<span class="error">' .
+                    get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated') . '</span>';
         }
         $formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata);
 
         parent::set_data((object)($formdata + (array)$question));
     }
 
-    function validation($data, $files) {
+    public function validation($data, $files) {
         $errors = array();
-        if (isset($data['savechanges']) && ($this->noofitems==0) ){
+        if (isset($data['savechanges']) && ($this->noofitems==0) ) {
             $errors['warning'] = get_string('warning', 'mnet');
         }
-        if ($this->outsidelimit){
-            $errors['outsidelimits'] = get_string('oneanswertrueansweroutsidelimits','qtype_calculated');
+        if ($this->outsidelimit) {
+            $errors['outsidelimits'] =
+                    get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated');
         }
         $numbers = $data['number'];
-        foreach ($numbers as $key => $number){
-            if(! is_numeric($number)){
-                if (stristr($number,',')){
-                    $errors['number['.$key.']'] = get_string('The , cannot be used, use . as in 0.013 or 1.3e-2', 'qtype_calculated');
-                }else {
-                    $errors['number['.$key.']'] = get_string('This is not a valid number', 'qtype_calculated');
+        foreach ($numbers as $key => $number) {
+            if (! is_numeric($number)) {
+                if (stristr($number, ', ')) {
+                    $errors['number['.$key.']'] = get_string(
+                        'The , cannot be used, use . as in 0.013 or 1.3e-2', 'qtype_calculated');
+                } else {
+                    $errors['number['.$key.']'] = get_string(
+                            'This is not a valid number', 'qtype_calculated');
                 }
-            }else if( stristr($number,'x')){
-                $errors['number['.$key.']'] = get_string('Hexadecimal format (i.e. 0X12d) is not allowed', 'qtype_calculated');
-            } else if( is_nan($number)){
-                $errors['number['.$key.']'] = get_string('is a NAN number', 'qtype_calculated');
+            } else if (stristr($number, 'x')) {
+                $errors['number['.$key.']'] = get_string(
+                        'Hexadecimal format (i.e. 0X12d) is not allowed', 'qtype_calculated');
+            } else if (is_nan($number)) {
+                $errors['number['.$key.']'] = get_string(
+                        'is a NAN number', 'qtype_calculated');
             }
         }
         return $errors;
     }
-
-
 }
-
index 9fa94da..8bb9650 100644 (file)
@@ -46,23 +46,22 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
 
     if ($oldversion < 2009082000) { //New version in version.php
 
-// this should be changed if merged to 1.9
-//    let if ($dbman->table_exists()) replace the normal $oldversion test
-//    as in any case the question question_calculated_options should be created
-
-    /// Define table question_calculated_options to be created
+        // Define table question_calculated_options to be created
         $table = new xmldb_table('question_calculated_options');
 
-    /// Adding fields to table question_calculated_options
-        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
-        $table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
-        $table->add_field('synchronize', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
+        // Adding fields to table question_calculated_options
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
+                XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+        $table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
+                XMLDB_NOTNULL, null, '0');
+        $table->add_field('synchronize', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED,
+                XMLDB_NOTNULL, null, '0');
 
-    /// Adding keys to table question_calculated_options
+        // Adding keys to table question_calculated_options
         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
         $table->add_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id'));
 
-    /// Conditionally launch create table for question_calculated_options
+        // Conditionally launch create table for question_calculated_options
         if (!$dbman->table_exists($table)) {
             // $dbman->create_table doesnt return a result, we just have to trust it
             $dbman->create_table($table);
@@ -72,62 +71,59 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
 
     if ( $oldversion < 2009092000) { //New version in version.php
 
-    /// Define field multichoice to be added to question_calculated_options
-    ///ALTER TABLE `moodle`.`mdl_question_calculated_options` DROP COLUMN `multichoice`;
-    //    $table = new xmldb_table('question_calculated_options');
-    //    $field = new xmldb_field('multichoice', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'synchronize');
-
-    /// Conditionally launch add field multichoice
-     //   if (!$dbman->field_exists($table, $field)) {
-    //        $dbman->add_field($table, $field);
-    //    }
-    /// Define field single to be added to question_calculated_options
+        // Define field single to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('single', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'synchronize');
+        $field = new xmldb_field('single', XMLDB_TYPE_INTEGER, '4', null,
+                XMLDB_NOTNULL, null, '0', 'synchronize');
 
-    /// Conditionally launch add field single
+        // Conditionally launch add field single
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
 
-    /// Define field shuffleanswers to be added to question_calculated_options
+        // Define field shuffleanswers to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('shuffleanswers', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'single');
+        $field = new xmldb_field('shuffleanswers', XMLDB_TYPE_INTEGER, '4', null,
+                XMLDB_NOTNULL, null, '0', 'single');
 
-    /// Conditionally launch add field shuffleanswers
+        // Conditionally launch add field shuffleanswers
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
-    /// Define field correctfeedback to be added to question_calculated_options
+        // Define field correctfeedback to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('correctfeedback', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'shuffleanswers');
+        $field = new xmldb_field('correctfeedback', XMLDB_TYPE_TEXT, 'small', null,
+                null, null, null, 'shuffleanswers');
 
-    /// Conditionally launch add field correctfeedback
+        // Conditionally launch add field correctfeedback
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
 
-    /// Define field partiallycorrectfeedback to be added to question_calculated_options
+        // Define field partiallycorrectfeedback to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('partiallycorrectfeedback', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'correctfeedback');
+        $field = new xmldb_field('partiallycorrectfeedback', XMLDB_TYPE_TEXT, 'small', null,
+                null, null, null, 'correctfeedback');
 
-    /// Conditionally launch add field partiallycorrectfeedback
+        // Conditionally launch add field partiallycorrectfeedback
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
-    /// Define field incorrectfeedback to be added to question_calculated_options
+        // Define field incorrectfeedback to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('incorrectfeedback', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'partiallycorrectfeedback');
+        $field = new xmldb_field('incorrectfeedback', XMLDB_TYPE_TEXT, 'small', null,
+                null, null, null, 'partiallycorrectfeedback');
 
-    /// Conditionally launch add field incorrectfeedback
+        // Conditionally launch add field incorrectfeedback
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
-    /// Define field answernumbering to be added to question_calculated_options
+        // Define field answernumbering to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('answernumbering', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'abc', 'incorrectfeedback');
+        $field = new xmldb_field('answernumbering', XMLDB_TYPE_CHAR, '10', null,
+                XMLDB_NOTNULL, null, 'abc', 'incorrectfeedback');
 
-    /// Conditionally launch add field answernumbering
+        // Conditionally launch add field answernumbering
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
@@ -136,16 +132,16 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
 
     if ($oldversion < 2010020800) {
 
-    /// Define field multiplechoice to be dropped from question_calculated_options
+        // Define field multiplechoice to be dropped from question_calculated_options
         $table = new xmldb_table('question_calculated_options');
         $field = new xmldb_field('multichoice');
 
-    /// Conditionally launch drop field multiplechoice
+        // Conditionally launch drop field multiplechoice
         if ($dbman->field_exists($table, $field)) {
             $dbman->drop_field($table, $field);
         }
 
-    /// calculated savepoint reached
+        // calculated savepoint reached
         upgrade_plugin_savepoint(true, 2010020800, 'qtype', 'calculated');
     }
 
@@ -153,7 +149,8 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
 
         // Define field correctfeedbackformat to be added to question_calculated_options
         $table = new xmldb_table('question_calculated_options');
-        $field = new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'correctfeedback');
+        $field = new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
+                XMLDB_NOTNULL, null, '0', 'correctfeedback');
 
         // Conditionally launch add field correctfeedbackformat
         if (!$dbman->field_exists($table, $field)) {
@@ -161,7 +158,8 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
         }
 
         // Define field partiallycorrectfeedbackformat to be added to question_calculated_options
-        $field = new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback');
+        $field = new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
+                XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback');
 
         // Conditionally launch add field partiallycorrectfeedbackformat
         if (!$dbman->field_exists($table, $field)) {
@@ -169,7 +167,8 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
         }
 
         // Define field incorrectfeedbackformat to be added to question_calculated_options
-        $field = new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'incorrectfeedback');
+        $field = new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
+                XMLDB_NOTNULL, null, '0', 'incorrectfeedback');
 
         // Conditionally launch add field incorrectfeedbackformat
         if (!$dbman->field_exists($table, $field)) {
@@ -185,12 +184,16 @@ function xmldb_qtype_calculated_upgrade($oldversion) {
                 FROM {question_calculated_options} qco
                 JOIN {question} q ON qco.question = q.id');
         foreach ($rs as $record) {
-            if ($CFG->texteditors !== 'textarea' && $record->oldquestiontextformat == FORMAT_MOODLE) {
-                $record->correctfeedback = text_to_html($record->correctfeedback, false, false, true);
+            if ($CFG->texteditors !== 'textarea' &&
+                    $record->oldquestiontextformat == FORMAT_MOODLE) {
+                $record->correctfeedback = text_to_html(
+                        $record->correctfeedback, false, false, true);
                 $record->correctfeedbackformat = FORMAT_HTML;
-                $record->partiallycorrectfeedback = text_to_html($record->partiallycorrectfeedback, false, false, true);
+                $record->partiallycorrectfeedback = text_to_html(
+                        $record->partiallycorrectfeedback, false, false, true);
                 $record->partiallycorrectfeedbackformat = FORMAT_HTML;
-                $record->incorrectfeedback = text_to_html($record->incorrectfeedback, false, false, true);
+                $record->incorrectfeedback = text_to_html(
+                        $record->incorrectfeedback, false, false, true);
                 $record->incorrectfeedbackformat = FORMAT_HTML;
             } else {
                 $record->correctfeedbackformat = $record->oldquestiontextformat;
index bfb2943..7517408 100644 (file)
@@ -37,7 +37,7 @@ class question_edit_calculated_form extends question_edit_form {
     /**
      * Handle to the question type for this question.
      *
-     * @var question_calculated_qtype
+     * @var qtype_calculated
      */
     public $qtypeobj;
     public $questiondisplay;
@@ -46,37 +46,40 @@ class question_edit_calculated_form extends question_edit_form {
     public $initialname = '';
     public $reload = false;
 
-    function question_edit_calculated_form(&$submiturl, &$question, &$category, &$contexts, $formeditable = true){
-        global $QTYPES, $SESSION, $CFG, $DB;
+    public function __construct($submiturl, $question, $category, $contexts,
+            $formeditable = true) {
+        global $CFG, $DB;
         $this->question = $question;
-        $this->qtypeobj =& $QTYPES[$this->question->qtype];
-        if  (  "1" == optional_param('reload','', PARAM_INT )) {
-            $this->reload = true ;
-        }else {
-            $this->reload = false ;
+        $this->qtypeobj = $QTYPES[$this->question->qtype];
+        if ('1' == optional_param('reload', '', PARAM_INT)) {
+            $this->reload = true;
+        } else {
+            $this->reload = false;
         }
 
-        if(!$this->reload ){ // use database data as this is first pass
-            if(isset($this->question->id )){
+        if (!$this->reload) { // use database data as this is first pass
+            if (isset($this->question->id)) {
                 // remove prefix #{..}# if exists
-                $this->initialname = $question->name ;
+                $this->initialname = $question->name;
                 $regs= array();
-                if(preg_match('~#\{([^[:space:]]*)#~',$question->name , $regs)){
+                if (preg_match('~#\{([^[:space:]]*)#~', $question->name , $regs)) {
                     $question->name = str_replace($regs[0], '', $question->name);
                 };
             }
-        }else {
         }
-        parent::question_edit_form($submiturl, $question, $category, $contexts, $formeditable);
+        parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
     }
 
-    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
-        // $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
+    public function get_per_answer_fields($mform, $label, $gradeoptions,
+            &$repeatedoptions, &$answersoption) {
         $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'question'), array('size' => 50));
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeated[] =& $mform->createElement('editor', 'feedback', get_string('feedback', 'question'), null, $this->editoroptions);
+        $repeated[] = $mform->createElement('header', 'answerhdr', $label);
+        $repeated[] = $mform->createElement('text', 'answer',
+                get_string('answer', 'question'), array('size' => 50));
+        $repeated[] = $mform->createElement('select', 'fraction',
+                get_string('grade'), $gradeoptions);
+        $repeated[] = $mform->createElement('editor', 'feedback',
+                get_string('feedback', 'question'), null, $this->editoroptions);
         $repeatedoptions['answer']['type'] = PARAM_RAW;
         $repeatedoptions['fraction']['default'] = 0;
         $answersoption = 'answers';
@@ -84,19 +87,26 @@ class question_edit_calculated_form extends question_edit_form {
         $mform->setType('answer', PARAM_NOTAGS);
 
         $addrepeated = array();
-        $addrepeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
-        $addrepeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'qtype_numerical'), $this->qtypeobj->tolerance_types());
+        $addrepeated[] = $mform->createElement('text', 'tolerance',
+                get_string('tolerance', 'qtype_calculated'));
+        $addrepeated[] = $mform->createElement('select', 'tolerancetype',
+                get_string('tolerancetype', 'qtype_numerical'),
+                $this->qtypeobj->tolerance_types());
         $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
         $repeatedoptions['tolerance']['default'] = 0.01;
 
-        $addrepeated[] =&  $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+        $addrepeated[] = $mform->createElement('select', 'correctanswerlength',
+                get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
         $repeatedoptions['correctanswerlength']['default'] = 2;
 
-        $answerlengthformats = array('1' => get_string('decimalformat', 'qtype_numerical'), '2' => get_string('significantfiguresformat', 'quiz'));
-        $addrepeated[] =&  $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
+        $answerlengthformats = array(
+            '1' => get_string('decimalformat', 'qtype_numerical'),
+            '2' => get_string('significantfiguresformat', 'quiz')
+        );
+        $addrepeated[] = $mform->createElement('select', 'correctanswerformat',
+                get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
         array_splice($repeated, 3, 0, $addrepeated);
-        $repeated[1]->setLabel(get_string('correctanswerformula', 'qtype_calculated').'=');
-
+        $repeated[1]->setLabel(get_string('correctanswerformula', 'qtype_calculated') . ' = ');
         return $repeated;
     }
 
@@ -106,40 +116,44 @@ class question_edit_calculated_form extends question_edit_form {
      * @param MoodleQuickForm $mform the form being built.
      */
     protected function definition_inner(&$mform) {
-        global $QTYPES;
-        $this->qtypeobj =& $QTYPES[$this->qtype()];
+        $this->qtypeobj = $QTYPES[$this->qtype()];
         $label = get_string('sharedwildcards', 'qtype_calculated');
         $mform->addElement('hidden', 'initialcategory', 1);
         $mform->addElement('hidden', 'reload', 1);
         $mform->setType('initialcategory', PARAM_INT);
         $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
-        $mform->insertElementBefore($mform->createElement('static','listcategory',$label,$html2),'name');
-        if(isset($this->question->id )){
-            $mform->insertElementBefore($mform->createElement('static','initialname',get_string('questionstoredname','qtype_calculated'),$this->initialname),'name');
+        $mform->insertElementBefore(
+                $mform->createElement('static', 'listcategory', $label, $html2), 'name');
+        if (isset($this->question->id)) {
+            $mform->insertElementBefore($mform->createElement('static', 'initialname',
+                    get_string('questionstoredname', 'qtype_calculated'),
+                    $this->initialname), 'name');
         };
-        $addfieldsname='updatecategory';
-        $addstring=get_string("updatecategory", "qtype_calculated");
+        $addfieldsname = 'updatecategory';
+        $addstring = get_string('updatecategory', 'qtype_calculated');
         $mform->registerNoSubmitButton($addfieldsname);
 
-        $mform->insertElementBefore(    $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
+        $mform->insertElementBefore(
+                $mform->createElement('submit', $addfieldsname, $addstring), 'listcategory');
         $mform->registerNoSubmitButton('createoptionbutton');
 
         //editing as regular
         $mform->setType('single', PARAM_INT);
 
-        $mform->addElement('hidden','shuffleanswers', '1');
+        $mform->addElement('hidden', 'shuffleanswers', '1');
         $mform->setType('shuffleanswers', PARAM_INT);
-        $mform->addElement('hidden','answernumbering', 'abc');
+        $mform->addElement('hidden', 'answernumbering', 'abc');
         $mform->setType('answernumbering', PARAM_SAFEDIR);
 
         $creategrades = get_grade_options();
 
-        $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'), $creategrades->gradeoptions, 1, 1);
+        $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
+                $creategrades->gradeoptions, 1, 1);
 
         $repeated = array();
 
-        $QTYPES['numerical']->add_units_options($mform,$this);
-        $QTYPES['numerical']->add_units_elements($mform,$this);
+        $QTYPES['numerical']->add_units_options($mform, $this);
+        $QTYPES['numerical']->add_units_elements($mform, $this);
 
         //hidden elements
         $mform->addElement('hidden', 'synchronize', '');
@@ -148,15 +162,15 @@ class question_edit_calculated_form extends question_edit_form {
         $mform->setType('wizard', PARAM_ALPHA);
     }
 
-    function data_preprocessing($question) {
+    public function data_preprocessing($question) {
         global $QTYPES;
 
         $default_values = array();
-        if (isset($question->options)){
+        if (isset($question->options)) {
             $answers = $question->options->answers;
             if (count($answers)) {
                 $key = 0;
-                foreach ($answers as $answer){
+                foreach ($answers as $answer) {
                     $draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
                     $default_values['answer['.$key.']'] = $answer->answer;
                     $default_values['fraction['.$key.']'] = $answer->fraction;
@@ -179,11 +193,11 @@ class question_edit_calculated_form extends question_edit_form {
                     $key++;
                 }
             }
-            $default_values['synchronize'] = $question->options->synchronize ;
+            $default_values['synchronize'] = $question->options->synchronize;
             // set unit data, prepare files in instruction area
             $QTYPES['numerical']->set_numerical_unit_data($this, $question, $default_values);
         }
-        if (isset($question->options->single)){
+        if (isset($question->options->single)) {
             $default_values['single'] =  $question->options->single;
             $default_values['answernumbering'] =  $question->options->answernumbering;
             $default_values['shuffleanswers'] =  $question->options->shuffleanswers;
@@ -191,61 +205,52 @@ class question_edit_calculated_form extends question_edit_form {
         }
         $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
         $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
-        $default_values['returnurl'] = '0' ;
-        /* set the wild cards category display given that on loading the category element is
-        unselected when processing this function but have a valid value when processing the
-        update category button. The value can be obtain by
-         $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
-         but is coded using existing functions
-         */
+        $default_values['returnurl'] = '0';
+
         $qu = new stdClass();
         $el = new stdClass();
         /* no need to call elementExists() here */
-        if ($this->_form->elementExists('category')){
-            $el=$this->_form->getElement('category');
+        if ($this->_form->elementExists('category')) {
+            $el = $this->_form->getElement('category');
         } else {
-            $el=$this->_form->getElement('categorymoveto');
+            $el = $this->_form->getElement('categorymoveto');
         }
-        if($value =$el->getSelected()) {
-            $qu->category =$value[0];
-        }else {
-            $qu->category=$question->category;// on load  $question->category is set by question.php
+        if ($value = $el->getSelected()) {
+            $qu->category = $value[0];
+        } else {
+            // on load $question->category is set by question.php
+            $qu->category = $question->category;
         }
         $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
-        $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
+        $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2;
         $question = (object)((array)$question + $default_values);
 
         return $question;
     }
 
-    function qtype() {
+    public function qtype() {
         return 'calculated';
     }
 
-    function validation($data, $files) {
-        global $QTYPES;
-        // echo code left for testing period
-
-        // echo "<p>question <pre>";print_r($this->question);echo "</pre></p>";
-        // echo "<p>data <pre>";print_r($data);echo "</pre></p>";
+    public function validation($data, $files) {
 
         $errors = parent::validation($data, $files);
-        //verifying for errors in {=...} in question text;
+        // verifying for errors in {=...} in question text;
         $qtext = "";
         $qtextremaining = $data['questiontext']['text'];
         $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
         foreach ($possibledatasets as $name => $value) {
             $qtextremaining = str_replace('{'.$name.'}', '1', $qtextremaining);
         }
-        // echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
         while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
             $qtextsplits = explode($regs1[0], $qtextremaining, 2);
-            $qtext =$qtext.$qtextsplits[0];
+            $qtext = $qtext.$qtextsplits[0];
             $qtextremaining = $qtextsplits[1];
-            if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
-                if(!isset($errors['questiontext'])){
-                    $errors['questiontext'] = $formulaerrors.':'.$regs1[1] ;
-                }else {
+            if (!empty($regs1[1]) && $formulaerrors =
+                    qtype_calculated_find_formula_errors($regs1[1])) {
+                if (!isset($errors['questiontext'])) {
+                    $errors['questiontext'] = $formulaerrors.':'.$regs1[1];
+                } else {
                     $errors['questiontext'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
                 }
             }
@@ -255,33 +260,35 @@ class question_edit_calculated_form extends question_edit_form {
         $maxgrade = false;
         $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
         $mandatorydatasets = array();
-        foreach ($answers as $key => $answer){
+        foreach ($answers as $key => $answer) {
             $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
         }
-        if ( count($mandatorydatasets )==0){
-            //  $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
-            foreach ($answers as $key => $answer){
-                $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
+        if (count($mandatorydatasets)== 0) {
+            foreach ($answers as $key => $answer) {
+                $errors['answer['.$key.']'] =
+                        get_string('atleastonewildcard', 'qtype_datasetdependent');
             }
         }
         // regular calculated
-        foreach ($answers as $key => $answer){
-            //check no of choices
+        foreach ($answers as $key => $answer) {
+            // check no of choices
             // the * for everykind of answer not actually implemented
             $trimmedanswer = trim($answer);
-            if (($trimmedanswer!='')||$answercount==0){
+            if ($trimmedanswer != '' || $answercount == 0) {
                 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
-                if (FALSE !== $eqerror){
+                if (false !== $eqerror) {
                     $errors['answer['.$key.']'] = $eqerror;
                 }
             }
-            if ($trimmedanswer!=''){
+            if ($trimmedanswer != '') {
                 if ('2' == $data['correctanswerformat'][$key]
-                    && '0' == $data['correctanswerlength'][$key]) {
-                        $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
-                    }
-                if (!is_numeric($data['tolerance'][$key])){
-                    $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
+                        && '0' == $data['correctanswerlength'][$key]) {
+                    $errors['correctanswerlength['.$key.']'] =
+                            get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
+                }
+                if (!is_numeric($data['tolerance'][$key])) {
+                    $errors['tolerance['.$key.']'] =
+                            get_string('mustbenumeric', 'qtype_calculated');
                 }
                 if ($data['fraction'][$key] == 1) {
                     $maxgrade = true;
@@ -289,61 +296,16 @@ class question_edit_calculated_form extends question_edit_form {
 
                 $answercount++;
             }
-            //check grades
-
-            //TODO how should grade checking work here??
-                /*if ($answer != '') {
-                    if ($data['fraction'][$key] > 0) {
-                        $totalfraction += $data['fraction'][$key];
-                    }
-                    if ($data['fraction'][$key] > $maxfraction) {
-                        $maxfraction = $data['fraction'][$key];
-                    }
-                }*/
         }
 
-        //grade checking :
-        /// Perform sanity checks on fractional grades
-            /*if ( ) {
-                if ($maxfraction != 1) {
-                    $maxfraction = $maxfraction * 100;
-                    $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
-                }
-            } else {
-                $totalfraction = round($totalfraction,2);
-                if ($totalfraction != 1) {
-                    $totalfraction = $totalfraction * 100;
-                    $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
-                }
-            }
-            $units = $data['unit'];
-            if (count($units)) {
-                foreach ($units as $key => $unit){
-                    if (is_numeric($unit)){
-                        $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
-                    }
-                    $trimmedunit = trim($unit);
-                    $trimmedmultiplier = trim($data['multiplier'][$key]);
-                    if (!empty($trimmedunit)){
-                        if (empty($trimmedmultiplier)){
-                            $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
-                        }
-                        if (!is_numeric($trimmedmultiplier)){
-                            $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
-                        }
-
-                    }
-                }
-            }*/
-        $QTYPES['numerical']->validate_numerical_options($data, $errors) ;
-        if ($answercount==0){
+        $QTYPES['numerical']->validate_numerical_options($data, $errors);
+        if ($answercount == 0) {
             $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
         }
         if ($maxgrade == false) {
             $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
         }
 
-
         return $errors;
     }
 }
index 092afd2..7b6d62d 100644 (file)
@@ -68,7 +68,7 @@ $string['functiontakesoneortwoargs'] = 'The function {$a} must have either one o
 $string['functiontakestwoargs'] = 'The function {$a} must have exactly two arguments';
 $string['generatevalue'] = 'Generate a new value between';
 $string['getnextnow'] = 'Get new \'Item to Add\' now';
-$string['hexanotallowed'] = 'Dataset <strong>{$a->name}</strong> hexadecimal format value $a->value is not allowed' ;
+$string['hexanotallowed'] = 'Dataset <strong>{$a->name}</strong> hexadecimal format value $a->value is not allowed';
 $string['illegalformulasyntax'] = 'Illegal formula syntax starting with \'{$a}\'';
 $string['incorrectfeedback'] = 'For any incorrect response';
 $string['item(s)'] = 'item(s)';
@@ -101,10 +101,10 @@ $string['newlocal3'] = 'a link from a new set of links that will only be used by
 $string['nextitemtoadd'] = 'Next \'Item to Add\'';
 $string['nextpage'] = 'Next page';
 $string['nocoherencequestionsdatyasetcategory'] = 'For question id {$a->qid}, the category id {$a->qcat} is not identical with the shared wild card {$a->name} category id {$a->sharedcat}. Edit the question.';
-$string['nocommaallowed'] = 'The , cannot be used, use . as in 0.013 or 1.3e-2' ;
+$string['nocommaallowed'] = 'The , cannot be used, use . as in 0.013 or 1.3e-2';
 $string['nodataset'] = 'nothing - it is not a wild card';
 $string['nosharedwildcard'] = 'No shared wild card in this category';
-$string['notvalidnumber'] = 'Wild card value is not a valid number ' ;
+$string['notvalidnumber'] = 'Wild card value is not a valid number ';
 $string['oneanswertrueansweroutsidelimits'] = 'At least one correct answer outside the true value limits.<br />Modify the answers tolerance settings available as Advanced parameters';
 $string['param'] = 'Param {<strong>{$a}</strong>}';
 $string['partiallycorrectfeedback'] = 'For any partially correct response';
index 463c4d6..a46131e 100644 (file)
@@ -33,7 +33,8 @@ defined('MOODLE_INTERNAL') || die();
  * @copyright  1999 onwards Martin Dougiamas {@link http://moodle.com}
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class question_calculated_qtype extends question_type {
+class qtype_calculated extends question_type {
+    const MAX_DATASET_ITEMS = 100;
 
     public $fileoptionsa = array(
         'subdirs' => false,
@@ -44,31 +45,22 @@ class question_calculated_qtype extends question_type {
     // Used by the function custom_generator_tools:
     public $calcgenerateidhasbeenadded = false;
     public $virtualqtype = false;
-    public $wizard_pages_number = 3 ;
+    public $wizardpagesnumber = 3;
 
-    function name() {
-        return 'calculated';
-    }
-
-    function has_wildcards_in_responses($question, $subqid) {
-        return true;
-    }
-
-    function requires_qtypes() {
+    public function requires_qtypes() {
         return array('numerical');
     }
 
-    function get_question_options(&$question) {
+    public function get_question_options($question) {
         // First get the datasets and default options
         // the code is used for calculated, calculatedsimple and calculatedmulti qtypes
-        global $CFG, $DB, $OUTPUT, $QTYPES;
-        if (!$question->options = $DB->get_record('question_calculated_options', array('question' => $question->id))) {
-            //  echo $OUTPUT->notification('Error: Missing question options for calculated question'.$question->id.'!');
-            //  return false;
+        global $CFG, $DB, $OUTPUT;
+        if (!$question->options = $DB->get_record('question_calculated_options',
+                array('question' => $question->id))) {
             $question->options->synchronize = 0;
-            $question->options->single = 0; //$question->single;
+            $question->options->single = 0;
             $question->options->answernumbering = 'abc';
-            $question->options->shuffleanswers = 0 ;
+            $question->options->shuffleanswers = 0;
             $question->options->correctfeedback = '';
             $question->options->partiallycorrectfeedback = '';
             $question->options->incorrectfeedback = '';
@@ -77,29 +69,28 @@ class question_calculated_qtype extends question_type {
             $question->options->incorrectfeedbackformat = 0;
         }
 
-        if (!$question->options->answers = $DB->get_records_sql(
-            "SELECT a.*, c.tolerance, c.tolerancetype, c.correctanswerlength, c.correctanswerformat " .
-            "FROM {question_answers} a, " .
-            "     {question_calculated} c " .
-            "WHERE a.question = ? " .
-            "AND   a.id = c.answer ".
-            "ORDER BY a.id ASC", array($question->id))) {
-                // echo $OUTPUT->notification('Error: Missing question answer for calculated question ' . $question->id . '!');
+        if (!$question->options->answers = $DB->get_records_sql("
+            SELECT a.*, c.tolerance, c.tolerancetype, c.correctanswerlength, c.correctanswerformat
+            FROM {question_answers} a,
+                 {question_calculated} c
+            WHERE a.question = ?
+            AND   a.id = c.answer
+            ORDER BY a.id ASC", array($question->id))) {
                 return false;
-            }
+        }
 
-        if ( $this->get_virtual_qtype() ==  $QTYPES['numerical']){
+        if ($this->get_virtual_qtype() == $QTYPES['numerical']) {
             $QTYPES['numerical']->get_numerical_units($question);
             $QTYPES['numerical']->get_numerical_options($question);
         }
 
-        if( isset($question->export_process)&&$question->export_process){
+        if (isset($question->export_process)&&$question->export_process) {
             $question->options->datasets = $this->get_datasets_for_export($question);
         }
         return true;
     }
 
-    function get_datasets_for_export(&$question){
+    public function get_datasets_for_export($question) {
         global $DB, $CFG;
         $datasetdefs = array();
         if (!empty($question->id)) {
@@ -108,84 +99,84 @@ class question_calculated_qtype extends question_type {
                      WHERE d.question = ? AND d.datasetdefinition = i.id";
             if ($records = $DB->get_records_sql($sql, array($question->id))) {
                 foreach ($records as $r) {
-                    $def = $r ;
-                    if ($def->category=='0'){
-                        $def->status='private';
+                    $def = $r;
+                    if ($def->category == '0') {
+                        $def->status = 'private';
                     } else {
-                        $def->status='shared';
+                        $def->status = 'shared';
                     }
-                    $def->type ='calculated' ;
-                    list($distribution, $min, $max,$dec) = explode(':', $def->options, 4);
-                    $def->distribution=$distribution;
-                    $def->minimum=$min;
-                    $def->maximum=$max;
-                    $def->decimals=$dec ;
-                    if ($def->itemcount > 0 ) {
+                    $def->type = 'calculated';
+                    list($distribution, $min, $max, $dec) = explode(':', $def->options, 4);
+                    $def->distribution = $distribution;
+                    $def->minimum = $min;
+                    $def->maximum = $max;
+                    $def->decimals = $dec;
+                    if ($def->itemcount > 0) {
                         // get the datasetitems
                         $def->items = array();
-                        if ($items = $this->get_database_dataset_items($def->id)){
+                        if ($items = $this->get_database_dataset_items($def->id)) {
                             $n = 0;
-                            foreach( $items as $ii){
+                            foreach ($items as $ii) {
                                 $n++;
                                 $def->items[$n] = new stdClass();
-                                $def->items[$n]->itemnumber=$ii->itemnumber;
-                                $def->items[$n]->value=$ii->value;
+                                $def->items[$n]->itemnumber = $ii->itemnumber;
+                                $def->items[$n]->value = $ii->value;
                             }
-                            $def->number_of_items=$n ;
+                            $def->number_of_items = $n;
                         }
                     }
                     $datasetdefs["1-$r->category-$r->name"] = $def;
                 }
             }
         }
-        return $datasetdefs ;
+        return $datasetdefs;
     }
 
-    function save_question_options($question) {
-        global $CFG, $DB, $QTYPES ;
+    public function save_question_options($question) {
+        global $CFG, $DB;
         // the code is used for calculated, calculatedsimple and calculatedmulti qtypes
         $context = $question->context;
         if (isset($question->answer) && !isset($question->answers)) {
             $question->answers = $question->answer;
         }
         // calculated options
-        $update = true ;
-        $options = $DB->get_record("question_calculated_options", array("question" => $question->id));
+        $update = true;
+        $options = $DB->get_record('question_calculated_options',
+                array('question' => $question->id));
         if (!$options) {
             $update = false;
             $options = new stdClass();
             $options->question = $question->id;
         }
         // as used only by calculated
-        if(isset($question->synchronize)){
+        if (isset($question->synchronize)) {
             $options->synchronize = $question->synchronize;
-        }else {
-            $options->synchronize = 0 ;
+        } else {
+            $options->synchronize = 0;
         }
-        $options->single = 0; //$question->single;
+        $options->single = 0;
         $options->answernumbering =  $question->answernumbering;
         $options->shuffleanswers = $question->shuffleanswers;
 
-        foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
+        foreach (array('correctfeedback', 'partiallycorrectfeedback',
+                'incorrectfeedback') as $feedbackname) {
             $options->$feedbackname = '';
             $feedbackformat = $feedbackname . 'format';
-            $options->$feedbackformat = 0 ;
+            $options->$feedbackformat = 0;
         }
 
         if ($update) {
-            $DB->update_record("question_calculated_options", $options);
+            $DB->update_record('question_calculated_options', $options);
         } else {
-            $DB->insert_record("question_calculated_options", $options);
+            $DB->insert_record('question_calculated_options', $options);
         }
 
         // Get old versions of the objects
-        if (!$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) {
-            $oldanswers = array();
-        }
+        $oldanswers = $DB->get_records('question_answers',
+                array('question' => $question->id), 'id ASC');
 
-        if (!$oldoptions = $DB->get_records('question_calculated', array('question' => $question->id), 'answer ASC')) {
-            $oldoptions = array();
-        }
+        $oldoptions = $DB->get_records('question_calculated',
+                array('question' => $question->id), 'answer ASC');
 
         // Save the units.
         $virtualqtype = $this->get_virtual_qtype();
@@ -197,13 +188,13 @@ class question_calculated_qtype extends question_type {
         }
         // Insert all the new answers
         if (isset($question->answer) && !isset($question->answers)) {
-            $question->answers=$question->answer;
+            $question->answers = $question->answer;
         }
         foreach ($question->answers as $key => $dataanswer) {
             if (is_array($dataanswer)) {
                 $dataanswer = $dataanswer['text'];
             }
-            if ( trim($dataanswer) != '' ) {
+            if (trim($dataanswer) != '') {
                 $answer = new stdClass();
                 $answer->question = $question->id;
                 $answer->answer = trim($dataanswer);
@@ -215,31 +206,39 @@ class question_calculated_qtype extends question_type {
 
                 if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
                     $answer->id = $oldanswer->id;
-                    $answer->feedback = file_save_draft_area_files($question->feedback[$key]['itemid'], $context->id, 'question', 'answerfeedback', $oldanswer->id, $this->fileoptionsa, trim($question->feedback[$key]['text']));
-                    $DB->update_record("question_answers", $answer);
+                    $answer->feedback = file_save_draft_area_files(
+                            $question->feedback[$key]['itemid'], $context->id, 'question',
+                            'answerfeedback', $oldanswer->id, $this->fileoptionsa,
+                            trim($question->feedback[$key]['text']));
+                    $DB->update_record('question_answers', $answer);
                 } else { // This is a completely new answer
                     $answer->feedback = trim($question->feedback[$key]['text']);
-                    $answer->id = $DB->insert_record("question_answers", $answer);
+                    $answer->id = $DB->insert_record('question_answers', $answer);
                     if (isset($files)) {
                         foreach ($files as $file) {
-                            $this->import_file($context, 'question', 'answerfeedback', $answer->id, $file);
+                            $this->import_file($context, 'question', 'answerfeedback',
+                                    $answer->id, $file);
                         }
                     } else {
-                        $answer->feedback = file_save_draft_area_files($question->feedback[$key]['itemid'], $context->id, 'question', 'answerfeedback', $answer->id, $this->fileoptionsa , trim($question->feedback[$key]['text']));
+                        $answer->feedback = file_save_draft_area_files(
+                                $question->feedback[$key]['itemid'], $context->id, 'question',
+                                'answerfeedback', $answer->id, $this->fileoptionsa ,
+                                trim($question->feedback[$key]['text']));
                     }
-                    $DB->set_field('question_answers', 'feedback', $answer->feedback, array('id'=>$answer->id));
+                    $DB->set_field('question_answers', 'feedback', $answer->feedback,
+                            array('id' => $answer->id));
                 }
 
                 // Set up the options object
                 if (!$options = array_shift($oldoptions)) {
                     $options = new stdClass();
                 }
-                $options->question  = $question->id;
-                $options->answer    = $answer->id;
-                $options->tolerance = trim($question->tolerance[$key]);
-                $options->tolerancetype  = trim($question->tolerancetype[$key]);
-                $options->correctanswerlength  = trim($question->correctanswerlength[$key]);
-                $options->correctanswerformat  = trim($question->correctanswerformat[$key]);
+                $options->question            = $question->id;
+                $options->answer              = $answer->id;
+                $options->tolerance           = trim($question->tolerance[$key]);
+                $options->tolerancetype       = trim($question->tolerancetype[$key]);
+                $options->correctanswerlength = trim($question->correctanswerlength[$key]);
+                $options->correctanswerformat = trim($question->correctanswerformat[$key]);
 
                 // Save options
                 if (isset($options->id)) { // reusing existing record
@@ -251,14 +250,14 @@ class question_calculated_qtype extends question_type {
         }
         // delete old answer records
         if (!empty($oldanswers)) {
-            foreach($oldanswers as $oa) {
+            foreach ($oldanswers as $oa) {
                 $DB->delete_records('question_answers', array('id' => $oa->id));
             }
         }
 
         // delete old answer records
         if (!empty($oldoptions)) {
-            foreach($oldoptions as $oo) {
+            foreach ($oldoptions as $oo) {
                 $DB->delete_records('question_calculated', array('id' => $oo->id));
             }
         }
@@ -268,7 +267,7 @@ class question_calculated_qtype extends question_type {
             return $result;
         }
 
-        if( isset($question->import_process)&&$question->import_process){
+        if (isset($question->import_process)&&$question->import_process) {
             $this->import_datasets($question);
         }
         // Report any problems.
@@ -278,20 +277,21 @@ class question_calculated_qtype extends question_type {
         return true;
     }
 
-    function import_datasets($question){
+    public function import_datasets($question) {
         global $DB;
         $n = count($question->dataset);
         foreach ($question->dataset as $dataset) {
             // name, type, option,
             $datasetdef = new stdClass();
             $datasetdef->name = $dataset->name;
-            $datasetdef->type = 1 ;
-            $datasetdef->options =  $dataset->distribution.':'.$dataset->min.':'.$dataset->max.':'.$dataset->length;
-            $datasetdef->itemcount=$dataset->itemcount;
-            if ( $dataset->status =='private'){
+            $datasetdef->type = 1;
+            $datasetdef->options =  $dataset->distribution . ':' . $dataset->min . ':' .
+                    $dataset->max . ':' . $dataset->length;
+            $datasetdef->itemcount = $dataset->itemcount;
+            if ($dataset->status == 'private') {
                 $datasetdef->category = 0;
-                $todo='create' ;
-            }else if ($dataset->status =='shared' ){
+                $todo = 'create';
+            } else if ($dataset->status == 'shared') {
                 if ($sharedatasetdefs = $DB->get_records_select(
                     'question_dataset_definitions',
                     "type = '1'
@@ -300,64 +300,65 @@ class question_calculated_qtype extends question_type {
                     ORDER BY id DESC ", array($dataset->name, $question->category)
                 )) { // so there is at least one
                     $sharedatasetdef = array_shift($sharedatasetdefs);
-                    if ( $sharedatasetdef->options ==  $datasetdef->options ){// identical so use it
-                        $todo='useit' ;
-                        $datasetdef =$sharedatasetdef ;
+                    if ($sharedatasetdef->options ==  $datasetdef->options) {// identical so use it
+                        $todo = 'useit';
+                        $datasetdef = $sharedatasetdef;
                     } else { // different so create a private one
                         $datasetdef->category = 0;
-                        $todo='create' ;
+                        $todo = 'create';
                     }
-                }else { // no so create one
-                    $datasetdef->category =$question->category ;
-                    $todo='create' ;
+                } else { // no so create one
+                    $datasetdef->category = $question->category;
+                    $todo = 'create';
                 }
             }
-            if (  $todo=='create'){
-                $datasetdef->id = $DB->insert_record( 'question_dataset_definitions', $datasetdef);
+            if ($todo == 'create') {
+                $datasetdef->id = $DB->insert_record('question_dataset_definitions', $datasetdef);
             }
             // Create relation to the dataset:
             $questiondataset = new stdClass();
             $questiondataset->question = $question->id;
             $questiondataset->datasetdefinition = $datasetdef->id;
             $DB->insert_record('question_datasets', $questiondataset);
-            if ($todo=='create'){ // add the items
-                foreach ($dataset->datasetitem as $dataitem ){
+            if ($todo == 'create') {
+                // add the items
+                foreach ($dataset->datasetitem as $dataitem) {
                     $datasetitem = new stdClass();
-                    $datasetitem->definition=$datasetdef->id ;
-                    $datasetitem->itemnumber = $dataitem->itemnumber ;
-                    $datasetitem->value = $dataitem->value ;
+                    $datasetitem->definition = $datasetdef->id;
+                    $datasetitem->itemnumber = $dataitem->itemnumber;
+                    $datasetitem->value = $dataitem->value;
                     $DB->insert_record('question_dataset_items', $datasetitem);
                 }
             }
         }
     }
 
-    function restore_session_and_responses(&$question, &$state) {
+    public function restore_session_and_responses(&$question, &$state) {
         global $OUTPUT;
         if (!preg_match('~^dataset([0-9]+)[^-]*-(.*)$~',
-            $state->responses[''], $regs)) {
-                echo $OUTPUT->notification("Wrongly formatted raw response answer " .
-                    "{$state->responses['']}! Could not restore session for " .
-                    " question #{$question->id}.");
-                $state->options->datasetitem = 1;
-                $state->options->dataset = array();
-                $state->responses = array('' => '');
-                return false;
-            }
+                $state->responses[''], $regs)) {
+            echo $OUTPUT->notification("Wrongly formatted raw response answer " .
+                "{$state->responses['']}! Could not restore session for " .
+                " question #{$question->id}.");
+            $state->options->datasetitem = 1;
+            $state->options->dataset = array();
+            $state->responses = array('' => '');
+            return false;
+        }
 
         // Restore the chosen dataset
         $state->options->datasetitem = $regs[1];
         $state->options->dataset =
-            $this->pick_question_dataset($question,$state->options->datasetitem);
+            $this->pick_question_dataset($question, $state->options->datasetitem);
         $state->responses = array('' => $regs[2]);
         $virtualqtype = $this->get_virtual_qtype();
         return $virtualqtype->restore_session_and_responses($question, $state);
     }
 
-    function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
+    public function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
         // Find out how many datasets are available
-        global $CFG, $DB, $QTYPES, $OUTPUT;
-        if(!$maxnumber = (int)$DB->get_field_sql(
+        global $CFG, $DB, $OUTPUT;
+        if (!$maxnumber = (int)$DB->get_field_sql(
             "SELECT MIN(a.itemcount)
                FROM {question_dataset_definitions} a, {question_datasets} b
               WHERE b.question = ? AND a.id = b.datasetdefinition", array($question->id))) {
@@ -368,74 +369,80 @@ class question_calculated_qtype extends question_type {
                   FROM {question_datasets} d, {question_dataset_definitions} i
                  WHERE d.question = ? AND d.datasetdefinition = i.id AND i.category != 0";
 
-        if (!$question->options->synchronize || !$records = $DB->get_records_sql($sql, array($question->id))) {
-            $synchronize_calculated  =  false ;
-        }else {
+        if (!$question->options->synchronize ||
+                !$records = $DB->get_records_sql($sql, array($question->id))) {
+            $synchronizecalculated = false;
+        } else {
             // i.e records is true so test coherence
-            $coherence = true ;
-            $a = new stdClass() ;
-            $a->qid = $question->id ;
-            $a->qcat = $question->category ;
-            foreach($records as $def ){
-                if ($def->category != $question->category){
+            $coherence = true;
+            $a = new stdClass();
+            $a->qid = $question->id;
+            $a->qcat = $question->category;
+            foreach ($records as $def) {
+                if ($def->category != $question->category) {
                     $a->name = $def->name;
-                    $a->sharedcat = $def->category ;
-                    $coherence = false ;
+                    $a->sharedcat = $def->category;
+                    $coherence = false;
                     break;
                 }
             }
-            if(!$coherence){
-                echo $OUTPUT->notification(get_string('nocoherencequestionsdatyasetcategory','qtype_calculated',$a));
+            if (!$coherence) {
+                echo $OUTPUT->notification(get_string(
+                        'nocoherencequestionsdatyasetcategory', 'qtype_calculated', $a));
             }
-            $synchronize_calculated  = true ;
+            $synchronizecalculated = true;
         }
 
         // Choose a random dataset
         // maxnumber sould not be breater than 100
-        if ($maxnumber > CALCULATEDQUESTIONMAXITEMNUMBER ){
-            $maxnumber = CALCULATEDQUESTIONMAXITEMNUMBER ;
+        if ($maxnumber > self::MAX_DATASET_ITEMS) {
+            $maxnumber = self::MAX_DATASET_ITEMS;
         }
-        if ( $synchronize_calculated === false ) {
+        if ($synchronizecalculated === false) {
             $state->options->datasetitem = rand(1, $maxnumber);
-        }else{
-            $state->options->datasetitem = intval( $maxnumber * substr($attempt->timestart,-2) /100 ) ;
+        } else {
+            $state->options->datasetitem =
+                    intval($maxnumber * substr($attempt->timestart, -2) /100);
             if ($state->options->datasetitem < 1) {
-                $state->options->datasetitem =;
-            } else if ($state->options->datasetitem > $maxnumber){
-                $state->options->datasetitem = $maxnumber ;
+                $state->options->datasetitem = 1;
+            } else if ($state->options->datasetitem > $maxnumber) {
+                $state->options->datasetitem = $maxnumber;
             }
 
         };
         $state->options->dataset =
-            $this->pick_question_dataset($question,$state->options->datasetitem);
-        $virtualqtype = $this->get_virtual_qtype( );
+            $this->pick_question_dataset($question, $state->options->datasetitem);
+        $virtualqtype = $this->get_virtual_qtype();
         return $virtualqtype->create_session_and_responses($question, $state, $cmoptions, $attempt);
     }
 
-    function save_session_and_responses(&$question, &$state) {
+    public function save_session_and_responses(&$question, &$state) {
         global $DB;
-        $responses = 'dataset'.$state->options->datasetitem.'-' ;
+        $responses = 'dataset'.$state->options->datasetitem.'-';
         // regular numeric type
-        if(isset($state->responses['unit']) && isset($question->options->units[$state->responses['unit']])){
-            $responses .= $state->responses['answer'].'|||||'.$question->options->units[$state->responses['unit']]->unit;
-        }else if(isset($state->responses['unit'])){
-            $responses .= $state->responses['answer'].'|||||'.$state->responses['unit'] ;
-        }else {
-            $responses .= $state->responses['answer'].'|||||';
+        if (isset($state->responses['unit']) &&
+                isset($question->options->units[$state->responses['unit']])) {
+            $responses .= $state->responses['answer'] . '|||||' .
+                    $question->options->units[$state->responses['unit']]->unit;
+        } else if (isset($state->responses['unit'])) {
+            $responses .= $state->responses['answer'] . '|||||' .
+                    $state->responses['unit'];
+        } else {
+            $responses .= $state->responses['answer'] . '|||||';
         }
 
         // Set the legacy answer field
-        $DB->set_field('question_states', 'answer', $responses, array('id'=> $state->id));
+        $DB->set_field('question_states', 'answer', $responses, array('id' => $state->id));
 
         return true;
     }
 
-    function create_runtime_question($question, $form) {
+    public function create_runtime_question($question, $form) {
         $question = parent::create_runtime_question($question, $form);
         $question->options->answers = array();
         foreach ($form->answers as $key => $answer) {
             $a->answer              = trim($form->answer[$key]);
-            $a->fraction              = $form->fraction[$key];//new
+            $a->fraction            = $form->fraction[$key];//new
             $a->tolerance           = $form->tolerance[$key];
             $a->tolerancetype       = $form->tolerancetype[$key];
             $a->correctanswerlength = $form->correctanswerlength[$key];
@@ -446,58 +453,57 @@ class question_calculated_qtype extends question_type {
         return $question;
     }
 
-    function validate_form($form) {
+    public function validate_form($form) {
         switch($form->wizardpage) {
-        case 'question':
-            $calculatedmessages = array();
-            if (empty($form->name)) {
-                $calculatedmessages[] = get_string('missingname', 'qtype_calculated');
-            }
-            if (empty($form->questiontext)) {
-                $calculatedmessages[] = get_string('missingquestiontext', 'qtype_calculated');
-            }
-            // Verify formulas
-            foreach ($form->answers as $key => $answer) {
-                if ('' === trim($answer)) {
-                    $calculatedmessages[] =
-                        get_string('missingformula', 'qtype_calculated');
+            case 'question':
+                $calculatedmessages = array();
+                if (empty($form->name)) {
+                    $calculatedmessages[] = get_string('missingname', 'qtype_calculated');
+                }
+                if (empty($form->questiontext)) {
+                    $calculatedmessages[] = get_string('missingquestiontext', 'qtype_calculated');
                 }
-                if ($formulaerrors =
-                    qtype_calculated_find_formula_errors($answer)) {
+                // Verify formulas
+                foreach ($form->answers as $key => $answer) {
+                    if ('' === trim($answer)) {
+                        $calculatedmessages[] = get_string(
+                                'missingformula', 'qtype_calculated');
+                    }
+                    if ($formulaerrors = qtype_calculated_find_formula_errors($answer)) {
                         $calculatedmessages[] = $formulaerrors;
                     }
-                if (! isset($form->tolerance[$key])) {
-                    $form->tolerance[$key] = 0.0;
-                }
-                if (! is_numeric($form->tolerance[$key])) {
-                    $calculatedmessages[] =
-                        get_string('tolerancemustbenumeric', 'qtype_calculated');
+                    if (! isset($form->tolerance[$key])) {
+                        $form->tolerance[$key] = 0.0;
+                    }
+                    if (! is_numeric($form->tolerance[$key])) {
+                        $calculatedmessages[] = get_string(
+                                'tolerancemustbenumeric', 'qtype_calculated');
+                    }
                 }
-            }
 
-            if (!empty($calculatedmessages)) {
-                $errorstring = "The following errors were found:<br />";
-                foreach ($calculatedmessages as $msg) {
-                    $errorstring .= $msg . '<br />';
+                if (!empty($calculatedmessages)) {
+                    $errorstring = "The following errors were found:<br />";
+                    foreach ($calculatedmessages as $msg) {
+                        $errorstring .= $msg . '<br />';
+                    }
+                    print_error($errorstring);
                 }
-                print_error($errorstring);
-            }
 
-            break;
-        default:
-            return parent::validate_form($form);
-            break;
+                break;
+            default:
+                return parent::validate_form($form);
+                break;
         }
         return true;
     }
-    function finished_edit_wizard(&$form) {
+    public function finished_edit_wizard(&$form) {
         return isset($form->savechanges);
     }
-    function wizard_pages_number() {
+    public function wizardpagesnumber() {
         return 3;
     }
     // This gets called by editquestion.php after the standard question is saved
-    function print_next_wizard_page(&$question, &$form, $course) {
+    public function print_next_wizard_page(&$question, &$form, $course) {
         global $CFG, $USER, $SESSION, $COURSE;
 
         // Catch invalid navigation & reloads
@@ -507,45 +513,48 @@ class question_calculated_qtype extends question_type {
 
         // See where we're coming from
         switch($form->wizardpage) {
-        case 'question':
-            require("$CFG->dirroot/question/type/calculated/datasetdefinitions.php");
-            break;
-        case 'datasetdefinitions':
-        case 'datasetitems':
-            require("$CFG->dirroot/question/type/calculated/datasetitems.php");
-            break;
-        default:
-            print_error('invalidwizardpage', 'question');
-            break;
+            case 'question':
+                require("$CFG->dirroot/question/type/calculated/datasetdefinitions.php");
+                break;
+            case 'datasetdefinitions':
+            case 'datasetitems':
+                require("$CFG->dirroot/question/type/calculated/datasetitems.php");
+                break;
+            default:
+                print_error('invalidwizardpage', 'question');
+                break;
         }
     }
 
     // This gets called by question2.php after the standard question is saved
-    function &next_wizard_form($submiturl, $question, $wizardnow){
+    public function &next_wizard_form($submiturl, $question, $wizardnow) {
         global $CFG, $SESSION, $COURSE;
 
         // Catch invalid navigation & reloads
         if (empty($question->id) && empty($SESSION->calculated)) {
-            redirect('edit.php?courseid='.$COURSE->id, 'The page you are loading has expired. Cannot get next wizard form.', 3);
+            redirect('edit.php?courseid=' . $COURSE->id,
+                    'The page you are loading has expired. Cannot get next wizard form.', 3);
         }
-        if (empty($question->id)){
-            $question =& $SESSION->calculated->questionform;
+        if (empty($question->id)) {
+            $question = $SESSION->calculated->questionform;
         }
 
         // See where we're coming from
         switch($wizardnow) {
-        case 'datasetdefinitions':
-            require("$CFG->dirroot/question/type/calculated/datasetdefinitions_form.php");
-            $mform = new question_dataset_dependent_definitions_form("$submiturl?wizardnow=datasetdefinitions", $question);
-            break;
-        case 'datasetitems':
-            require("$CFG->dirroot/question/type/calculated/datasetitems_form.php");
-            $regenerate = optional_param('forceregeneration', 0, PARAM_BOOL);
-            $mform = new question_dataset_dependent_items_form("$submiturl?wizardnow=datasetitems", $question, $regenerate);
-            break;
-        default:
-            print_error('invalidwizardpage', 'question');
-            break;
+            case 'datasetdefinitions':
+                require("$CFG->dirroot/question/type/calculated/datasetdefinitions_form.php");
+                $mform = new question_dataset_dependent_definitions_form(
+                        "$submiturl?wizardnow=datasetdefinitions", $question);
+                break;
+            case 'datasetitems':
+                require("$CFG->dirroot/question/type/calculated/datasetitems_form.php");
+                $regenerate = optional_param('forceregeneration', 0, PARAM_BOOL);
+                $mform = new question_dataset_dependent_items_form(
+                        "$submiturl?wizardnow=datasetitems", $question, $regenerate);
+                break;
+            default:
+                print_error('invalidwizardpage', 'question');
+                break;
         }
 
         return $mform;
@@ -559,20 +568,23 @@ class question_calculated_qtype extends question_type {
      * @param object $question
      * @param string $wizardnow is '' for first page.
      */
-    function display_question_editing_page(&$mform, $question, $wizardnow){
-        global $OUTPUT ;
-        switch ($wizardnow){
-        case '':
-            //on first page default display is fine
-            parent::display_question_editing_page($mform, $question, $wizardnow);
-            return;
-            break;
-        case 'datasetdefinitions':
-            echo $OUTPUT->heading_with_help(get_string("choosedatasetproperties", "qtype_calculated"), 'questiondatasets', 'qtype_calculated');
-            break;
-        case 'datasetitems':
-            echo $OUTPUT->heading_with_help(get_string("editdatasets", "qtype_calculated"), 'questiondatasets', 'qtype_calculated');
-            break;
+    public function display_question_editing_page(&$mform, $question, $wizardnow) {
+        global $OUTPUT;
+        switch ($wizardnow) {
+            case '':
+                //on first page default display is fine
+                parent::display_question_editing_page($mform, $question, $wizardnow);
+                return;
+                break;
+            case 'datasetdefinitions':
+                echo $OUTPUT->heading_with_help(
+                        get_string('choosedatasetproperties', 'qtype_calculated'),
+                        'questiondatasets', 'qtype_calculated');
+                break;
+            case 'datasetitems':
+                echo $OUTPUT->heading_with_help(get_string('editdatasets', 'qtype_calculated'),
+                        'questiondatasets', 'qtype_calculated');
+                break;
         }
 
         $mform->display();
@@ -590,8 +602,9 @@ class question_calculated_qtype extends question_type {
      * @param object $form
      * @param int $questionfromid default = '0'
      */
-    function preparedatasets(&$form , $questionfromid='0'){
-        // the dataset names present in the edit_question_form and edit_calculated_form are retrieved
+    public function preparedatasets($form , $questionfromid = '0') {
+        // the dataset names present in the edit_question_form and edit_calculated_form
+        // are retrieved
         $possibledatasets = $this->find_dataset_names($form->questiontext);
         $mandatorydatasets = array();
         foreach ($form->answers as $answer) {
@@ -600,63 +613,66 @@ class question_calculated_qtype extends question_type {
         // if there are identical datasetdefs already saved in the original question.
         // either when editing a question or saving as new
         // they are retrieved using $questionfromid
-        if ($questionfromid!='0'){
-            $form->id = $questionfromid ;
+        if ($questionfromid != '0') {
+            $form->id = $questionfromid;
         }
         $datasets = array();
-        $key = 0 ;
+        $key = 0;
         // always prepare the mandatorydatasets present in the answers
         // the $options are not used here
         foreach ($mandatorydatasets as $datasetname) {
             if (!isset($datasets[$datasetname])) {
                 list($options, $selected) =
                     $this->dataset_options($form, $datasetname);
-                $datasets[$datasetname]='';
-                $form->dataset[$key]=$selected ;
+                $datasets[$datasetname] = '';
+                $form->dataset[$key] = $selected;
                 $key++;
             }
         }
         // do not prepare possibledatasets when creating a question
         // they will defined and stored with datasetdefinitions_form.php
         // the $options are not used here
-        if ($questionfromid!='0'){
+        if ($questionfromid != '0') {
 
             foreach ($possibledatasets as $datasetname) {
                 if (!isset($datasets[$datasetname])) {
                     list($options, $selected) =
-                        $this->dataset_options($form, $datasetname,false);
-                    $datasets[$datasetname]='';
-                    $form->dataset[$key]=$selected ;
+                        $this->dataset_options($form, $datasetname, false);
+                    $datasets[$datasetname] = '';
+                    $form->dataset[$key] = $selected;
                     $key++;
                 }
             }
         }
-        return $datasets ;
+        return $datasets;
     }
-    function addnamecategory(&$question){
+    public function addnamecategory(&$question) {
         global $DB;
         $categorydatasetdefs = $DB->get_records_sql(
             "SELECT  a.*
                FROM {question_datasets} b, {question_dataset_definitions} a
-              WHERE a.id = b.datasetdefinition AND a.type = '1' AND a.category != 0 AND b.question = ?
+              WHERE a.id = b.datasetdefinition
+                AND a.type = '1'
+                AND a.category != 0
+                AND b.question = ?
            ORDER BY a.name ", array($question->id));
-        $questionname = $question->name ;
+        $questionname = $question->name;
         $regs= array();
-        if(preg_match('~#\{([^[:space:]]*)#~',$questionname , $regs)){
+        if (preg_match('~#\{([^[:space:]]*)#~', $questionname , $regs)) {
             $questionname = str_replace($regs[0], '', $questionname);
         };
 
-        if (!empty($categorydatasetdefs)){ // there is at least one with the same name
-            $questionname  ="#".$questionname;
-            foreach($categorydatasetdefs as $def) {
-                if(strlen("{$def->name}")+strlen($questionname) < 250 ){
-                    $questionname = '{'.$def->name.'}'
-                        .$questionname;
+        if (!empty($categorydatasetdefs)) {
+            // there is at least one with the same name
+            $questionname = '#' . $questionname;
+            foreach ($categorydatasetdefs as $def) {
+                if (strlen($def->name) + strlen($questionname) < 250) {
+                    $questionname = '{' . $def->name . '}' . $questionname;
                 }
             }
-            $questionname ="#".$questionname;
+            $questionname = '#' . $questionname;
         }
-        $DB->set_field('question', 'name', $questionname, array("id" => $question->id));
+        $DB->set_field('question', 'name', $questionname, array('id' => $question->id));
     }
 
     /**
@@ -678,11 +694,11 @@ class question_calculated_qtype extends question_type {
      * @param int $course
      * @param PARAM_ALPHA $wizardnow should be added as we are coming from question2.php
      */
-    function save_question($question, $form) {
+    public function save_question($question, $form) {
         global $DB;
-        if ($this->wizard_pages_number() == 1 ){
+        if ($this->wizardpagesnumber() == 1) {
                 $question = parent::save_question($question, $form);
-            return $question ;
+            return $question;
         }
 
         $wizardnow =  optional_param('wizardnow', '', PARAM_ALPHA);
@@ -697,137 +713,141 @@ class question_calculated_qtype extends question_type {
 
         // See where we're coming from
         switch($wizardnow) {
-        case '' :
-        case 'question': // coming from the first page, creating the second
-            if (empty($form->id)) { // for a new question $form->id is empty
-                $question = parent::save_question($question, $form);
-                //prepare the datasets using default $questionfromid
-                $this->preparedatasets($form);
-                $form->id = $question->id;
-                $this->save_dataset_definitions($form);
-                if(isset($form->synchronize) && $form->synchronize == 2 ){
-                    $this->addnamecategory($question);
+            case '' :
+            case 'question': // coming from the first page, creating the second
+                if (empty($form->id)) { // for a new question $form->id is empty
+                    $question = parent::save_question($question, $form);
+                    //prepare the datasets using default $questionfromid
+                    $this->preparedatasets($form);
+                    $form->id = $question->id;
+                    $this->save_dataset_definitions($form);
+                    if (isset($form->synchronize) && $form->synchronize == 2) {
+                        $this->addnamecategory($question);
+                    }
+                } else if (!empty($form->makecopy)) {
+                    $questionfromid =  $form->id;
+                    $question = parent::save_question($question, $form);
+                    //prepare the datasets
+                    $this->preparedatasets($form, $questionfromid);
+                    $form->id = $question->id;
+                    $this->save_as_new_dataset_definitions($form, $questionfromid);
+                    if (isset($form->synchronize) && $form->synchronize == 2) {
+                        $this->addnamecategory($question);
+                    }
+                } else {
+                    // editing a question
+                    $question = parent::save_question($question, $form);
+                    //prepare the datasets
+                    $this->preparedatasets($form, $question->id);
+                    $form->id = $question->id;
+                    $this->save_dataset_definitions($form);
+                    if (isset($form->synchronize) && $form->synchronize == 2) {
+                        $this->addnamecategory($question);
+                    }
                 }
-            } else if (!empty($form->makecopy)){
-                $questionfromid =  $form->id ;
-                $question = parent::save_question($question, $form);
-                //prepare the datasets
-                $this->preparedatasets($form,$questionfromid);
-                $form->id = $question->id;
-                $this->save_as_new_dataset_definitions($form,$questionfromid );
-                if(isset($form->synchronize) && $form->synchronize == 2 ){
-                    $this->addnamecategory($question);
+                break;
+            case 'datasetdefinitions':
+                // calculated options
+                // it cannot go here without having done the first page
+                // so the question_calculated_options should exist
+                // only need to update the synchronize field
+                if (isset($form->synchronize)) {
+                    $optionssynchronize = $form->synchronize;
+                } else {
+                    $optionssynchronize = 0;
                 }
-            }  else {// editing a question
-                $question = parent::save_question($question, $form);
-                //prepare the datasets
-                $this->preparedatasets($form,$question->id);
-                $form->id = $question->id;
-                $this->save_dataset_definitions($form);
-                if(isset($form->synchronize) && $form->synchronize == 2 ){
+                $DB->set_field('question_calculated_options', 'synchronize', $optionssynchronize,
+                        array('question' => $question->id));
+                if (isset($form->synchronize) && $form->synchronize == 2) {
                     $this->addnamecategory($question);
                 }
-            }
-            break;
-        case 'datasetdefinitions':
-            // calculated options
-            // it cannot go here without having done the first page
-            // so the question_calculated_options should exist
-            // only need to update the synchronize field
-            if(isset($form->synchronize) ){
-                $options_synchronize = $form->synchronize ;
-            }else {
-                $options_synchronize = 0 ;
-            }
-            $DB->set_field('question_calculated_options', 'synchronize', $options_synchronize, array("question" => $question->id));
-            if(isset($form->synchronize) && $form->synchronize == 2 ){
-                $this->addnamecategory($question);
-            }
-
-            $this->save_dataset_definitions($form);
-            break;
-        case 'datasetitems':
-            $this->save_dataset_items($question, $form);
-            $this->save_question_calculated($question, $form);
-            break;
-        default:
-            print_error('invalidwizardpage', 'question');
-            break;
+
+                $this->save_dataset_definitions($form);
+                break;
+            case 'datasetitems':
+                $this->save_dataset_items($question, $form);
+                $this->save_question_calculated($question, $form);
+                break;
+            default:
+                print_error('invalidwizardpage', 'question');
+                break;
         }
         return $question;
     }
 
-    function delete_question($questionid, $contextid) {
+    public function delete_question($questionid, $contextid) {
         global $DB;
 
-        $DB->delete_records("question_calculated", array("question" => $questionid));
-        $DB->delete_records("question_calculated_options", array("question" => $questionid));
-        $DB->delete_records("question_numerical_units", array("question" => $questionid));
+        $DB->delete_records('question_calculated', array('question' => $questionid));
+        $DB->delete_records('question_calculated_options', array('question' => $questionid));
+        $DB->delete_records('question_numerical_units', array('question' => $questionid));
         if ($datasets = $DB->get_records('question_datasets', array('question' => $questionid))) {
             foreach ($datasets as $dataset) {
-                if (!$DB->get_records_select(
-                    'question_datasets',
-                    "question != ?
-                    AND datasetdefinition = ? ", array($questionid, $dataset->datasetdefinition))){
-                        $DB->delete_records('question_dataset_definitions', array('id' => $dataset->datasetdefinition));
-                        $DB->delete_records('question_dataset_items', array('definition' => $dataset->datasetdefinition));
-                    }
+                if (!$DB->get_records_select('question_datasets',
+                        "question != ? AND datasetdefinition = ? ",
+                        array($questionid, $dataset->datasetdefinition))) {
+                    $DB->delete_records('question_dataset_definitions',
+                            array('id' => $dataset->datasetdefinition));
+                    $DB->delete_records('question_dataset_items',
+                            array('definition' => $dataset->datasetdefinition));
+                }
             }
         }
-        $DB->delete_records("question_datasets", array("question" => $questionid));
+        $DB->delete_records('question_datasets', array('question' => $questionid));
 
         parent::delete_question($questionid, $contextid);
     }
 
-    function test_response(&$question, &$state, $answer) {
+    public function test_response(&$question, &$state, $answer) {
         $virtualqtype = $this->get_virtual_qtype();
         return $virtualqtype->test_response($question, $state, $answer);
 
     }
 
-    function compare_responses(&$question, $state, $teststate) {
+    public function compare_responses(&$question, $state, $teststate) {
 
         $virtualqtype = $this->get_virtual_qtype();
         return $virtualqtype->compare_responses($question, $state, $teststate);
     }
 
-    function convert_answers (&$question, &$state){
+    public function convert_answers (&$question, &$state) {
         foreach ($question->options->answers as $key => $answer) {
             $answer = fullclone($question->options->answers[$key]);
-            $question->options->answers[$key]->answer = $this->substitute_variables_and_eval($answer->answer,
+            $question->options->answers[$key]->answer =
+                    $this->substitute_variables_and_eval($answer->answer,
                 $state->options->dataset);
         }
     }
-    function convert_questiontext (&$question, &$state){
-        $tolerancemax =0.01;
-        $tolerancetypemax = 1 ;
-        $correctanswerlengthmax = 2 ;
-        $correctanswerformatmax = 1 ;
-        $tolerancemaxset = false ;
+    public function convert_questiontext (&$question, &$state) {
+        $tolerancemax = 0.01;
+        $tolerancetypemax = 1;
+        $correctanswerlengthmax = 2;
+        $correctanswerformatmax = 1;
+        $tolerancemaxset = false;
         foreach ($question->options->answers as $key => $answer) {
-            if($answer->fraction == 1.0 && !$tolerancemaxset){
+            if ($answer->fraction == 1.0 && !$tolerancemaxset) {
                 $tolerancemax = $answer->tolerance;
-                $tolerancetypemax = $answer->tolerancetype ;
+                $tolerancetypemax = $answer->tolerancetype;
                 $correctanswerlengthmax = $answer->correctanswerlength;
-                $correctanswerformatmax =$answer->correctanswerformat;
-                $tolerancemaxset = true ;
+                $correctanswerformatmax = $answer->correctanswerformat;
+                $tolerancemaxset = true;
             }
         }
-        $question->questiontext = $this->substitute_variables($question->questiontext, $state->options->dataset);
+        $question->questiontext = $this->substitute_variables($question->questiontext,
+                $state->options->dataset);
         //evaluate the equations i.e {=5+4)
-        $qtext = "";
-        $qtextremaining = $question->questiontext ;
-        while  (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
-            //  while  (preg_match('~\{=|%=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
+        $qtext = '';
+        $qtextremaining = $question->questiontext;
+        while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
             $qtextsplits = explode($regs1[0], $qtextremaining, 2);
-            $qtext =$qtext.$qtextsplits[0];
+            $qtext = $qtext . $qtextsplits[0];
             $qtextremaining = $qtextsplits[1];
             if (empty($regs1[1])) {
                 $str = '';
             } else {
-                if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){
-                    $str=$formulaerrors ;
-                }else {
+                if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
+                    $str = $formulaerrors;
+                } else {
                     eval('$str = '.$regs1[1].';');
                     $texteval= qtype_calculated_calculate_answer(
                         $str, $state->options->dataset, $tolerancemax,
@@ -835,97 +855,52 @@ class question_calculated_qtype extends question_type {
                         $correctanswerformatmax, '');
                     $str = $texteval->answer;
 
-                    ;
                 }
             }
-            $qtext = $qtext.$str ;
+            $qtext = $qtext.$str;
         }
-        $question->questiontext = $qtext.$qtextremaining ; // end replace equations
+        $question->questiontext = $qtext.$qtextremaining; // end replace equations
     }
 
-    function get_default_numerical_unit($question,$virtualqtype){
-        if($unit = $virtualqtype->get_default_numerical_unit($question)){
+    public function get_default_numerical_unit($question, $virtualqtype) {
+        if ($unit = $virtualqtype->get_default_numerical_unit($question)) {
             $unit = $unit->unit;
         } else {
             $unit = '';
         }
-        return $unit ;
+        return $unit;
 
     }
-    function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
+    public function print_question_formulation_and_controls($question, $state,
+            $cmoptions, $options) {
         // Substitute variables in questiontext before giving the data to the
         // virtual type for printing
         $virtualqtype = $this->get_virtual_qtype();
         // why $unit as it is not use
-        $unit = $this-> get_default_numerical_unit($question,$virtualqtype);
+        $unit = $this->get_default_numerical_unit($question, $virtualqtype);
         // We modify the question to look like a numerical question
         $numericalquestion = fullclone($question);
         $this->convert_answers($numericalquestion, $state);
         $this->convert_questiontext($numericalquestion, $state);
- /*        $tolerancemax =0.01;
-         $tolerancetypemax = 1 ;
-         $correctanswerlengthmax = 2 ;
-         $correctanswerformatmax = 1 ;
-         $tolerancemaxset = false ;
-        foreach ($numericalquestion->options->answers as $key => $answer) {
-             if($answer->fraction == 1.0 && !$tolerancemaxset){
-                $tolerancemax = $answer->tolerance;
-                $tolerancetypemax = $answer->tolerancetype ;
-                $correctanswerlengthmax = $answer->correctanswerlength;
-                $correctanswerformatmax =$answer->correctanswerformat;
-                $tolerancemaxset = true ;
-            }
-        }
 
-        $numericalquestion->questiontext = $this->substitute_variables(
-        $numericalquestion->questiontext, $state->options->dataset);
-        //evaluate the equations i.e {=5+4)
-        $qtext = "";
-        $qtextremaining = $numericalquestion->questiontext ;
-        while  (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
-      //  while  (preg_match('~\{=|%=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
-            $qtextsplits = explode($regs1[0], $qtextremaining, 2);
-            $qtext =$qtext.$qtextsplits[0];
-            $qtextremaining = $qtextsplits[1];
-            if (empty($regs1[1])) {
-                    $str = '';
-                } else {
-                    if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){
-                        $str=$formulaerrors ;
-                    }else {
-                       eval('$str = '.$regs1[1].';');
-                       $texteval= qtype_calculated_calculate_answer(
-                     $str, $state->options->dataset, $tolerancemax,
-                     $tolerancetypemax, $correctanswerlengthmax,
-                        $correctanswerformatmax, '');
-                        $str = $texteval->answer;
-
-                        ;
-                    }
-                }
-                $qtext = $qtext.$str ;
-        }
-        $numericalquestion->questiontext = $qtext.$qtextremaining ; // end replace equations
-  */
-
-        $virtualqtype->print_question_formulation_and_controls($numericalquestion, $state, $cmoptions, $options);
+        $virtualqtype->print_question_formulation_and_controls(
+                $numericalquestion, $state, $cmoptions, $options);
     }
-    function grade_responses(&$question, &$state, $cmoptions) {
+    public function grade_responses(&$question, &$state, $cmoptions) {
         // Forward the grading to the virtual qtype
         // We modify the question to look like a numerical question
         $numericalquestion = fullclone($question);
         foreach ($numericalquestion->options->answers as $key => $answer) {
-            $answer = $numericalquestion->options->answers[$key]->answer; // for PHP 4.x
-            $numericalquestion->options->answers[$key]->answer = $this->substitute_variables_and_eval($answer,
-                $state->options->dataset);
+            $answer = $numericalquestion->options->answers[$key]->answer;
+            $numericalquestion->options->answers[$key]->answer =
+                    $this->substitute_variables_and_eval($answer, $state->options->dataset);
         }
         $virtualqtype = $this->get_virtual_qtype();
-        return $virtualqtype->grade_responses($numericalquestion, $state, $cmoptions) ;
+        return $virtualqtype->grade_responses($numericalquestion, $state, $cmoptions);
     }
 
-
     // ULPGC ecastro
-    function check_response(&$question, &$state) {
+    public function check_response(&$question, &$state) {
         // Forward the checking to the virtual qtype
         // We modify the question to look like a numerical question
         $numericalquestion = clone($question);
@@ -938,11 +913,11 @@ class question_calculated_qtype extends question_type {
                 $state->options->dataset);
         }
         $virtualqtype = $this->get_virtual_qtype();
-        return $virtualqtype->check_response($numericalquestion, $state) ;
+        return $virtualqtype->check_response($numericalquestion, $state);
     }
 
     // ULPGC ecastro
-    function get_actual_response(&$question, &$state) {
+    public function get_actual_response(&$question, &$state) {
         // Substitute variables in questiontext before giving the data to the
         // virtual type
         $virtualqtype = $this->get_virtual_qtype();
@@ -968,43 +943,50 @@ class question_calculated_qtype extends question_type {
 
         $responses = $virtualqtype->get_actual_response($numericalquestion, $state);
 
-        foreach ($responses as $key=>$response){
+        foreach ($responses as $key => $response) {
             $responses[$key] = $correct.$response;
         }
 
         return $responses;
     }
 
-    function create_virtual_qtype() {
+    public function create_virtual_qtype() {
         global $CFG;
         require_once("$CFG->dirroot/question/type/numerical/questiontype.php");
         return new question_numerical_qtype();
     }
 
-    function supports_dataset_item_generation() {
+    public function supports_dataset_item_generation() {
         // Calcualted support generation of randomly distributed number data
         return true;
     }
-    function custom_generator_tools_part(&$mform, $idx, $j){
+    public function custom_generator_tools_part(&$mform, $idx, $j) {
 
         $minmaxgrp = array();
-        $minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_calculated'));
-        $minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_calculated'));
-        $mform->addGroup($minmaxgrp, 'minmaxgrp', get_string('minmax', 'qtype_calculated'), ' - ', false);
+        $minmaxgrp[] = $mform->createElement('text', "calcmin[$idx]",
+                get_string('calcmin', 'qtype_calculated'));
+        $minmaxgrp[] = $mform->createElement('text', "calcmax[$idx]",
+                get_string('calcmax', 'qtype_calculated'));
+        $mform->addGroup($minmaxgrp, 'minmaxgrp',
+                get_string('minmax', 'qtype_calculated'), ' - ', false);
         $mform->setType("calcmin[$idx]", PARAM_NUMBER);
         $mform->setType("calcmax[$idx]", PARAM_NUMBER);
 
         $precisionoptions = range(0, 10);
-        $mform->addElement('select', "calclength[$idx]", get_string('calclength', 'qtype_calculated'), $precisionoptions);
+        $mform->addElement('select', "calclength[$idx]",
+                get_string('calclength', 'qtype_calculated'), $precisionoptions);
 
-        $distriboptions = array('uniform' => get_string('uniform', 'qtype_calculated'), 'loguniform' => get_string('loguniform', 'qtype_calculated'));
-        $mform->addElement('select', "calcdistribution[$idx]", get_string('calcdistribution', 'qtype_calculated'), $distriboptions);
+        $distriboptions = array('uniform' => get_string('uniform', 'qtype_calculated'),
+                'loguniform' => get_string('loguniform', 'qtype_calculated'));
+        $mform->addElement('select', "calcdistribution[$idx]",
+                get_string('calcdistribution', 'qtype_calculated'), $distriboptions);
     }
 
-    function custom_generator_set_data($datasetdefs, $formdata){
+    public function custom_generator_set_data($datasetdefs, $formdata) {
         $idx = 1;
-        foreach ($datasetdefs as $datasetdef){
-            if (preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~', $datasetdef->options, $regs)) {
+        foreach ($datasetdefs as $datasetdef) {
+            if (preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~',
+                    $datasetdef->options, $regs)) {
                 $defid = "$datasetdef->type-$datasetdef->category-$datasetdef->name";
                 $formdata["calcdistribution[$idx]"] = $regs[1];
                 $formdata["calcmin[$idx]"] = $regs[2];
@@ -1016,64 +998,66 @@ class question_calculated_qtype extends question_type {
         return $formdata;
     }
 
-    function custom_generator_tools($datasetdef) {
+    public function custom_generator_tools($datasetdef) {
         global $OUTPUT;
         if (preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~',
-            $datasetdef->options, $regs)) {
-                $defid = "$datasetdef->type-$datasetdef->category-$datasetdef->name";
-                for ($i = 0 ; $i<10 ; ++$i) {
-                    $lengthoptions[$i] = get_string(($regs[1] == 'uniform'
-                        ? 'decimals'
-                        : 'significantfigures'), 'qtype_calculated', $i);
-                }
-                $menu1 = html_writer::select($lengthoptions, 'calclength[]', $regs[4], null);
-
-                $options = array('uniform' => get_string('uniformbit', 'qtype_calculated'), 'loguniform' => get_string('loguniformbit', 'qtype_calculated'));
-                $menu2 = html_writer::select($options, 'calcdistribution[]', $regs[1], null);
-                return '<input type="submit" onclick="'
-                    . "getElementById('addform').regenerateddefid.value='$defid'; return true;"
-                    .'" value="'. get_string('generatevalue', 'qtype_calculated') . '"/><br/>'
-                    . '<input type="text" size="3" name="calcmin[]" '
-                    . " value=\"$regs[2]\"/> &amp; <input name=\"calcmax[]\" "
-                    . ' type="text" size="3" value="' . $regs[3] .'"/> '
-                    . $menu1 . '<br/>'
-                    . $menu2;
-            } else {
-                return '';
-            }
+                $datasetdef->options, $regs)) {
+            $defid = "$datasetdef->type-$datasetdef->category-$datasetdef->name";
+            for ($i = 0; $i<10; ++$i) {
+                $lengthoptions[$i] = get_string(($regs[1] == 'uniform'
+                    ? 'decimals'
+                    : 'significantfigures'), 'qtype_calculated', $i);
+            }
+            $menu1 = html_writer::select($lengthoptions, 'calclength[]', $regs[4], null);
+
+            $options = array('uniform' => get_string('uniformbit', 'qtype_calculated'),
+                    'loguniform' => get_string('loguniformbit', 'qtype_calculated'));
+            $menu2 = html_writer::select($options, 'calcdistribution[]', $regs[1], null);
+            return '<input type="submit" onclick="'
+                . "getElementById('addform').regenerateddefid.value='$defid'; return true;"
+                .'" value="'. get_string('generatevalue', 'qtype_calculated') . '"/><br/>'
+                . '<input type="text" size="3" name="calcmin[]" '
+                . " value=\"$regs[2]\"/> &amp; <input name=\"calcmax[]\" "
+                . ' type="text" size="3" value="' . $regs[3] .'"/> '
+                . $menu1 . '<br/>'
+                . $menu2;
+        } else {
+            return '';
+        }
     }
 
 
-    function update_dataset_options($datasetdefs, $form) {
+    public function update_dataset_options($datasetdefs, $form) {
         global $OUTPUT;
-        // Do we have informatin about new options???
+        // Do we have information about new options???
         if (empty($form->definition) || empty($form->calcmin)
-            || empty($form->calcmax) || empty($form->calclength)
-            || empty($form->calcdistribution)) {
-                // I guess not
+                || empty($form->calcmax) || empty($form->calclength)
+                || empty($form->calcdistribution)) {
+            // I guess not
 
-            } else {
-                // Looks like we just could have some new information here
-                $uniquedefs = array_values(array_unique($form->definition));
-                foreach ($uniquedefs as $key => $defid) {
-                    if (isset($datasetdefs[$defid])
+        } else {
+            // Looks like we just could have some new information here
+            $uniquedefs = array_values(array_unique($form->definition));
+            foreach ($uniquedefs as $key => $defid) {
+                if (isset($datasetdefs[$defid])
                         && is_numeric($form->calcmin[$key+1])
                         && is_numeric($form->calcmax[$key+1])
                         && is_numeric($form->calclength[$key+1])) {
-                            switch     ($form->calcdistribution[$key+1]) {
-                            case 'uniform': case 'loguniform':
-                                $datasetdefs[$defid]->options =
-                                    $form->calcdistribution[$key+1] . ':'
-                                    . $form->calcmin[$key+1] . ':'
-                                    . $form->calcmax[$key+1] . ':'
-                                    . $form->calclength[$key+1];
-                                break;
-                            default:
-                                echo $OUTPUT->notification("Unexpected distribution ".$form->calcdistribution[$key+1]);
-                            }
-                        }
+                    switch     ($form->calcdistribution[$key+1]) {
+                        case 'uniform': case 'loguniform':
+                            $datasetdefs[$defid]->options =
+                                $form->calcdistribution[$key+1] . ':'
+                                . $form->calcmin[$key+1] . ':'
+                                . $form->calcmax[$key+1] . ':'
+                                . $form->calclength[$key+1];
+                            break;
+                        default:
+                            echo $OUTPUT->notification(
+                                    "Unexpected distribution ".$form->calcdistribution[$key+1]);
+                    }
                 }
             }
+        }
 
         // Look for empty options, on which we set default values
         foreach ($datasetdefs as $defid => $def) {
@@ -1084,7 +1068,7 @@ class question_calculated_qtype extends question_type {
         return $datasetdefs;
     }
 
-    function save_question_calculated($question, $fromform){
+    public function save_question_calculated($question, $fromform) {
         global $DB;
 
         foreach ($question->options->answers as $key => $answer) {
@@ -1105,42 +1089,39 @@ class question_calculated_qtype extends question_type {
      * i.e with the greatest id is used, the others are ignored but not deleted.
      * MDL-19210
      */
-    function get_database_dataset_items($definition){
+    public function get_database_dataset_items($definition) {
         global $CFG, $DB;
-        $databasedataitems = $DB->get_records_sql( // Use number as key!!
+        $databasedataitems = $DB->get_records_sql(// Use number as key!!
             " SELECT id , itemnumber, definition,  value
             FROM {question_dataset_items}
             WHERE definition = $definition order by id DESC ", array($definition));
         $dataitems = Array();
-        foreach($databasedataitems as $id => $dataitem  ){
-            if (!isset($dataitems[$dataitem->itemnumber])){
-                $dataitems[$dataitem->itemnumber] = $dataitem ;
-            }else {
-                // deleting the unused records could be added here
+        foreach ($databasedataitems as $id => $dataitem) {
+            if (!isset($dataitems[$dataitem->itemnumber])) {
+                $dataitems[$dataitem->itemnumber] = $dataitem;
             }
         }
         ksort($dataitems);
-        return $dataitems ;
+        return $dataitems;
     }
 
-    function save_dataset_items($question, $fromform){
+    public function save_dataset_items($question, $fromform) {
         global $CFG, $DB;
         // max datasets = 100 items
-        $max100 = CALCULATEDQUESTIONMAXITEMNUMBER ;
-        $synchronize = false ;
-        if(isset($fromform->nextpageparam["forceregeneration"])) {
-            $regenerate = $fromform->nextpageparam["forceregeneration"];
-        }else{
-            $regenerate = 0 ;
+        $max100 = self::MAX_DATASET_ITEMS;
+        $synchronize = false;
+        if (isset($fromform->nextpageparam['forceregeneration'])) {
+            $regenerate = $fromform->nextpageparam['forceregeneration'];
+        } else {
+            $regenerate = 0;
         }
         if (empty($question->options)) {
             $this->get_question_options($question);
         }
-        if(!empty($question->options->synchronize)){
-            $synchronize = true ;
+        if (!empty($question->options->synchronize)) {
+            $synchronize = true;
         }
 
-
         //get the old datasets for this question
         $datasetdefs = $this->get_dataset_definitions($question->id, array());
         // Handle generator options...
@@ -1149,11 +1130,11 @@ class question_calculated_qtype extends question_type {
         $maxnumber = -1;
         foreach ($datasetdefs as $defid => $datasetdef) {
             if (isset($datasetdef->id)
-                && $datasetdef->options != $olddatasetdefs[$defid]->options) {
-                    // Save the new value for options
-                    $DB->update_record('question_dataset_definitions', $datasetdef);
+                    && $datasetdef->options != $olddatasetdefs[$defid]->options) {
+                // Save the new value for options
+                $DB->update_record('question_dataset_definitions', $datasetdef);
 
-                }
+            }
             // Get maxnumber
             if ($maxnumber == -1 || $datasetdef->itemcount < $maxnumber) {
                 $maxnumber = $datasetdef->itemcount;
@@ -1161,15 +1142,15 @@ class question_calculated_qtype extends question_type {
         }
         // Handle adding and removing of dataset items
         $i = 1;
-        if ($maxnumber > CALCULATEDQUESTIONMAXITEMNUMBER ){
-            $maxnumber = CALCULATEDQUESTIONMAXITEMNUMBER ;
+        if ($maxnumber > self::MAX_DATASET_ITEMS) {
+            $maxnumber = self::MAX_DATASET_ITEMS;
         }
 
         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.
-            if ( $i > count($datasetdefs)*$maxnumber ) {
+            if ($i > count($datasetdefs)*$maxnumber) {
                 break;
             }
             $addeditem = new stdClass();
@@ -1188,22 +1169,23 @@ class question_calculated_qtype extends question_type {
             $i++;
         }
         if (isset($addeditem->itemnumber) && $maxnumber < $addeditem->itemnumber
-            && $addeditem->itemnumber < CALCULATEDQUESTIONMAXITEMNUMBER ){
-                $maxnumber = $addeditem->itemnumber;
-                foreach ($datasetdefs as $key => $newdef) {
-                    if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) {
-                        $newdef->itemcount = $maxnumber;
-                        // Save the new value for options
-                        $DB->update_record('question_dataset_definitions', $newdef);
-                    }
+                && $addeditem->itemnumber < self::MAX_DATASET_ITEMS) {
+            $maxnumber = $addeditem->itemnumber;
+            foreach ($datasetdefs as $key => $newdef) {
+                if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) {
+                    $newdef->itemcount = $maxnumber;
+                    // Save the new value for options
+                    $DB->update_record('question_dataset_definitions', $newdef);
                 }
             }
+        }
         // adding supplementary items
-        $numbertoadd =0;
-        if (isset($fromform->addbutton) && $fromform->selectadd > 0 && $maxnumber < CALCULATEDQUESTIONMAXITEMNUMBER ) {
-            $numbertoadd =$fromform->selectadd ;
-            if ( $max100 - $maxnumber < $numbertoadd ) {
-                $numbertoadd = $max100 - $maxnumber ;
+        $numbertoadd = 0;
+        if (isset($fromform->addbutton) && $fromform->selectadd > 0 &&
+                $maxnumber < self::MAX_DATASET_ITEMS) {
+            $numbertoadd = $fromform->selectadd;
+            if ($max100 - $maxnumber < $numbertoadd) {
+                $numbertoadd = $max100 - $maxnumber;
             }
             //add the other items.
             // Generate a new dataset item (or reuse an old one)
@@ -1211,33 +1193,39 @@ class question_calculated_qtype extends question_type {
                 // in case that for category datasets some new items has been added
                 // get actual values
                 // fix regenerate for this datadefs
-                $defregenerate = 0 ;
-                if($synchronize && !empty ($fromform->nextpageparam["datasetregenerate[$datasetdef->name"])) {
-                    $defregenerate = 1 ;
-                }else if(!$synchronize && (($regenerate == 1 && $datasetdef->category == 0) ||$regenerate == 2 )){
-                    $defregenerate = 1 ;
+                $defregenerate = 0;
+                if ($synchronize &&
+                        !empty ($fromform->nextpageparam["datasetregenerate[$datasetdef->name"])) {
+                    $defregenerate = 1;
+                } else if (!$synchronize &&
+                        (($regenerate == 1 && $datasetdef->category == 0) ||$regenerate == 2)) {
+                    $defregenerate = 1;
                 }
                 if (isset($datasetdef->id)) {
-                    $datasetdefs[$defid]->items = $this->get_database_dataset_items($datasetdef->id);
+                    $datasetdefs[$defid]->items =
+                            $this->get_database_dataset_items($datasetdef->id);
                 }
-                for ($numberadded =$maxnumber+1 ; $numberadded <= $maxnumber+$numbertoadd ; $numberadded++){
-                    if (isset($datasetdefs[$defid]->items[$numberadded])  ){
+                for ($numberadded = $maxnumber+1; $numberadded <= $maxnumber + $numbertoadd;
+                        $numberadded++) {
+                    if (isset($datasetdefs[$defid]->items[$numberadded])) {
                         // in case of regenerate it modifies the already existing record
-                        if ( $defregenerate  ) {
+                        if ($defregenerate) {
                             $datasetitem = new stdClass();
                             $datasetitem->id = $datasetdefs[$defid]->items[$numberadded]->id;
-                            $datasetitem->definition = $datasetdef->id ;
+                            $datasetitem->definition = $datasetdef->id;
                             $datasetitem->itemnumber = $numberadded;
-                            $datasetitem->value = $this->generate_dataset_item($datasetdef->options);
+                            $datasetitem->value =
+                                    $this->generate_dataset_item($datasetdef->options);
                             $DB->update_record('question_dataset_items', $datasetitem);
                         }
                         //if not regenerate do nothing as there is already a record
                     } else {
                         $datasetitem = new stdClass();
-                        $datasetitem->definition = $datasetdef->id ;
+                        $datasetitem->definition = $datasetdef->id;
                         $datasetitem->itemnumber = $numberadded;
                         if ($this->supports_dataset_item_generation()) {
-                            $datasetitem->value = $this->generate_dataset_item($datasetdef->options);
+                            $datasetitem->value =
+                                    $this->generate_dataset_item($datasetdef->options);
                         } else {
                             $datasetitem->value = '';
                         }
@@ -1245,7 +1233,7 @@ class question_calculated_qtype extends question_type {
                     }
                 }//for number added
             }// datasetsdefs end
-            $maxnumber += $numbertoadd ;
+            $maxnumber += $numbertoadd;
             foreach ($datasetdefs as $key => $newdef) {
                 if (isset($newdef->id) && $newdef->itemcount <= $maxnumber) {
                     $newdef->itemcount = $maxnumber;
@@ -1255,32 +1243,37 @@ class question_calculated_qtype extends question_type {
             }
         }
 
-        if (isset($fromform->deletebutton))  {
-            if(isset($fromform->selectdelete)) $newmaxnumber = $maxnumber-$fromform->selectdelete ;
-            else $newmaxnumber = $maxnumber-1 ;
-            if ($newmaxnumber < 0 ) $newmaxnumber = 0 ;
+        if (isset($fromform->deletebutton)) {
+            if (isset($fromform->selectdelete)) {
+                $newmaxnumber = $maxnumber-$fromform->selectdelete;
+            } else {
+                $newmaxnumber = $maxnumber-1;
+            }
+            if ($newmaxnumber < 0) {
+                $newmaxnumber = 0;
+            }
             foreach ($datasetdefs as $datasetdef) {
                 if ($datasetdef->itemcount == $maxnumber) {
-                    $datasetdef->itemcount= $newmaxnumber ;
+                    $datasetdef->itemcount= $newmaxnumber;
                     $DB->update_record('question_dataset_definitions', $datasetdef);
                 }
             }
         }
     }
-    function generate_dataset_item($options) {
+    public function generate_dataset_item($options) {
         if (!preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~',
-            $options, $regs)) {
-                // Unknown options...
-                return false;
-            }
+                $options, $regs)) {
+            // Unknown options...
+            return false;
+        }
         if ($regs[1] == 'uniform') {
             $nbr = $regs[2] + ($regs[3]-$regs[2])*mt_rand()/mt_getrandmax();
-            return sprintf("%.".$regs[4]."f",$nbr);
+            return sprintf("%.".$regs[4].'f', $nbr);
 
         } else if ($regs[1] == 'loguniform') {
             $log0 = log(abs($regs[2])); // It would have worked the other way to
             $nbr = exp($log0 + (log(abs($regs[3])) - $log0)*mt_rand()/mt_getrandmax());
-            return sprintf("%.".$regs[4]."f",$nbr);
+            return sprintf("%.".$regs[4].'f', $nbr);
 
         } else {
             print_error('disterror', 'question', '', $regs[1]);
@@ -1288,8 +1281,7 @@ class question_calculated_qtype extends question_type {
         return '';
     }
 
-    function comment_header($question) {
-        //$this->get_question_options($question);
+    public function comment_header($question) {
         $strheader = '';
         $delimiter = '';
 
@@ -1306,14 +1298,16 @@ class question_calculated_qtype extends question_type {
         return $strheader;
     }
 
-    function comment_on_datasetitems($qtypeobj, $questionid, $questiontext, $answers, $data, $number) {
-        global $DB, $QTYPES;
+    public function comment_on_datasetitems($qtypeobj, $questionid, $questiontext,
+            $answers, $data, $number) {
+        global $DB;
         $comment = new stdClass();
         $comment->stranswers = array();
-        $comment->outsidelimit = false ;
+        $comment->outsidelimit = false;
         $comment->answers = array();
-        /// Find a default unit:
-        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) {
+        // Find a default unit:
+        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units',
+                array('question' => $questionid, 'multiplier' => 1.0))) {
             $unit = $unit->unit;
         } else {
             $unit = '';
@@ -1324,52 +1318,57 @@ class question_calculated_qtype extends question_type {
         $strmax = get_string('max');
         $errors = '';
         $delimiter = ': ';
-        $virtualqtype =  $qtypeobj->get_virtual_qtype();//& $QTYPES['numerical'];
+        $virtualqtype =  $qtypeobj->get_virtual_qtype();
         foreach ($answers as $key => $answer) {
-            $formula = $this->substitute_variables($answer->answer,$data);
+            $formula = $this->substitute_variables($answer->answer, $data);
             $formattedanswer = qtype_calculated_calculate_answer(
                 $answer->answer, $data, $answer->tolerance,
                 $answer->tolerancetype, $answer->correctanswerlength,
                 $answer->correctanswerformat, $unit);
-            if ( $formula === '*'){
+            if ($formula === '*') {
                 $answer->min = ' ';
-                $formattedanswer->answer = $answer->answer ;
-            }else {
-                eval('$answer->answer = '.$formula.';') ;
+                $formattedanswer->answer = $answer->answer;
+            } else {
+                eval('$answer->answer = '.$formula.';');
                 $virtualqtype->get_tolerance_interval($answer);
             }
             if ($answer->min === '') {
                 // This should mean that something is wrong
                 $comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
-            } else if ($formula === '*'){
-                $comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/><br/>';
-            }else{
-                $comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'<br/>' ;
-                $correcttrue->correct = $formattedanswer->answer ;
-                $correcttrue->true = $answer->answer ;
-                if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
-                    $comment->outsidelimit = true ;
+            } else if ($formula === '*') {
+                $comment->stranswers[$key] = $formula . ' = ' .
+                        get_string('anyvalue', 'qtype_calculated') . '<br/><br/><br/>';
+            } else {
+                $comment->stranswers[$key] = $formula . ' = ' . $formattedanswer->answer . '<br/>';
+                $correcttrue->correct = $formattedanswer->answer;
+                $correcttrue->true = $answer->answer;
+                if ($formattedanswer->answer < $answer->min ||
+                        $formattedanswer->answer > $answer->max) {
+                    $comment->outsidelimit = true;
                     $comment->answers[$key] = $key;
-                    $comment->stranswers[$key] .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
-                }else {
-                    $comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
+                    $comment->stranswers[$key] .=
+                            get_string('trueansweroutsidelimits', 'qtype_calculated', $correcttrue);
+                } else {
+                    $comment->stranswers[$key] .=
+                            get_string('trueanswerinsidelimits', 'qtype_calculated', $correcttrue);
                 }
-                $comment->stranswers[$key] .='<br/>';
-                $comment->stranswers[$key] .= $strmin.$delimiter.$answer->min.' --- ';
-                $comment->stranswers[$key] .= $strmax.$delimiter.$answer->max;
-                $comment->stranswers[$key] .='';
+                $comment->stranswers[$key] .= '<br/>';
+                $comment->stranswers[$key] .= $strmin . $delimiter . $answer->min . ' --- ';
+                $comment->stranswers[$key] .= $strmax . $delimiter . $answer->max;
             }
         }
         return fullclone($comment);
     }
-    function multichoice_comment_on_datasetitems($questionid, $questiontext, $answers,$data, $number) {
+    public function multichoice_comment_on_datasetitems($questionid, $questiontext,
+            $answers, $data, $number) {
         global $DB;
         $comment = new stdClass();
         $comment->stranswers = array();
-        $comment->outsidelimit = false ;
+        $comment->outsidelimit = false;
         $comment->answers = array();
-        /// Find a default unit:
-        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units', array('question'=> $questionid, 'multiplier' => 1.0))) {
+        // Find a default unit:
+        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units',
+                array('question' => $questionid, 'multiplier' => 1.0))) {
             $unit = $unit->unit;
         } else {
             $unit = '';
@@ -1383,18 +1382,18 @@ class question_calculated_qtype extends question_type {
         foreach ($answers as $key => $answer) {
             $answer->answer = $this->substitute_variables($answer->answer, $data);
             //evaluate the equations i.e {=5+4)
-            $qtext = "";
-            $qtextremaining = $answer->answer ;
-            while  (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
+            $qtext = '';
+            $qtextremaining = $answer->answer;
+            while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
                 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
-                $qtext =$qtext.$qtextsplits[0];
+                $qtext = $qtext.$qtextsplits[0];
                 $qtextremaining = $qtextsplits[1];
                 if (empty($regs1[1])) {
                     $str = '';
                 } else {
-                    if( $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])){
-                        $str=$formulaerrors ;
-                    }else {
+                    if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
+                        $str = $formulaerrors;
+                    } else {
                         eval('$str = '.$regs1[1].';');
 
                         $texteval= qtype_calculated_calculate_answer(
@@ -1405,62 +1404,29 @@ class question_calculated_qtype extends question_type {
 
                     }
                 }
-                $qtext = $qtext.$str ;
+                $qtext = $qtext.$str;
             }
-            $answer->answer = $qtext.$qtextremaining ; ;
-            $comment->stranswers[$key]= $answer->answer ;
-
+            $answer->answer = $qtext.$qtextremaining;;
+            $comment->stranswers[$key]= $answer->answer;
 
-          /*  $formula = $this->substitute_variables($answer->answer,$data);
-            $formattedanswer = qtype_calculated_calculate_answer(
-                    $answer->answer, $data, $answer->tolerance,
-                    $answer->tolerancetype, $answer->correctanswerlength,
-                    $answer->correctanswerformat, $unit);
-                    if ( $formula === '*'){
-                        $answer->min = ' ';
-                        $formattedanswer->answer = $answer->answer ;
-                    }else {
-                        eval('$answer->answer = '.$formula.';') ;
-                        $virtualqtype->get_tolerance_interval($answer);
-                    }
-            if ($answer->min === '') {
-                // This should mean that something is wrong
-                $comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
-            } else if ($formula === '*'){
-                $comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/><br/>';
-            }else{
-                $comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'<br/>' ;
-                $comment->stranswers[$key] .= $strmin. $delimiter.$answer->min.'---';
-                $comment->stranswers[$key] .= $strmax.$delimiter.$answer->max;
-                $comment->stranswers[$key] .='<br/>';
-                $correcttrue->correct = $formattedanswer->answer ;
-                $correcttrue->true = $answer->answer ;
-                if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
-                    $comment->outsidelimit = true ;
-                    $comment->answers[$key] = $key;
-                    $comment->stranswers[$key] .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
-                }else {
-                    $comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
-                }
-                $comment->stranswers[$key] .='';
-          }*/
         }
         return fullclone($comment);
     }
 
-    function tolerance_types() {
-        return array('1'  => get_string('relative', 'qtype_numerical'),
-            '2'  => get_string('nominal', 'qtype_numerical'),
-            '3'  => get_string('geometric', 'qtype_numerical'));
+    public function tolerance_types() {
+        return array('1' => get_string('relative', 'qtype_numerical'),
+            '2' => get_string('nominal', 'qtype_numerical'),
+            '3' => get_string('geometric', 'qtype_numerical'));
     }
 
-    function dataset_options($form, $name, $mandatory=true,$renameabledatasets=false) {
+    public function dataset_options($form, $name, $mandatory = true,
+            $renameabledatasets = false) {
         // Takes datasets from the parent implementation but
         // filters options that are currently not accepted by calculated
         // It also determines a default selection...
-        //$renameabledatasets not implemented anmywhere
-        list($options, $selected) = $this->dataset_options_from_database($form, $name,'','qtype_calculated');
-        //  list($options, $selected) = $this->dataset_optionsa($form, $name);
+        // $renameabledatasets not implemented anmywhere
+        list($options, $selected) = $this->dataset_options_from_database(
+                $form, $name, '', 'qtype_calculated');
 
         foreach ($options as $key => $whatever) {
             if (!preg_match('~^1-~', $key) && $key != '0') {
@@ -1468,66 +1434,68 @@ class question_calculated_qtype extends question_type {
             }
         }
         if (!$selected) {
-            if ($mandatory){
+            if ($mandatory) {
                 $selected =  "1-0-$name"; // Default
-            }else {
-                $selected = "0"; // Default
+            } else {
+                $selected = '0'; // Default
             }
         }
         return array($options, $selected);
     }
 
-    function construct_dataset_menus($form, $mandatorydatasets,
-        $optionaldatasets) {
-            global $OUTPUT;
-            $datasetmenus = array();
-            foreach ($mandatorydatasets as $datasetname) {
-                if (!isset($datasetmenus[$datasetname])) {
-                    list($options, $selected) =
-                        $this->dataset_options($form, $datasetname);
-                    unset($options['0']); // Mandatory...
-                    $datasetmenus[$datasetname] = html_writer::select($options, 'dataset[]', $selected, null);
-                }
+    public function construct_dataset_menus($form, $mandatorydatasets,
+            $optionaldatasets) {
+        global $OUTPUT;
+        $datasetmenus = array();
+        foreach ($mandatorydatasets as $datasetname) {
+            if (!isset($datasetmenus[$datasetname])) {
+                list($options, $selected) =
+                    $this->dataset_options($form, $datasetname);
+                unset($options['0']); // Mandatory...
+                $datasetmenus[$datasetname] = html_writer::select(
+                        $options, 'dataset[]', $selected, null);
             }
-            foreach ($optionaldatasets as $datasetname) {
-                if (!isset($datasetmenus[$datasetname])) {
-                    list($options, $selected) =
-                        $this->dataset_options($form, $datasetname);
-                    $datasetmenus[$datasetname] = html_writer::select($options, 'dataset[]', $selected, null);
-                }
+        }
+        foreach ($optionaldatasets as $datasetname) {
+            if (!isset($datasetmenus[$datasetname])) {
+                list($options, $selected) =
+                    $this->dataset_options($form, $datasetname);
+                $datasetmenus[$datasetname] = html_writer::select(
+                        $options, 'dataset[]', $selected, null);
             }
-            return $datasetmenus;
         }
+        return $datasetmenus;
+    }
 
-    function print_question_grading_details(&$question, &$state, &$cmoptions, &$options) {
+    public function print_question_grading_details($question, $state, $cmoptions, $options) {
         $virtualqtype = $this->get_virtual_qtype();
-        $virtualqtype->print_question_grading_details($question, $state, $cmoptions, $options) ;
+        $virtualqtype->print_question_grading_details($question, $state, $cmoptions, $options);
     }
 
-    function get_correct_responses(&$question, &$state) {
+    public function get_correct_responses($question, $state) {
         // virtual type for printing
         $virtualqtype = $this->get_virtual_qtype();
-        $unit = $this->get_default_numerical_unit($question,$virtualqtype);
+        $unit = $this->get_default_numerical_unit($question, $virtualqtype);
         // We modify the question to look like a numerical question
         $this->convert_answers($question, $state);
-        return $virtualqtype->get_correct_responses($question, $state) ;
+        return $virtualqtype->get_correct_responses($question, $state);
     }
 
-    function substitute_variables($str, $dataset) {
-        global $OUTPUT ;
+    public function substitute_variables($str, $dataset) {
+        global $OUTPUT;
         //  testing for wrong numerical values
         // all calculations used this function so testing here should be OK
 
         foreach ($dataset as $name => $value) {
-            $val = $value ;
-            if(! is_numeric($val)){
+            $val = $value;
+            if (! is_numeric($val)) {
                 $a = new stdClass();
-                $a->name = '{'.$name.'}' ;
-                $a->value = $value ;
-                echo $OUTPUT->notification(get_string('notvalidnumber','qtype_calculated',$a));
-                $val = 1.0 ;
+                $a->name = '{'.$name.'}';
+                $a->value = $value;
+                echo $OUTPUT->notification(get_string('notvalidnumber', 'qtype_calculated', $a));
+                $val = 1.0;
             }
-            if($val < 0 ){
+            if ($val < 0) {
                 $str = str_replace('{'.$name.'}', '('.$val.')', $str);
             } else {
                 $str = str_replace('{'.$name.'}', $val, $str);
@@ -1535,8 +1503,8 @@ class question_calculated_qtype extends question_type {
         }
         return $str;
     }
-    function evaluate_equations($str, $dataset){
-        $formula = $this->substitute_variables($str, $dataset) ;
+    public function evaluate_equations($str, $dataset) {
+        $formula = $this->substitute_variables($str, $dataset);
         if ($error = qtype_calculated_find_formula_errors($formula)) {
             return $error;
         }
@@ -1544,15 +1512,15 @@ class question_calculated_qtype extends question_type {
     }
 
 
-    function substitute_variables_and_eval($str, $dataset) {
-        $formula = $this->substitute_variables($str, $dataset) ;
+    public function substitute_variables_and_eval($str, $dataset) {
+        $formula = $this->substitute_variables($str, $dataset);
         if ($error = qtype_calculated_find_formula_errors($formula)) {
             return $error;
         }
-        /// Calculate the correct answer
+        // Calculate the correct answer
         if (empty($formula)) {
             $str = '';
-        } else if ($formula === '*'){
+        } else if ($formula === '*') {
             $str = '*';
         } else {
             eval('$str = '.$formula.';');
@@ -1560,7 +1528,7 @@ class question_calculated_qtype extends question_type {
         return $str;
     }
 
-    function get_dataset_definitions($questionid, $newdatasets) {
+    public function get_dataset_definitions($questionid, $newdatasets) {
         global $DB;
         //get the existing datasets for this question
         $datasetdefs = array();
@@ -1596,7 +1564,7 @@ class question_calculated_qtype extends question_type {
         return $datasetdefs;
     }
 
-    function save_dataset_definitions($form) {
+    public function save_dataset_definitions($form) {
         global $DB;
         // save synchronize
 
@@ -1612,10 +1580,14 @@ class question_calculated_qtype extends question_type {
             if (isset($datasetdef->id)) {
                 if (!isset($tmpdatasets[$defid])) {
                     // This dataset is not used any more, delete it
-                    $DB->delete_records('question_datasets', array('question' => $form->id, 'datasetdefinition' => $datasetdef->id));
-                    if ($datasetdef->category == 0) { // Question local dataset
-                        $DB->delete_records('question_dataset_definitions', array('id' => $datasetdef->id));
-                        $DB->delete_records('question_dataset_items', array('definition' => $datasetdef->id));
+                    $DB->delete_records('question_datasets',
+                            array('question' => $form->id, 'datasetdefinition' => $datasetdef->id));
+                    if ($datasetdef->category == 0) {
+                        // Question local dataset
+                        $DB->delete_records('question_dataset_definitions',
+                                array('id' => $datasetdef->id));
+                        $DB->delete_records('question_dataset_items',
+                                array('definition' => $datasetdef->id));
                     }
                 }
                 // This has already been saved or just got deleted
@@ -1632,14 +1604,14 @@ class question_calculated_qtype extends question_type {
                 // can manage to automatically take care of
                 // some possible realtime concurrence
                 if ($olderdatasetdefs = $DB->get_records_select('question_dataset_definitions',
-                         "type = ?
-                      AND name = ?
-                      AND category = ?
-                      AND id < ?
-                    ORDER BY id DESC", array($datasetdef->type, $datasetdef->name, $datasetdef->category, $datasetdef->id))) {
+                        'type = ? AND name = ? AND category = ? AND id < ?
+                        ORDER BY id DESC',
+                        array($datasetdef->type, $datasetdef->name,
+                                $datasetdef->category, $datasetdef->id))) {
 
                     while ($olderdatasetdef = array_shift($olderdatasetdefs)) {
-                        $DB->delete_records('question_dataset_definitions', array('id' => $datasetdef->id));
+                        $DB->delete_records('question_dataset_definitions',
+                                array('id' => $datasetdef->id));
                         $datasetdef = $olderdatasetdef;
                     }
                 }
@@ -1657,21 +1629,24 @@ class question_calculated_qtype extends question_type {
         // to datasets in other categories:
         if (!empty($datasetdefinitions)) {
             foreach ($datasetdefinitions as $def) {
-                $DB->delete_records('question_datasets', array('question' => $form->id, 'datasetdefinition' => $def->id));
+                $DB->delete_records('question_datasets',
+                        array('question' => $form->id, 'datasetdefinition' => $def->id));
 
                 if ($def->category == 0) { // Question local dataset
-                    $DB->delete_records('question_dataset_definitions', array('id' => $def->id));
-                    $DB->delete_records('question_dataset_items', array('definition' => $def->id));
+                    $DB->delete_records('question_dataset_definitions',
+                            array('id' => $def->id));
+                    $DB->delete_records('question_dataset_items',
+                            array('definition' => $def->id));
                 }
             }
         }
     }
-    /** This function create a copy of the datasets ( definition and dataitems)
+    /** This function create a copy of the datasets (definition and dataitems)
      * from the preceding question if they remain in the new question
      * otherwise its create the datasets that have been added as in the
      * save_dataset_definitions()
      */
-    function save_as_new_dataset_definitions($form, $initialid) {
+    public function save_as_new_dataset_definitions($form, $initialid) {
         global $CFG, $DB;
         // Get the datasets from the intial question
         $datasetdefinitions = $this->get_dataset_definitions($initialid, $form->dataset);
@@ -1689,25 +1664,26 @@ class question_calculated_qtype extends question_type {
                 }
                 // create a copy but not for category one
                 if (0 == $datasetdef->category) {
-                    $olddatasetid = $datasetdef->id ;
-                    $olditemcount = $datasetdef->itemcount ;
-                    $datasetdef->itemcount =0;
-                    $datasetdef->id = $DB->insert_record('question_dataset_definitions', $datasetdef);
+                    $olddatasetid = $datasetdef->id;
+                    $olditemcount = $datasetdef->itemcount;
+                    $datasetdef->itemcount = 0;
+                    $datasetdef->id = $DB->insert_record('question_dataset_definitions',
+                            $datasetdef);
                     //copy the dataitems
                     $olditems = $this->get_database_dataset_items($olddatasetid);
-                    if (count($olditems) > 0 ) {
+                    if (count($olditems) > 0) {
                         $itemcount = 0;
-                        foreach($olditems as $item ){
+                        foreach ($olditems as $item) {
                             $item->definition = $datasetdef->id;
                             $DB->insert_record('question_dataset_items', $item);
                             $itemcount++;
                         }
                         //update item count to olditemcount if
                         // at least this number of items has been recover from the database
-                        if( $olditemcount <= $itemcount ) {
+                        if ($olditemcount <= $itemcount) {
                             $datasetdef->itemcount = $olditemcount;
                         } else {
-                            $datasetdef->itemcount = $itemcount ;
+                            $datasetdef->itemcount = $itemcount;
                         }
                         $DB->update_record('question_dataset_definitions', $datasetdef);
                     } // end of  copy the dataitems
@@ -1730,19 +1706,18 @@ class question_calculated_qtype extends question_type {
                 // By first creating the datasetdefinition above we
                 // can manage to automatically take care of
                 // some possible realtime concurrence
-                if ($olderdatasetdefs = $DB->get_records_select(
-                    'question_dataset_definitions',
-                    "type = ?
-                    AND name = ?
-                    AND category = ?
-                    AND id < ?
-                    ORDER BY id DESC", array($datasetdef->type, $datasetdef->name, $datasetdef->category, $datasetdef->id))) {
+                if ($olderdatasetdefs = $DB->get_records_select('question_dataset_definitions',
+                        "type = ? AND name = ? AND category = ? AND id < ?
+                        ORDER BY id DESC",
+                        array($datasetdef->type, $datasetdef->name,
+                                $datasetdef->category, $datasetdef->id))) {
 
-                        while ($olderdatasetdef = array_shift($olderdatasetdefs)) {
-                            $DB->delete_records('question_dataset_definitions', array('id' => $datasetdef->id));
-                            $datasetdef = $olderdatasetdef;
-                        }
+                    while ($olderdatasetdef = array_shift($olderdatasetdefs)) {
+                        $DB->delete_records('question_dataset_definitions',
+                                array('id' => $datasetdef->id));
+                        $datasetdef = $olderdatasetdef;
                     }
+                }
             }
 
             // Create relation to this dataset:
@@ -1757,56 +1732,63 @@ class question_calculated_qtype extends question_type {
         // to datasets in other categories:
         if (!empty($datasetdefinitions)) {
             foreach ($datasetdefinitions as $def) {
-                $DB->delete_records('question_datasets', array('question' => $form->id, 'datasetdefinition' => $def->id));
+                $DB->delete_records('question_datasets',
+                        array('question' => $form->id, 'datasetdefinition' => $def->id));
 
                 if ($def->category == 0) { // Question local dataset
-                    $DB->delete_records('question_dataset_definitions', array('id' => $def->id));
-                    $DB->delete_records('question_dataset_items', array('definition' => $def->id));
+                    $DB->delete_records('question_dataset_definitions',
+                            array('id' => $def->id));
+                    $DB->delete_records('question_dataset_items',
+                            array('definition' => $def->id));
                 }
             }
         }
     }
 
-    /// Dataset functionality
-    function pick_question_dataset($question, $datasetitem) {
+    // Dataset functionality
+    public function pick_question_dataset($question, $datasetitem) {
         // Select a dataset in the following format:
         // An array indexed by the variable names (d.name) pointing to the value
         // to be substituted
         global $CFG, $DB;
         if (!$dataitems = $DB->get_records_sql(
-            "SELECT i.id, d.name, i.value
-               FROM {question_dataset_definitions} d, {question_dataset_items} i, {question_datasets} q
-              WHERE q.question = ? AND q.datasetdefinition = d.id AND d.id = i.definition AND i.itemnumber = ?
-           ORDER by i.id DESC ", array($question->id, $datasetitem))) {
-           $a = new stdClass();
-           $a->id = $question->id;
-           $a->item = $datasetitem ;
-            print_error('cannotgetdsfordependent', 'question', '', $a );
+                "SELECT i.id, d.name, i.value
+                   FROM {question_dataset_definitions} d,
+                        {question_dataset_items} i,
+                        {question_datasets} q
+                  WHERE q.question = ?
+                    AND q.datasetdefinition = d.id
+                    AND d.id = i.definition
+                    AND i.itemnumber = ?
+               ORDER BY i.id DESC ", array($question->id, $datasetitem))) {
+            $a = new stdClass();
+            $a->id = $question->id;
+            $a->item = $datasetitem;
+            print_error('cannotgetdsfordependent', 'question', '', $a);
         }
         $dataset = Array();
-        foreach($dataitems as $id => $dataitem  ){
-            if (!isset($dataset[$dataitem->name])){
-                $dataset[$dataitem->name] = $dataitem->value ;
-            }else {
-                // deleting the unused records could be added here
+        foreach ($dataitems as $id => $dataitem) {
+            if (!isset($dataset[$dataitem->name])) {
+                $dataset[$dataitem->name] = $dataitem->value;
             }
         }
         return $dataset;
     }
 
-    function dataset_options_from_database($form, $name,$prefix='',$langfile='qtype_calculated') {
+    public function dataset_options_from_database($form, $name, $prefix = '',
+            $langfile = 'qtype_calculated') {
         global $CFG, $DB;
-        $type = 1 ; // only type = 1 (i.e. old 'LITERAL') has ever been used
+        $type = 1; // only type = 1 (i.e. old 'LITERAL') has ever been used
 
         // First options - it is not a dataset...
         $options['0'] = get_string($prefix.'nodataset', $langfile);
         // new question no local
-        if (!isset($form->id) || $form->id == 0 ){
+        if (!isset($form->id) || $form->id == 0{
             $key = "$type-0-$name";
             $options[$key] = get_string($prefix."newlocal$type", $langfile);
             $currentdatasetdef = new stdClass();
             $currentdatasetdef->type = '0';
-        }else {
+        } else {
 
             // Construct question local options
             $sql = "SELECT a.*
@@ -1818,11 +1800,11 @@ class question_calculated_qtype extends question_type {
             }
             $key = "$type-0-$name";
             if ($currentdatasetdef->type == $type
-                and $currentdatasetdef->category == 0) {
-                    $options[$key] = get_string($prefix."keptlocal$type", $langfile);
-                } else {
-                    $options[$key] = get_string($prefix."newlocal$type", $langfile);
-                }
+                    and $currentdatasetdef->category == 0) {
+                $options[$key] = get_string($prefix."keptlocal$type", $langfile);
+            } else {
+                $options[$key] = get_string($prefix."newlocal$type", $langfile);
+            }
         }
         // Construct question category options
         $categorydatasetdefs = $DB->get_records_sql(
@@ -1833,10 +1815,12 @@ class question_calculated_qtype extends question_type {
             AND a.type = '1'
             AND a.category = ?
             AND a.name = ?", array($form->category, $name));
-        $type = 1 ;
+        $type = 1;
         $key = "$type-$form->category-$name";
-        if (!empty($categorydatasetdefs)){ // there is at least one with the same name
-            if (isset($form->id) && isset($categorydatasetdefs[$form->id])) {// it is already used by this question
+        if (!empty($categorydatasetdefs)) {
+            // there is at least one with the same name
+            if (isset($form->id) && isset($categorydatasetdefs[$form->id])) {
+                // it is already used by this question
                 $options[$key] = get_string($prefix."keptcategory$type", $langfile);
             } else {
                 $options[$key] = get_string($prefix."existingcategory$type", $langfile);
@@ -1850,9 +1834,9 @@ class question_calculated_qtype extends question_type {
             : '');
     }
 
-    function find_dataset_names($text) {
-        /// Returns the possible dataset names found in the text as an array
-        /// The array has the dataset name for both key and value
+    public function find_dataset_names($text) {
+        // Returns the possible dataset names found in the text as an array
+        // The array has the dataset name for both key and value
         $datasetnames = array();
         while (preg_match('~\\{([[:alpha:]][^>} <{"\']*)\\}~', $text, $regs)) {
             $datasetnames[$regs[1]] = $regs[1];
@@ -1866,21 +1850,23 @@ class question_calculated_qtype extends question_type {
      * wild cards that is added as a comment displayed when a wild card with
      * the same name is displayed in datasetdefinitions_form.php
      */
-    function get_dataset_definitions_category($form) {
+    public function get_dataset_definitions_category($form) {
         global $CFG, $DB;
         $datasetdefs = array();
         $lnamemax = 30;
         if (!empty($form->category)) {
-            $sql = "SELECT i.*,d.*
+            $sql = "SELECT i.*, d.*
                       FROM {question_datasets} d, {question_dataset_definitions} i
                      WHERE i.id = d.datasetdefinition AND i.category = ?";
             if ($records = $DB->get_records_sql($sql, array($form->category))) {
                 foreach ($records as $r) {
-                    if ( !isset ($datasetdefs["$r->name"])) $datasetdefs["$r->name"] = $r->itemcount;
+                    if (!isset ($datasetdefs["$r->name"])) {
+                        $datasetdefs["$r->name"] = $r->itemcount;
+                    }
                 }
             }
         }
-        return  $datasetdefs ;
+        return $datasetdefs;
     }
 
     /**
@@ -1890,20 +1876,20 @@ class question_calculated_qtype extends question_type {
      * This table is intended to be add before the question text to help the user use
      * these wild cards
      */
-    function print_dataset_definitions_category($form) {
+    public function print_dataset_definitions_category($form) {
         global $CFG, $DB;
         $datasetdefs = array();
         $lnamemax = 22;
-        $namestr =get_string('name');
-        $minstr=get_string('min');
-        $maxstr=get_string('max');
-        $rangeofvaluestr=get_string('minmax','qtype_calculated');
-        $questionusingstr = get_string('usedinquestion','qtype_calculated');
-        $itemscountstr = get_string('itemscount','qtype_calculated');
-        $text ='';
+        $namestr          = get_string('name');
+        $minstr           = get_string('min');
+        $maxstr           = get_string('max');
+        $rangeofvaluestr  = get_string('minmax', 'qtype_calculated');
+        $questionusingstr = get_string('usedinquestion', 'qtype_calculated');
+        $itemscountstr    = get_string('itemscount', 'qtype_calculated');
+        $text = '';
         if (!empty($form->category)) {
-            list($category) = explode(',', $form->category);
-            $sql = "SELECT i.*,d.*
+            list($category) = explode(', ', $form->category);
+            $sql = "SELECT i.*, d.*
                 FROM {question_datasets} d,
         {question_dataset_definitions} i
         WHERE i.id = d.datasetdefinition
@@ -1913,21 +1899,35 @@ class question_calculated_qtype extends question_type {
                     $sql1 = "SELECT q.*
                                FROM {question} q
                               WHERE q.id = ?";
-                    if ( !isset ($datasetdefs["$r->type-$r->category-$r->name"])){
+                    if (!isset ($datasetdefs["$r->type-$r->category-$r->name"])) {
                         $datasetdefs["$r->type-$r->category-$r->name"]= $r;
                     }
                     if ($questionb = $DB->get_records_sql($sql1, array($r->question))) {
-                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[$r->question]->name =$questionb[$r->question]->name ;
+                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[
+                                $r->question]->name = $questionb[$r->question]->name;
                     }
                 }
             }
         }
-        if (!empty ($datasetdefs)){
+        if (!empty ($datasetdefs)) {
 
-            $text ="<table width=\"100%\" border=\"1\"><tr><th  style=\"white-space:nowrap;\" class=\"header\" scope=\"col\" >$namestr</th><th   style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$rangeofvaluestr</th><th  style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$itemscountstr</th><th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$questionusingstr</th></tr>";
-            foreach ($datasetdefs as $datasetdef){
-                list($distribution, $min, $max,$dec) = explode(':', $datasetdef->options, 4);
-                $text .="<tr><td valign=\"top\" align=\"center\"> $datasetdef->name </td><td align=\"center\" valign=\"top\"> $min <strong>-</strong> $max </td><td align=\"right\" valign=\"top\">$datasetdef->itemcount&nbsp;&nbsp;</td><td align=\"left\">";
+            $text = "<table width=\"100%\" border=\"1\"><tr>
+                    <th style=\"white-space:nowrap;\" class=\"header\"
+                            scope=\"col\">$namestr</th>
+                    <th style=\"white-space:nowrap;\" class=\"header\"
+                            scope=\"col\">$rangeofvaluestr</th>
+                    <th style=\"white-space:nowrap;\" class=\"header\"
+                            scope=\"col\">$itemscountstr</th>
+                    <th style=\"white-space:nowrap;\" class=\"header\"
+                            scope=\"col\">$questionusingstr</th>
+                    </tr>";
+            foreach ($datasetdefs as $datasetdef) {
+                list($distribution, $min, $max, $dec) = explode(':', $datasetdef->options, 4);
+                $text .= "<tr>
+                        <td valign=\"top\" align=\"center\">$datasetdef->name</td>
+                        <td align=\"center\" valign=\"top\">$min <strong>-</strong> $max</td>
+                        <td align=\"right\" valign=\"top\">$datasetdef->itemcount&nbsp;&nbsp;</td>
+                        <td align=\"left\">";
                 foreach ($datasetdef->questions as $qu) {
                     //limit the name length displayed
                     if (!empty($qu->name)) {
@@ -1936,15 +1936,15 @@ class question_calculated_qtype extends question_type {
                     } else {
                         $qu->name = '';
                     }
-                    $text .=" &nbsp;&nbsp; $qu->name <br/>";
+                    $text .= " &nbsp;&nbsp; $qu->name <br/>";
                 }
-                $text .="</td></tr>";
+                $text .= "</td></tr>";
             }
-            $text .="</table>";
-        }else{
-            $text .=get_string('nosharedwildcard', 'qtype_calculated');
+            $text .= "</table>";
+        } else {
+            $text .= get_string('nosharedwildcard', 'qtype_calculated');
         }
-        return  $text ;
+        return $text;
     }
 
     /**
@@ -1955,20 +1955,20 @@ class question_calculated_qtype extends question_type {
      * these wild cards
      */
 
-    function print_dataset_definitions_category_shared($question,$datasetdefsq) {
+    public function print_dataset_definitions_category_shared($question, $datasetdefsq) {
         global $CFG, $DB;
         $datasetdefs = array();
         $lnamemax = 22;
-        $namestr =get_string('name', 'quiz');
-        $minstr=get_string('min', 'quiz');
-        $maxstr=get_string('max', 'quiz');
-        $rangeofvaluestr=get_string('minmax','qtype_calculated');
-        $questionusingstr = get_string('usedinquestion','qtype_calculated');
-        $itemscountstr = get_string('itemscount','qtype_calculated');
-        $text ='';
+        $namestr          = get_string('name', 'quiz');
+        $minstr           = get_string('min', 'quiz');
+        $maxstr           = get_string('max', 'quiz');
+        $rangeofvaluestr  = get_string('minmax', 'qtype_calculated');
+        $questionusingstr = get_string('usedinquestion', 'qtype_calculated');
+        $itemscountstr    = get_string('itemscount', 'qtype_calculated');
+        $text = '';
         if (!empty($question->category)) {
             list($category) = explode(',', $question->category);
-            $sql = "SELECT i.*,d.*
+            $sql = "SELECT i.*, d.*
                       FROM {question_datasets} d, {question_dataset_definitions} i
                      WHERE i.id = d.datasetdefinition AND i.category = ?";
             if ($records = $DB->get_records_sql($sql, array($category))) {
@@ -1976,29 +1976,40 @@ class question_calculated_qtype extends question_type {
                     $sql1 = "SELECT q.*
                                FROM {question} q
                               WHERE q.id = ?";
-                    if ( !isset ($datasetdefs["$r->type-$r->category-$r->name"])){
+                    if (!isset ($datasetdefs["$r->type-$r->category-$r->name"])) {
                         $datasetdefs["$r->type-$r->category-$r->name"]= $r;
                     }
                     if ($questionb = $DB->get_records_sql($sql1, array($r->question))) {
-                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[$r->question]->name =$questionb[$r->question]->name ;
-                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[$r->question]->id =$questionb[$r->question]->id ;
+                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[
+                                $r->question]->name = $questionb[$r->question]->name;
+                        $datasetdefs["$r->type-$r->category-$r->name"]->questions[
+                                $r->question]->id = $questionb[$r->question]->id;
                     }
                 }
             }
         }
-        if (!empty ($datasetdefs)){
+        if (!empty ($datasetdefs)) {
 
-            $text ="<table width=\"100%\" border=\"1\"><tr><th  style=\"white-space:nowrap;\" class=\"header\" scope=\"col\" >$namestr</th>";
-            $text .="<th  style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$itemscountstr</th>";
-            $text .="<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">&nbsp;&nbsp;$questionusingstr &nbsp;&nbsp; </th>";
-            $text .="<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">Quiz</th>";
-            $text .="<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">Attempts</th></tr>";
-            foreach ($datasetdefs as $datasetdef){
-                list($distribution, $min, $max,$dec) = explode(':', $datasetdef->options, 4);
+            $text  = "<table width=\"100%\" border=\"1\"><tr>
+                    <th style=\"white-space:nowrap;\" class=\"header\"
+                            scope=\"col\">$namestr</th>";
+            $text .= "<th style=\"white-space:nowrap;\" class=\"header\"
+                    scope=\"col\">$itemscountstr</th>";
+            $text .= "<th style=\"white-space:nowrap;\" class=\"header\"
+                    scope=\"col\">&nbsp;&nbsp;$questionusingstr &nbsp;&nbsp;</th>";
+            $text .= "<th style=\"white-space:nowrap;\" class=\"header\"
+                    scope=\"col\">Quiz</th>";
+            $text .= "<th style=\"white-space:nowrap;\" class=\"header\"
+                    scope=\"col\">Attempts</th></tr>";
+            foreach ($datasetdefs as $datasetdef) {
+                list($distribution, $min, $max, $dec) = explode(':', $datasetdef->options, 4);
                 $count = count($datasetdef->questions);
-                $text .="<tr><td style=\"white-space:nowrap;\" valign=\"top\" align=\"center\" rowspan=\"$count\"> $datasetdef->name </td><td align=\"right\" valign=\"top\" rowspan=\"$count\" >$datasetdef->itemcount&nbsp;&nbsp;</td>";
-                //$text .="<td align=\"left\">";
-                $line = 0 ;
+                $text .= "<tr>
+                        <td style=\"white-space:nowrap;\" valign=\"top\"
+                                align=\"center\" rowspan=\"$count\"> $datasetdef->name </td>
+                        <td align=\"right\" valign=\"top\"
+                                rowspan=\"$count\">$datasetdef->itemcount</td>";
+                $line = 0;
                 foreach ($datasetdef->questions as $qu) {
                     //limit the name length displayed
                     if (!empty($qu->name)) {
@@ -2007,47 +2018,49 @@ class question_calculated_qtype extends question_type {
                     } else {
                         $qu->name = '';
                     }
-                    if( $line ) {
-                        $text .="<tr>";
+                    if ($line) {
+                        $text .= "<tr>";
                     }
                     $line++;
-                    $text .="<td align=\"left\" style=\"white-space:nowrap;\" >$qu->name</td>";
-                    $nb_of_quiz = 0;
-                    $nb_of_attempts=0;
-                    $used_in_quiz = false ;
-                    if ($list = $DB->get_records('quiz_question_instances', array( 'question'=> $qu->id))){
-                        $used_in_quiz = true;
-                        foreach($list as $key => $li){
-                            $nb_of_quiz ++;
-                            if($att = $DB->get_records('quiz_attempts',array( 'quiz'=> $li->quiz, 'preview'=> '0'))){
-                                $nb_of_attempts+= count($att);
+                    $text .= "<td align=\"left\" style=\"white-space:nowrap;\">$qu->name</td>";
+                    $nbofquiz = 0;
+                    $nbofattempts= 0;
+                    $usedinquiz = false;
+                    if ($list = $DB->get_records('quiz_question_instances',
+                            array('question' => $qu->id))) {
+                        $usedinquiz = true;
+                        foreach ($list as $key => $li) {
+                            $nbofquiz ++;
+                            if ($att = $DB->get_records('quiz_attempts',
+                                    array('quiz' => $li->quiz, 'preview' => '0'))) {
+                                $nbofattempts+= count($att);
                             }
                         }
                     }
-                    if($used_in_quiz){
-                        $text .="<td align=\"center\">$nb_of_quiz</td>";
-                    }else {
-                        $text .="<td align=\"center\">0</td>";
+                    if ($usedinquiz) {
+                        $text .= "<td align=\"center\">$nbofquiz</td>";
+                    } else {
+                        $text .= "<td align=\"center\">0</td>";
                     }
-                    if($used_in_quiz){
-                        $text .="<td align=\"center\">$nb_of_attempts";
-                    }else {
-                        $text .="<td align=\"left\"><br/>";
+                    if ($usedinquiz) {
+                        $text .= "<td align=\"center\">$nbofattempts";
+                    } else {
+                        $text .= "<td align=\"left\"><br/>";
                     }
 
-                    $text .="</td></tr>";
+                    $text .= "</td></tr>";
                 }
             }
-            $text .="</table>";
-        }else{
-            $text .=get_string('nosharedwildcard', 'qtype_calculated');
+            $text .= "</table>";
+        } else {
+            $text .= get_string('nosharedwildcard', 'qtype_calculated');
         }
-        return  $text ;
+        return $text;
     }
 
-    function find_math_equations($text) {
-        /// Returns the possible dataset names found in the text as an array
-        /// The array has the dataset name for both key and value
+    public function find_math_equations($text) {
+        // Returns the possible dataset names found in the text as an array
+        // The array has the dataset name for both key and value
         $equations = array();
         while (preg_match('~\{=([^[:space:]}]*)}~', $text, $regs)) {
             $equations[] = $regs[1];
@@ -2056,9 +2069,8 @@ class question_calculated_qtype extends question_type {
         return $equations;
     }
 
-    function get_virtual_qtype() {
-        global $QTYPES;
-        $this->virtualqtype =& $QTYPES['numerical'];
+    public function get_virtual_qtype() {
+        $this->virtualqtype = $QTYPES['numerical'];
         return $this->virtualqtype;
     }
 
@@ -2066,15 +2078,15 @@ class question_calculated_qtype extends question_type {
      * Runs all the code required to set up and save an essay question for testing purposes.
      * Alternate DB table prefix may be used to facilitate data deletion.
      */
-    function generate_test($name, $courseid = null) {
+    public function generate_test($name, $courseid = null) {
         global $DB;
         list($form, $question) = parent::generate_test($name, $courseid);
         $form->feedback = 1;
         $form->multiplier = array(1, 1);
         $form->shuffleanswers = 1;
         $form->noanswers = 1;
-        $form->qtype ='calculated';
-        $question->qtype ='calculated';
+        $form->qtype = 'calculated';
+        $question->qtype = 'calculated';
         $form->answers = array('{a} + {b}');
         $form->fraction = array(1);
         $form->tolerance = array(0.01);
@@ -2084,33 +2096,33 @@ class question_calculated_qtype extends question_type {
         $form->questiontext = "What is {a} + {b}?";
 
         if ($courseid) {
-            $course = $DB->get_record('course', array('id'=> $courseid));
+            $course = $DB->get_record('course', array('id' => $courseid));
         }
 
-        $new_question = $this->save_question($question, $form);
+        $newquestion = $this->save_question($question, $form);
 
-        $dataset_form = new stdClass();
-        $dataset_form->nextpageparam["forceregeneration"]= 1;
-        $dataset_form->calcmin = array(1 => 1.0, 2 => 1.0);
-        $dataset_form->calcmax = array(1 => 10.0, 2 => 10.0);
-        $dataset_form->calclength = array(1 => 1, 2 => 1);
-        $dataset_form->number = array(1 => 5.4 , 2 => 4.9);
-        $dataset_form->itemid = array(1 => '' , 2 => '');
-        $dataset_form->calcdistribution = array(1 => 'uniform', 2 => 'uniform');
-        $dataset_form->definition = array(1 => "1-0-a",
+        $datasetform = new stdClass();
+        $datasetform->nextpageparam['forceregeneration']= 1;
+        $datasetform->calcmin = array(1 => 1.0, 2 => 1.0);
+        $datasetform->calcmax = array(1 => 10.0, 2 => 10.0);
+        $datasetform->calclength = array(1 => 1, 2 => 1);
+        $datasetform->number = array(1 => 5.4 , 2 => 4.9);
+        $datasetform->itemid = array(1 => '' , 2 => '');
+        $datasetform->calcdistribution = array(1 => 'uniform', 2 => 'uniform');
+        $datasetform->definition = array(1 => "1-0-a",
             2 => "1-0-b");
-        $dataset_form->nextpageparam = array('forceregeneration' => false);
-        $dataset_form->addbutton = 1;
-        $dataset_form->selectadd = 1;
-        $dataset_form->courseid = $courseid;
-        $dataset_form->cmid = 0;
-        $dataset_form->id = $new_question->id;
-        $this->save_dataset_items($new_question, $dataset_form);
+        $datasetform->nextpageparam = array('forceregeneration' => false);
+        $datasetform->addbutton = 1;
+        $datasetform->selectadd = 1;
+        $datasetform->courseid = $courseid;
+        $datasetform->cmid = 0;
+        $datasetform->id = $newquestion->id;
+        $this->save_dataset_items($newquestion, $datasetform);
 
-        return $new_question;
+        return $newquestion;
     }
 
-    function move_files($questionid, $oldcontextid, $newcontextid) {
+    public function move_files($questionid, $oldcontextid, $newcontextid) {
         $fs = get_file_storage();
 
         parent::move_files($questionid, $oldcontextid, $newcontextid);
@@ -2128,13 +2140,14 @@ class question_calculated_qtype extends question_type {
         $fs->delete_area_files($contextid, 'qtype_calculated', 'instruction', $questionid);
     }
 
-    function check_file_access($question, $state, $options, $contextid, $component,
+    public function check_file_access($question, $state, $options, $contextid, $component,
             $filearea, $args) {
         $itemid = reset($args);
         if ($component == 'question' && $filearea == 'answerfeedback') {
 
             // check if answer id exists
-            $result = $options->feedback && array_key_exists($itemid, $question->options->answers);
+            $result = $options->feedback && array_key_exists($itemid,
+                    $question->options->answers);
             if (!$result) {
                 return false;
             }
@@ -2150,37 +2163,33 @@ class question_calculated_qtype extends question_type {
             } else {
                 return true;
             }
-        } else if (in_array($filearea, array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
+        } else if (in_array($filearea, array('correctfeedback', 'partiallycorrectfeedback',
+                'incorrectfeedback'))) {
             // Note: calculated type doesn't display question feedback yet
             return false;
         } else {
-            return parent::check_file_access($question, $state, $options, $contextid, $component,
-                    $filearea, $args);
+            return parent::check_file_access($question, $state, $options, $contextid,
+                    $component, $filearea, $args);
         }
     }
 }
-//// END OF CLASS ////
 
-if ( ! defined ("CALCULATEDQUESTIONMAXITEMNUMBER")) {
-    define("CALCULATEDQUESTIONMAXITEMNUMBER", 100);
-}
 
 function qtype_calculated_calculate_answer($formula, $individualdata,
-    $tolerance, $tolerancetype, $answerlength, $answerformat='1', $unit='') {
-    /// The return value has these properties:
-    /// ->answer    the correct answer
-    /// ->min       the lower bound for an acceptable response
-    /// ->max       the upper bound for an accetpable response
-
-    /// Exchange formula variables with the correct values...
-    global $QTYPES;
+    $tolerance, $tolerancetype, $answerlength, $answerformat = '1', $unit = '') {
+    // The return value has these properties:
+    // ->answer    the correct answer
+    // ->min       the lower bound for an acceptable response
+    // ->max       the upper bound for an accetpable response
+
+    // Exchange formula variables with the correct values...
     $answer = $QTYPES['calculated']->substitute_variables_and_eval($formula, $individualdata);
     if ('1' == $answerformat) { /* Answer is to have $answerlength decimals */
         /*** Adjust to the correct number of decimals ***/
-        if (stripos($answer,'e')>0 ){
-            $answerlengthadd = strlen($answer)-stripos($answer,'e');
-        }else {
-            $answerlengthadd = 0 ;
+        if (stripos($answer, 'e')>0) {
+            $answerlengthadd = strlen($answer)-stripos($answer, 'e');
+        } else {
+            $answerlengthadd = 0;
         }
         $calculated->answer = round(floatval($answer), $answerlength+$answerlengthadd);
 
@@ -2229,16 +2238,16 @@ function qtype_calculated_calculate_answer($formula, $individualdata,
         // Either scientific or plain numeric
         if (-2 > $p10 || 4 < $p10) {
             // Use scientific format:
-            $eX = 'e'.--$p10;
+            $exponent = 'e'.--$p10;
             $answer *= 10;
             if (1 == $answerlength) {
-                $calculated->answer = $sign.$answer.$eX.$unit;
+                $calculated->answer = $sign.$answer.$exponent.$unit;
             } else {
                 // Attach additional zeros at the end of $answer,
-                $answer .= (1==strlen($answer) ? '.' : '')
+                $answer .= (1 == strlen($answer) ? '.' : '')
                     . '00000000000000000000000000000000000000000x';
                 $calculated->answer = $sign
-                    .substr($answer, 0, $answerlength +1).$eX.$unit;
+                    .substr($answer, 0, $answerlength +1).$exponent.$unit;
             }
         } else {
             // Stick to plain numeric format
@@ -2258,15 +2267,15 @@ function qtype_calculated_calculate_answer($formula, $individualdata,
         $calculated->answer = 0.0;
     }
 
-    /// Return the result
+    // Return the result
     return $calculated;
 }
 
 
 function qtype_calculated_find_formula_errors($formula) {
-    /// Validates the formula submitted from the question edit page.
-    /// Returns false if everything is alright.
-    /// Otherwise it constructs an error message
+    // Validates the formula submitted from the question edit page.
+    // Returns false if everything is alright.
+    // Otherwise it constructs an error message
     // Strip away dataset names
     while (preg_match('~\\{[[:alpha:]][^>} <{"\']*\\}~', $formula, $regs)) {
         $formula = str_replace($regs[0], '1', $formula);
@@ -2278,59 +2287,60 @@ function qtype_calculated_find_formula_errors($formula) {
     $safeoperatorchar = '-+/*%>:^\~<?=&|!'; /* */
     $operatorornumber = "[$safeoperatorchar.0-9eE]";
 
-    while ( preg_match("~(^|[$safeoperatorchar,(])([a-z0-9_]*)\\(($operatorornumber+(,$operatorornumber+((,$operatorornumber+)+)?)?)?\\)~",
+    while (preg_match("~(^|[$safeoperatorchar,(])([a-z0-9_]*)" .
+            "\\(($operatorornumber+(,$operatorornumber+((,$operatorornumber+)+)?)?)?\\)~",
         $formula, $regs)) {
         switch ($regs[2]) {
             // Simple parenthesis
-        case '':
-            if ((isset($regs[4])&& $regs[4]) || strlen($regs[3])==0) {
-                return get_string('illegalformulasyntax', 'qtype_calculated', $regs[0]);
-            }
-            break;
+            case '':
+                if ((isset($regs[4]) && $regs[4]) || strlen($regs[3]) == 0) {
+                    return get_string('illegalformulasyntax', 'qtype_calculated', $regs[0]);
+                }
+                break;
 
-            // Zero argument functions
-        case 'pi':
-            if ($regs[3]) {
-                return get_string('functiontakesnoargs', 'qtype_calculated', $regs[2]);
-            }
-            break;
+                // Zero argument functions
+            case 'pi':
+                if ($regs[3]) {
+                    return get_string('functiontakesnoargs', 'qtype_calculated', $regs[2]);
+                }
+                break;
 
-            // Single argument functions (the most common case)
-        case 'abs': case 'acos': case 'acosh': case 'asin': case 'asinh':
-        case 'atan': case 'atanh': case 'bindec': case 'ceil': case 'cos':
-        case 'cosh': case 'decbin': case 'decoct': case 'deg2rad':
-        case 'exp': case 'expm1': case 'floor': case 'is_finite':
-        case 'is_infinite': case 'is_nan': case 'log10': case 'log1p':
-        case 'octdec': case 'rad2deg': case 'sin': case 'sinh': case 'sqrt':
-        case 'tan': case 'tanh':
-            if (!empty($regs[4]) || empty($regs[3])) {
-                return get_string('functiontakesonearg','qtype_calculated',$regs[2]);
-            }
-            break;
+                // Single argument functions (the most common case)
+            case 'abs': case 'acos': case 'acosh': case 'asin': case 'asinh':
+            case 'atan': case 'atanh': case 'bindec': case 'ceil': case 'cos':
+            case 'cosh': case 'decbin': case 'decoct': case 'deg2rad':
+            case 'exp': case 'expm1': case 'floor': case 'is_finite':
+            case 'is_infinite': case 'is_nan': case 'log10': case 'log1p':
+            case 'octdec': case 'rad2deg': case 'sin': case 'sinh': case 'sqrt':
+            case 'tan': case 'tanh':
+                if (!empty($regs[4]) || empty($regs[3])) {
+                    return get_string('functiontakesonearg', 'qtype_calculated', $regs[2]);
+                }
+                break;
 
-            // Functions that take one or two arguments
-        case 'log': case 'round':
-            if (!empty($regs[5]) || empty($regs[3])) {
-                return get_string('functiontakesoneortwoargs','qtype_calculated',$regs[2]);
-            }
-            break;
+                // Functions that take one or two arguments
+            case 'log': case 'round':
+                if (!empty($regs[5]) || empty($regs[3])) {
+                    return get_string('functiontakesoneortwoargs', 'qtype_calculated', $regs[2]);
+                }
+                break;
 
-            // Functions that must have two arguments
-        case 'atan2': case 'fmod': case 'pow':
-            if (!empty($regs[5]) || empty($regs[4])) {
-                return get_string('functiontakestwoargs', 'qtype_calculated', $regs[2]);
-            }
-            break;
+                // Functions that must have two arguments
+            case 'atan2': case 'fmod': case 'pow':
+                if (!empty($regs[5]) || empty($regs[4])) {
+                    return get_string('functiontakestwoargs', 'qtype_calculated', $regs[2]);
+                }
+                break;
 
-            // Functions that take two or more arguments
-        case 'min': case 'max':
-            if (empty($regs[4])) {
-                return get_string('functiontakesatleasttwo','qtype_calculated',$regs[2]);
-            }
-            break;
+                // Functions that take two or more arguments
+            case 'min': case 'max':
+                if (empty($regs[4])) {
+                    return get_string('functiontakesatleasttwo', 'qtype_calculated', $regs[2]);
+                }
+                break;
 
-        default:
-            return get_string('unsupportedformulafunction','qtype_calculated',$regs[2]);
+            default:
+                return get_string('unsupportedformulafunction', 'qtype_calculated', $regs[2]);
         }
 
         // Exchange the function call with '1' and then chack for
index 7511ca0..f8d7773 100644 (file)
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
  * @copyright  2009 Pierre Pichet
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class question_calculatedmulti_qtype extends question_calculated_qtype {
+class question_calculatedmulti_qtype extends qtype_calculated {
 
 
     // Used by the function custom_generator_tools:
@@ -44,15 +44,10 @@ class question_calculatedmulti_qtype extends question_calculated_qtype {
         return 'calculatedmulti';
     }
 
-    function has_wildcards_in_responses($question, $subqid) {
-        return true;
-    }
-
     function requires_qtypes() {
         return array('multichoice');
     }
 
-
     function save_question_options($question) {
         global $CFG, $DB, $QTYPES ;
         $context = $question->context;
@@ -229,8 +224,8 @@ class question_calculatedmulti_qtype extends question_calculated_qtype {
 
         // Choose a random dataset
         // maxnumber sould not be breater than 100
-        if ($maxnumber > CALCULATEDQUESTIONMAXITEMNUMBER ){
-            $maxnumber = CALCULATEDQUESTIONMAXITEMNUMBER ;
+        if ($maxnumber > qtype_calculated::MAX_DATASET_ITEMS) {
+            $maxnumber = qtype_calculated::MAX_DATASET_ITEMS;
         }
         if ( $synchronize_calculated === false ) {
             $state->options->datasetitem = rand(1, $maxnumber);
index 4e4b2df..b45a579 100644 (file)
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
  * @copyright  2009 Pierre Pichet
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class question_calculatedsimple_qtype extends question_calculated_qtype {
+class question_calculatedsimple_qtype extends qtype_calculated {
 
     // Used by the function custom_generator_tools:
     public $calcgenerateidhasbeenadded = false;
index a723dde..2b0a071 100644 (file)
@@ -39,17 +39,6 @@ class embedded_cloze_qtype extends question_type {
         return 'multianswer';
     }
 
-    function has_wildcards_in_responses($question, $subqid) {
-        global $QTYPES, $OUTPUT;
-        foreach ($question->options->questions as $subq){
-            if ($subq->id == $subqid){
-                return $QTYPES[$subq->qtype]->has_wildcards_in_responses($subq, $subqid);
-            }
-        }
-        echo $OUTPUT->notification('Could not find sub question!');
-        return true;
-    }
-
     function requires_qtypes() {
         return array('shortanswer', 'numerical', 'multichoice');
     }
index b2c2289..1c973a2 100644 (file)
@@ -50,10 +50,6 @@ class qtype_numerical extends question_type {
     const UNITGRADEDOUTOFMARK = 1;
     const UNITGRADEDOUTOFMAX = 2;
 
-    public function has_wildcards_in_responses() {
-        return true;
-    }
-
     public function get_question_options($question) {
         global $CFG, $DB, $OUTPUT;
         parent::get_question_options($question);