Course restore from Moodle v3.4.6 to v3.6.3 error/unknown_context_mapping

H5P version 1.17.2 with all library updated.

attempting to restore a Moodle v3.4.6+ course with H5P activity(Interactive video) to a Moodle v3.6.3 would result "error/unknown_context_mapping"

Steps to reproduce the error:

  1. Add an h5p activity to a moodle 3.4 course.
  2. Backup the moodle 3.4 course.
  3. Restore into a moodle 3.6 course.
  4. The backup will fail with an error/unknown_context_mapping error.

On screen debug output on screen from Moodle v3.6 is attached in the post.

nadavkav's picture

I am experiancing the same issue

(And it was also reported here: )

This happens when for some reason, in mdl_context the first line id is not 1. (The context line of the site itself)

We were able to get around this currently by editing: mod/hvp/backup/moodle2/restore_hvp_stepslib.php

in function after_execute() line 291


$this->add_related_files('mod_hvp', 'libraries', null, $context->id);

with:$this->add_related_files('mod_hvp', 'libraries', null, 1);
This will only work when the origin site had his id set to one ( need to really pass the old context id, I currently don't know how to do this Thanks



Can you please explain where should I change the file?
Is it on the original server or the server of the moodle that should get the file?

kiril's picture

When i restore activity on Moodle 3.73 i got error in the same function. But diferent type of error.

Unknown exception related to local files (Invalid file path




I get an error: error/unknown_context_mapping when I restoring a course from moodle version 3.9 to 3.8.3.

What can be done?


I have the same issue. Is there any workaround?

I have the same issue and I did a temporary workaround for it.

My context:

  • source Moodle is a fresh Moodle 3.11.x, the course with H5p-content. System Context ID = 1 (checked with <code>select * from mdl_context</code>)
  • target Moodle is an "old" 3.9.20. System Context ID = 2

Both Databases are on MariaDb-Galera-cluster with autoincrement = 3. The restore on Moodle 3.9 was failing with:

Error code: unknown_context_mapping$a contents: error×Stack trace: line 925 of /backup/util/dbops/restore_dbops.class.php: restore_dbops_exception thrownline 239 of /backup/util/plan/restore_structure_step.class.php: call to restore_dbops::send_files_to_pool()line 291 of /mod/hvp/backup/moodle2/restore_hvp_stepslib.php: call to restore_structure_step->add_related_files('mod_hvp', 'libraries', null, $context->id: 2)line 409 of /backup/util/plan/restore_structure_step.class.php: call to restore_hvp_libraries_structure_step->after_execute()line 113 of /backup/util/plan/restore_structure_step.class.php: call to restore_structure_step->launch_after_execute_methods()line 181 of /backup/util/plan/base_task.class.php: call to restore_structure_step->execute()line 210 of /backup/moodle2/restore_activity_task.class.php: call to base_task->execute()line 191 of /backup/util/plan/base_plan.class.php: call to restore_activity_task->execute()line 168 of /backup/util/plan/restore_plan.class.php: call to base_plan->execute()line 394 of /backup/controller/restore_controller.class.php: call to restore_plan->execute()line 219 of /backup/util/ui/restore_ui.class.php: call to restore_controller->execute_plan() line 143 of /backup/restore.php: call to restore_ui->execute() 


  • method add_related_files was called with params:  'mod_hvp', 'libraries', null, 2
  • context_id=2 is a proper system context id for this Moodle instance
  • later in restore_dbops.class.php the context was not found in the table "backup_ids_temp" while calling <code>$newcontextrecord = self::get_backup_ids_record($restoreid, 'context', $oldcontextid); //oldcontextid==2</code>
  • long BEFORE the failed method was called, the temporary table "backup_ids_temp" was populated with an item (1, "context", $restoreid) in file backup_structure_dbops.class.php:104, line <code>$DB->insert_record('backup_ids_temp'</code>). That was supposedly done from the Moodle code.
  • => real situaion is following: 1. restore process saves a "context"-record in table backup_ids_temp with id=1 (which is wrong because the real context in this case =2); 2. then it cannot find it by id=2 (which is right).
  • I haven't found why it stores context to backup_ids_temp always with "1". I tried to replace tag "contextid" in xml-files of the MBZ-backup from "1" to "2" but it didnt helped.
  • Because I already spent a lot of time for analysis, I decided to stop researching and create a simple workaround for my usecase:

in file moodle/backup/util/dbops/restore_dbops.class.php: in method public static function send_files_to_pool (near line 9xx) replace:

if (!$newcontextrecord || !$newcontextrecord->newitemid) {
    throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid); // <<<-------  this line

with the following code:

if (!$newcontextrecord || !$newcontextrecord->newitemid) {
if ($oldcontextid == 2) {
$newcontextrecord = self::get_backup_ids_record($restoreid, 'context', 1);
if (!$newcontextrecord || !$newcontextrecord->newitemid) {
throw new restore_dbops_exception('unknown_context_mapping', 1);
} else {
throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);


It worked for my case.