예제 #1
0
    public function test_import_essay_21() {
        $xml = '  <question type="essay">
    <name>
      <text>An essay</text>
    </name>
    <questiontext format="moodle_auto_format">
      <text>Write something.</text>
    </questiontext>
    <generalfeedback>
      <text>I hope you wrote something interesting.</text>
    </generalfeedback>
    <defaultgrade>1</defaultgrade>
    <penalty>0</penalty>
    <hidden>0</hidden>
    <responseformat>monospaced</responseformat>
    <responsefieldlines>42</responsefieldlines>
    <attachments>-1</attachments>
    <graderinfo format="html">
        <text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text>
    </graderinfo>
    <responsetemplate format="html">
        <text><![CDATA[<p>Here is something <b>really</b> interesting.</p>]]></text>
    </responsetemplate>
  </question>';
        $xmldata = xmlize($xml);

        $importer = new qformat_xml();
        $q = $importer->import_essay($xmldata['question']);

        $expectedq = new stdClass();
        $expectedq->qtype = 'essay';
        $expectedq->name = 'An essay';
        $expectedq->questiontext = 'Write something.';
        $expectedq->questiontextformat = FORMAT_MOODLE;
        $expectedq->generalfeedback = 'I hope you wrote something interesting.';
        $expectedq->defaultmark = 1;
        $expectedq->length = 1;
        $expectedq->penalty = 0;
        $expectedq->responseformat = 'monospaced';
        $expectedq->responsefieldlines = 42;
        $expectedq->attachments = -1;
        $expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>';
        $expectedq->graderinfo['format'] = FORMAT_HTML;
        $expectedq->responsetemplate['text'] = '<p>Here is something <b>really</b> interesting.</p>';
        $expectedq->responsetemplate['format'] = FORMAT_HTML;

        $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
    }
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;
}