Ejemplo n.º 1
0
 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', '');
     }
 }
Ejemplo n.º 2
0
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');
    }
}