local plugin backup, questions hook

local plugin backup, questions hook

by Ray Morris -
Number of replies: 4

I've been struggling with trying to figure out how to get my local plugin data backed up and after carefully studying all of the documentation I've found and trying things I don't think I'm getting anywhere.  Does anyone perhaps have a local plugin that uses backup?  Maybe the local_dev plugin used on Moodle.org?


I know there is an undocumented API, and it has various hooks such as the question hook. Knowing that, I've tried to adapt the documentation that exists for activity backup and reports backup.  That only resulted in utter confusion and cussing. I don't know if I'm heading the right way at all since the docs for activities are so different than the ones for reports.


My plugin stores some extra data related to questions, and there's no obvious way to know which questions are included in a backup other than to use the question hook (define_question_plugin_structure), I guess that's part of what I need to do.  What the heck to code inside of define_question_plugin_structure I have no idea.  I'm attaching the code I have now, which throws the error below.  What I have now is partially based on a qtype plugin, since that also has a question hook for backup. I sure could use some pointers. 


Really, really awesome to have would be a "hello world" plugin that uses define_question_plugin_structure to backup some simple data item associated with a question.  That's probably too much to ask, though. smile


Thanks for any pointers or suggestions.



error/baseelementincorrectfinalorattribute

More information about this error

Debug info: 
Error code: baseelementincorrectfinalorattribute 
$a contents: include
Stack trace:
  • line 125 of \backup\util\structure\base_final_element.class.php: base_element_struct_exception thrown
  • line 333 of \backup\util\structure\backup_nested_element.class.php: call to base_final_element->find_element_by_path()
  • line 73 of \backup\util\structure\backup_optigroup_element.class.php: call to backup_nested_element->find_element()
  • line 191 of \backup\util\structure\backup_optigroup_element.class.php: call to backup_optigroup_element->set_condition()
  • line 175 of \backup\util\structure\base_nested_element.class.php: call to backup_optigroup_element->set_parent()
  • line 56 of \backup\util\structure\backup_optigroup.class.php: call to base_nested_element->add_child()
  • line 89 of \backup\moodle2\backup_plugin.class.php: call to backup_optigroup->add_child()
  • line 56 of \local\enemyquestions\backup\moodle2\backup_local_enemyquestions_plugin.class.php: call to backup_plugin->get_plugin_element()


Average of ratings: -
In reply to Ray Morris

Re: local plugin backup, questions hook

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

It depends of whether you need to attach your local plugin's data to the course, the section or a module instance in the backup. Given that your plugin is called local_foobar, the following should work (not tested) to attach your plugin's data to other modules' data.

// backup/moodle2/backup_local_foobar_plugin.class.php

class backup_local_foobar_plugin extends backup_local_plugin {

    /**
     * Returns the information to be attached to a module instance
     */
    protected function define_module_plugin_structure() {

        $plugin = $this->get_plugin_element();
        $foobar = new backup_nested_element($this->get_recommended_name());
        $plugin->add_child($filterurlresource);

        // Continue here to add more child elements to your $foobar and
        // set their data source as normally.
    }
}

For an example of how to attach your plugin's data to a course and section, see e.g. this file - it is for a course format but it should work similarly AFAIK.

In reply to Ray Morris

Re: local plugin backup, questions hook

by Mark Nielsen -

Hey Ray, in looking at your code, it seems like you are on the right track.  I think what is causing the error is this line:

$plugin = $this->get_plugin_element(null, $this->get_include_condition(), 'include');

I haven't use those condition parameters before and I really don't know what they are all about.  I think you are trying to see if the question is included in the backup or not, but by the time your hook is being called, the question is being written out to the backup, so it is included.  Maybe try just removing all the parameters here like in David's post.

In reply to Mark Nielsen

Re: local plugin backup, questions hook

by Ray Morris -

Thanks so much for the help.  If anyone else having similar trouble comes across this post, here is a working example of local plugin backup and restore:


https://github.com/MorrisR2/moodle_local_enemyquestions/tree/master/backup/moodle2

Where I used $pluginwrapper->set_source_sql, many plugins will use the simpler $pluginwrapper->set_source_table.

In reply to Ray Morris

Re: local plugin backup, questions hook

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators
Thanks Ray. One thing I noticed is that your local plugin's table is called "enemyquestions" without the plugin type prefix. That exception should be used for activity modules only. If you ever plan to distribute your plugin, the table should be named "local_enemyquestions".