示例#1
0
function restore_create_groupings($restore, $xml_file)
{
    //Check it exists
    if (!file_exists($xml_file)) {
        return false;
    }
    //Get info from xml
    if (!($groupings = restore_read_xml_groupings($restore, $xml_file))) {
        return false;
    } else {
        if ($groupings === true) {
            return true;
        }
    }
    $status = true;
    //Iterate over each group
    foreach ($groupings as $grouping) {
        if ($data = backup_getid($restore->backup_unique_code, "groupings", $grouping->id)) {
            //Now get completed xmlized object
            $info = $data->info;
            //Now build the GROUPING record structure
            $gro = new Object();
            ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
            $gro->courseid = $restore->course_id;
            $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
            $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
            $gro->configdata = backup_todb($info['GROUPING']['#']['CONFIGDATA']['0']['#']);
            $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
            //Now search if that group exists (by name and description field) in
            if ($gro_db = get_record('groupings', 'courseid', $restore->course_id, 'name', $gro->name, 'description', $gro->description)) {
                //get current group id
                $newid = $gro_db->id;
            } else {
                //The structure is equal to the db, so insert the grouping
                if (!($newid = insert_record('groupings', $gro))) {
                    $status = false;
                    continue;
                }
            }
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, "groupings", $grouping->id, $newid);
        }
    }
    // now fix the defaultgroupingid in course
    $course = get_record('course', 'id', $restore->course_id);
    if ($course->defaultgroupingid) {
        if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) {
            set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
        } else {
            set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
        }
    }
    return $status;
}
示例#2
0
function restore_create_groupings($restore, $xml_file)
{
    global $CFG, $db;
    $status = true;
    $status2 = true;
    //Check it exists
    if (!file_exists($xml_file)) {
        $status = false;
    }
    //Get info from xml
    if ($status) {
        //groupings will contain the old_id of every group
        //in backup_ids->info will be the real info (serialized)
        $groupings = restore_read_xml_groupings($restore, $xml_file);
    }
    //Now, if we have anything in groupings, we have to restore that grouping
    if ($groupings) {
        if ($groupings !== true) {
            //Iterate over each group
            foreach ($groupings as $grouping) {
                //Get record from backup_ids
                $data = backup_getid($restore->backup_unique_code, "groupings", $grouping->id);
                //Init variables
                $create_grouping = false;
                if ($data) {
                    //Now get completed xmlized object
                    $info = $data->info;
                    //Now build the GROUPING record structure
                    $gro = new Object();
                    ///$gro->id = backup_todb($info['GROUPING']['#']['ID']['0']['#']);
                    $gro->name = backup_todb($info['GROUPING']['#']['NAME']['0']['#']);
                    $gro->description = backup_todb($info['GROUPING']['#']['DESCRIPTION']['0']['#']);
                    $gro->timecreated = backup_todb($info['GROUPING']['#']['TIMECREATED']['0']['#']);
                    //Now search if that group exists (by name and description field) in
                    //restore->course_id course
                    $gro_db = groups_grouping_matches($restore->course_id, $gro->name, $gro->description);
                    //If it doesn't exist, create
                    if (!$gro_db) {
                        $create_grouping = true;
                    }
                    //If we must create the group
                    if ($create_grouping) {
                        //The structure is equal to the db, so insert the grouping TODO: RESTORE.
                        $newid = groups_create_grouping($restore->course_id, $gro);
                    } else {
                        //get current group id
                        $newid = $gro_db->id;
                    }
                    if ($newid) {
                        //We have the newid, update backup_ids
                        backup_putid($restore->backup_unique_code, "groupings", $grouping->id, $newid);
                    }
                    //Now restore links from groupings to groups
                    $status2 = restore_create_groupings_groups($newid, $info, $restore);
                }
            }
            //(Now, restore grouping_files)
        }
    } else {
        $status = false;
    }
    return $status && $status2;
}