/**
 * セクションのリストア
 * 
 * @param object $restore
 * @param string $xml_file
 * @return bool
 */
function project_restore_section(&$restore, $xml_file)
{
    global $CFG, $db;
    $status = true;
    // $restoreのsectionパラメーターのチェック
    if (empty($restore->section)) {
        return false;
    }
    // セクションを取得
    if (!($section = get_course_section($restore->section, $restore->course_id))) {
        error("Section data was incorrect (can't find it)");
    }
    //Check it exists
    if (!file_exists($xml_file)) {
        $status = false;
    }
    //Get info from xml
    if ($status) {
        $info = restore_read_xml_sections($xml_file);
    }
    //Put the info in the DB, recoding ids and saving the in backup tables
    $sequence = "";
    if ($info) {
        //For each, section, save it to db
        foreach ($info->sections as $key => $sect) {
            $sequence = "";
            $update_rec = new object();
            $update_rec->id = $section->id;
            $update_rec->course = $restore->course_id;
            $update_rec->section = $restore->section;
            $update_rec->summary = backup_todb($sect->summary);
            $update_rec->visible = $sect->visible;
            $update_rec->sequence = "";
            $status = update_record('course_sections', $update_rec);
            $newid = $update_rec->id;
            if ($newid) {
                //save old and new section id
                backup_putid($restore->backup_unique_code, "course_sections", $key, $newid);
            } else {
                $status = false;
            }
            //If all is OK, go with associated mods
            if ($status) {
                //If we have mods in the section
                if (!empty($sect->mods)) {
                    //For each mod inside section
                    foreach ($sect->mods as $keym => $mod) {
                        // Yu: This part is called repeatedly for every instance,
                        // so it is necessary to set the granular flag and check isset()
                        // when the first instance of this type of mod is processed.
                        //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
                        if (!isset($restore->mods[$mod->type]->granular)) {
                            if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) {
                                // This defines whether we want to restore specific
                                // instances of the modules (granular restore), or
                                // whether we don't care and just want to restore
                                // all module instances (non-granular).
                                $restore->mods[$mod->type]->granular = true;
                            } else {
                                $restore->mods[$mod->type]->granular = false;
                            }
                        }
                        //Check if we've to restore this module (and instance)
                        if (!empty($restore->mods[$mod->type]->restore)) {
                            if (empty($restore->mods[$mod->type]->granular) || array_key_exists($mod->instance, $restore->mods[$mod->type]->instances) && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore)) {
                                //Get the module id from modules
                                $module = get_record("modules", "name", $mod->type);
                                if ($module) {
                                    $course_module = new object();
                                    $course_module->course = $restore->course_id;
                                    $course_module->module = $module->id;
                                    $course_module->section = $newid;
                                    $course_module->added = $mod->added;
                                    $course_module->score = $mod->score;
                                    $course_module->indent = $mod->indent;
                                    $course_module->visible = $mod->visible;
                                    $course_module->groupmode = $mod->groupmode;
                                    if ($mod->groupingid and $grouping = backup_getid($restore->backup_unique_code, "groupings", $mod->groupingid)) {
                                        $course_module->groupingid = $grouping->new_id;
                                    } else {
                                        $course_module->groupingid = 0;
                                    }
                                    $course_module->groupmembersonly = $mod->groupmembersonly;
                                    $course_module->instance = 0;
                                    //NOTE: The instance (new) is calculated and updated in db in the
                                    //      final step of the restore. We don't know it yet.
                                    //print_object($course_module);                    //Debug
                                    //Save it to db
                                    $newidmod = insert_record("course_modules", $course_module);
                                    if ($newidmod) {
                                        //save old and new module id
                                        //In the info field, we save the original instance of the module
                                        //to use it later
                                        backup_putid($restore->backup_unique_code, "course_modules", $keym, $newidmod, $mod->instance);
                                        $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod;
                                    } else {
                                        $status = false;
                                    }
                                    //Now, calculate the sequence field
                                    if ($status) {
                                        if ($sequence) {
                                            $sequence .= "," . $newidmod;
                                        } else {
                                            $sequence = $newidmod;
                                        }
                                    }
                                } else {
                                    $status = false;
                                }
                            }
                        }
                    }
                }
            }
            //If all is OK, update sequence field in course_sections
            if ($status) {
                if (isset($sequence)) {
                    $update_rec = new object();
                    $update_rec->id = $newid;
                    $update_rec->sequence = $sequence;
                    $status = update_record("course_sections", $update_rec);
                }
            }
        }
    } else {
        $status = false;
    }
    return $status;
}
Пример #2
0
/**
 * this function restores role assignments and role overrides
 * in course/user/block/mod level, it passed through
 * the xml file again
 */
function restore_roles_settings($restore, $xmlfile)
{
    // data pulls from course, mod, user, and blocks
    /*******************************************************
     * Restoring from course level assignments *
     *******************************************************/
    if (!defined('RESTORE_SILENTLY')) {
        echo "<li>" . get_string("creatingcourseroles") . '</li>';
    }
    $course = restore_read_xml_course_header($xmlfile);
    if (!isset($restore->rolesmapping)) {
        $isimport = true;
        // course import from another course, or course with no role assignments
    } else {
        $isimport = false;
        // course restore with role assignments
    }
    if (!empty($course->roleassignments) && !$isimport) {
        $courseassignments = $course->roleassignments;
        foreach ($courseassignments as $oldroleid => $courseassignment) {
            restore_write_roleassignments($restore, $courseassignment->assignments, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
        }
    }
    /*****************************************************
     * Restoring from course level overrides *
     *****************************************************/
    if (!empty($course->roleoverrides) && !$isimport) {
        $courseoverrides = $course->roleoverrides;
        foreach ($courseoverrides as $oldroleid => $courseoverride) {
            // if not importing into exiting course, or creating new role, we are ok
            // local course overrides to be respected (i.e. restored course overrides ignored)
            if ($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING || empty($restore->rolesmapping[$oldroleid])) {
                restore_write_roleoverrides($restore, $courseoverride->overrides, "course", CONTEXT_COURSE, $course->course_id, $oldroleid);
            }
        }
    }
    /*******************************************************
     * Restoring role assignments/overrdies                *
     * from module level assignments                       *
     *******************************************************/
    if (!defined('RESTORE_SILENTLY')) {
        echo "<li>" . get_string("creatingmodroles") . '</li>';
    }
    $sections = restore_read_xml_sections($xmlfile);
    $secs = $sections->sections;
    foreach ($secs as $section) {
        if (isset($section->mods)) {
            foreach ($section->mods as $modid => $mod) {
                if (isset($mod->roleassignments) && !$isimport) {
                    foreach ($mod->roleassignments as $oldroleid => $modassignment) {
                        restore_write_roleassignments($restore, $modassignment->assignments, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
                    }
                }
                // role overrides always applies, in import or backup/restore
                if (isset($mod->roleoverrides)) {
                    foreach ($mod->roleoverrides as $oldroleid => $modoverride) {
                        restore_write_roleoverrides($restore, $modoverride->overrides, "course_modules", CONTEXT_MODULE, $modid, $oldroleid);
                    }
                }
            }
        }
    }
    /*************************************************
     * Restoring assignments from blocks level       *
     * role assignments/overrides                    *
     *************************************************/
    if ($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING) {
        // skip altogether if restoring to exisitng course by adding
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingblocksroles") . '</li>';
        }
        $blocks = restore_read_xml_blocks($restore, $xmlfile);
        if (isset($blocks->instances)) {
            foreach ($blocks->instances as $instance) {
                if (isset($instance->roleassignments) && !$isimport) {
                    foreach ($instance->roleassignments as $oldroleid => $blockassignment) {
                        restore_write_roleassignments($restore, $blockassignment->assignments, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
                    }
                }
                // likewise block overrides should always be restored like mods
                if (isset($instance->roleoverrides)) {
                    foreach ($instance->roleoverrides as $oldroleid => $blockoverride) {
                        restore_write_roleoverrides($restore, $blockoverride->overrides, "block_instance", CONTEXT_BLOCK, $instance->id, $oldroleid);
                    }
                }
            }
        }
    }
    /************************************************
     * Restoring assignments from userid level      *
     * role assignments/overrides                   *
     ************************************************/
    if (!defined('RESTORE_SILENTLY')) {
        echo "<li>" . get_string("creatinguserroles") . '</li>';
    }
    $info = restore_read_xml_users($restore, $xmlfile);
    if (!empty($info->users) && !$isimport) {
        // no need to restore user assignments for imports (same course)
        //For each user, take its info from backup_ids
        foreach ($info->users as $userid) {
            $rec = backup_getid($restore->backup_unique_code, "user", $userid);
            if (isset($rec->info->roleassignments)) {
                foreach ($rec->info->roleassignments as $oldroleid => $userassignment) {
                    restore_write_roleassignments($restore, $userassignment->assignments, "user", CONTEXT_USER, $userid, $oldroleid);
                }
            }
            if (isset($rec->info->roleoverrides)) {
                foreach ($rec->info->roleoverrides as $oldroleid => $useroverride) {
                    restore_write_roleoverrides($restore, $useroverride->overrides, "user", CONTEXT_USER, $userid, $oldroleid);
                }
            }
        }
    }
    return true;
}
 protected function restoreSection()
 {
     // コースモジュールIDマップ ("モジュール名:インスタンスID" => コースモジュールID)
     $inst2cmid_map = array();
     // モジュールをコースに登録
     foreach ($this->prefs->mods as $name => $mod) {
         $module = get_record('modules', 'name', $name);
         foreach ($mod->instances as $old_id => $inst) {
             $backup_id = get_record('backup_ids', 'backup_code', $this->getUnique(), 'table_name', $name, 'old_id', $old_id);
             $course_module = new stdClass();
             $course_module->course = $this->course->id;
             $course_module->module = $module->id;
             $course_module->instance = $backup_id->new_id;
             $course_module->section = $this->section->id;
             $course_module->added = $this->getUnique();
             $course_module->score = 0;
             $course_module->indent = 0;
             $course_module->visible = 1;
             $course_module->groupmode = 0;
             $course_module->groupingid = 0;
             $course_module->groupmembersonly = 0;
             $course_module->id = insert_record('course_modules', $course_module);
             if (!$course_module->id) {
                 throw new SharingCart_ModuleException('Insertion failure');
             }
             // コースモジュールIDマップに追加
             $inst2cmid_map["{$name}:{$old_id}"] = $course_module->id;
         }
     }
     if ($info = restore_read_xml_sections($this->getTempDir() . '/moodle.xml')) {
         // 旧バージョンの共有ライブラリによるバックアップには
         // セクション情報が格納されていないので存在をチェック
         if (!empty($info->sections) and $section = reset($info->sections)) {
             if (!empty($section->mods)) {
                 // バックアップ時の並び順にコースモジュールIDを並べ直す
                 $ordered_cmids = array();
                 foreach ($section->mods as $mod) {
                     if (empty($inst2cmid_map["{$mod->type}:{$mod->instance}"])) {
                         throw new SharingCart_XmlException('Section modules mismatch');
                     }
                     $ordered_cmids[] = $inst2cmid_map["{$mod->type}:{$mod->instance}"];
                     if ($this->opt_modules_status) {
                         // コースモジュールの状態(非表示、インデントなど)を復元
                         $course_module = get_record('course_modules', 'id', end($ordered_cmids));
                         $fields = array('score', 'indent', 'visible', 'groupmode', 'groupmembersonly', 'idnumber');
                         foreach ($fields as $field) {
                             $course_module->{$field} = $mod->{$field};
                         }
                         update_record('course_modules', $course_module);
                     }
                 }
                 if (count($ordered_cmids) != count($inst2cmid_map)) {
                     throw new SharingCart_XmlException('Section modules mismatch');
                 }
                 $inst2cmid_map = $ordered_cmids;
             }
             if ($this->opt_section_status) {
                 // セクションの状態(非表示、サマリなど)を復元
                 $fields = array('summary', 'visible');
                 foreach ($fields as $field) {
                     $this->section->{$field} = addslashes($section->{$field});
                 }
                 update_record('course_sections', $this->section);
             }
         }
     }
     // array_merge で連結するために連想配列から単純配列に変換
     $cmids = array_values($inst2cmid_map);
     // セクションシーケンスを更新
     $section_sequence = explode(',', $this->section->sequence);
     $section_sequence = array_merge($section_sequence, $cmids);
     $section_sequence = array_filter($section_sequence);
     $this->section->sequence = implode(',', $section_sequence);
     // NOTE: update_record() する場合は summary を addslashes() でエスケープする必要あり
     if (!set_field('course_sections', 'sequence', $this->section->sequence, 'id', $this->section->id)) {
         throw new SharingCart_SectionException('Updating failure');
     }
 }