示例#1
0
    public function test_import_files_as_draft() {
        $this->resetAfterTest();
        $this->setAdminUser();

        $xml = <<<END
<questiontext format="html">
    <text><![CDATA[<p><a href="@@PLUGINFILE@@/moodle.txt">This text file</a> contains the word 'Moodle'.</p>]]></text>
    <file name="moodle.txt" encoding="base64">TW9vZGxl</file>
</questiontext>
END;

        $textxml = xmlize($xml);
        $qo = new stdClass();

        $importer = new qformat_xml();
        $draftitemid = $importer->import_files_as_draft($textxml['questiontext']['#']['file']);
        $files = file_get_drafarea_files($draftitemid);

        $this->assertEquals(1, count($files->list));

        $file = $files->list[0];
        $this->assertEquals('moodle.txt', $file->filename);
        $this->assertEquals('/',          $file->filepath);
        $this->assertEquals(6,            $file->size);
    }
示例#2
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'ddimageortext';
     $question->shuffleanswers = array_key_exists('shuffleanswers', $format->getpath($data, array('#'), array()));
     $filexml = $format->getpath($data, array('#', 'file'), array());
     $question->bgimage = $format->import_files_as_draft($filexml);
     $drags = $data['#']['drag'];
     $question->drags = array();
     foreach ($drags as $dragxml) {
         $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
         $dragindex = $dragno - 1;
         $question->drags[$dragindex] = array();
         $question->draglabel[$dragindex] = $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
         $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
         $question->drags[$dragindex]['draggroup'] = $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1);
         $filexml = $format->getpath($dragxml, array('#', 'file'), array());
         $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml);
         if (count($filexml)) {
             $question->drags[$dragindex]['dragitemtype'] = 'image';
         } else {
             $question->drags[$dragindex]['dragitemtype'] = 'word';
         }
     }
     $drops = $data['#']['drop'];
     $question->drops = array();
     foreach ($drops as $dropxml) {
         $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
         $dropindex = $dropno - 1;
         $question->drops[$dropindex] = array();
         $question->drops[$dropindex]['choice'] = $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
         $question->drops[$dropindex]['droplabel'] = $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true);
         $question->drops[$dropindex]['xleft'] = $format->getpath($dropxml, array('#', 'xleft', 0, '#'), '');
         $question->drops[$dropindex]['ytop'] = $format->getpath($dropxml, array('#', 'ytop', 0, '#'), '');
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
     return $question;
 }
示例#3
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     global $CFG;
     $question_type = "poodllrecording";
     //omit table name
     $qo = $format->import_headers($data);
     $qo->qtype = $question_type;
     $q = $data;
     $qo->responseformat = $format->getpath($q, array('#', 'responseformat', 0, '#'), 'picture');
     $qo->responsefieldlines = $format->getpath($q, array('#', 'responsefieldlines', 0, '#'), 15);
     $qo->attachments = $format->getpath($q, array('#', 'attachments', 0, '#'), 0);
     //older versions handled files diff. SeeM DL-39-57
     if ($CFG->version < 2013051400) {
         $qo->graderinfo['text'] = $format->getpath($q, array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true);
         $qo->graderinfo['format'] = $format->trans_format($format->getpath($q, array('#', 'graderinfo', 0, '@', 'format'), $format->get_format($qo->questiontextformat)));
         $qo->graderinfo['files'] = $format->import_files($format->getpath($q, array('#', 'graderinfo', '0', '#', 'file'), array()));
         $qo->backimage = $format->import_files($format->getpath($q, array('#', 'backimage', '0', '#', 'file'), array()));
     } else {
         $qo->graderinfo = $format->import_text_with_files($q, array('#', 'graderinfo', 0), '', $qo->questiontextformat);
         $qo->backimage = $format->import_files_as_draft($format->getpath($q, array('#', 'backimage', '0', '#', 'file'), array()));
     }
     $qo->boardsize = $format->getpath($q, array('#', 'boardsize', 0, '#'), '320x320');
     $qo->timelimit = $format->getpath($q, array('#', 'timelimit', 0, '#'), 0);
     return $qo;
 }
示例#4
0
 public function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if (!isset($data['@']['type']) || $data['@']['type'] != 'ddmarker') {
         return false;
     }
     $question = $format->import_headers($data);
     $question->qtype = 'ddmarker';
     $question->shuffleanswers = array_key_exists('shuffleanswers', $format->getpath($data, array('#'), array()));
     $question->showmisplaced = array_key_exists('showmisplaced', $format->getpath($data, array('#'), array()));
     $filexml = $format->getpath($data, array('#', 'file'), array());
     $question->bgimage = $format->import_files_as_draft($filexml);
     $drags = $data['#']['drag'];
     $question->drags = array();
     foreach ($drags as $dragxml) {
         $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
         $dragindex = $dragno - 1;
         $question->drags[$dragindex] = array();
         $question->drags[$dragindex]['label'] = $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
         if (array_key_exists('infinite', $dragxml['#'])) {
             $question->drags[$dragindex]['noofdrags'] = 0;
             // Means infinite in the form.
         } else {
             // Defaults to 1 if 'noofdrags' not set.
             $question->drags[$dragindex]['noofdrags'] = $format->getpath($dragxml, array('#', 'noofdrags', 0, '#'), 1);
         }
     }
     $drops = $data['#']['drop'];
     $question->drops = array();
     foreach ($drops as $dropxml) {
         $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
         $dropindex = $dropno - 1;
         $question->drops[$dropindex] = array();
         $question->drops[$dropindex]['choice'] = $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
         $question->drops[$dropindex]['shape'] = $format->getpath($dropxml, array('#', 'shape', 0, '#'), '');
         $question->drops[$dropindex]['coords'] = $format->getpath($dropxml, array('#', 'coords', 0, '#'), '');
     }
     $format->import_combined_feedback($question, $data, true);
     $format->import_hints($question, $data, true, true, $format->get_format($question->questiontextformat));
     return $question;
 }
示例#5
0
 function import_from_xml($data, $question, qformat_xml $format, $extra = null)
 {
     if ($extra != null) {
         throw new coding_exception("coderunner:import_from_xml: unexpected 'extra' parameter");
     }
     $questiontype = $data['@']['type'];
     if ($questiontype != $this->name()) {
         return false;
     }
     $extraquestionfields = $this->extra_question_fields();
     if (!is_array($extraquestionfields)) {
         return false;
     }
     //omit table name
     array_shift($extraquestionfields);
     $qo = $format->import_headers($data);
     $qo->qtype = $questiontype;
     $newdefaults = array('allornothing' => 1, 'answerboxlines' => 15, 'answerboxcolumns' => 90, 'useace' => 1);
     foreach ($extraquestionfields as $field) {
         if ($field === 'pertesttemplate' && isset($data['#']['custom_template'])) {
             // Legacy import
             $qo->pertesttemplate = $format->getpath($data, array('#', 'custom_template', 0, '#'), '');
         } else {
             $map = $this->legacy_field_name_map();
             if (isset($map[$field]) && isset($data['#'][$map[$field]])) {
                 $data['#'][$field] = $data['#'][$map[$field]];
                 // Map old field names to new
                 unset($data['#'][$map[$field]]);
             }
             if (array_key_exists($field, $newdefaults)) {
                 $default = $newdefaults[$field];
             } else {
                 $default = '';
             }
             $qo->{$field} = $format->getpath($data, array('#', $field, 0, '#'), $default);
         }
     }
     $qo->isnew = true;
     $qo->testcases = array();
     if (isset($data['#']['testcases'][0]['#']['testcase']) && is_array($data['#']['testcases'][0]['#']['testcase'])) {
         $testcases = $data['#']['testcases'][0]['#']['testcase'];
         foreach ($testcases as $testcase) {
             $tc = new stdClass();
             $tc->testcode = $testcase['#']['testcode'][0]['#']['text'][0]['#'];
             $tc->stdin = $testcase['#']['stdin'][0]['#']['text'][0]['#'];
             if (isset($testcase['#']['output'])) {
                 // Handle old exports
                 $tc->expected = $testcase['#']['output'][0]['#']['text'][0]['#'];
             } else {
                 $tc->expected = $testcase['#']['expected'][0]['#']['text'][0]['#'];
             }
             $tc->extra = $testcase['#']['extra'][0]['#']['text'][0]['#'];
             $tc->display = 'SHOW';
             $tc->mark = 1.0;
             if (isset($testcase['@']['mark'])) {
                 $tc->mark = floatval($testcase['@']['mark']);
             }
             if (isset($testcase['@']['hidden']) && $testcase['@']['hidden'] == "1") {
                 $tc->display = 'HIDE';
                 // Handle old-style export too
             }
             if (isset($testcase['#']['display'])) {
                 $tc->display = $testcase['#']['display'][0]['#']['text'][0]['#'];
             }
             if (isset($testcase['@']['hiderestiffail'])) {
                 $tc->hiderestiffail = $testcase['@']['hiderestiffail'] == "1" ? 1 : 0;
             } else {
                 $tc->hiderestiffail = 0;
             }
             $tc->useasexample = $testcase['@']['useasexample'] == "1" ? 1 : 0;
             $qo->testcases[] = $tc;
         }
     }
     $datafiles = $format->getpath($data, array('#', 'testcases', 0, '#', 'file'), array());
     if (is_array($datafiles)) {
         // Seems like a non-array does occur in some versions of PHP :-/
         $qo->datafiles = $format->import_files_as_draft($datafiles);
     }
     return $qo;
 }