function wrsqz_print_question_form_end($questionType, $dbType, &$question, &$submitscript, &$hiddenfields)
{
    if (($objProblem = get_record('question_' . $dbType, 'question', $question->id)) !== false) {
        $program = $objProblem->program;
        if ($program == 'NULL') {
            $program = wrsqz_getDefaultCASCode(wrsqz_currentCASLanguage());
        }
    } else {
        $program = wrsqz_getDefaultCASCode(wrsqz_currentCASLanguage());
    }
    echo '<script type="text/javascript">' . "\n";
    echo 'var i = true;' . "\n";
    echo 'function changeState() {' . "\n";
    echo '  if (i) {' . "\n";
    echo '      document.getElementsByName(\'WirisCASApplet\')[0].height = \'0\';' . "\n";
    echo '      document.getElementById(\'myapplet\').style.visibility = \'hidden\';' . "\n";
    echo '      document.getElementById(\'mybutton\').value = \'SHOW\';' . "\n";
    echo '  }' . "\n";
    echo '  else if (!i) {' . "\n";
    echo '      document.getElementById(\'myapplet\').style.visibility = \'visible\';' . "\n";
    echo '      document.getElementById(\'mybutton\').value = \'HIDE\';' . "\n";
    echo '      document.getElementsByName(\'WirisCASApplet\')[0].height = \'400\';' . "\n";
    echo '  }' . "\n";
    echo '  i = !i;' . "\n";
    echo '  return true;' . "\n";
    echo '}' . "\n";
    echo 'function onSubmit() {' . "\n";
    echo '  document.getElementsByName(\'hiddenCASValue\')[0].value = document.getElementsByName(\'WirisCASApplet\')[0].getXML();' . "\n";
    echo '}';
    echo '</script>' . "\n";
    echo '<tr valign="top">' . "\n";
    echo '	<td align="right">' . "\n";
    echo '		<b>Wiris Quizzes:</b>' . "\n";
    echo '	</td>' . "\n";
    echo '	<td align="left">' . "\n";
    echo '      <p align="right">' . "\n";
    echo '          <input type="button" onclick="javascript:changeState();" id="mybutton" value="SHOW"/>' . "\n";
    echo '      </p>' . "\n";
    echo '      <div id="myapplet" style="visibility:hidden;" >' . "\n";
    echo '          <applet name="WirisCASApplet" code="' . wrsqz_getCASClass(wrsqz_currentCASLanguage()) . '" codebase="' . $CFG->wiriscascodebase . '" archive="' . wrsqz_getCASArchive(wrsqz_currentCASLanguage()) . '" height="0" WIDTH="100%">' . "\n";
    echo '              <param name="version" value="2.0"/>' . "\n";
    echo '              <param name="command" value="false"/>' . "\n";
    echo '				<param name="commands" value="false"/>' . "\n";
    echo '				<param name="interface" value="false"/>' . "\n";
    echo '				<param name="XMLinitialText" value="<?php echo $program; ?>"/>' . "\n";
    echo '			</applet>' . "\n";
    echo '		</div>' . "\n";
    echo '	</td>' . "\n";
    echo '</tr>' . "\n";
    $submitscript = 'onclick="javascript:onSubmit();"';
    $hiddenfields = '<input type="hidden" name="hiddenCASValue" />';
}
/**

@param $prefix String a name prefix. Usually $question->name_prefix. It is used
       in order to distinguish between applets.
@param $wirisCASContent String the MathML content of WIRIS CAS.
@param $printString if true, prints a string telling user that can do auxiliar
       computations.
@return an html with a tag <applet> that includes the WIRS CAS applet.

**/
function wrsqz_wirisCASAppletHTML($prefix, $wirisCASContent='', $printString = TRUE, $width = 500, $height = 265){
  global $CFG;
$casLang = empty($wirisCASContent) ? wrsqz_currentCASLanguage() : wrsqz_CASLanguageFromSession($wirisCASContent);
  $html = '<div class="wirisCASApplet">';
  if($printString){
    $html .= wrsqz_get_string('wrsqz_candocomputations')."\n";
    $html .= '<br />'."\n";
  }
  $html .= '<applet'."\n";
  $html .= 'id="'.$prefix.'wirisCAS"'."\n";
  $html .= 'width="'.$width.'" height="'.$height.'"'."\n";
  $html .= 'name="'.$prefix.'wirisCAS"'."\n";
  $html .= 'codebase="'.$CFG->wiriscascodebase.'"'."\n";
  $html .= 'archive="'.wrsqz_getCASArchive($casLang).'"'."\n";
  $html .= 'code="'.wrsqz_getCASClass($casLang).'"'."\n";
  $html .= '>'."\n";
  if($wirisCASContent){
    $html .= '<param name="command" value="false" />'."\n";
    $html .= '<param name="commands"       value="false" />'."\n";
    $html .= '<param name="interface"      value="false" />'."\n";
    $html .= '<param name="XMLinitialText" value="'.$wirisCASContent.'" />'."\n";
  }
  $html .= '</applet>';
  $html .= '<input type="hidden" id="'.$prefix.'wirisCASHidden" name="'.$prefix.'wirisCASHidden" value=""/>';
  $html .= '</div>'."\n";
  return $html;
}
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);
  }

}