function restore_create_groups($restore, $xml_file) { global $CFG; //Check it exists if (!file_exists($xml_file)) { return false; } //Get info from xml if (!($groups = restore_read_xml_groups($restore, $xml_file))) { //groups will contain the old_id of every group //in backup_ids->info will be the real info (serialized) return false; } else { if ($groups === true) { return true; } } $status = true; //Iterate over each group foreach ($groups as $group) { //Get record from backup_ids $data = backup_getid($restore->backup_unique_code, "groups", $group->id); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now build the GROUP record structure $gro = new Object(); $gro->courseid = $restore->course_id; $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']); $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']); if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) { $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']); } else { $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']); } $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']); $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']); $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']); $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']); //Now search if that group exists (by name and description field) in //restore->course_id course //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides. $description_clause = ''; if (!empty($gro->description)) { /// Only for groups having a description $literal_description = "'" . $gro->description . "'"; $description_clause = " AND " . sql_compare_text('description') . " = " . sql_compare_text($literal_description); } if (!($gro_db = get_record_sql("SELECT *\n FROM {$CFG->prefix}groups\n WHERE courseid = {$restore->course_id} AND\n name = '{$gro->name}'" . $description_clause, true))) { //If it doesn't exist, create $newid = insert_record('groups', $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, "groups", $group->id, $newid); } else { $status = false; continue; } //Now restore members in the groups_members, only if //users are included if ($restore->users != 2) { if (!restore_create_groups_members($newid, $info, $restore)) { $status = false; } } } } //Now, restore group_files if ($status) { $status = restore_group_files($restore); } return $status; }
function restore_create_groups($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) { //groups will contain the old_id of every group //in backup_ids->info will be the real info (serialized) $groups = restore_read_xml_groups($restore, $xml_file); } //Now, if we have anything in groups, we have to restore that //groups if ($groups) { if ($groups !== true) { //Iterate over each group foreach ($groups as $group) { //Get record from backup_ids $data = backup_getid($restore->backup_unique_code, "groups", $group->id); //Init variables $create_group = false; if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now build the GROUP record structure $gro = new Object(); ///$gro->courseid = backup_todb($info['GROUP']['#']['COURSEID']['0']['#']); $gro->name = backup_todb($info['GROUP']['#']['NAME']['0']['#']); $gro->description = backup_todb($info['GROUP']['#']['DESCRIPTION']['0']['#']); if (isset($info['GROUP']['#']['ENROLMENTKEY']['0']['#'])) { $gro->enrolmentkey = backup_todb($info['GROUP']['#']['ENROLMENTKEY']['0']['#']); } else { //if (! isset($gro->enrolment)) { $gro->enrolmentkey = backup_todb($info['GROUP']['#']['PASSWORD']['0']['#']); } $gro->lang = backup_todb($info['GROUP']['#']['LANG']['0']['#']); $gro->theme = backup_todb($info['GROUP']['#']['THEME']['0']['#']); $gro->picture = backup_todb($info['GROUP']['#']['PICTURE']['0']['#']); $gro->hidepicture = backup_todb($info['GROUP']['#']['HIDEPICTURE']['0']['#']); $gro->timecreated = backup_todb($info['GROUP']['#']['TIMECREATED']['0']['#']); $gro->timemodified = backup_todb($info['GROUP']['#']['TIMEMODIFIED']['0']['#']); //Now search if that group exists (by name and description field) in //restore->course_id course //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides. $description_clause = ''; if (!empty($gro->description)) { /// Only for groups having a description $literal_description = "'" . $gro->description . "'"; $description_clause = " AND " . sql_compare_text('description') . " = " . sql_compare_text($literal_description); } $gro_db = get_record_sql("SELECT *\n FROM {$CFG->prefix}groups g, {$CFG->prefix}groups_courses_groups gcg\n WHERE gcg.courseid = {$restore->course_id} \n AND g.id = gcg.groupid\n AND g.name = '{$gro->name}'" . $description_clause, true); //If it doesn't exist, create if (!$gro_db) { $create_group = true; } //If we must create the group if ($create_group) { //Me must recode the courseid to the restore->course_id $gro->courseid = $restore->course_id; //Check if the theme exists in destination server $themes = get_list_of_themes(); if (!in_array($gro->theme, $themes)) { $gro->theme = ''; } //The structure is equal to the db, so insert the group $newid = groups_restore_group($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, "groups", $group->id, $newid); } //Now restore members in the groups_members, only if //users are included if ($restore->users != 2) { $status2 = restore_create_groups_members($newid, $info, $restore); } } } //Now, restore group_files if ($status && $status2) { $status2 = restore_group_files($restore); } } } else { $status = false; } return $status && $status2; }