MDL-20296 creating common functions to handle units in numerical, calculated and...
[moodle.git] / question / type / numerical / display.html
CommitLineData
aeb15530 1<?php
b003b818 2// as the entry is controlled the question type here is numerical
3 $nameunit = "name=\"".$question->name_prefix."unit\"";
4 $nameanswer = "name=\"".$question->name_prefix."answer\"";
aeb15530 5//if left unit is placed before answer so we need to answer field text and a center pice for the show units options
b003b818 6// split the respnse in 2 element numerical and units
7/*if (!empty($question->options->showunits)){
8 if(isset($question->options->units) && count($question->options->units)){
9 $length = 0;
10 $goodkey = 0 ;
11 foreach ($question->options->units as $key => $unit){
aeb15530 12 if (false === stripos($value, $unit->unit)){
b003b818 13 }else{
aeb15530 14
b003b818 15 if(strlen($unit->unit)> $length) {$length =strlen($unit->unit);
16 $goodkey = $key ;
17 }
18 }
19 }
20 if($length){
21 $value = str_replace($question->options->units[$goodkey]->unit, '' , $value);
22 }
23 $state->responses['unit']=$goodkey ;<div class="ablock clearfix">
24 }
aeb15530 25
b003b818 26 }*/
27 if (isset($state->responses['answer']) && $state->responses['answer']!='') {
28 $valueanswer = ' value="'.s($state->responses['answer']).'" ';
29 } else {
30 $valueanswer = ' value="" ';
31 }
32 if (isset($state->responses['unit']) && $state->responses['unit']!='') {
33 $valueunit = ' value="'.s($state->responses['unit']).'" ';
34 } else {
aeb15530 35
b003b818 36 $valueunit = ' value="" ';
04e91671 37 if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ){
aeb15530
PS
38 $valueunit = ' value="'.s($question->options->units[0]->unit).'" ';
39 }
b003b818 40 }
aeb15530 41 // put a div to enclose the answer and unit elements<div class="clearfix"></div>
b003b818 42 $textlength = 20 ;
43?>
44
45<?php
46 if ( $question->options->unitsleft == 0 ){ ?>
47
48 <div class="answer">
49 <fieldset class="answer" id="generalheader">
4454447d
PS
50 <legend class="ftoggler">
51 <?php echo get_string('datasetnumber', 'quiz') ; ?>
52 </legend>
b003b818 53 <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly $nameanswer $valueanswer"; //
54 ?> size="<?php echo $textlength;?>" />
55 <?php echo $feedbackimg; ?>
56</fieldset>
57 </div>
aeb15530
PS
58
59
b003b818 60<?php }// display unit
aeb15530 61 $class = '';
b003b818 62 $feedbackimgunit = '';
63 if ($options->feedback){
3987461a
PP
64 $classunitvalue = 0 ;
65 if ( isset($state->responses['unit']) && $state->responses['unit']!='' && count( $question->options->units) > 0){//&& isset($question->options->units['0'])
66 foreach($question->options->units as $key => $unitv){
67 if ($unitv->unit == trim($state->responses['unit'])){
68 $classunitvalue = 1 ;
69 // break ;
70 }
71 }
72 }
73 // $classunitvalue = 0 ;
74 $classunit = question_get_feedback_class($classunitvalue); //1-$this->raw_unitpenalty
75 $feedbackimgunit = question_get_feedback_image($classunitvalue, $options->feedback);
b003b818 76 }
04e91671 77 if ($question->options->showunits == NUMERICALQUESTIONUNITNODISPLAY || (isset($question->options->units) && count($question->options->units) == 0 )) {// no unit display or tested
aeb15530 78
b003b818 79?>
80 <div class="answer">
aeb15530
PS
81 <input type="hidden"
82 <?php
83 echo $nameunit;
84 ?>
b003b818 85 value ="" />
86 </div>
87<?php
04e91671 88 } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTINPUTDISPLAY ) {// display unit text input
aeb15530 89?>
b003b818 90<div class="answer">
91 <fieldset class="clearfix" id="generalheader">
4454447d
PS
92 <legend class="ftoggler">
93 <?php echo get_string('unit', 'quiz'); ?>
94 </legend>
b003b818 95 <input type="text" class="<?php echo $classunit; ?>" <?php echo "$readonly $nameunit $valueunit"; //
96 ?> size="<?php echo $textlength;?>"/>
97 <?php echo $feedbackimgunit; ?>
98</fieldset>
99 </div>
aeb15530 100<?php
b003b818 101
04e91671 102 } else if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {// display text
aeb15530 103
b003b818 104?>
105 <div class="answer">
106 <fieldset class="clearfix" id="generalheader">
4454447d
PS
107 <legend class="ftoggler">
108 <?php echo get_string('unit', 'quiz');
4454447d
PS
109 ?>
110 </legend>
aeb15530
PS
111 <input type="hidden"
112 <?php
113 echo $nameunit; echo $valueunit ;
b003b818 114
aeb15530 115 ?>
b003b818 116 />
117 <?php echo $question->options->units[0]->unit ;
118
aeb15530 119 echo '&nbsp; ' ;
b003b818 120 ?>
121 </fieldset>
122 </div>
aeb15530 123
b003b818 124 <?php
125
aeb15530
PS
126 //display the units as choice
127
04e91671 128 } else if ($question->options->showunits == NUMERICALQUESTIONUNITMULTICHOICEDISPLAY){?>
b003b818 129
130
aeb15530
PS
131
132<?php
133 //display the units as choice
04e91671 134 if ( isset($question->options->units)){?>
b003b818 135 <div class="answer">
aa384ade
PP
136 <fieldset class="clearfix" id="generalheader">
137 <legend class="ftoggler">
138 <?php echo get_string('unit', 'quiz');
139 ?>
140 </legend>
b003b818 141 <table class="answer">
142
aeb15530 143<?php foreach ($question->options->units as $key => $unit) {
b003b818 144 $checked = '';
145 $chosen = false;
aeb15530 146
b003b818 147 $type = 'type="radio"';
148 // $nameunit = "name=\"".$question->name_prefix;//."unit\"";
aa384ade 149 if (isset($state->responses['unit']) && $state->responses['unit'] != '' ){
b003b818 150 if ($state->responses['unit'] == $unit->unit) {
151 $checked = 'checked="checked"';
152 $chosen = true;
153 }
154 }else if ($key == 0) {
155 $checked = 'checked="checked"';
156 $chosen = true;
157 }
aeb15530
PS
158
159 $aid = $question->id ;
b003b818 160 $a = new stdClass;
161 $a->id = $question->name_prefix."unit" ;//. "2"
162 $a->class = '';
163 $a->feedbackimg = '';
164
165 // Print the control
166 $a->control = "<input $readonly $nameunit $checked $type value=\"$key\" />";
167
168 if ($options->correct_responses && $answer->fraction > 0) {
169 $a->class = question_get_feedback_class(1);
170 }
171 if (($options->feedback && $chosen) || $options->correct_responses) {
172 if ($type == ' type="checkbox" ') {
173 $a->feedbackimg = question_get_feedback_image($answer->fraction > 0 ? 1 : 0, $chosen && $options->feedback);
174 } else {
175 $a->feedbackimg = question_get_feedback_image($answer->fraction, $chosen && $options->feedback);
176 }
177 }
178
179 // Print the answer text
180 $a->text = format_text($unit->unit, FORMAT_MOODLE, $formatoptions, $cmoptions->course);
181 $row = 0 ;
182
183?>
184 <tr class="<?php echo 'r'.$row = $row ? 0 : 1; ?>">
185 <td class="c0 control">
186 <?php echo $a->control; ?>
187 </td>
188 <td class="c1 text <?php echo $a->class ?>">
189 <label for="<?php echo $a->id ?>">
190 <?php echo $a->text; ?>
191 <?php echo $a->feedbackimg; ?>
192 </label>
193 </td>
194 </tr>
195 <?php } ?>
196
197 </table>
aa384ade 198 </fieldset>
aeb15530
PS
199
200 </div>
b003b818 201 <?php } // end unit choices
202 // display intructions
203 // general message first
204 }
aeb15530 205
b003b818 206 if ($question->options->unitsleft == 1 ){?>
aeb15530 207
b003b818 208 <div class="answer">
209 <fieldset class="clearfix" id="generalheader">
4454447d
PS
210 <legend class="ftoggler">
211 <?php echo get_string('datasetnumber', 'quiz'); ?>
212 </legend>
b003b818 213 <input type="text" class="<?php echo $class; ?>" <?php echo "$readonly $nameanswer $valueanswer"; //
214 ?> size="<?php echo $textlength;?>"/>
215 <?php echo $feedbackimg; ?>
216 </fieldset>
aeb15530
PS
217 </div>
218<?php }
b003b818 219 //</div> put a div to enclose the answer and unit elements
220?>
221
222
223
224 <div class="feedback">
225 <fieldset class="clearfix" id="generalheader">
4454447d
PS
226 <legend class="ftoggler">
227 <?php echo get_string('instructions', 'auth'); ?>
228 </legend>
b003b818 229 <div class="feedback">
5774a5f5 230 <fieldset class="clearfix" id="generalheader">
4454447d 231 <legend class="ftoggler">
5774a5f5 232 <?php echo get_string('validnumberformats', 'qtype_numerical');?>
4454447d 233 </legend>
5774a5f5 234 <?php echo get_string('validnumbers', 'qtype_numerical');?>
aeb15530 235 </fieldset>
b003b818 236 </div>
237 <?php if (!empty($question->options->instructions)){?>
238 <div>
239 <?php echo format_text($question->options->instructions, true, $formatoptions, $cmoptions->course);?>
aeb15530
PS
240 </div>
241 <?php }?>
242 </fieldset>
243 </div>
b003b818 244
245 <?php ?>
246