public function test_import_backslash() { // There was a bug (MDL-34171) where \\ in the import was getting changed // to \. This test checks for that. // We need \\\\ in the test code, because of PHPs string escaping rules. $gift = ' // essay ::double backslash:: A \\\\ B \\\\\\\\ C{}'; $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); $importer = new qformat_gift(); $q = $importer->readquestion($lines); $expectedq = (object) array('name' => 'double backslash', 'questiontext' => 'A \\ B \\\\ C', 'questiontextformat' => FORMAT_MOODLE, 'generalfeedback' => '', 'generalfeedbackformat' => FORMAT_MOODLE, 'qtype' => 'essay', 'defaultmark' => 1, 'penalty' => 0.3333333, 'length' => 1, 'responseformat' => 'editor', 'responsefieldlines' => 15, 'attachments' => 0, 'graderinfo' => array('text' => '', 'format' => FORMAT_HTML, 'files' => array())); $this->assert(new question_check_specified_fields_expectation($expectedq), $q); }
$description = optional_param("description", $q->description, PARAM_TEXT); $content = optional_param("content", $q->props['content'], PARAM_TEXT); $tags = optional_param("tags", $q->tags, PARAM_TEXT); $format = optional_param("format", "gift", PARAM_TEXT); $supported_qtypes = array('truefalse', 'multichoice', 'essay', 'shortanswer', 'numerical'); if ($submit != "") { if ($title == "") { array_push($MSG, getstring('import.quiz.error.notitle')); } if ($content == "") { array_push($MSG, getstring('import.quiz.error.nocontent')); } $questions_to_import = array(); if ($format == 'gift') { include_once './gift/import.php'; $import = new qformat_gift(); $lines = explode("\n", $content); $questions = $import->readquestions($lines); foreach ($questions as $qu) { if (in_array($qu->qtype, $supported_qtypes)) { array_push($questions_to_import, $qu); } else { if ($qu->qtype != 'category') { array_push($IMPORT_INFO, $qu->qtype . " question type not yet supported ('" . $qu->questiontext . "')"); } } } } if (count($questions_to_import) == 0) { array_push($MSG, getstring('import.quiz.error.nosuppportedquestions')); }
function createQuizfromGIFT($content, $title, $quizdraft, $description, $tags) { global $IMPORT_INFO, $MSG, $CONFIG, $USER; //first check if this quiz already exists $sql = sprintf("SELECT q.qref FROM quizprop qp\n\t\t\t\t\t\tINNER JOIN quiz q ON q.quizid = qp.quizid\n\t\t\t\t\t\tWHERE qp.quizpropname='content' \n\t\t\t\t\t\tAND qp.quizpropvalue='%s'\n\t\t\t\t\t\tAND q.createdby=%d", $content, $USER->userid); $result = _mysql_query($sql, $this->DB); while ($o = mysql_fetch_object($result)) { // store JSON object for quiz (for caching) $obj = $this->getQuizObject($o->qref); return $obj; } $supported_qtypes = array('truefalse', 'multichoice', 'essay', 'shortanswer', 'numerical'); $questions_to_import = array(); include_once $CONFIG->homePath . 'quiz/import/gift/import.php'; $import = new qformat_gift(); $lines = explode("\n", $content); $questions = $import->readquestions($lines); foreach ($questions as $q) { if (in_array($q->qtype, $supported_qtypes)) { array_push($questions_to_import, $q); } else { if ($q->qtype != 'category') { array_push($IMPORT_INFO, $q->qtype . " question type not yet supported ('" . $q->questiontext . "')"); } } } if (count($questions_to_import) == 0) { array_push($MSG, getstring('import.quiz.error.nosuppportedquestions')); return; } if (count($MSG) == 0) { // now do the actual import // setup quiz with default props $quizid = $this->addQuiz($title, $quizdraft, $description); $this->setProp('quiz', $quizid, 'generatedby', 'import'); $this->setProp('quiz', $quizid, 'content', $content); $this->updateQuizTags($quizid, $tags); $importer = new GIFTImporter(); $importer->quizid = $quizid; $importer->import($questions_to_import); $this->setProp('quiz', $quizid, 'maxscore', $importer->quizmaxscore); $q = $this->getQuizById($quizid); // store JSON object for quiz (for caching) $obj = $this->getQuizObject($q->ref); $json = json_encode($obj); $this->setProp('quiz', $quizid, 'json', $json); return $obj; } return; }
public function test_import_pre_content() { $gift = ' ::Q001::[html]<p>What would running the test method print?</p> <pre> public void test() \\{ method1(); method2(); method3(); \\} </pre> {}'; $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); $importer = new qformat_gift(); $q = $importer->readquestion($lines); $expectedq = (object) array('name' => 'Q001', 'questiontext' => '<p>What would running the test method print?</p> <pre> public void test() { method1(); method2(); method3(); } </pre>', 'questiontextformat' => FORMAT_HTML, 'generalfeedback' => '', 'generalfeedbackformat' => FORMAT_HTML, 'qtype' => 'essay', 'defaultmark' => 1, 'penalty' => 0.3333333, 'length' => 1, 'responseformat' => 'editor', 'responsefieldlines' => 15, 'attachments' => 0, 'graderinfo' => array('text' => '', 'format' => FORMAT_HTML, 'files' => array())); $this->assert(new question_check_specified_fields_expectation($expectedq), $q); }
public function test_export_truefalse() { $qdata = (object) array( 'id' => 666 , 'name' => 'Q1', 'questiontext' => "42 is the Absolute Answer to everything.", 'questiontextformat' => FORMAT_MOODLE, 'generalfeedback' => '', 'generalfeedbackformat' => FORMAT_MOODLE, 'defaultmark' => 1, 'penalty' => 1, 'length' => 1, 'qtype' => 'truefalse', 'options' => (object) array( 'id' => 123, 'question' => 666, 'trueanswer' => 1, 'falseanswer' => 2, 'answers' => array( 1 => (object) array( 'id' => 123, 'answer' => 'True', 'answerformat' => 0, 'fraction' => 1, 'feedback' => 'You gave the right answer.', 'feedbackformat' => FORMAT_MOODLE, ), 2 => (object) array( 'id' => 124, 'answer' => 'False', 'answerformat' => 0, 'fraction' => 0, 'feedback' => "42 is the Ultimate Answer.", 'feedbackformat' => FORMAT_HTML, ), ), ), ); $exporter = new qformat_gift(); $gift = $exporter->writequestion($qdata); $expectedgift = "// question: 666 name: Q1 ::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.} "; $this->assert_same_gift($expectedgift, $gift); }
function export_quest($quest_list, $file_format) { $quest_export = ''; switch ($file_format) { case 0: // gift format ------------------- require_once $GLOBALS['where_lms'] . '/modules/question/format.gift.php'; $qgift = new qformat_gift(); while (list($id_quest, $type_quest) = each($quest_list)) { $oQuest = $this->instanceQuestType($id_quest, $type_quest); if ($oQuest) { $oRawQuest = $oQuest->exportToRaw($id_quest); $quest_export .= $qgift->writequestion($oRawQuest); } else { die($type_quest); } } break; case 1: // xml moodle format ------------- break; } return $quest_export; }