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; }
$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')); } if (count($MSG) == 0) { // now do the actual import
function import_quest($file_lines, $file_format, $id_test = 0, $id_category = 0) { $result = array(); switch ($file_format) { case 0: // gift format ------------------- require_once $GLOBALS['where_lms'] . '/modules/question/format.gift.php'; $qgift = new qformat_gift(); $formatted = $qgift->readquestions($file_lines); foreach ($formatted as $question) { if ((int) $id_category > 0 && is_object($question)) { $question->id_category = (int) $id_category; } $oQuest = $this->instanceQuestType(0, $question->qtype); $re = $oQuest->importFromRaw($question, $id_test); if ($re) { if (isset($result[$question->qtype]['success'])) { $result[$question->qtype]['success']++; } else { $result[$question->qtype]['success'] = 1; } } else { if (isset($result[$question->qtype]['fail'])) { $result[$question->qtype]['fail']++; } else { $result[$question->qtype]['fail'] = 1; } } } break; case 1: // xml moodle format ------------- break; } return $result; }