public function execute() { global $DB, $CFG; require_once $CFG->dirroot . '/question/editlib.php'; require_once $CFG->dirroot . '/question/import_form.php'; require_once $CFG->dirroot . '/question/format.php'; $arguments = $this->arguments; $this->checkFileArg($arguments[0]); $file = $arguments[0]; $quiz = $arguments[1]; $quiz = $DB->get_record('quiz', array('id' => $quiz), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST); $coursecontext = \context_course::instance($course->id); $coursemodule = get_coursemodule_from_instance('quiz', $quiz->id); $quizcontext = \context_module::instance($coursemodule->id, MUST_EXIST); // Use existing questions category for quiz. $category = $DB->get_record('question_categories', array('contextid' => $coursecontext->id)); //$category = $DB->get_record("question_categories", array('id' => $category),MUST_EXIST); $formatfile = $CFG->dirroot . '/question/format/xml/format.php'; if (!is_readable($formatfile)) { throw new moodle_exception('formatnotfound', 'question', '', 'xml'); } require_once $formatfile; $qformat = new \qformat_xml(); // load data into class $qformat->setCategory($category); $qformat->setContexts(array($quizcontext)); $qformat->setCourse($course); $qformat->setFilename($file); $qformat->setRealfilename($file); $qformat->setMatchgrades('nearest'); $qformat->setStoponerror(true); // Do anything before that we need to if (!$qformat->importpreprocess()) { print_error('cannotimport', ''); } // Process the uploaded file if (!$qformat->importprocess($category)) { print_error('cannotimport', ''); } // In case anything needs to be done after if (!$qformat->importpostprocess()) { print_error('cannotimport', ''); } }
function load_questions($category, $importfilename, $contextid) { // Load all the questions from the given import file into the given category // The category from the import file will be ignored if present. global $COURSE; $qformat = new qformat_xml(); $qformat->setCategory($category); $systemcontext = context::instance_by_id($contextid); $contexts = new question_edit_contexts($systemcontext); $qformat->setContexts($contexts->having_one_edit_tab_cap('import')); $qformat->setCourse($COURSE); $qformat->setFilename($importfilename); $qformat->setRealfilename($importfilename); $qformat->setMatchgrades('error'); $qformat->setCatfromfile(false); $qformat->setContextfromfile(false); $qformat->setStoponerror(true); // Do anything before that we need to if (!$qformat->importpreprocess()) { throw new coding_exception('Upgrade failed: error preprocessing prototype upload'); } // Process the given file if (!$qformat->importprocess($category)) { throw new coding_exception('Upgrade failed: error uploading prototype questions'); } // In case anything needs to be done after if (!$qformat->importpostprocess()) { throw new coding_exception('Upgrade failed: error postprocessing prototype upload'); } }
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST); get_question_options($questiondata); $question = question_bank::load_question($questionid); // Process any other URL parameters, and do require_login. list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question); $contexts = new question_edit_contexts($context); // Check permissions. question_require_capability_on($questiondata, 'edit'); require_sesskey(); // Initialise $PAGE. $nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams); $PAGE->set_url($nexturl); // Since this script always ends in a redirect. $PAGE->set_heading($COURSE->fullname); $PAGE->set_pagelayout('admin'); // Set up the export format. $qformat = new qformat_xml(); $filename = question_default_export_filename($COURSE, $questiondata) . $qformat->export_file_extension(); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($COURSE); $qformat->setQuestions(array($questiondata)); $qformat->setCattofile(false); $qformat->setContexttofile(false); // Do the export. if (!$qformat->exportpreprocess()) { send_file_not_found(); } if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());