\n";
echo "
{$importerrorquestion} {$questionname}";
if (!empty($text)) {
$text = s($text);
echo "
{$text}
\n";
}
echo "
{$message}\n";
echo "
";
$this->importerrors++;
}
/**
* Import for questiontype plugins
* Do not override.
* @param data mixed The segment of data containing the question
* @param question object processed (so far) by standard import code if appropriate
* @param extra mixed any additional format specific data that may be passed by the format
* @param qtypehint hint about a question type from format
* @return object question object suitable for save_options() or false if cannot handle
*/
public function try_importing_using_qtypes($data, $question = null, $extra = null,
$qtypehint = '') {
// work out what format we are using
$formatname = substr(get_class($this), strlen('qformat_'));
$methodname = "import_from_{$formatname}";
//first try importing using a hint from format
if (!empty($qtypehint)) {
$qtype = question_bank::get_qtype($qtypehint, false);
if (is_object($qtype) && method_exists($qtype, $methodname)) {
$question = $qtype->$methodname($data, $question, $this, $extra);
if ($question) {
return $question;
}
}
}
// loop through installed questiontypes checking for
// function to handle this question
foreach (question_bank::get_all_qtypes() as $qtype) {
if (method_exists($qtype, $methodname)) {
if ($question = $qtype->$methodname($data, $question, $this, $extra)) {
return $question;
}
}
}
return false;
}
/**
* Perform any required pre-processing
* @return bool success
*/
public function importpreprocess() {
return true;
}
/**
* Process the file
* This method should not normally be overidden
* @return bool success
*/
public function importprocess() {
global $USER, $DB, $OUTPUT;
// Raise time and memory, as importing can be quite intensive.
core_php_time_limit::raise();
raise_memory_limit(MEMORY_EXTRA);
// STAGE 1: Parse the file
if ($this->displayprogress) {
echo $OUTPUT->notification(get_string('parsingquestions', 'question'), 'notifysuccess');
}
if (! $lines = $this->readdata($this->filename)) {
echo $OUTPUT->notification(get_string('cannotread', 'question'));
return false;
}
if (!$questions = $this->readquestions($lines)) { // Extract all the questions
echo $OUTPUT->notification(get_string('noquestionsinfile', 'question'));
return false;
}
// STAGE 2: Write data to database
if ($this->displayprogress) {
echo $OUTPUT->notification(get_string('importingquestions', 'question',
$this->count_questions($questions)), 'notifysuccess');
}
// check for errors before we continue
if ($this->stoponerror and ($this->importerrors>0)) {
echo $OUTPUT->notification(get_string('importparseerror', 'question'));
return true;
}
// get list of valid answer grades
$gradeoptionsfull = question_bank::fraction_options_full();
// check answer grades are valid
// (now need to do this here because of 'stop on error': MDL-10689)
$gradeerrors = 0;
$goodquestions = array();
foreach ($questions as $question) {
if (!empty($question->fraction) and (is_array($question->fraction))) {
$fractions = $question->fraction;
$invalidfractions = array();
foreach ($fractions as $key => $fraction) {
$newfraction = match_grade_options($gradeoptionsfull, $fraction,
$this->matchgrades);
if ($newfraction === false) {
$invalidfractions[] = $fraction;
} else {
$fractions[$key] = $newfraction;
}
}
if ($invalidfractions) {
echo $OUTPUT->notification(get_string('invalidgrade', 'question',
implode(', ', $invalidfractions)));
++$gradeerrors;
continue;
} else {
$question->fraction = $fractions;
}
}
$goodquestions[] = $question;
}
$questions = $goodquestions;
// check for errors before we continue
if ($this->stoponerror && $gradeerrors > 0) {
return false;
}
// count number of questions processed
$count = 0;
foreach ($questions as $question) { // Process and store each question
$transaction = $DB->start_delegated_transaction();
// reset the php timeout
core_php_time_limit::raise();
// check for category modifiers
if ($question->qtype == 'category') {
if ($this->catfromfile) {
// find/create category object
$catpath = $question->category;
$newcategory = $this->create_category_path($catpath, $question);
if (!empty($newcategory)) {
$this->category = $newcategory;
}
}
$transaction->allow_commit();
continue;
}
$question->context = $this->importcontext;
$count++;
if ($this->displayprogress) {
echo "