Search code examples
phpphpmyadminmoodlebitnamimoodle-api

Error after copying a moodle course (Qtype)


I'm currently writting a Question Type plugin for moodle.

I have the issue that after copying a course (Site Administration -> Courses -> Manage courses and categories -> ...) no second entry is created in $DB and the error accures: "Can't find data record in database table '...'".

My $DB before and after copying the course. The entry is after saving the Question.

Database

Debug info: SELECT * FROM {qtype_aligator_options} WHERE questionid = ?
[array (
0 => '169',
)]
Error code: invalidrecord

Stack trace:
line 1599 of \lib\dml\moodle_database.php: dml_missing_record_exception thrown
line 1575 of \lib\dml\moodle_database.php: call to moodle_database->get_record_select()
line 23 of \question\type\aligator\questiontype.php: call to moodle_database->get_record()
line 913 of \lib\questionlib.php: call to qtype_aligator->get_question_options()
line 982 of \lib\questionlib.php: call to _tidy_question()
line 126 of \question\question.php: call to get_question_options()

Usually there should be after copying the course a second entry with a different questionid (i.E. 169). (I think).

I'm unsure where I'm making the mistake. If I'm not completely wrong the $DB saving is managed in the questiontype.php, isn't it?

Perhabs someone has the patience to look over my code snippets or give me some input which might be the issue for this error. I think personally that I'm doing something wrong in save_question_options.

questiontype.php

    public function get_question_options($question)
{
    global $DB;
    $question->options = $DB->get_record(
        'qtype_aligator_options',
        array('questionid' => $question->id),
        '*',
        MUST_EXIST
    );
    parent::get_question_options($question);
}

public function save_question_options($formdata)
{
    global $DB;
    $options = $DB->get_record('qtype_aligator_options', array('questionid' => $formdata->id));

    if (!$options) {
        $options = new stdClass();
        $options->questionid = $formdata->id;
        $options->id = $DB->insert_record('qtype_aligator_options', $options);
    }

    $options->custom_input = $formdata->custom_input;
    $options->wkz = $formdata->wkz;

    $DB->update_record('qtype_aligator_options', $options);
}

protected function initialise_question_instance(question_definition $question, $questiondata)
{
    parent::initialise_question_instance($question, $questiondata);
    $question->custom_input = $questiondata->options->custom_input;
    $questiondata->formatwkz = $questiondata->options->formatwkz;
}

edit_aligator_form.php

class qtype_aligator_edit_form extends question_edit_form
{

protected function definition_inner($mform)
{
    $qtype = question_bank::get_qtype('aligator');
    // Header
    $mform->addElement('header', 'header_1', get_string('header_1', 'qtype_aligator'));
    $mform->setExpanded('header_1');

    // input field
    $mform->addElement('text', 'custom_input', get_string('custom_input', 'qtype_aligator'));
    $mform->setType('custom_input', PARAM_RAW);

    // WKZ drop down
    $wkz_options = array(
        get_string('formateur', 'qtype_aligator'),
        get_string('formatusd', 'qtype_aligator'),
    );
    $mform->addElement(
        'select',
        'wkz',
        get_string('formatwkz', 'qtype_aligator'),
        $wkz_options,
    );
    $mform->setDefault('wkz', get_config('qtype_aligator', 'formateur'));
}

public function data_preprocessing($question)
{
    $question = parent::data_preprocessing($question);

    if (empty($question->options)) {
        return $question;
    }

    $question->custom_input = $question->options->custom_input;
    $question->wkz = $question->options->wkz;

    return $question;
}

public function qtype()
{
    return 'aligator';
}
}

Thanks in advance!

P.S. I'm using the bitnami moodle stack 3.9.2


Solution

  • Thanks to Russell England (https://stackoverflow.com/users/1603711/russell-england) I managed to solve the issue. He was completely right with his assumption that I'm missing backup/moodle2 /backup.class.php and /restore.class.php

    If someone has the same issue: I was able to reuse some of the code from the qtype_essay from moodle + modified some oft those parts.