MDL-10110 MDL-20296 improve grading when unit input is empty
[moodle.git] / question / type / numerical / display.html
1 <div class="qtext">
2   <?php echo $questiontext; ?>
3 </div>
5 <div class="ablock clearfix">
6   <div class="prompt">
7     <?php echo get_string("answer", "quiz").': '; ?>
8   </div>
9 <div  class="que numerical clearfix">
10 <?php
11         $textlength = 20 ;
12 ?>
14 <?php
15  if ( $question->options->unitsleft == 0 ){ ?>
17   <div class="answer">
18     <fieldset class="answer"  id="generalheader">
19          <legend class="ftoggler">
20             <?php 
21                 echo $OUTPUT->help_icon('validnumberformats', 'qtype_numerical', '');
22                 echo get_string('datasetnumber', 'quiz') ; 
23             ?>
24          </legend>
25     <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly  $nameanswer $valueanswer"; //
26     ?> size="<?php echo $textlength;?>" />
27     <?php echo $feedbackimg; 
28     if(! $answerasterisk  && $question->options->unitgradingtype != 0 &&  $unit_in_numerical_answer){
29         print_string('nonvalidcharactersinnumber', 'qtype_numerical'); 
30     }
31         
33     ?>
34 </fieldset>
35   </div>
38 <?php }// display unit
39         
40         if ($question->options->showunits == NUMERICALQUESTIONUNITNODISPLAY || (isset($question->options->units) && count($question->options->units) == 0 )) {// no unit display or tested
42 ?>
43   <div class="answer">
44     <input type="hidden"
45     <?php
46     echo $nameunit;
47     ?>
48    value ="" />
49   </div>
50 <?php
51   } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTINPUTDISPLAY  ) {// display unit text input
52 ?>
53 <div class="answer numerical ">
54     <fieldset class="clearfix"  id="generalheader">
55          <legend class="ftoggler">
56             <?php
57                 if ( $question->options->unitgradingtype == 0 ){
58                     echo $OUTPUT->help_icon('unitoptional', 'qtype_numerical', '');
59                 }else { // unitgradingtype ==  1 
60                     echo $OUTPUT->help_icon('unitmandatory', 'qtype_numerical', '');
61                 }             
62                 echo get_string('unit', 'quiz'); 
63                 if ($state->responses['unit'] == '')$classunit = '';
64                 ?>
65          </legend>
66     <input type="text" class="<?php echo $classunit; ?>" <?php echo "$readonly  $nameunit $valueunit"; //
67     ?> size="<?php echo $textlength;?>"/>
68     <?php if ($state->responses['unit'] != '') echo $feedbackimgunit; 
69     if ($options->feedback &&  $question->options->unitgradingtype == 1 && ! $valid_numerical_unit && ! $answerasterisk ){
70         if ( $empty_unit) {
71             print_string('unitmandatory', 'qtype_numerical'); 
72         }else {
73             if(isset($question->options->units) && count($question->options->units) > 0){
74                 $found = 0 ;
75                 $valid_unit_found = 0 ;
76                 foreach($question->options->units as $key => $unit){
77                     if($state->responses['unit'] == $unit->unit){
78                         print_string('unitnotvalid', 'qtype_numerical'); 
79                         $valid_unit_found = 1 ;
80                         break;
81                     }
82                 }
83                 if ($valid_unit_found == 0) {
84                     print_string('unitunknown', 'qtype_numerical'); 
85                 }
86             }
87         }
88     }
89     ?>
90 </fieldset>
91   </div>
92 <?php
94   } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {// display text
96 ?>
97   <div class="answer">
98     <fieldset class="clearfix"  id="generalheader">
99          <legend class="ftoggler">
100             <?php echo get_string('unit', 'quiz');
101              ?>
102          </legend>
103     <input type="hidden"
104     <?php
105     echo $nameunit; echo $valueunit ;
107     ?>
108    />
109  <?php   echo $question->options->units[0]->unit  ;
111          echo '&nbsp; ' ;
112      ?>
113     </fieldset>
114   </div>
116   <?php
118     //display the units as choice
120     } else if ($question->options->showunits == NUMERICALQUESTIONUNITMULTICHOICEDISPLAY){?>
124 <?php
125     //display the units as choice 
126     if ( isset($question->options->units)){?>
127     <div class="answer">
128     <fieldset class="clearfix"  id="generalheader">
129          <legend class="ftoggler">
130             <?php 
131                echo get_string('selectunit', 'qtype_numerical');
132              ?>
133          </legend>
134          <table >
136 <?php       
137             // the order is not shuffled
138             //however the unitvalue is related to the number value
139             // if the response/unit->multiplier is true then
140             // the  
141            // if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){
142            $valid_numerical_unit_index = -1;
143             foreach ($question->options->units as $key => $unit) {
144                 $checked = '';
145                 $chosen = false;
146                 $classunit = 0 ;
147                 $feedbackimgunit = question_get_feedback_image(0); 
148                 $valid_numerical_unit = false ;
149                 $validunit = false ;
150                 $type = 'type="radio"';
151                  //    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>";                                       
152                 if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){   
153                     //   $nameunit   = "name=\"".$question->name_prefix;//."unit\"";
154                     //    if (isset($response->number) &&  $response->number != false && isset($state->responses['unit']) && $state->responses['unit'] != '' ){
155                     // if ($unit->unit == $state->responses['unit']){
156                     // test if the numerical value as is or as multiplied by this unit 
157                     // could give a good response
158                     // we need to extract the numerical and apply it with this unit 
159                     // as if it was used
160                        $testresponse = $state->responses['answer']/$unit->multiplier ;
161                    /* if(isset($state->responses['unit'])){
162                          $testresponse .= $state->responses['unit'] ;
163                         } */                             
164                     $response = $this->apply_unit($testresponse, array($question->options->units[$key])) ;
165                     $invalid_unit_found = 0 ;                                    
166                     if ($response !== false) {
167                         $this->get_tolerance_interval($answer);
168                         if($answer->min <= $response && $response <= $answer->max){
169                            // then it could give a good response
170                            // has it been used ?
171                             $classunit = 1; // or max response i.e. 1
172                             $feedbackimgunit = question_get_feedback_image($rawgrade);
173                             $valid_numerical_unit = true ;
174                             $validunit = true ;
175                             
176                             $valid_numerical_unit_index = $key ;
177                         }
178                     }
179                 }
180                 // if ($unit->unit == $state->responses['unit']){
181                     // we have a valid unit response 
182                     if($answerasterisk || $valid_numerical_unit_index == $key ){ 
183                         $classunit = $rawgrade ; //question_get_feedback_class(1) ; // ;
184                     }                 
185                 // we have a unit response
186                 // this unit is the one chosen
187                 // test if the unit give a valid response 
188                 //   $testresponse = $response->number /$unit->multiplier ;                        
189                 //   if($answerasterisk || ($answer->min <= $testresponse && $testresponse <= $answer->max)) {
190                 //     $classunitvalue = $answer->fraction ;
191                 //   }
192                 //     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>";                                       
193                 //  if ($state->responses['unit'] == $unit->unit) {
194                   if (isset($state->responses['unit']) && $unit->unit == $state->responses['unit']){ 
195                     $checked = 'checked="checked"';
196                     $chosen = true;
197                 }else {
198                     $checked = '';
199                 }
200                             
201                 
202                 $aid = $question->id ;
203                 $a = new stdClass;
204                 $a->id   = $question->name_prefix."unit" ;//. "2"
205                 $a->class =  '' ;
206                 $a->feedbackimg = '';
207     
208                 $a->control = "<input $readonly  $nameunit $checked $type  value=\"$key\" />";
209     
210                 if ($options->correct_responses && $classunit > 0 ) { //$answer->fraction 
211                     $a->class = question_get_feedback_class($classunit);
212                 }
213                 if (($options->feedback && $chosen) || $options->correct_responses) {
214                         $a->feedbackimg = question_get_feedback_image($classunit, $chosen && $options->feedback);
215                 }
216     
217                 // Print the control
218                 // Print the answer text
219                 $a->text = format_text($unit->unit, FORMAT_MOODLE, $formatoptions, $cmoptions->course);
220                 $row = 0 ;
221     
222 ?>
223                         <tr class="<?php echo 'r'.$row = $row ? 0 : 1; ?>">
224                             <td class="c0 control " style=" ">
225                       <?php echo $a->control.'&nbsp;&nbsp;&nbsp;'; ?>
226                             </td>
227                             <td style=" " class="c1 text <?php echo $a->class ?>">
228                                 <label for="<?php echo $a->id ?>">
229                         <?php echo $a->text.'<br />'; ?>
230                         <?php echo $a->feedbackimg; 
231                         if ($options->feedback &&  $classunit == 0  && $chosen && !$answerasterisk){
232                             print_string('unitnotvalid', 'qtype_numerical');
233                         }else {
234                             echo '&nbsp;&nbsp;&nbsp;';
235                         }
236 ?>
237                                 </label>
238                             </td>
239                         </tr>
240                 <?php } ?>
241                     </table>
242              <?php    if ($options->feedback && isset($state->responses['answer'])&& $state->responses['answer'] != '' &&  (!isset($state->responses['unit']) || $state->responses['unit'] == '') && ! $answerasterisk ){
243         
244             print_string('unitnotselected', 'qtype_numerical'); 
245         }
246         ?>
247         
248          </fieldset>    
249     </div>
250             <?php } // end unit choices
251             // display intructions
252             // general message first
253         }
255  if ($question->options->unitsleft == 1 ){?>
257  <div class="answer">
258     <fieldset class="clearfix"  id="generalheader">
259          <legend class="ftoggler">
260             <?php 
261                 echo $OUTPUT->help_icon('validnumberformats', 'qtype_numerical', ''); 
262                 echo get_string('datasetnumber', 'quiz'); 
263             ?>
264          </legend>
265     <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly  $nameanswer $valueanswer"; //
266     ?> size="<?php echo $textlength;?>"/>
267     <?php echo $feedbackimg; ?>
268    </fieldset>
269   </div>
270 <?php }
272         //</div> put a div to enclose the answer and unit elements
273 ?>
274 </div>
277      <?php if (!empty($question->options->instructions)){?>
278        <div class="feedback">
279         <fieldset class="clearfix"  id="generalheader">
280          <legend class="ftoggler">
281             <?php echo get_string('instructions', 'auth'); ?>
282          </legend>
283         <div>
284      <?php   echo  format_text($question->options->instructions, $question->options->instructionsformat, $formatoptions, $cmoptions->course);?>
285         </div>
286      </fieldset>
287      </div>
288      <?php }?>
290   <?php if ($feedback) { ?>
291     <div class="feedback">
292       <?php echo $feedback ?>
293     </div>
294   <?php } ?>
295   <?php $this->print_question_submit_buttons($question, $state, $cmoptions, $options); ?>
296 </div>