function wrsqz_save_question_options($questionType, $dbType, &$question) {

    /*Delete old tables*/
  if (($oldproblem = get_records('question_' . $questionType . 'wiris', 'question', $question->id, 'id ASC')) === false) {
		$oldproblem = array();
	}
	
	foreach ($oldproblem as $op) {
		delete_records('question_' . $questionType . 'wiris', 'id', $op->id);
	}
	
	if(($oldproblem = get_records('question_' . $dbType, 'question', $question->id, 'id ASC')) === false) {
		$oldproblem = array();
	}
	
	foreach ($oldproblem as $op) {
		delete_records('question_' . $dbType, 'id', $op->id);
	}

  /*Store new values*/
	if (wrsqz_wirisQuizzesUsed($question)) {
		//common fields
    $problem->id = $problem->idcache = 0;
		$problem->question = $question->id;
    $program = $question->hiddenCASValue;
		$problem->md5 = md5($program);
    //options field

    //1. General options
    $options = array();
    if(!empty($question->wirisCASForComputations)){
      $options['wirisCASForComputations'] = $question->wirisCASForComputations;
    }
    if(!empty($question->hiddenInitialCASValue)){
      $options['hiddenInitialCASValue'] = stripslashes_safe($question->hiddenInitialCASValue);
    }
		
    //2. Question-type specific options
		if ($questionType == 'shortanswer') {
      $eqoptionArray=array();
      if(!empty($question->wirisEditor))
        $eqoptionArray['editor'] = 'true';
      if(!empty($question->multipleAnswers))
        $eqoptionArray['multipleAnswers'] = 'true';
      /*if(!empty($question->wirisCASForComputations))
        $eqoptionArray['wirisCASForComputations']='true';*/
      if(isset($question->testFunctionName)){
        $eqoptionArray['testFunctionName']=array();
        $eqoptionArray['testFunction'] = array();
        //get an indexed answer id array. this is done because answers are reordered
        //when are saved, skipping blank ones.
        if(isset($question->answers)){
          $answersList = explode(',',$question->answers);
        }else{
          $answersList = explode(',',wrsqz_deprecated_answersList($question));
        }
        $k=0;
        $answersId=array();
        foreach ($question->answer as $key => $dataanswer) {
          if (!(trim($dataanswer) == '' && $question->fraction[$key] == 0 && wrsqz_html_is_blank($question->feedback[$key]))) {
            $answersId[$key]=$answersList[$k];
            $k++;
          }
        }

        foreach($question->testFunctionName as $index => $value){
          if(!empty($value) && trim($value)!=''){
            $eqoptionArray['testFunctionName'][$index] = $value;
            if(isset($answersId[$index])){
              $eqoptionArray['testFunction'][$answersId[$index]]=$index;
            }
          }
        }
      }
      if(function_exists('http_build_query')){
        $problem->eqoption = http_build_query($eqoptionArray,null,'&');
      }else{
        $problem->eqoption = wrsqz_http_build_query($eqoptionArray);
      }
            
      if(!isset($problem->eqoption) || $problem->eqoption == null){
        $problem->eqoption='';
      }
            
		}else if ($questionType == 'truefalse') {
			$wirisAnswer = trim($question->wirisAnswer);
			$problem->override = (!empty($wirisAnswer)) ? $wirisAnswer : '';
		}else if ($questionType == 'multichoice') {
			$problem->override = implode(';', $question->gradeOverride);
		}else if ($questionType == 'multianswer'){
      $problem->eqoption = (empty($question->wirisEditor)) ? '' : 'editor=true&';
    }

    //serialize & save options field
    $problem->options = serialize($options);

		if (insert_record('question_' . $questionType . 'wiris', $problem) !== false) {
			unset($problem);
			
			$problem->id = 0;
			$problem->question = $question->id;
			$problem->program = $question->hiddenCASValue;

			if (insert_record('question_' . $dbType, $problem) !== false) {
				return true;
			}

			$result->error = 'Could not insert quiz WIRIS exercice!'; 
			return $result;
		}

		$result->error = 'Could not insert quiz ' . $questionType . 'wiris options!';
		return $result;
	}
	
	$question->qtype = $questionType;
	return update_record('question', $question);
}
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);
  }

}