General developer forum

Restore - mapping to other activites

 
Picture of Zoran Jeremic
Restore - mapping to other activites
 
Hi,

I'm developing backup/restore for custom activity in Moodle 2.6 for collaborative work. This activity refers to 2 other activites (chat and forum) that exists in the course, so there are links to these activities inside of my activity. For both activities I have a a table that keeps a record in the form
(id, project_id, chat_id, chat_cm, forum_id, forum_mod)

Chat and Forum activities are restored by default restore features, but I'm not sure how to match that again to my restored activity, since ids are changed and I can't create mapppings to new ids as my restore code is not handling it.
I've tried something like this
$data->chat_id=$this->get_mappingid('chat',$data->chat_id);
$data->forum_id=$this->get_mappingid('forum',$data->forum_id);
but it doesn't work seems chat and forum mappings are not set in core code.

Is there any solution to provide these mappings without changing the core code?

Thanks,
Zoran


 
Average of ratings: -
Picture of Martin Greenaway
Re: Restore - mapping to other activites
 

Are these the only forum / chat activities in the course? Are you able to *infer* the IDs of the activities based on their activity type and the course ID?

 
Average of ratings: -
Picture of Darko Miletić
Re: Restore - mapping to other activites
Core developersParticularly helpful Moodlers

Core only handles core things. Each activity is plugin that may or may not be present. Hence each plugin handles it's own backup and restore.

I suggest that you take a look at restore_structure_step::set_mapping because it appears there is a global cache that stores all things as they are being restored. Also restore_dbops::set_backup_ids_record. They all operate on table backup_ids_temp.

However you do not have the control over the order of restoring. So if your thing get's restored before the linked activity there will be no data.



 
Average of ratings: -
Davo
Re: Restore - mapping to other activites
Core developersParticularly helpful MoodlersPlugin developers

Best way to solve this is to do the following:

  • During restore, set the chat id or forum id values to original id * -1 (so, forum ID '5' is stored as '-5' by the restore process)
  • Add a function to your restore class called either after_execute_course() or after_restore_course() (I have never really got my head around which one you want, you'll have to try them both until one works - maybe restore using a debugger + breakpoints to check)
  • In this after_restore/execute_course() function, do a DB search for every record on the course, related to your plugin, with an negative forum or chat id, then negate it again and call $this->get_mappingid() to convert it back into the number you want
  • If you still can't find the number, then you will need to set it to null (and make sure the rest of your code can handle this).

Doing this means you can delay sorting out the mappings until after the restore of the relevant activities is complete.

 
Average of ratings: Useful (1)
Picture of Zoran Jeremic
Re: Restore - mapping to other activites
 

Hey Davo,


You're genius. That's exactly what I needed. 

It's called after_restore() function.


Thanks,

Zoran

 
Average of ratings: -