953f27f59fdd6f43bbab96c002e721f7349837fc
[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          </legend>
64     <input type="text" class="<?php echo $classunit; ?>" <?php echo "$readonly  $nameunit $valueunit"; //
65     ?> size="<?php echo $textlength;?>"/>
66     <?php echo $feedbackimgunit; 
67     if ($options->feedback &&  $question->options->unitgradingtype == 1 && ! $valid_numerical_unit && ! $answerasterisk ){
68         if ( $empty_unit) {
69             print_string('unitmandatory', 'qtype_numerical'); 
70         }else {
71             if(isset($question->options->units) && count($question->options->units) > 0){
72                 $found = 0 ;
73                 $valid_unit_found = 0 ;
74                 foreach($question->options->units as $key => $unit){
75                     if($state->responses['unit'] == $unit->unit){
76                         print_string('unitnotvalid', 'qtype_numerical'); 
77                         $valid_unit_found = 1 ;
78                         break;
79                     }
80                 }
81                 if ($valid_unit_found == 0) {
82                     print_string('unitunknown', 'qtype_numerical'); 
83                 }
84             }
85         }
86     }
87     ?>
88 </fieldset>
89   </div>
90 <?php
92   } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {// display text
94 ?>
95   <div class="answer">
96     <fieldset class="clearfix"  id="generalheader">
97          <legend class="ftoggler">
98             <?php echo get_string('unit', 'quiz');
99              ?>
100          </legend>
101     <input type="hidden"
102     <?php
103     echo $nameunit; echo $valueunit ;
105     ?>
106    />
107  <?php   echo $question->options->units[0]->unit  ;
109          echo '&nbsp; ' ;
110      ?>
111     </fieldset>
112   </div>
114   <?php
116     //display the units as choice
118     } else if ($question->options->showunits == NUMERICALQUESTIONUNITMULTICHOICEDISPLAY){?>
122 <?php
123     //display the units as choice 
124     if ( isset($question->options->units)){?>
125     <div class="answer">
126     <fieldset class="clearfix"  id="generalheader">
127          <legend class="ftoggler">
128             <?php 
129                echo get_string('selectunit', 'qtype_numerical');
130              ?>
131          </legend>
132          <table >
134 <?php       
135             // the order is not shuffled
136             //however the unitvalue is related to the number value
137             // if the response/unit->multiplier is true then
138             // the  
139            // if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){
140            $valid_numerical_unit_index = -1;
141             foreach ($question->options->units as $key => $unit) {
142                 $checked = '';
143                 $chosen = false;
144                 $classunit = 0 ;
145                 $feedbackimgunit = question_get_feedback_image(0); 
146                 $valid_numerical_unit = false ;
147                 $validunit = false ;
148                 $type = 'type="radio"';
149                  //    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>";                                       
150                 if(isset($state->responses['unit']) && $state->responses['unit'] != '' ){   
151                     //   $nameunit   = "name=\"".$question->name_prefix;//."unit\"";
152                     //    if (isset($response->number) &&  $response->number != false && isset($state->responses['unit']) && $state->responses['unit'] != '' ){
153                     // if ($unit->unit == $state->responses['unit']){
154                     // test if the numerical value as is or as multiplied by this unit 
155                     // could give a good response
156                     // we need to extract the numerical and apply it with this unit 
157                     // as if it was used
158                        $testresponse = $state->responses['answer']/$unit->multiplier ;
159                    /* if(isset($state->responses['unit'])){
160                          $testresponse .= $state->responses['unit'] ;
161                         } */                             
162                     $response = $this->apply_unit($testresponse, array($question->options->units[$key])) ;
163                     $invalid_unit_found = 0 ;                                    
164                     if ($response !== false) {
165                         $this->get_tolerance_interval($answer);
166                         if($answer->min <= $response && $response <= $answer->max){
167                            // then it could give a good response
168                            // has it been used ?
169                             $classunit = 1; // or max response i.e. 1
170                             $feedbackimgunit = question_get_feedback_image($rawgrade);
171                             $valid_numerical_unit = true ;
172                             $validunit = true ;
173                             
174                             $valid_numerical_unit_index = $key ;
175                         }
176                     }
177                 }
178                 // if ($unit->unit == $state->responses['unit']){
179                     // we have a valid unit response 
180                     if($answerasterisk || $valid_numerical_unit_index == $key ){ 
181                         $classunit = $rawgrade ; //question_get_feedback_class(1) ; // ;
182                     }                 
183                 // we have a unit response
184                 // this unit is the one chosen
185                 // test if the unit give a valid response 
186                 //   $testresponse = $response->number /$unit->multiplier ;                        
187                 //   if($answerasterisk || ($answer->min <= $testresponse && $testresponse <= $answer->max)) {
188                 //     $classunitvalue = $answer->fraction ;
189                 //   }
190                 //     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>";                                       
191                 //  if ($state->responses['unit'] == $unit->unit) {
192                   if (isset($state->responses['unit']) && $unit->unit == $state->responses['unit']){ 
193                     $checked = 'checked="checked"';
194                     $chosen = true;
195                 }else {
196                     $checked = '';
197                 }
198                             
199                 
200                 $aid = $question->id ;
201                 $a = new stdClass;
202                 $a->id   = $question->name_prefix."unit" ;//. "2"
203                 $a->class =  '' ;
204                 $a->feedbackimg = '';
205     
206                 $a->control = "<input $readonly  $nameunit $checked $type  value=\"$key\" />";
207     
208                 if ($options->correct_responses && $classunit > 0 ) { //$answer->fraction 
209                     $a->class = question_get_feedback_class($classunit);
210                 }
211                 if (($options->feedback && $chosen) || $options->correct_responses) {
212                         $a->feedbackimg = question_get_feedback_image($classunit, $chosen && $options->feedback);
213                 }
214     
215                 // Print the control
216                 // Print the answer text
217                 $a->text = format_text($unit->unit, FORMAT_MOODLE, $formatoptions, $cmoptions->course);
218                 $row = 0 ;
219     
220 ?>
221                         <tr class="<?php echo 'r'.$row = $row ? 0 : 1; ?>">
222                             <td class="c0 control " style=" ">
223                       <?php echo $a->control.'&nbsp;&nbsp;&nbsp;'; ?>
224                             </td>
225                             <td style=" " class="c1 text <?php echo $a->class ?>">
226                                 <label for="<?php echo $a->id ?>">
227                         <?php echo $a->text.'<br />'; ?>
228                         <?php echo $a->feedbackimg; 
229                         if ($options->feedback &&  $classunit == 0  && $chosen && !$answerasterisk){
230                             print_string('unitnotvalid', 'qtype_numerical');
231                         }else {
232                             echo '&nbsp;&nbsp;&nbsp;';
233                         }
234 ?>
235                                 </label>
236                             </td>
237                         </tr>
238                 <?php } ?>
239                     </table>
240              <?php    if ($options->feedback && isset($state->responses['answer'])&& $state->responses['answer'] != '' &&  (!isset($state->responses['unit']) || $state->responses['unit'] == '') && ! $answerasterisk ){
241         
242             print_string('unitnotselected', 'qtype_numerical'); 
243         }
244         ?>
245         
246          </fieldset>    
247     </div>
248             <?php } // end unit choices
249             // display intructions
250             // general message first
251         }
253  if ($question->options->unitsleft == 1 ){?>
255  <div class="answer">
256     <fieldset class="clearfix"  id="generalheader">
257          <legend class="ftoggler">
258             <?php 
259                 echo $OUTPUT->help_icon('validnumberformats', 'qtype_numerical', ''); 
260                 echo get_string('datasetnumber', 'quiz'); 
261             ?>
262          </legend>
263     <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly  $nameanswer $valueanswer"; //
264     ?> size="<?php echo $textlength;?>"/>
265     <?php echo $feedbackimg; ?>
266    </fieldset>
267   </div>
268 <?php }
270         //</div> put a div to enclose the answer and unit elements
271 ?>
272 </div>
275      <?php if (!empty($question->options->instructions)){?>
276        <div class="feedback">
277         <fieldset class="clearfix"  id="generalheader">
278          <legend class="ftoggler">
279             <?php echo get_string('instructions', 'auth'); ?>
280          </legend>
281         <div>
282      <?php   echo  format_text($question->options->instructions, $question->options->instructionsformat, $formatoptions, $cmoptions->course);?>
283         </div>
284      </fieldset>
285      </div>
286      <?php }?>
288   <?php if ($feedback) { ?>
289     <div class="feedback">
290       <?php echo $feedback ?>
291     </div>
292   <?php } ?>
293   <?php $this->print_question_submit_buttons($question, $state, $cmoptions, $options); ?>
294 </div>