示例#1
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');
    }
}