]> git.mjollnir.org Git - moodle.git/commitdiff
xhtml validity fix for static element type
authorjamiesensei <jamiesensei>
Wed, 10 Jan 2007 05:05:26 +0000 (05:05 +0000)
committerjamiesensei <jamiesensei>
Wed, 10 Jan 2007 05:05:26 +0000 (05:05 +0000)
qtype calculated form - started

lang/en_utf8/qtype_calculated.php [new file with mode: 0644]
lib/form/static.php
lib/formslib.php
question/type/calculated/edit_calculated_form.php

diff --git a/lang/en_utf8/qtype_calculated.php b/lang/en_utf8/qtype_calculated.php
new file mode 100644 (file)
index 0000000..def5ace
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+$string['correctanswershows'] = 'Correct answer shows';
+$string['correctanswershowsformat'] = 'Format';
+$string['tolerance'] = 'Tolerance &plusmn;';
+$string['addmoreanswerblanks'] = 'Add another answer blank.';
+$string['addmoreunitblanks'] = 'Blanks for $a More Units';
+$string['answerhdr'] = 'Answer $a';
+$string['atleastoneanswer'] = 'You need to provide at least one answer.';
+$string['mustbenumeric'] = 'You must enter a number here.';
+$string['mustnotbenumeric'] = 'This can\'t be a number.';
+$string['youmustenteramultiplierhere'] = 'You must enter a multiplier here.';
+?>
\ No newline at end of file
index dc0a05baf8d44aa12e2811d3db60b673b940c152..4e5503d08fb0fe7b01611488bc7be3b6b3139cb2 100644 (file)
@@ -8,6 +8,7 @@ require_once("HTML/QuickForm/static.php");
  * @access       public
  */
 class MoodleQuickForm_static extends HTML_QuickForm_static{
+    var $_elementTemplateType='static';
     /**
      * html for help button, if empty then no help
      *
@@ -44,5 +45,9 @@ class MoodleQuickForm_static extends HTML_QuickForm_static{
     function getHelpButton(){
         return $this->_helpbutton;
     }
+
+    function getElementTemplateType(){
+        return $this->_elementTemplateType;
+    }
 }
 ?>
\ No newline at end of file
index 8e052addd285f153bbc758f3cbd55569c3c00953..b7dfda460015bdf04d4bea908984ec34042a06f6 100644 (file)
@@ -295,7 +295,9 @@ class moodleform {
         static $validated = null; // one validation is enough
         $mform =& $this->_form;
 
-        if ($validated === null) {
+        if ($this->no_submit_button_pressed()){
+            return false;
+        } elseif ($validated === null) {
             $internal_val = $mform->validate();
             $moodle_val = $this->validation($mform->exportValues(null, true));
             if ($moodle_val !== true) {
@@ -319,11 +321,7 @@ class moodleform {
             }
             $validated = ($internal_val and $moodle_val and $file_val);
         }
-        if ($this->no_submit_button_pressed()){
-            return false;
-        } else {
-            return $validated;
-        }
+        return $validated;
     }
 
     /**
@@ -506,7 +504,15 @@ class moodleform {
                             $mform->setHelpButton($realelementname, $params);
                             break;
                         case 'disabledif' :
-                            $mform->disabledIf($realelementname, $params[0], $params[1], $params[2]);
+                            $params = array_merge(array($realelementname), $params);
+                            call_user_func_array(array(&$mform, 'disabledIf'), $params);
+                            break;
+                        case 'rule' :
+                            if (is_string($params)){
+                                $params = array(null, $params, null, 'client');
+                            }
+                            $params = array_merge(array($realelementname), $params);
+                            call_user_func_array(array(&$mform, 'addRule'), $params);
                             break;
 
                     }
@@ -1279,8 +1285,10 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{
     function MoodleQuickForm_Renderer(){
         // switch next two lines for ol li containers for form items.
         //        $this->_elementTemplates=array('default'=>"\n\t\t<li class=\"fitem\"><label>{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></li>");
-        $this->_elementTemplates = array('default'=>"\n\t\t<div class=\"fitem {advanced}<!-- BEGIN required --> required<!-- END required -->\"><span class=\"fitemtitle\"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</label>{help}</span><div class=\"felement {type}<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\" id=\"id_error_{name}\">{error}</span><br /><!-- END error -->{element}</div></div>",
-        'fieldset'=>"\n\t\t<div class=\"fitem {advanced}<!-- BEGIN required --> required<!-- END required -->\"><span class=\"fitemtitle\"><div class=\"fgrouplabel\">{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</div>{help}</span><fieldset class=\"felement {type}<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\" id=\"id_error_{name}\">{error}</span><br /><!-- END error -->{element}</fieldset></div>");
+        $this->_elementTemplates = array(
+        'default'=>"\n\t\t<div class=\"fitem {advanced}<!-- BEGIN required --> required<!-- END required -->\"><span class=\"fitemtitle\"><label>{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</label>{help}</span><div class=\"felement {type}<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div>",
+        'fieldset'=>"\n\t\t<div class=\"fitem {advanced}<!-- BEGIN required --> required<!-- END required -->\"><span class=\"fitemtitle\"><div class=\"fgrouplabel\">{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</div>{help}</span><fieldset class=\"felement {type}<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</fieldset></div>",
+        'static'=>"\n\t\t<div class=\"fitem {advanced}<!-- BEGIN required --> required<!-- END required -->\"><span class=\"fitemtitle\"><div class=\"fstaticlabel\">{label}<!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</div>{help}</span><div class=\"felement {type}<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div>");
 
         parent::HTML_QuickForm_Renderer_Tableless();
     }
@@ -1492,5 +1500,4 @@ MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", '
 MoodleQuickForm::registerElementType('submit', "$CFG->libdir/form/submit.php", 'MoodleQuickForm_submit');
 MoodleQuickForm::registerElementType('questioncategory', "$CFG->libdir/form/questioncategory.php", 'MoodleQuickForm_questioncategory');
 MoodleQuickForm::registerElementType('advcheckbox', "$CFG->libdir/form/advcheckbox.php", 'MoodleQuickForm_advcheckbox');
-
-?>
+?>
\ No newline at end of file
index 90291a05da58bc274cd45327be566bc57690422e..d82474cc8f09c5d33f2f59073229575cef67dea2 100644 (file)
  * calculated editing form definition.
  */
 class question_edit_calculated_form extends question_edit_form {
+    /**
+     * Handle to the question type for this question.
+     *
+     * @var question_calculated_qtype
+     */
+    var $qtypeobj;
     /**
      * Add question-type specific form fields.
      *
-     * @param object $mform the form being built.
+     * @param MoodleQuickForm $mform the form being built.
      */
     function definition_inner(&$mform) {
-        $mform->addElement('text', 'answers[0]', get_string("correctanswerformula", "quiz"));
-        $mform->setType('answers[0]', PARAM_NOTAGS);
+        global $QTYPES;
+        $this->qtypeobj = $QTYPES['calculated'];
 
-        $mform->addElement('hidden', 'fraction[0]', '1.0');
-
-        $mform->addElement('text', 'tolerance[0]', get_string("tolerance", "quiz"));
-        $mform->setType('tolerance[0]', PARAM_NOTAGS);
+//------------------------------------------------------------------------------------------
+        $repeated = array();
+        $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
 
-        $mform->addElement('select', 'shuffleanswers', get_string('shuffleanswers', 'qtype_calculated') );
-        $mform->setHelpButton('shuffleanswers', array('calculatedshuffle', get_string('shuffleanswers','qtype_calculated'), 'quiz'));
+        $repeated[] =& $mform->createElement('text', 'answers', get_string('correctanswerformula', 'quiz'));
+        $repeatedoptions['answers']['type'] = PARAM_NOTAGS;
 
-/*      $mform->addElement('static', 'answersinstruct', get_string('choices', 'qtype_calculated'), get_string('fillouttwochoices', 'qtype_calculated'));
-        $mform->closeHeaderBefore('answersinstruct');
-*/
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptionsfull;
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_calculated', '{no}'));
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
+        $gradeoptions = $creategrades->gradeoptions;
         $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
+        $repeatedoptions['fraction']['default'] = 0;
+
+        $repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
+        $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
+        $repeatedoptions['tolerance']['default'] = 0.01;
+        $repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
+
+        $repeated[] =&  $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+        $repeatedoptions['correctanswerlength']['default'] = 2;
+
+        $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
+        $repeated[] =&  $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
+
+        $repeated[] =&  $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
+        $repeatedoptions['feedback']['type'] = PARAM_RAW;
 
         if (isset($this->question->options)){
-            $countanswers = count($this->question->options->answers);
+            $count = count($this->question->options->answers);
         } else {
-            $countanswers = 0;
+            $count = 0;
         }
-        $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
-                            QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
+        $repeatsatstart = $count + 1;
+        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));
+
+//------------------------------------------------------------------------------------------
+        $repeated = array();
         $repeatedoptions = array();
-        $repeatedoptions['answer']['type'] = PARAM_TEXT;
-        $repeatedoptions['fraction']['default'] = 0;
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_calculated'));
+        $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
 
-        $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_calculated'));
+        $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
+        $repeatedoptions['unit']['type'] = PARAM_NOTAGS;
 
-        $mform->addElement('htmleditor', 'correctfeedback', get_string('correctfeedback', 'qtype_calculated'));
-        $mform->setType('correctfeedback', PARAM_RAW);
+        $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
+        $repeatedoptions['multiplier']['type'] = PARAM_NUMBER;
+
+        if (isset($this->question->options)){
+            $countunits = count($this->question->options->units);
+        } else {
+            $countunits = 0;
+        }
+        $repeatsatstart = $countunits + 1;
+        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
 
-        $mform->addElement('htmleditor', 'partiallycorrectfeedback', get_string('partiallycorrectfeedback', 'qtype_calculated'));
-        $mform->setType('partiallycorrectfeedback', PARAM_RAW);
+        $firstunit = $mform->getElement('multiplier[0]');
+        $firstunit->freeze();
+        $firstunit->setValue('1.0');
+        $firstunit->setPersistantFreeze(true);
 
-        $mform->addElement('htmleditor', 'incorrectfeedback', get_string('incorrectfeedback', 'qtype_calculated'));
-        $mform->setType('incorrectfeedback', PARAM_RAW);
+        //hidden elements
+        $mform->addElement('hidden', 'wizardpage', 'question');
 
     }
 
@@ -71,19 +96,29 @@ class question_edit_calculated_form extends question_edit_form {
             if (count($answers)) {
                 $key = 0;
                 foreach ($answers as $answer){
-                    $default_values['answer['.$key.']'] = $answer->answer;
-                    $default_values['fraction['.$key.']'] = $answer->fraction;
+                    $default_values['answers['.$key.']'] = $answers->answer;
+                    $default_values['fraction['.$key.']'] = $answers->fraction;
+                    $default_values['tolerance['.$key.']'] = $answers->tolerance;
+                    $default_values['correctanswerlength['.$key.']'] = $answers->correctanswerlength;
+                    $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
                     $default_values['feedback['.$key.']'] = $answer->feedback;
                     $key++;
                 }
             }
-            $default_values['single'] =  $question->options->single;
-            $default_values['shuffleanswers'] =  $question->options->shuffleanswers;
-            $default_values['correctfeedback'] =  $question->options->correctfeedback;
-            $default_values['partiallycorrectfeedback'] =  $question->options->partiallycorrectfeedback;
-            $default_values['overallincorrectfeedback'] =  $question->options->overallincorrectfeedback;
-            $question = (object)((array)$question + $default_values);
-        }
+            $units  = array_values($question->options->units);
+            // make sure the default unit is at index 0
+            usort($units, create_function('$a, $b', // make sure the default unit is at index 0
+            'if (1.0 === (float)$a->multiplier) { return -1; } else '.
+            'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
+            if (count($units)) {
+                $key = 0;
+                foreach ($units as $unit){
+                    $default_values['unit['.$key.']'] = $unit->unit;
+                    $default_values['multiplier['.$key.']'] = $unit->multiplier;
+                    $key++;
+                }
+            }
+            $question = (object)((array)$question + $default_values);        }
         parent::set_defaults($question);
     }
 
@@ -93,21 +128,79 @@ class question_edit_calculated_form extends question_edit_form {
 
     function validation($data){
         $errors = array();
-        $answers = $data['answer'];
+        $answers = $data['answers'];
         $answercount = 0;
-        foreach ($answers as $answer){
+        //check grades
+        /*$totalfraction = 0;
+        $maxfraction = -1;*/
+
+        foreach ($answers as $key => $answer){
+            //check no of choices
             $trimmedanswer = trim($answer);
             if (!empty($trimmedanswer)){
+                $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
+                if (FALSE !== $eqerror){
+                    $errors['answers['.$key.']'] = $eqerror;
+                }
+                if ('2' == $data['correctanswerformat'][$key]
+                        && '0' == $data['correctanswerlength'][$key]) {
+                    $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
+                }
+                if (!is_numeric($data['tolerance'][$key])){
+                    $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
+                }
+
                 $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');
+                    }
+
+                }
+            }
         }
         if ($answercount==0){
-            $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
-            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
-        } elseif ($answercount==1){
-            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
-
+            $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
         }
+
         return $errors;
     }
 }