function wrsqz_import_from_xml($questionType, $dbType, &$data, &$question, &$format, &$extra) {
	if (isset($data['@']['type']) && $data['@']['type'] == $questionType . 'wiris') {
		$qfxml = new qformat_xml();

		if ($questionType == 'essay') {
			$question = $qfxml->import_essay($data);
		}else if ($questionType == 'match') {
			$question = $qfxml->import_matching($data);
		}else if ($questionType == 'multichoice') {
			$question = $qfxml->import_multichoice($data);
		}else if ($questionType == 'truefalse') {
			$question = $qfxml->import_truefalse($data);
		}else if ($questionType == 'shortanswer') {
			$question = $qfxml->import_shortanswer($data);
		}else if ($questionType == 'multianswer') {
      $questiontext = $data['#']['wirisquestiontext'][0]['#']['text'];
      $container = new stdClass;
      $container->questiontext = $qfxml->import_text($questiontext);
      $question = wrsqz_qtype_multianswer_extract_question($container);
      $question->course = $qfxml->course;
      $question->generalfeedback = $qfxml->getpath($data, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
      $question->name = $qfxml->import_text( $data['#']['name'][0]['#']['text'] );
    }
		
		unset($qfxml);

		$question->qtype = $questionType . 'wiris';
		$program = wrsqz_mathmlDecode($format->getpath($data, array('#', 'wirisquestion', 0, '#'), 0));
        $imported = wrsqz_importCASSession($program);

        if($imported===false) return false;

        $question->hiddenCASValue = $imported[0];
        $translation = $imported[1];

		if ($questionType == 'shortanswer') {
			mb_parse_str($format->getpath($data, array('#', 'wiriseditor', 0, '#'), 0), $eqoptionArray);
			$question->wirisEditor = (isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true') ? 'editor=true' : '';
			$question->multipleAnswers = (isset($eqoptionArray['multipleAnswers']) && $eqoptionArray['multipleAnswers'] == 'true') ? 'multipleAnswers=true' : '';
			$question->wirisCASForComputations = (isset($eqoptionArray['wirisCASForComputations']) && $eqoptionArray['wirisCASForComputations'] == 'true') ? 'wirisCASForComputations=true' : '';
			
			if(isset($eqoptionArray['testFunctionName'])){
				foreach ($eqoptionArray['testFunctionName'] as $index => $functionName){
					if(!empty($functionName)){
						$question->testFunctionName[$index]=$functionName;
					}
				}
			}
		}else if ($questionType == 'multichoice') {
			$question->gradeOverride = explode(';', $format->getpath($data, array('#', 'wirisoverrideanswer', 0, '#'), 0));
		}else if ($questionType == 'truefalse') {
			$question->wirisAnswer = $format->getpath($data, array('#', 'wirisoverrideanswer', 0, '#'), 0);
		}else if ($questionType == 'multianswer'){
            //mb_parse_str($format->getpath($data, array('#', 'wiriseditor', 0, '#'), 0), $eqoptionArray);
            //$question->wirisEditor = (isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true') ? 'editor=true' : '';
    }
    
    $options = $format->getpath($data, array('#', 'wirisoptions',0,'#'), array());
    $question->wirisCASForComputations = $format->getpath($options, array('wirisCASForComputations',0,'#'),0);
    //TODO: translate cas if needed.
    $question->hiddenInitialCASValue = wrsqz_mathmlDecode($format->getpath($options, array('hiddenInitialCASValue',0,'#'),0));

    if(!empty($translation)){
      wrsqz_replaceVarReferencesInFlatQuestion($questionType, $question, $translation);
    }

		return $question;
	}

	return false;
}
function wrsqz_set_data($questionType, $dbType, &$mform, &$question) {
	$default_values = array();
	//common options
  if(isset($question->options->wiris->options)){
    $options = $question->options->wiris->options;
    if(isset($options['wirisCASForComputations'])){
      $default_values['wirisCASForComputations'] = $options['wirisCASForComputations'];
    }
    if(isset($options['hiddenInitialCASValue'])){
      $default_values['hiddenInitialCASValue'] = $options['hiddenInitialCASValue'];
    }
  }

  //question-type specific options
	if ($questionType == 'shortanswer' && isset($question) && isset($question->options) && isset($question->options->wiris) && isset($question->options->wiris->eqoption)) {
		$default_values['wirisEditor'] = '';
		mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);

		if (isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true') {
			$default_values['wirisEditor'] = '1';
		}
		if (isset($eqoptionArray['multipleAnswers']) && $eqoptionArray['multipleAnswers'] == 'true') {
			$default_values['multipleAnswers'] = '1';
		}
		/*if (isset($eqoptionArray['wirisCASForComputations']) && $eqoptionArray['wirisCASForComputations'] == 'true') {
			$default_values['wirisCASForComputations'] = '1';
		}*/

		if (isset($eqoptionArray['testFunction'])){
      //set testFunctionNames where their associated answers are
      $index = 0;
      
      foreach ($question->options->answers as $answer){
        if (isset($eqoptionArray['testFunction'][$answer->id])){
          $testFunctionNameIndex = $eqoptionArray['testFunction'][$answer->id];
          $default_values['testFunctionName['.$index.']'] = $eqoptionArray['testFunctionName'][$testFunctionNameIndex];
          unset($eqoptionArray['testFunctionName'][$testFunctionNameIndex]);
        }
        $index++;
      }
      //set test functions with no associated answer.
      foreach($eqoptionArray['testFunctionName'] as $key => $value){
        $default_values['testFunctionName['.$index.']'] = $value;
        $index++;
      }
    }else if (isset($eqoptionArray['testFunctionName'])){
      //Backwards compatibility for old WIRIS quizzes (didn't have testFunction attribute).
      foreach ($eqoptionArray['testFunctionName'] as $index => $value){
        $default_values['testFunctionName['.$index.']'] = $eqoptionArray['testFunctionName'][$index];
      }
    }

	}else if($questionType == 'multianswer' && isset($question) 
    && isset($question->options) && isset($question->options->wiris)
    && isset($question->options->wiris->eqoption)){

    mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
    if (isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true') {
			$default_values['wirisEditor'] = '1';
		} else {
      $default_values['wirisEditor'] = '';
    }
  }

	if (isset($question) && isset($question->options) && isset($question->options->wiris) && !empty($question->options->wiris->override)) {
		if ($questionType == 'multichoice') {
			$overrideList = explode(';', $question->options->wiris->override);
			
			foreach ($overrideList as $key => $value) {
				$default_values['gradeOverride[' . $key . ']'] = $value;
			}
		}else if ($questionType == 'truefalse') {
			$overrideList = explode(';', $question->options->wiris->override);
			$default_values['wirisAnswer'] = $overrideList[0];
		}
	}

	global $CFG;
	
	$pos = ($mform->_elementIndex['hiddenCASValue']);
	$value = trim($mform->_elements[$pos]->_attributes['value']);

  $selectedLang = optional_param('WIRISCASLang','',PARAM_ALPHA);
  $availableLangs = wrsqz_getAvailableCASLangs();

	if (!empty($value)) {
		$program = $value;
    $sessionLang = wrsqz_CASLanguageFromSession($program);
    if(in_array($selectedLang, $availableLangs) && $selectedLang != $sessionLang){
      //We have to translate the WIRIS session
      $translation = wrsqz_translateCASSession($program,$selectedLang);
      if($translation !==false){
        $program = $translation[0];
        $CASLang = $selectedLang;
        //Rearrange all form fields with the new named variables (at the end of the function).
      }else{
        //We couldn't translate the program. Do nothing
        $CASLang = $sessionLang;
      }
    }else{
      $CASLang = $sessionLang;
    }
	}else{
        if (isset($question) && isset($question->id)
        && ($program = wrsqz_getProgram($questionType, $question->id)) !== false
        && !empty($program)) {
            $CASLang = wrsqz_CASLanguageFromSession($program);
        }else {
            $CASLang = wrsqz_currentCASLanguage();
            $program = wrsqz_getDefaultCASCode($CASLang);
        }
    }

  $default_values['htmlWirisEditor']  = '<applet  name="WirisCASApplet" code="' . wrsqz_getCASClass($CASLang) . '" codebase="' . $CFG->wiriscascodebase . '" archive="' . wrsqz_getCASArchive($CASLang) . '" height="450" WIDTH="100%">';
	$default_values['htmlWirisEditor'] .= '<param name="version" value="2.0"/>';
	$default_values['htmlWirisEditor'] .= '<param name="command" value="false"/>';
	$default_values['htmlWirisEditor'] .= '<param name="commands" value="false"/>';
	$default_values['htmlWirisEditor'] .= '<param name="interface" value="false"/>';
	$default_values['htmlWirisEditor'] .= '<param name="XMLinitialText" value="' . htmlentities($program, ENT_QUOTES, 'UTF-8') . '"/>';
	$default_values['htmlWirisEditor'] .= '</applet>';
  $default_values['WIRISCASLang'] = $CASLang;
    
	// defined only because we call wrsqz_wirisQuizzesUsed
  $default_values['hiddenCASValue'] = $program;

  $question = (object)(array_merge((array)$question, $default_values));

  if(isset($translation) && ! empty($translation[1])){
        //If WIRIS CAS program had to be translated, there may be references (#a) to
        //old variable names, so replace them.
    wrsqz_replaceVarReferencesInFlatQuestion($questionType, $question, $translation[1]);
    wrsqz_replaceVarReferencesInForm($questionType, $mform, $translation[1]);
  }

  // If uses wiris quizzes, display advanced otherwise the program
  // is lost if the advanced button is never clicked
  if (wrsqz_wirisQuizzesUsed($question)) {
     $mform->setShowAdvanced(1);
  }

}