function restore_with_guid($restore, $table, $field, $record, $guid = null) { //echo "<pre>\nDEBUG: ".print_r($restore, true)."\n</pre>"; if (!isset($guid)) { $newid = insert_record($table, $record); //save_restore_guid($guid, $table, $field, $newid, $restore->course_id); return $newid; } else { if ($restoreto = get_id_from_backup_guid($guid, $restore->course_id)) { $record->{$restoreto->src_field} = $restoreto->src_value; //check to see if the record already exists, and if it does, update it. if ($recordexists = get_record($table, $field, $restoreto->src_value)) { if (!update_record($table, $record)) { return false; } } else { //record doesn't exist so need to insert it. debugging("this " . $table . " doesn't exist"); echo "record"; print_object($record); echo "table"; print_object($table); echo "restore"; print_object($restore); echo "restoreto"; print_object($restoreto); return false; //should return false as this is a failure! } return $restoreto->src_value; } else { $newid = insert_record($table, $record); save_restore_guid($guid, $table, $field, $newid, $restore->course_id); return $newid; } } }
function forum_restore_mods($mod, $restore) { global $CFG, $db; $status = true; //echo "<pre>\nDEBUG: ".print_r($restore, true)."\n</pre>"; //Get record from backup_ids $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id); if ($data) { //Now get completed xmlized object $info = $data->info; //if necessary, write to restorelog and adjust date/time fields if ($restore->course_startdateoffset) { restore_log_date_changes('Forum', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH')); } //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now, build the FORUM record structure $forum->course = $restore->course_id; $forum->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']); $forum->name = backup_todb($info['MOD']['#']['NAME']['0']['#']); $forum->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']); // These get dropped in Moodle 1.7 when the new Roles System gets // set up. Therefore they might or not be there depending on whether // we are restoring a 1.6+ forum or a 1.7 or later forum backup. if (isset($info['MOD']['#']['OPEN']['0']['#'])) { $forum->open = backup_todb($info['MOD']['#']['OPEN']['0']['#']); } if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) { $forum->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']); } $forum->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']); $forum->assesstimestart = backup_todb($info['MOD']['#']['ASSESSTIMESTART']['0']['#']); $forum->assesstimefinish = backup_todb($info['MOD']['#']['ASSESSTIMEFINISH']['0']['#']); $forum->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']); $forum->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']); $forum->forcesubscribe = backup_todb($info['MOD']['#']['FORCESUBSCRIBE']['0']['#']); $forum->trackingtype = backup_todb($info['MOD']['#']['TRACKINGTYPE']['0']['#']); $forum->rsstype = backup_todb($info['MOD']['#']['RSSTYPE']['0']['#']); $forum->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']); $forum->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']); $forum->warnafter = isset($info['MOD']['#']['WARNAFTER']['0']['#']) ? backup_todb($info['MOD']['#']['WARNAFTER']['0']['#']) : ''; $forum->blockafter = isset($info['MOD']['#']['BLOCKAFTER']['0']['#']) ? backup_todb($info['MOD']['#']['BLOCKAFTER']['0']['#']) : ''; $forum->blockperiod = isset($info['MOD']['#']['BLOCKPERIOD']['0']['#']) ? backup_todb($info['MOD']['#']['BLOCKPERIOD']['0']['#']) : ''; //We have to recode the scale field if it's <0 (positive is a grade, not a scale) if ($forum->scale < 0) { $scale = backup_getid($restore->backup_unique_code, "scale", abs($forum->scale)); if ($scale) { $forum->scale = -$scale->new_id; } } $guid = backup_todb($info['MOD']['#']['GUID']['0']['#']); if (!($cmid = get_id_from_backup_guid($guid, $forum->course))) { //get course_module Id for this forum debugging("couldn't get cmid from backup_guid guid:" . $guid . " course:" . $restore->course_id); } if (!($cm = get_record('course_modules', 'id', $cmid->src_value, 'course', $forum->course))) { debugging("couldn't get cm:" . $cmid->src_value . " course:" . $forum->course); } if ($forumindb = get_record('forum', 'id', $cm->instance, 'course', $forum->course)) { debugging("course exists, need to update", DEBUG_DEVELOPER); $forum->id = $cm->instance; if (!update_record('forum', $forum)) { debugging("update forum record failed!"); } $newid = $forum->id; } else { //debugging("forum doesn't exist, so insert it", DEBUG_DEVELOPER); $forum->id = $cm->instance; if (!($newid = insert_record('forum', $forum))) { debugging("inserting forum record failed!"); } } //Do some output if (!defined('RESTORE_SILENTLY')) { echo "<li>" . get_string("modulename", "forum") . " \"" . format_string(stripslashes($forum->name), true) . "\"</li>"; } backup_flush(300); if ($newid) { //We have the newid, update backup_ids backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid); $forum->id = $newid; //Now check if want to restore user data and do it. if (restore_userdata_selected($restore, 'forum', $mod->id)) { //Restore forum_subscriptions $status = forum_subscriptions_restore_mods($newid, $info, $restore); if ($status) { //Restore forum_discussions $status = forum_discussions_restore_mods($newid, $info, $restore); } if ($status) { //Restore forum_read $status = forum_read_restore_mods($newid, $info, $restore); } } // If forum type is single, just recreate the initial discussion/post automatically // if it hasn't been created still (because no user data was selected on backup or // restore. if ($forum->type == 'single' && !record_exists('forum_discussions', 'forum', $newid)) { //Load forum/lib.php require_once $CFG->dirroot . '/mod/forum/lib.php'; // Calculate the default format if (can_use_html_editor()) { $defaultformat = FORMAT_HTML; } else { $defaultformat = FORMAT_MOODLE; } //Create discussion/post data $sd = new stdClass(); $sd->course = $forum->course; $sd->forum = $newid; $sd->name = $forum->name; $sd->intro = $forum->intro; $sd->assessed = $forum->assessed; $sd->format = $defaultformat; $sd->mailnow = false; //Insert dicussion/post data $sdid = forum_add_discussion($sd, $sd->intro, $forum); //Now, mark the initial post of the discussion as mailed! if ($sdid) { set_field('forum_posts', 'mailed', '1', 'discussion', $sdid); } } } else { $status = false; } // If the backup contained $forum->open and $forum->assesspublic, // we need to convert the forum to use Roles. It means the backup // was made pre Moodle 1.7. if (isset($forum->open) && isset($forum->assesspublic)) { $forummod = get_record('modules', 'name', 'forum'); if (!($teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW))) { notice('Default teacher role was not found. Roles and permissions ' . 'for all your forums will have to be manually set.'); } if (!($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW))) { notice('Default student role was not found. Roles and permissions ' . 'for all your forums will have to be manually set.'); } if (!($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW))) { notice('Default guest role was not found. Roles and permissions ' . 'for teacher forums will have to be manually set.'); } require_once $CFG->dirroot . '/mod/forum/lib.php'; forum_convert_to_roles($forum, $forummod->id, $teacherroles, $studentroles, $guestroles, $restore->mods['forum']->instances[$mod->id]->restored_as_course_module); } } else { $status = false; } return $status; }