How to restore a coursemodule id?

How to restore a coursemodule id?

by Lukas Dürrenberger -
Number of replies: 1
The previous developers of the plugin I'm now working on, have saved the coursemodule id into our plugin database (I assume for easier access). This seems to work all fine, however when you restore a course backup, the coursemodule id needs updating, yet I have no idea how to figure out a mapping between old cmid and new cmid.

The information I have is the course id and the module id, what I'm basically missing now is the mapping of the (studentquiz) entries  to the course_modules instance.

I would greatly appreciate any input!

Average of ratings: -
In reply to Lukas Dürrenberger

Re: How to restore a coursemodule id?

by Lukas Dürrenberger -

I figured out a way to do it. I feel if Moodle was built a bit better structured, this all wouldn't be such an issue. After everything has been restored, we go in and find the the matching instance in course_modules and add the course_modules id to our table.

    protected function after_execute() {
        global $DB;

        $courseid = $this->get_courseid();
        $moduleid = $DB->get_field('modules', 'id', array('name'=>'studentquiz'));

        $cms = $DB->get_records('course_modules', array('course'=>$courseid, 'module'=>$moduleid));

        foreach ($cms as $cm) {
            $studentquiz = $DB->get_record('studentquiz', array('id'=>$cm->instance));
            $studentquiz->coursemodule = $cm->id;
            $DB->update_record('studentquiz', $studentquiz);
        }
    }