MDL-50310 glossary: Fix restore of random glossary entry block
[moodle.git] / blocks / glossary_random / backup / moodle2 / restore_glossary_random_block_task.class.php
index 017e407..cfc5bc1 100644 (file)
@@ -62,9 +62,16 @@ class restore_glossary_random_block_task extends restore_block_task {
             if (!empty($config->glossary)) {
                 // Get glossary mapping and replace it in config
                 if ($glossarymap = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'glossary', $config->glossary)) {
-                    $config->glossary = $glossarymap->newitemid;
+                    $mappedglossary = $DB->get_record('glossary', array('id' => $glossarymap->newitemid),
+                        'id,course,globalglossary', MUST_EXIST);
+                    $config->glossary = $mappedglossary->id;
+                    $config->courseid = $mappedglossary->course;
+                    $config->globalglossary = $mappedglossary->globalglossary;
                     $configdata = base64_encode(serialize($config));
                     $DB->set_field('block_instances', 'configdata', $configdata, array('id' => $blockid));
+                } else {
+                    // The block refers to a glossary not present in the backup file.
+                    $DB->set_field('block_instances', 'configdata', '', array('id' => $blockid));
                 }
             }
         }