function wrsqz_multipleAnswersTable($answer, $variables, $names) {
	$openmath = '<math xmlns="http://www.w3.org/1998/Math/MathML">';
	$closemath = '</math>';
	
	// x=v1;y=v2
    if(!isset($names)) $names = $answer;
    //clean $names array
    $names = explode(' ',$names);
    $cleanNames=array();
    foreach ($names as $name){
        if ($name[0]!='(') { //skip parameters
            if($name[0]=='#'){
                $name = mb_substr($name,1);
            }
            $cleanNames[]=$name;
        }
    }
    //make values array
	$vars = explode(' ',$answer);
    $values=array();
    foreach ($vars as $var){
        if ($var[0]!='(') { // skip parameters
            if($var[0]!='#') $var = '#'.$var;
            $value = wrsqz_assemble(wrsqz_textToVariables($var), $variables);
		//remove <math...> and </math>
		    $value = mb_substr($value,mb_strlen($openmath),-mb_strlen($closemath));
            $values[] = $value;
        }
    }

    //Write mathml
    $r = $openmath .'<mtable columnalign="left" rowspacing="0">';
	foreach ($cleanNames as $index => $name) {
		if ($var[0]!='(') { // skip parameters
			$r .= '<mtr><mtd>';
			$r .= '<mi>' . $name . '</mi>';
			$r .= '<mo>=</mo>';
			$r .= $values[$index];
			$r .= '</mtd></mtr>';
		}
	}
	$r .= '</mtable>'. $closemath;
	return $r;
}
function wrsqz_get_correct_responses($questionType, $dbType, $response, &$question, &$state, $mode = 'M') {
//mode='M' => MathML <math xmlns=".......
//mode='W' => encoded MathML for filtering  �math xmlns=�...
//mode='T' => Plain Text 2^(1/2)...

	if ($questionType == 'shortanswer') {
            if (!is_array($response)){
                $response = array();
            }
            
            mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
            $editor = isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true';
            $multipleAnswers = $editor && isset($eqoptionArray['multipleAnswers']) && $eqoptionArray['multipleAnswers'] == 'true';
            
            //get the first maximum graded answer and, in the multipleanswer case,
            //the names of the variables to be used (depending on if we use test
            //functions or not).
            if($question->options->answers){
            	foreach ($question->options->answers as $answer) {
                    if (((int) $answer->fraction) === 1) {
                        $correctanswer = $answer->answer;
                        if($multipleAnswers){
                            if (isset($eqoptionArray['testFunction']) && isset($eqoptionArray['testFunction'][$answer->id])){
                                $index = $eqoptionArray['testFunction'][$answer->id];
                                $names = $eqoptionArray['testFunctionName'][$index];
                            }else{
                                $names = $answer->answer;
                            }
                        }
                    }
                }
	    }else{
                $correctanswer=null;
            }
		//$correctanswer=#p
		//encode it depending on the use of wiris editor and the desired mode

		if ($editor) {
			if ($multipleAnswers) {
				if ($mode == 'T'){
					//plain text for directly display (space separated)
					$response['']=wrsqz_assemble(wrsqz_variablesToText($correctanswer), $state->vars);
				}else if($mode == 'M'){
					//mathml for an applet
					$response['']=wrsqz_multipleAnswersTable($correctanswer,$state->vars,$names);
				}else if($mode == 'W'){
					//encoded mathml for filtering
					$response['']=wrsqz_multipleAnswersTable($correctanswer,$state->vars, $names);
					$response['']=wrsqz_mathmlEncode($response['']);
				}
			} else {
				if ($mode == 'T'){
					$response[''] = wrsqz_assemble(wrsqz_variablesToText($correctanswer), $state->vars);
				}else if ($mode == 'M'){
					$response[''] = wrsqz_assemble(wrsqz_textToVariables($correctanswer), $state->vars);
				}else if ($mode == 'W'){
					$response[''] = wrsqz_assemble($correctanswer, $state->vars);
				}
			}
		} else {
			//if editor is not used, we use text mode (T) always.
			$response[''] = wrsqz_assemble(wrsqz_variablesToText($correctanswer), $state->vars);
		}
	}
	return $response;
}