function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)
    {
        global $QTYPES, $CFG, $USER;
        $readonly = empty($options->readonly) ? '' : 'readonly="readonly"';
        $disabled = empty($options->readonly) ? '' : 'disabled="disabled"';
        $formatoptions = new stdClass();
        $formatoptions->noclean = true;
        $formatoptions->para = false;
        $nameprefix = $question->name_prefix;
        // adding an icon with alt to warn user this is a fill in the gap question
        // MDL-7497
        if (!empty($USER->screenreader)) {
            echo "<img src=\"{$CFG->wwwroot}/question/type/{$question->qtype}/icon.gif\" " . "class=\"icon\" alt=\"" . get_string('clozeaid', 'qtype_multichoice') . "\" />  ";
        }
        echo '<div class="ablock clearfix">';
        // For this question type, we better print the image on top:
        if ($image = get_question_image($question)) {
            echo '<img class="qimage" src="' . $image . '" alt="" /><br />';
        }
        $qtextremaining = format_text($question->questiontext, $question->questiontextformat, $formatoptions, $cmoptions->course);
        $strfeedback = get_string('feedback', 'quiz');
        // The regex will recognize text snippets of type {#X}
        // where the X can be any text not containg } or white-space characters.
        while (ereg('\\{#([^[:space:]}]*)}', $qtextremaining, $regs)) {
            $qtextsplits = explode($regs[0], $qtextremaining, 2);
            echo $qtextsplits[0];
            echo "<label>";
            // MDL-7497
            $qtextremaining = $qtextsplits[1];
            $positionkey = $regs[1];
            if (isset($question->options->questions[$positionkey]) && $question->options->questions[$positionkey] != '') {
                $wrapped =& $question->options->questions[$positionkey];
                $answers =& $wrapped->options->answers;
                // $correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state);
                $inputname = $nameprefix . $positionkey;
                if (isset($state->responses[$positionkey])) {
                    $response = $state->responses[$positionkey];
                } else {
                    $response = null;
                }
                // Determine feedback popup if any
                $popup = '';
                $style = '';
                $feedbackimg = '';
                $feedback = '';
                $correctanswer = '';
                $strfeedbackwrapped = $strfeedback;
                $testedstate = clone $state;
                if (($correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $testedstate)) !== false) {
                    if ($options->readonly && $options->correct_responses) {
                        $delimiter = '';
                        if ($correctanswers) {
                            foreach ($correctanswers as $ca) {
                                switch ($wrapped->qtype) {
                                    case 'numerical':
                                    case 'shortanswer':
                                    case 'shortanswerwiris':
                                        $correctanswer .= $delimiter . $ca;
                                        break;
                                    case 'multichoice':
                                    case 'multichoicewiris':
                                        if (isset($answers[$ca])) {
                                            $correctanswer .= $delimiter . $answers[$ca]->answer;
                                        }
                                        break;
                                }
                                $delimiter = ', ';
                            }
                        }
                    }
                    if ($correctanswer != '') {
                        $feedback = '<div class="correctness">';
                        $feedback .= get_string('correctansweris', 'quiz', s($correctanswer, true));
                        $feedback .= '</div>';
                    }
                }
                if ($options->feedback) {
                    $chosenanswer = null;
                    switch ($wrapped->qtype) {
                        case 'shortanswerwiris':
                            $index = 0;
                            $maxgrade = 0.0;
                            if (isset($state->grades) && isset($state->grades[$positionkey])) {
                                foreach ($answers as $answer) {
                                    if (isset($state->grades[$positionkey][$index]) && $state->grades[$positionkey][$index] > $maxgrade) {
                                        $chosenanswer = clone $answer;
                                        $maxgrade = $state->grades[$positionkey][$index];
                                    }
                                    $index++;
                                }
                            }
                            //Try string comparation if wiris grade is zero for this response.
                            if (!empty($chosenanswer)) {
                                break;
                            }
                        case 'numerical':
                        case 'shortanswer':
                            $testedstate = clone $state;
                            $testedstate->responses[''] = $response;
                            foreach ($answers as $answer) {
                                if ($QTYPES[$wrapped->qtype]->test_response($wrapped, $testedstate, $answer)) {
                                    $chosenanswer = clone $answer;
                                    break;
                                }
                            }
                            break;
                        case 'multichoicewiris':
                        case 'multichoice':
                            if (isset($answers[$response])) {
                                $chosenanswer = clone $answers[$response];
                            }
                            break;
                        default:
                            break;
                    }
                    // Set up a default chosenanswer so that all non-empty wrong
                    // answers are highlighted red
                    if (empty($chosenanswer) && $response != '') {
                        $chosenanswer = new stdClass();
                        $chosenanswer->fraction = 0.0;
                    }
                    if (isset($chosenanswer->feedback) && $chosenanswer->feedback !== '') {
                        $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback . $chosenanswer->feedback));
                        if ($options->readonly && $options->correct_responses) {
                            $strfeedbackwrapped = get_string('correctanswerandfeedback', 'qtype_multianswer');
                        } else {
                            $strfeedbackwrapped = get_string('feedback', 'quiz');
                        }
                        $popup = " onmouseover=\"return overlib('{$feedback}', STICKY, MOUSEOFF, CAPTION, '{$strfeedbackwrapped}', FGCOLOR, '#FFFFFF');\" " . " onmouseout=\"return nd();\" ";
                    }
                    /// Determine style
                    if ($options->feedback && $response != '') {
                        $style = 'class = "' . question_get_feedback_class($chosenanswer->fraction) . '"';
                        $feedbackimg = question_get_feedback_image($chosenanswer->fraction);
                    } else {
                        $style = '';
                        $feedbackimg = '';
                    }
                }
                if ($feedback !== '' && $popup === '') {
                    $strfeedbackwrapped = get_string('correctanswer', 'qtype_multianswer');
                    $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback));
                    $popup = " onmouseover=\"return overlib('{$feedback}', STICKY, MOUSEOFF, CAPTION, '{$strfeedbackwrapped}', FGCOLOR, '#FFFFFF');\" " . " onmouseout=\"return nd();\" ";
                }
                // Print the input control
                $editor = false;
                switch ($wrapped->qtype) {
                    case 'shortanswerwiris':
                        mb_parse_str($wrapped->options->wiris->eqoption, $eqoptionArray);
                        $editor = isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true';
                    case 'shortanswer':
                    case 'numerical':
                        if (!$editor) {
                            $size = 1;
                            foreach ($answers as $answer) {
                                if (strlen(trim($answer->answer)) > $size) {
                                    $assembledAnswer = wrsqz_assemble(wrsqz_variablesToText($answer->answer), $state->vars);
                                    $size = strlen(trim($assembledAnswer));
                                }
                            }
                            if (strlen(trim($response)) > $size) {
                                $size = strlen(trim($response)) + 1;
                            }
                            $size = $size + rand(0, $size * 0.15);
                            $size > 60 ? $size = 60 : ($size = $size);
                            $styleinfo = "size=\"{$size}\"";
                            /**
                             * Uncomment the following lines if you want to limit for small sizes.
                             * Results may vary with browsers see MDL-3274
                             */
                            /*
                            if ($size < 2) {
                                $styleinfo = 'style="width: 1.1em;"';
                            }
                            if ($size == 2) {
                                $styleinfo = 'style="width: 1.9em;"';
                            }
                            if ($size == 3) {
                                $styleinfo = 'style="width: 2.3em;"';
                            }
                            if ($size == 4) {
                                $styleinfo = 'style="width: 2.8em;"';
                            }
                            */
                            echo "<input {$style} {$readonly} {$popup} name=\"{$inputname}\"";
                            echo "  type=\"text\" value=\"" . s($response, true) . "\" " . $styleinfo . " /> ";
                        } else {
                            echo wrsqz_embeddedShortanswerInput($wrapped, $response, $inputname, $style, $readonly, $popup);
                        }
                        if ($feedback !== '' && !empty($USER->screenreader)) {
                            echo "<img src=\"{$CFG->pixpath}/i/feedback.gif\" alt=\"{$feedback}\" />";
                        }
                        echo $feedbackimg;
                        break;
                    case 'multichoicewiris':
                    case 'multichoice':
                        if ($wrapped->options->layout == 0) {
                            $outputoptions = '<option></option>';
                            // Default empty option
                            foreach ($answers as $mcanswer) {
                                $selected = '';
                                if ($response == $mcanswer->id) {
                                    $selected = ' selected="selected"';
                                }
                                $outputoptions .= "<option value=\"{$mcanswer->id}\"{$selected}>" . s($mcanswer->answer, true) . '</option>';
                            }
                            // In the next line, $readonly is invalid HTML, but it works in
                            // all browsers. $disabled would be valid, but then the JS for
                            // displaying the feedback does not work. Of course, we should
                            // not be relying on JS (for accessibility reasons), but that is
                            // a bigger problem.
                            //
                            // The span is used for safari, which does not allow styling of
                            // selects.
                            echo "<span {$style}><select {$popup} {$readonly} {$style} name=\"{$inputname}\">";
                            echo $outputoptions;
                            echo '</select></span>';
                            if ($feedback !== '' && !empty($USER->screenreader)) {
                                echo "<img src=\"{$CFG->pixpath}/i/feedback.gif\" alt=\"{$feedback}\" />";
                            }
                            echo $feedbackimg;
                        } else {
                            if ($wrapped->options->layout == 1 || $wrapped->options->layout == 2) {
                                $ordernumber = 0;
                                $anss = array();
                                foreach ($answers as $mcanswer) {
                                    $ordernumber++;
                                    $checked = '';
                                    $chosen = false;
                                    $type = 'type="radio"';
                                    $name = "name=\"{$inputname}\"";
                                    if ($response == $mcanswer->id) {
                                        $checked = 'checked="checked"';
                                        $chosen = true;
                                    }
                                    $a = new stdClass();
                                    $a->id = $question->name_prefix . $mcanswer->id;
                                    $a->class = '';
                                    $a->feedbackimg = '';
                                    // Print the control
                                    $a->control = "<input {$readonly} id=\"{$a->id}\" {$name} {$checked} {$type} value=\"{$mcanswer->id}\" />";
                                    if ($options->correct_responses && $mcanswer->fraction > 0) {
                                        $a->class = question_get_feedback_class(1);
                                    }
                                    if ($options->feedback && $chosen || $options->correct_responses) {
                                        if ($type == ' type="checkbox" ') {
                                            $a->feedbackimg = question_get_feedback_image($mcanswer->fraction > 0 ? 1 : 0, $chosen && $options->feedback);
                                        } else {
                                            $a->feedbackimg = question_get_feedback_image($mcanswer->fraction, $chosen && $options->feedback);
                                        }
                                    }
                                    // Print the answer text: no automatic numbering
                                    $a->text = format_text($mcanswer->answer, FORMAT_MOODLE, $formatoptions, $cmoptions->course);
                                    // Print feedback if feedback is on
                                    if (($options->feedback || $options->correct_responses) && $checked) {
                                        //|| $options->readonly
                                        $a->feedback = format_text($mcanswer->feedback, true, $formatoptions, $cmoptions->course);
                                    } else {
                                        $a->feedback = '';
                                    }
                                    $anss[] = clone $a;
                                }
                                ?>
            <?php 
                                if ($wrapped->options->layout == 1) {
                                    ?>
                  <table class="answer">
                    <?php 
                                    $row = 1;
                                    foreach ($anss as $answer) {
                                        ?>
                      <tr class="<?php 
                                        echo 'r' . ($row = $row ? 0 : 1);
                                        ?>
">
                        <td class="c0 control">
                          <?php 
                                        echo $answer->control;
                                        ?>
                        </td>
                        <td class="c1 text <?php 
                                        echo $answer->class;
                                        ?>
">
                          <label for="<?php 
                                        echo $answer->id;
                                        ?>
">
                            <?php 
                                        echo $answer->text;
                                        ?>
                            <?php 
                                        echo $answer->feedbackimg;
                                        ?>
                          </label>
                        </td>
                        <td class="c0 feedback">
                          <?php 
                                        echo $answer->feedback;
                                        ?>
                        </td>
                      </tr>
                    <?php 
                                    }
                                    ?>
                  </table>
                  <?php 
                                } else {
                                    if ($wrapped->options->layout == 2) {
                                        ?>

                  <table class="answer">
                      <tr class="<?php 
                                        echo 'r' . ($row = $row ? 0 : 1);
                                        ?>
">
                    <?php 
                                        $row = 1;
                                        foreach ($anss as $answer) {
                                            ?>
                        <td class="c0 control">
                          <?php 
                                            echo $answer->control;
                                            ?>
                        </td>
                        <td class="c1 text <?php 
                                            echo $answer->class;
                                            ?>
">
                          <label for="<?php 
                                            echo $answer->id;
                                            ?>
">
                            <?php 
                                            echo $answer->text;
                                            ?>
                            <?php 
                                            echo $answer->feedbackimg;
                                            ?>
                          </label>
                        </td>
                        <td class="c0 feedback">
                          <?php 
                                            echo $answer->feedback;
                                            ?>
                        </td>
                    <?php 
                                        }
                                        ?>
                      </tr>
                  </table>
                  <?php 
                                    }
                                }
                            } else {
                                echo "no valid layout";
                            }
                        }
                        break;
                    default:
                        $a = new stdClass();
                        $a->type = $wrapped->qtype;
                        $a->sub = $positionkey;
                        print_error('unknownquestiontypeofsubquestion', 'qtype_multianswer', '', $a);
                        break;
                }
                echo "</label>";
                // MDL-7497
            } else {
                if (!isset($question->options->questions[$positionkey])) {
                    echo $regs[0] . "</label>";
                } else {
                    echo '</label><div class="error" >' . get_string('questionnotfound', 'qtype_multianswer', $positionkey) . '</div>';
                }
            }
        }
        // Print the final piece of question text:
        echo $qtextremaining;
        if (!empty($question->options->wiris->options['wirisCASForComputations'])) {
            $wirisCASContent = '';
            if (isset($state->responses['wirisCASHidden'])) {
                $wirisCASContent = htmlentities(stripslashes_safe($state->responses['wirisCASHidden']), ENT_QUOTES, 'UTF-8');
            }
            echo '<br />';
            echo wrsqz_wirisCASAppletHTML($nameprefix, $wirisCASContent, false, 630, 300);
            require_js(array('yui_yahoo', 'yui_dom-event'));
            require_js($CFG->wwwroot . '/wiris-quizzes/js/wiris-quizzes.js');
        }
        $this->print_question_submit_buttons($question, $state, $cmoptions, $options);
        echo '</div>';
    }
function wrsqz_computeAnswerToSend($answerArray,$question,$state) {
	mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
	$multipleAnswers = isset($eqoptionArray['multipleAnswers']) && $eqoptionArray['multipleAnswers'] == 'true';

	if (!$multipleAnswers) {
		// Expand variables
		$exapndAnswerArray = array();
		foreach ($answerArray as $answer) {
			$exapndAnswerArray[]=wrsqz_assemble(wrsqz_variablesToText($answer), $state->vars);
		}
		// A ";" separated list of expanded answers
		$r=implode(';', $exapndAnswerArray);

		return $r;
	}

	$r='';
	// x=v1;y=v2;;x=v3;y=v4
	foreach ($answerArray as $answer) {
		if (mb_strlen($r)!=0) $r=$r . ';;';
		$vars = explode(' ',$answer);
		foreach ($vars as $var) {
			if ($var[0]!='(') {
				if (mb_strlen($r)!=0 && $r[mb_strlen($r)-1]!=';') $r=$r . ';';
				// if $var= xx% then --> xx%
				$expandVar = wrsqz_assemble(wrsqz_variablesToText($var), $state->vars);
				// var=#<var>  (so we remove the heading # )
				$r = $r . mb_substr($var,1) . '=( ' . $expandVar . ' )';
			} else { // append parameters
				$r = $r . ' ' . mb_substr($var,1,mb_strlen($var)-2);
			}
		}
	}

	return $r;
}
function wrsqz_response_summary($questionType, $dbType, $question, $state, $length=80){
    if($questionType == 'match'){
        //assemble Questiontexts and answers.
        $subquestions = &$state->options->subquestions;
        $responses    = &$state->responses;
        $table = new stdClass();
        $table->data = array();
        $table->width = '100%';
        foreach ($subquestions as $key => $sub) {
            foreach ($responses as $ind => $code) {
               if (isset($sub->options->answers[$code])) {
                   //assemble $text in encoded MathML.
                   $text = format_text(wrsqz_assemble($subquestions[$ind]->questiontext,$state->vars),$question->questiontextformat);
                   //assemble $answer in text mode.
                   $answer = wrsqz_assemble(wrsqz_variablesToText($sub->options->answers[$code]->answer),$state->vars);
                   $table->data[] =  array($text ,$answer);
               }
           }
       }
       $summary = print_table($table, true);

    }else if($questionType == 'shortanswer'){
        if(isset($state->responses[''])){
            mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
            if(isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true'){
                $summary = format_text(wrsqz_mathmlEncode(stripslashes($state->responses[''])),FORMAT_HTML);
            }else{
                $summary = stripslashes($state->responses['']);
            }
        }else{
            $summary='';
        }
    }else if($questionType == 'truefalse'){
        if(isset($state->responses['']) && isset($question->options->answers[$state->responses['']])){
            $summary = $question->options->answers[$state->responses['']]->answer;
        }else{
            $summary = '';
        }
    }else if($questionType == 'multianswer'){
        global $QTYPES;
        
        $table = new stdClass();
        $table->data = array();
        $table->data[0] = array();
        $table->width = '100%';
        $numquestions = count($question->options->questions);
        $table->size=array_fill(0,$numquestions,100/$numquestions . '%');
        foreach ($question->options->questions as $key=>$wrapped) {
            $wrappedstate = clone($state);
            $wrappedstate->responses = array(''=>$state->responses[$key]);
            $table->data[0][] = $QTYPES[$wrapped->qtype]->response_summary($wrapped,$wrappedstate);
        }
        $summary = print_table($table, true);
    }
    return $summary;
}