<div class="qtext">
  <?php echo $questiontext; ?>
</div>

<div class="ablock clearfix">
  <div class="prompt">
    <?php echo get_string("answer", "quiz").': '; ?>
  </div>
<div  class="que numerical clearfix">
<?php
        $textlength = 20 ;
?>

<?php
 if ( $question->options->unitsleft == 0 ){ ?>

  <div class="answer">
    <fieldset class="answer"  id="generalheader">
         <legend class="ftoggler">
            <?php 
                echo $OUTPUT->help_icon('validnumberformats', 'qtype_numerical', '');
                echo get_string('datasetnumber', 'quiz') ; 
            ?>
         </legend>
    <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly  $nameanswer $valueanswer"; //
    ?> size="<?php echo $textlength;?>" />
    <?php echo $feedbackimg; 
    if(! $answerasterisk  && $question->options->unitgradingtype != 0 &&  $unit_in_numerical_answer){
        print_string('nonvalidcharactersinnumber', 'qtype_numerical'); 
    }
        

    ?>
</fieldset>
  </div>


<?php }// display unit
        
        if ($question->options->showunits == NUMERICALQUESTIONUNITNODISPLAY || (isset($question->options->units) && count($question->options->units) == 0 )) {// no unit display or tested

?>
  <div class="answer">
    <input type="hidden" <?php echo $nameunit; ?> value ="" />
  </div>
<?php
  } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTINPUTDISPLAY  ) {// display unit text input
?>
<div class="answer numerical ">
    <fieldset class="clearfix"  id="generalheader">
         <legend class="ftoggler">
            <?php
                if ( $question->options->unitgradingtype == 0 ){
                    echo $OUTPUT->help_icon('unitoptional', 'qtype_numerical', '');
                }else { // unitgradingtype ==  1 
                    echo $OUTPUT->help_icon('unitmandatory', 'qtype_numerical', '');
                }             
                echo get_string('unit', 'quiz'); 
                if ($state->responses['unit'] == '')$classunit = '';
                ?>
         </legend>
    <input type="text" class="<?php echo $classunit; ?>" <?php echo "$readonly  $nameunit $valueunit"; //
    ?> size="<?php echo $textlength;?>"/>
    <?php if ($state->responses['unit'] != '') echo $feedbackimgunit; 
    if ($options->feedback &&  $question->options->unitgradingtype == 1 && ! $valid_numerical_unit && ! $answerasterisk ){
        if (!empty($empty_unit)) {
            print_string('unitmandatory', 'qtype_numerical'); 
        } else {
            if(isset($question->options->units) && count($question->options->units) > 0){
                $found = 0 ;
                $valid_unit_found = 0 ;
                foreach($question->options->units as $key => $unit){
                    if($state->responses['unit'] == $unit->unit){
                        print_string('unitnotvalid', 'qtype_numerical'); 
                        $valid_unit_found = 1 ;
                        break;
                    }
                }
                if ($valid_unit_found == 0) {
                    print_string('unitunknown', 'qtype_numerical'); 
                }
            }
        }
    }
    ?>
</fieldset>
  </div>
<?php

  } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {// display text

?>
  <div class="answer">
    <fieldset class="clearfix"  id="generalheader">
         <legend class="ftoggler">
            <?php echo get_string('unit', 'quiz');
             ?>
         </legend>
    <input type="hidden" <?php echo $nameunit; echo $valueunit; ?> />
    <?php echo $question->options->units[0]->unit; echo '&nbsp; '; ?>
    </fieldset>
  </div>

  <?php

    //display the units as choice
    } else if ($question->options->showunits == NUMERICALQUESTIONUNITMULTICHOICEDISPLAY){?>
<?php
    //display the units as choice 
    if ( isset($question->options->units)){?>
    <div class="answer">
    <fieldset class="clearfix"  id="generalheader">
         <legend class="ftoggler">
            <?php 
               echo get_string('selectunit', 'qtype_numerical');
             ?>
         </legend>
         <table >

<?php
            // the order is not shuffled
            //however the unitvalue is related to the number value
            // if the response/unit->multiplier is true then
            // the  
           // if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){
           $valid_numerical_unit_index = -1;
            foreach ($question->options->units as $key => $unit) {
                $checked = '';
                $chosen = false;
                $classunit = 0 ;
                $feedbackimgunit = question_get_feedback_image(0); 
                $valid_numerical_unit = false ;
                $validunit = false ;
                $type = 'type="radio"';
                 //    echo "<p> boucle unit $key $unit->unit answerfraction  $answer->fraction classunit $classunit index $valid_numerical_unit_index $key response $response $unit->multiplier  $unit->unit state  <pre>";print_r($answer);echo "</pre></p>";                                       
                if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){   
                    //   $nameunit   = "name=\"".$question->name_prefix;//."unit\"";
                    //    if (isset($response->number) &&  $response->number != false && isset($state->responses['unit']) && $state->responses['unit'] != '' ){
                    // if ($unit->unit == $state->responses['unit']){
                    // test if the numerical value as is or as multiplied by this unit 
                    // could give a good response
                    // we need to extract the numerical and apply it with this unit 
                    // as if it was used
                       $testresponse = $state->responses['answer']/$unit->multiplier ;
                   /* if(isset($state->responses['unit'])){
                         $testresponse .= $state->responses['unit'] ;
                        } */                             
                    $response = $this->apply_unit($testresponse, array($question->options->units[$key])) ;
                    $invalid_unit_found = 0 ;                                    
                    if ($response !== false) {
                        $this->get_tolerance_interval($answer);
                        if($answer->min <= $response && $response <= $answer->max){
                           // then it could give a good response
                           // has it been used ?
                            $classunit = 1; // or max response i.e. 1
                            $feedbackimgunit = question_get_feedback_image($rawgrade);
                            $valid_numerical_unit = true ;
                            $validunit = true ;
                            
                            $valid_numerical_unit_index = $key ;
                        }
                    }
                }
                // if ($unit->unit == $state->responses['unit']){
                    // we have a valid unit response 
                    if($answerasterisk || $valid_numerical_unit_index == $key ){ 
                        $classunit = $rawgrade ; //question_get_feedback_class(1) ; // ;
                    }                 
                // we have a unit response
                // this unit is the one chosen
                // test if the unit give a valid response 
                //   $testresponse = $response->number /$unit->multiplier ;                        
                //   if($answerasterisk || ($answer->min <= $testresponse && $testresponse <= $answer->max)) {
                //     $classunitvalue = $answer->fraction ;
                //   }
                //     echo "<p> dans display $answer->fraction classunit $classunit index $valid_numerical_unit_index $key response $response $unit->multiplier  $unit->unit state  <pre>";print_r($answer);echo "</pre></p>";                                       
                //  if ($state->responses['unit'] == $unit->unit) {
                  if (isset($state->responses['unit']) && $unit->unit == $state->responses['unit']){ 
                    $checked = 'checked="checked"';
                    $chosen = true;
                }else {
                    $checked = '';
                }
                            
                
                $aid = $question->id ;
                $a = new stdClass;
                $a->id   = $question->name_prefix."unit" ;//. "2"
                $a->class =  '' ;
                $a->feedbackimg = '';

                $a->control = "<input $readonly  $nameunit $checked $type  value=\"$key\" />";

                if ($options->correct_responses && $classunit > 0 ) { //$answer->fraction 
                    $a->class = question_get_feedback_class($classunit);
                }
                if (($options->feedback && $chosen) || $options->correct_responses) {
                        $a->feedbackimg = question_get_feedback_image($classunit, $chosen && $options->feedback);
                }
    
                // Print the control
                // Print the answer text
                $a->text = s($unit->unit);
                $row = 0 ;
?>
                        <tr class="<?php echo 'r'.$row = $row ? 0 : 1; ?>">
                            <td class="c0 control " style=" ">
                      <?php echo $a->control.'&nbsp;&nbsp;&nbsp;'; ?>
                            </td>
                            <td style=" " class="c1 text <?php echo $a->class ?>">
                                <label for="<?php echo $a->id ?>">
                        <?php echo $a->text.'<br />'; ?>
                        <?php echo $a->feedbackimg; 
                        if ($options->feedback &&  $classunit == 0  && $chosen && !$answerasterisk){
                            print_string('unitnotvalid', 'qtype_numerical');
                        }else {
                            echo '&nbsp;&nbsp;&nbsp;';
                        }
?>
                                </label>
                            </td>
                        </tr>
                <?php } ?>
                    </table>
             <?php    if ($options->feedback && isset($state->responses['answer'])&& $state->responses['answer'] != '' &&  (!isset($state->responses['unit']) || $state->responses['unit'] == '') && ! $answerasterisk ){
        
            print_string('unitnotselected', 'qtype_numerical'); 
        }
        ?>
        
         </fieldset>    
    </div>
            <?php } // end unit choices
            // display intructions
            // general message first
        }

 if ($question->options->unitsleft == 1 ){?>

 <div class="answer">
    <fieldset class="clearfix"  id="generalheader">
         <legend class="ftoggler">
            <?php 
                echo $OUTPUT->help_icon('validnumberformats', 'qtype_numerical', ''); 
                echo get_string('datasetnumber', 'quiz'); 
            ?>
         </legend>
    <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly  $nameanswer $valueanswer"; //
    ?> size="<?php echo $textlength;?>"/>
    <?php echo $feedbackimg; ?>
   </fieldset>
  </div>
<?php }

        //</div> put a div to enclose the answer and unit elements
?>
</div>


     <?php if (!empty($question->options->instructions)){?>
       <div class="feedback">
        <fieldset class="clearfix"  id="generalheader">
         <legend class="ftoggler">
            <?php echo get_string('instructions', 'auth'); ?>
         </legend>
        <div>
     <?php echo format_text($question->options->instructions, $question->options->instructionsformat, $formatoptions, $cmoptions->course);?>
        </div>
     </fieldset>
     </div>
     <?php }?>

  <?php if ($feedback) { ?>
    <div class="feedback">
      <?php echo $feedback ?>
    </div>
  <?php } ?>
  <?php $this->print_question_submit_buttons($question, $state, $cmoptions, $options); ?>
</div>
