libdir.'/questionlib.php'); require_once($CFG->dirroot.'/lib/uploadlib.php'); // get parameters $params = new stdClass; $params->choosefile = optional_param('choosefile','',PARAM_PATH); $categoryid = optional_param('category', 0, PARAM_INT); $courseid = optional_param('course', 0, PARAM_INT); $format = optional_param('format','',PARAM_FILE); $params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA); // get display strings $txt = new stdClass(); $txt->category = get_string('category','quiz'); $txt->choosefile = get_string('choosefile','quiz'); $txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz"); $txt->file = get_string('file'); $txt->fileformat = get_string('fileformat','quiz'); $txt->importerror = get_string('importerror','quiz'); $txt->importfilearea = get_string('importfilearea','quiz'); $txt->importfileupload = get_string('importfileupload','quiz'); $txt->importfromthisfile = get_string('importfromthisfile','quiz'); $txt->importquestions = get_string("importquestions", "quiz"); $txt->matchgrades = get_string('matchgrades','quiz'); $txt->matchgradeserror = get_string('matchgradeserror','quiz'); $txt->matchgradesnearest = get_string('matchgradesnearest','quiz'); $txt->modulename = get_string('modulename','quiz'); $txt->modulenameplural = get_string('modulenameplural','quiz'); $txt->nocategory = get_string('nocategory','quiz'); $txt->onlyteachersimport = get_string('onlyteachersimport','quiz'); $txt->questions = get_string("questions", "quiz"); $txt->quizzes = get_string('modulenameplural', 'quiz'); $txt->upload = get_string('upload'); $txt->uploadproblem = get_string('uploadproblem'); $txt->uploadthisfile = get_string('uploadthisfile'); // matching options $matchgrades = array(); $matchgrades['error'] = $txt->matchgradeserror; $matchgrades['nearest'] = $txt->matchgradesnearest; if (!$categoryid) { // try to get category from modform $showcatmenu = true; // will ensure that user can choose category if (isset($SESSION->modform)) { $categoryid = $SESSION->modform->category; } } if (! $category = get_record("question_categories", "id", $categoryid)) { // if no valid category was given, use the default category if ($courseid) { $category = get_default_question_category($courseid); } else { error( $txt->nocategory ); } } if (!$courseid) { // need to get the course from the chosen category $courseid = $category->course; } if (! $course = get_record("course", "id", $courseid)) { error("Invalid course!"); } require_login($course->id, false); if (!isteacheredit($course->id)) { error( $txt->onlyteachersimport ); } // ensure the files area exists for this course make_upload_directory( "$course->id" ); //========== // PAGE HEADER //========== if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { $strupdatemodule = isteacheredit($course->id) ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename) : ""; print_header_simple($txt->importquestions, '', "wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural.''. " -> wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).''. ' -> '.$txt->importquestions, "", "", true, $strupdatemodule); $currenttab = 'edit'; $mode = 'import'; include($CFG->dirroot.'/mod/quiz/tabs.php'); } else { print_header_simple($txt->importquestions, '', $txt->importquestions); // print tabs $currenttab = 'import'; include('tabs.php'); } // file upload form sumitted if (!empty($format) and confirm_sesskey() ) { // file checks out ok $fileisgood = false; // work out if this is an uploaded file // or one from the filesarea. if (!empty($params->choosefile)) { $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}"; if (file_exists($importfile)) { $fileisgood = true; } else { notify($txt->uploadproblem); } } else { // must be upload file if (empty($_FILES['newfile'])) { notify( $txt->uploadproblem ); } else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) { notify( $txt->uploadproblem ); } else { $importfile = $_FILES['newfile']['tmp_name']; $fileisgood = true; } } // process if we are happy file is ok if ($fileisgood) { if (! is_readable("format/$format/format.php")) { error( get_string('formatnotfound','quiz', $format) ); } require("format.php"); // Parent class require("format/$format/format.php"); $classname = "qformat_$format"; $qformat = new $classname(); if (! $qformat->importpreprocess($category,$course)) { // Do anything before that we need to error( $txt->importerror , "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } if (! $qformat->importprocess($importfile, $params->matchgrades) ) { // Process the uploaded file error( $txt->importerror , "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } if (! $qformat->importpostprocess()) { // In case anything needs to be done after error( $txt->importerror , "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id"); } echo "