function qcreate_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Qcreate', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
        }
        //Now, build the QCREATE record structure
        $qcreate = new object();
        $qcreate->course = $restore->course_id;
        $qcreate->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $qcreate->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $qcreate->graderatio = backup_todb($info['MOD']['#']['GRADERATIO']['0']['#']);
        $qcreate->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $qcreate->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
        $qcreate->allowed = backup_todb($info['MOD']['#']['ALLOWED']['0']['#']);
        $qcreate->totalrequired = backup_todb($info['MOD']['#']['TOTALREQUIRED']['0']['#']);
        $qcreate->studentqaccess = backup_todb($info['MOD']['#']['STUDENTQACCESS']['0']['#']);
        $qcreate->timesync = 0;
        $qcreate->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
        $qcreate->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
        $qcreate->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $qcreate->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //We have to recode the grade field if it is <0 (scale)
        if ($qcreate->grade < 0) {
            $scale = backup_getid($restore->backup_unique_code, "scale", abs($qcreate->grade));
            if ($scale) {
                $qcreate->grade = -$scale->new_id;
            }
        }
        //The structure is equal to the db, so insert the qcreate
        $newid = insert_record("qcreate", $qcreate);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "qcreate") . " \"" . format_string(stripslashes($qcreate->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);
            $status = qcreate_requireds_restore_mods($mod->id, $newid, $info, $restore) && $status;
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'qcreate', $mod->id)) {
                //Restore qcreate_grades
                $status = qcreate_grades_restore_mods($mod->id, $newid, $info, $restore) && $status;
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function voiceauthoring_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    $userdata = restore_userdata_selected($restore, "voiceauthoring", $mod->id);
    if ($data) {
        //Now get completed xmlized object
        $info = $data->info;
        //add logs
        //Now, build the voiceauthoring record structure
        $resource = get_record("voiceauthoring_resources", "fromrid", $info['MOD']['#']['RID']['0']['#'], "course", $restore->course_id);
        if (empty($resource)) {
            $resourceCopy = voicetools_api_copy_resource($info['MOD']['#']['RID']['0']['#'], null, "0");
            if ($resourceCopy === false) {
                return false;
                //error to copy the resource
            }
            $resourceId = $resourceCopy->getRid();
            voiceauthoring_createResourceFromResource($info['MOD']['#']['RID']['0']['#'], $resourceId, $restore->course_id);
        }
        if ($userdata === false) {
            $mid = $resource->mid + 1;
            $resource->mid = $resource->mid + 1;
            update_record("voiceauthoring_resources", $resource);
            $name = str_replace(backup_todb($info['MOD']['#']['MID']['0']['#']), $mid, backup_todb($info['MOD']['#']['NAME']['0']['#']));
        } else {
            $mid = backup_todb(backup_todb($info['MOD']['#']['MID']['0']['#']));
            $name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        }
        $voiceauthoring->course = backup_todb($restore->course_id);
        $voiceauthoring->rid = backup_todb($resourceId);
        $voiceauthoring->mid = $mid;
        $voiceauthoring->name = str_replace(backup_todb($info['MOD']['#']['RID']['0']['#']), $resourceId, $name);
        $voiceauthoring->activityname = backup_todb($info['MOD']['#']['ACTIVITY_NAME']['0']['#']);
        $voiceauthoring->section = backup_todb($info['MOD']['#']['SECTION']['0']['#']);
        $voiceauthoring->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $voiceauthoring->isfirst = 1;
        //The structure is equal to the db, so insert the voiceauthoring
        $newid = insert_record("voiceauthoring", $voiceauthoring);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "voiceauthoring") . " \"" . format_string(stripslashes(stripslashes($voiceauthoring->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);
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #3
0
function wiki_restore_mods($mod, $restore)
{
    global $CFG, $DB;
    $status = true;
    //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('Wiki', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the wiki record structure
        $wiki->course = $restore->course_id;
        $wiki->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $wiki->intro = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
        $wiki->pagename = backup_todb($info['MOD']['#']['PAGENAME']['0']['#']);
        $wiki->wtype = backup_todb($info['MOD']['#']['WTYPE']['0']['#']);
        $wiki->ewikiprinttitle = backup_todb($info['MOD']['#']['EWIKIPRINTTITLE']['0']['#']);
        $wiki->htmlmode = backup_todb($info['MOD']['#']['HTMLMODE']['0']['#']);
        $wiki->ewikiacceptbinary = backup_todb($info['MOD']['#']['EWIKIACCEPTBINARY']['0']['#']);
        $wiki->disablecamelcase = backup_todb($info['MOD']['#']['DISABLECAMELCASE']['0']['#']);
        $wiki->setpageflags = backup_todb($info['MOD']['#']['SETPAGEFLAGS']['0']['#']);
        $wiki->strippages = backup_todb($info['MOD']['#']['STRIPPAGES']['0']['#']);
        $wiki->removepages = backup_todb($info['MOD']['#']['REMOVEPAGES']['0']['#']);
        $wiki->revertchanges = backup_todb($info['MOD']['#']['REVERTCHANGES']['0']['#']);
        $wiki->initialcontent = backup_todb($info['MOD']['#']['INITIALCONTENT']['0']['#']);
        $wiki->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the wiki
        $newid = $DB->insert_record("wiki", $wiki);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "wiki") . " \"" . format_string($wiki->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'wiki', $mod->id)) {
                //Restore wiki_entries
                $status = wiki_entries_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function journal_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Journal', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the JOURNAL record structure
        $journal->course = $restore->course_id;
        $journal->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $journal->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $journal->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
        $journal->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
        $journal->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
        $journal->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //We have to recode the assessed field if it is <0 (scale)
        if ($journal->assessed < 0) {
            $scale = backup_getid($restore->backup_unique_code, "scale", abs($journal->assessed));
            if ($scale) {
                $journal->assessed = -$scale->new_id;
            }
        }
        //The structure is equal to the db, so insert the journal
        $newid = insert_record("journal", $journal);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "journal") . " \"" . format_string(stripslashes($journal->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'journal', $mod->id)) {
                //Restore journal_entries
                $status = journal_entries_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function game_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Game', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
        }
        //Now, build the GAME record structure
        $game = new stdClass();
        $game->course = $restore->course_id;
        $mod_info = $info['MOD'];
        $fields = array('name', 'sourcemodule', 'quizid', 'glossaryid', 'glossarycategoryid', 'questioncategoryid', 'bookid', 'gameinputid', 'gamekind', 'param1', 'param2', 'param3', 'param4', 'param5', 'param6', 'param7', 'param8', 'timemodified', 'bottomtext', 'grademethod', 'grade', 'decimalpoints', 'popup', 'review', 'attempts', 'glossaryid2', 'glossarycategoryid2', 'language');
        game_restore_record($mod_info, $game, $fields);
        game_recode($restore->backup_unique_code, $game, 'quizid', 'quiz');
        game_recode($restore->backup_unique_code, $game, 'glossaryid', 'glossary');
        game_recode($restore->backup_unique_code, $game, 'glossarycategoryid', 'glossary_categories');
        game_recode($restore->backup_unique_code, $game, 'glossaryid2', 'glossary');
        game_recode($restore->backup_unique_code, $game, 'glossarycategoryid2', 'glossary_categories');
        game_restore_stamp($info, $restore, $map_question_categories, 'QUESTION_CATEGORIES', 'QUESTION_CATEGORY');
        game_restore_stamp($info, $restore, $map_questions, 'QUESTIONS', 'QUESTION');
        game_recode_questioncategoryid($game, $map_question_categories);
        game_recode($restore->backup_unique_code, $game, 'bookid', 'book');
        //The structure is equal to the db, so insert the game
        $newid = insert_record("game", $game);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "game") . " \"" . format_string(stripslashes($game->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'game', $mod->id)) {
                $status = game_restore_grades($newid, $info, $restore);
                $status = game_restore_snakes_database($info, $restore);
                $status = game_restore_bookquiz_questions($newid, $info, $restore, $map_question_categories);
                //Restore game_attempts
                $status = game_attempts_restore_mods($newid, $info, $restore, $map_questions);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function blended_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        // if necessary, write to restorelog and adjust date/time fields
        if ($restore->course_startdateoffset) {
            restore_log_date_changes('Blended', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
        }
        //Now, build the BLENDED record structure
        $blended->course = $restore->course_id;
        $blended->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $blended->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
        $blended->idmethod = backup_todb($info['MOD']['#']['IDMETHOD']['0']['#']);
        $blended->idtype = backup_todb($info['MOD']['#']['IDTYPE']['0']['#']);
        $blended->codebartype = backup_todb($info['MOD']['#']['CODEBARTYPE']['0']['#']);
        $blended->lengthuserinfo = backup_todb($info['MOD']['#']['LENGTHUSERINFO']['0']['#']);
        $blended->teammethod = backup_todb($info['MOD']['#']['TEAMMETHOD']['0']['#']);
        $blended->numteams = backup_todb($info['MOD']['#']['NUMTEAMS']['0']['#']);
        $blended->nummembers = backup_todb($info['MOD']['#']['MUMMEMBERS']['0']['#']);
        $blended->assignment = backup_todb($info['MOD']['#']['ASSIGNMENT']['0']['#']);
        $blended->randomkey = backup_todb($info['MOD']['#']['RANDOMKEY']['0']['#']);
        //The structure is equal to the db, so insert the chat
        $newid = insert_record("blended", $blended);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "chat") . " \"" . format_string(stripslashes($blended->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'blended', $mod->id)) {
                //Restore chat_messages
                $status = blended_jobs_restore_mods($newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function voiceemail_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    $userdata = restore_userdata_selected($restore, "voiceemail", $mod->id);
    if ($data) {
        //Now get completed xmlized object
        $info = $data->info;
        //add logs
        //Now, build the voiceemail record structure
        if ($userdata === true) {
            $copyOptions = "0";
            //top message
        } else {
            $copyOptions = "2";
            //top message
        }
        $oldResource = get_record("voiceemail_resources", "id", $info['MOD']['#']['RID']['0']['#']);
        $resourceId = $oldResource->rid;
        $resource = voicetools_api_copy_resource($resourceId, null, $copyOptions);
        if ($resource === false) {
            return false;
            //error to copy the resource
        }
        $bdId = voiceemail_createResourceFromResource($resourceId, $resource->getRid(), $restore->course_id);
        $voiceemail->course = backup_todb($restore->course_id);
        $voiceemail->rid = backup_todb($bdId);
        $voiceemail->recipients_email = backup_todb(backup_todb($info['MOD']['#']['RECIPIENTS_EMAIL']['0']['#']));
        $voiceemail->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $voiceemail->section = backup_todb($info['MOD']['#']['SECTION']['0']['#']);
        $voiceemail->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $voiceemail->isfirst = 1;
        //The structure is equal to the db, so insert the voiceemail
        $newid = insert_record("voiceemail", $voiceemail);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "voiceemail") . " \"" . format_string(stripslashes($voiceemail->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);
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
/**
 * Fonction de sauvegarde de l'activité Podcast. 
 */
function podcast_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        // Now, build the podcast record structure
        $podcast->course = $restore->course_id;
        $podcast->userid = backup_todb($info['MOD']['USERID']['0']['#']);
        $podcast->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $podcast->author = backup_todb($info['MOD']['#']['AUTHOR']['0']['#']);
        $podcast->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $podcast->owner = backup_todb($info['MOD']['#']['OWNER']['0']['#']);
        $podcast->owner_email = backup_todb($info['MOD']['#']['OWNER_EMAIL']['0']['#']);
        $podcast->copyright = backup_todb($info['MOD']['#']['COPYRIGHT']['0']['#']);
        $podcast->lang = backup_todb($info['MOD']['#']['LANG']['0']['#']);
        $podcast->pubdate = backup_todb($info['MOD']['#']['PUBDATE']['0']['#']);
        $podcast->image_url = backup_todb($info['MOD']['#']['IMAGE_URL']['0']['#']);
        $podcast->image_img = backup_todb($info['MOD']['#']['IMAGE_IMG']['0']['#']);
        $podcast->category = backup_todb($info['MOD']['#']['CATEGORY']['0']['#']);
        $podcast->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the podcast
        $newid = insert_record("podcast", $podcast);
        //We have to recode the userid field
        $user = backup_getid($restore->backup_unique_code, "user", $item->userid);
        if ($user) {
            $podcast->userid = $user->new_id;
        }
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "podcast") . " \"" . format_string(stripslashes($podcast->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'podcast', $mod->id)) {
                //Restore podcast_items
                $status = podcast_items_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
function nanogong_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('NanoGong', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        // Now, build the NANOGONG record structure
        $nanogong->course = $restore->course_id;
        $nanogong->id = backup_todb($info['MOD']['#']['ID']['0']['#']);
        $nanogong->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $nanogong->message = backup_todb($info['MOD']['#']['MESSAGE']['0']['#']);
        $nanogong->color = backup_todb($info['MOD']['#']['COLOR']['0']['#']);
        $nanogong->maxduration = backup_todb($info['MOD']['#']['MAXDURATION']['0']['#']);
        $nanogong->maxmessages = backup_todb($info['MOD']['#']['MAXMESSAGES']['0']['#']);
        $nanogong->maxscore = backup_todb($info['MOD']['#']['MAXSCORE']['0']['#']);
        $nanogong->allowguestaccess = backup_todb($info['MOD']['#']['ALLOWGUESTACCESS']['0']['#']);
        $nanogong->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $nanogong->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the nanogong
        $newid = insert_record("nanogong", $nanogong);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "nanogong") . " \"" . format_string(stripslashes($nanogong->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'nanogong', $mod->id)) {
                //Restore nanogong messages
                $status = nanogong_messages_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #10
0
function mail_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                              //Debug
        //print_object ($GLOBALS['traverse_array']);                                           //Debug
        //$GLOBALS['traverse_array']="";                                                       //Debug
        $oldid = backup_todb($info['#']['ID']['0']['#']);
        //Now, build the mail record structure
        $mail->course = $restore->course_id;
        $mail->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $mail->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
        $mail->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
        $mail->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the mail
        $newid = insert_record("mail", $mail);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "mail") . " \"" . format_string(stripslashes($mail->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);
            //We have to restore the mail which are held in their logical order...
            $userdata = restore_userdata_selected($restore, "mail", $mod->id);
            $status = mail_folders_restore_mods($mod->id, $newid, $info, $restore, $userdata);
            //...and the user grades, high scores, and timer (if required)
            if ($status) {
                if ($userdata) {
                    if (!mail_groups_restore_mods($newid, $info, $restore)) {
                        return false;
                    }
                    if (!mail_statistics_restore_mods($restore->course_id, $info, $restore)) {
                        return false;
                    }
                }
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function checklist_restore_mods($mod, $restore)
{
    global $CFG, $db;
    $status = true;
    //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('checklist', $restore, $info['MOD']['#'], array('TIMECREATED', 'TIMEMODIFIED'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the CHECKLIST record structure
        $checklist = new stdClass();
        $checklist->course = $restore->course_id;
        $checklist->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $checklist->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $checklist->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
        $checklist->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $checklist->timecreated += $restore->course_startdateoffset;
        $checklist->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $checklist->timemodified += $restore->course_startdateoffset;
        $checklist->useritemsallowed = backup_todb($info['MOD']['#']['USERITEMSALLOWED']['0']['#']);
        $checklist->teacheredit = backup_todb($info['MOD']['#']['TEACHEREDIT']['0']['#']);
        $checklist->theme = backup_todb($info['MOD']['#']['THEME']['0']['#']);
        $checklist->duedatesoncalendar = backup_todb_chk_optional_field($info['MOD'], 'DUEDATESONCALENDAR', false);
        $checklist->teachercomments = backup_todb_chk_optional_field($info['MOD'], 'TEACHERCOMMENTS', false);
        $newid = insert_record('checklist', $checklist);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string('modulename', 'checklist') . " \"" . format_string(stripslashes($checklist->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);
            $checklist->id = $newid;
            $restore_user = restore_userdata_selected($restore, 'checklist', $mod->id);
            $status = checklist_items_restore($newid, $info, $restore, $restore_user);
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function realtimequiz_restore_mods($mod, $restore)
{
    global $CFG, $db;
    $status = true;
    //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('Realtimequiz', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the REALTIMEQUIZ record structure
        $quiz->course = $restore->course_id;
        $quiz->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']);
        $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $quiz->questiontime = backup_todb($info['MOD']['#']['QUESTIONTIME']['0']['#']);
        $newid = insert_record("realtimequiz", $quiz);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "realtimequiz") . " \"" . format_string(stripslashes($quiz->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);
            $quiz->id = $newid;
            $restore_user = false;
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'realtimequiz', $mod->id)) {
                $status = realtimequiz_sessions_restore($newid, $info, $restore);
                $restore_user = true;
            }
            // Restore the questions and answers (and any submissions)
            if ($status) {
                $status = realtimequiz_questions_restore($newid, $info, $restore, $restore_user);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #13
0
/**
 * API function called by the Moodle restore system
 */
function webinar_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    if ($data) {
        $info = $data->info;
        $webinar->course = $restore->course_id;
        $webinar->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $webinar->thirdparty = backup_todb($info['MOD']['#']['THIRDPARTY']['0']['#']);
        $webinar->display = backup_todb($info['MOD']['#']['DISPLAY']['0']['#']);
        $webinar->confirmationsubject = backup_todb($info['MOD']['#']['CONFIRMATIONSUBJECT']['0']['#']);
        $webinar->confirmationinstrmngr = backup_todb($info['MOD']['#']['CONFIRMATIONINSTRMNGR']['0']['#']);
        $webinar->confirmationmessage = backup_todb($info['MOD']['#']['CONFIRMATIONMESSAGE']['0']['#']);
        $webinar->waitlistedsubject = backup_todb($info['MOD']['#']['WAITLISTEDSUBJECT']['0']['#']);
        $webinar->waitlistedmessage = backup_todb($info['MOD']['#']['WAITLISTEDMESSAGE']['0']['#']);
        $webinar->cancellationsubject = backup_todb($info['MOD']['#']['CANCELLATIONSUBJECT']['0']['#']);
        $webinar->cancellationinstrmngr = backup_todb($info['MOD']['#']['CANCELLATIONINSTRMNGR']['0']['#']);
        $webinar->cancellationmessage = backup_todb($info['MOD']['#']['CANCELLATIONMESSAGE']['0']['#']);
        $webinar->remindersubject = backup_todb($info['MOD']['#']['REMINDERSUBJECT']['0']['#']);
        $webinar->reminderinstrmngr = backup_todb($info['MOD']['#']['REMINDERINSTRMNGR']['0']['#']);
        $webinar->remindermessage = backup_todb($info['MOD']['#']['REMINDERMESSAGE']['0']['#']);
        $webinar->reminderperiod = backup_todb($info['MOD']['#']['REMINDERPERIOD']['0']['#']);
        $webinar->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $webinar->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $newid = insert_record('webinar', $webinar);
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('modulename', 'webinar') . ' "' . format_string(stripslashes($webinar->name), true) . '"</li>';
        }
        backup_flush(300);
        if ($newid) {
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
            // Table: webinar_sessions
            $status &= restore_webinar_sessions($newid, $info, $restore);
            if (restore_userdata_selected($restore, 'webinar', $mod->id)) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo '<br />';
                }
                // Table: webinar_signups_
                $status &= restore_webinar_signups($newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #14
0
function textanalysis_restore_mods($mod, $restore)
{
    global $CFG, $oldidarray;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        // if necessary, write to restorelog and adjust date/time fields
        if ($restore->course_startdateoffset) {
            restore_log_date_changes('textanalysis', $restore, $info['MOD']['#'], array('TEXTANALYSISTIME'));
        }
        //Now, build the textanalysis record structure
        $textanalysis->course = $restore->course_id;
        $textanalysis->teacher = backup_todb($info['MOD']['#']['TEACHER']['0']['#']);
        $textanalysis->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $textanalysis->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $textanalysis->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']);
        $textanalysis->time = backup_todb($info['MOD']['#']['TIME']['0']['#']);
        $user = backup_getid($restore->backup_unique_code, "user", $textanalysis->teacher);
        if ($user) {
            $textanalysis->teacher = $user->new_id;
        }
        //The structure is equal to the db, so insert the textanalysis
        $newid = insert_record("textanalysis", $textanalysis);
        //Do some output
        //if (!defined('RESTORE_SILENTLY')) {
        //    echo "<li>".get_string("modulename","textanalysis")." \"".format_string(stripslashes($textanalysis->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'textanalysis', $mod->id)) {
                //Restore textanalysis_messages
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function chat_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        // if necessary, write to restorelog and adjust date/time fields
        if ($restore->course_startdateoffset) {
            restore_log_date_changes('Chat', $restore, $info['MOD']['#'], array('CHATTIME'));
        }
        //Now, build the CHAT record structure
        $chat->course = $restore->course_id;
        $chat->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $chat->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $chat->keepdays = backup_todb($info['MOD']['#']['KEEPDAYS']['0']['#']);
        $chat->studentlogs = backup_todb($info['MOD']['#']['STUDENTLOGS']['0']['#']);
        $chat->schedule = backup_todb($info['MOD']['#']['SCHEDULE']['0']['#']);
        $chat->chattime = backup_todb($info['MOD']['#']['CHATTIME']['0']['#']);
        $chat->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the chat
        $newid = insert_record("chat", $chat);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "chat") . " \"" . format_string(stripslashes($chat->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'chat', $mod->id)) {
                //Restore chat_messages
                $status = chat_messages_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function survey_restore_mods($mod, $restore)
{
    global $CFG, $db;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the SURVEY record structure
        $survey->course = $restore->course_id;
        $survey->template = backup_todb($info['MOD']['#']['TEMPLATE']['0']['#']);
        $survey->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
        $survey->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $survey->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $survey->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $survey->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $survey->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
        //The structure is equal to the db, so insert the survey
        $newid = insert_record("survey", $survey);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "survey") . " \"" . format_string(stripslashes($survey->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'survey', $mod->id)) {
                //Restore survey_answers
                $status = survey_answers_restore_mods($newid, $info, $restore);
                //Restore survey_analysis
                if ($status) {
                    $status = survey_analysis_restore_mods($newid, $info, $restore);
                }
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
/**
 * Restore everything from the given backup.
 */
function sloodle_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    if ($data) {
        // Get the XML backup data as an array
        $info = $data->info;
        // Build our SLOODLE DB record
        $sloodle = new object();
        $sloodle->course = $restore->course_id;
        $sloodle->type = backup_todb($info['MOD']['#']['SUBTYPE']['0']['#']);
        $sloodle->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $sloodle->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $sloodle->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $sloodle->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $newid = insert_record("sloodle", $sloodle);
        // Inform the user what we are restoring
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "sloodle") . " \"" . format_string(stripslashes($sloodle->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);
            // Should we restore userdata?
            $includeuserdata = restore_userdata_selected($restore, 'sloodle', $mod->id);
            // Attempt to get a SloodleModule object for this module sub-type
            $dummysession = new SloodleSession(false);
            // We need to provide this to keep the module happy!
            $moduleobj = sloodle_load_module($sloodle->type, $dummysession);
            if ($moduleobj != false) {
                // Attempt to restore this module's secondary data
                if (!$moduleobj->restore($newid, $info['MOD']['#']['SECONDARYDATA']['0']['#'], $includeuserdata)) {
                    $status = false;
                }
            } else {
                echo "<li>Failed to fully restore SLOODLE module type {$sloodle->type}. This type may not be available on your installation.</li>";
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #18
0
function openmeetings_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        // if necessary, write to restorelog and adjust date/time fields
        if ($restore->course_startdateoffset) {
            // restore_log_date_changes('openmeetings', $restore, $info['MOD']['#'], array('openmeetingsTIME'));
        }
        //Now, build the openmeetings record structure
        $openmeetings->course = $restore->course_id;
        $openmeetings->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $openmeetings->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $openmeetings->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
        $openmeetings->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $openmeetings->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $openmeetings->teacher = backup_todb($info['MOD']['#']['TEACHER']['0']['#']);
        $newid = openmeetings_add_instance($openmeetings);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "openmeetings") . " \"" . format_string(stripslashes($openmeetings->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'openmeetings', $mod->id)) {
                //Restore nothing
                $status = false;
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function map_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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;
        //Debug
        //Now, build the MAP record structure
        $map->course = $restore->course_id;
        valuesBackup2Object($map, $info['MOD'], array('name', 'text', 'format', 'studentlocations', 'requireok', 'extralocations', 'showaddress4extra', 'timemodified'));
        //The structure is equal to the db, so insert the map
        $newid = insert_record("map", $map);
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
            //now restore the locations for this map.
            if (restore_userdata_selected($restore, 'map', $mod->id)) {
                //Restore map_locations
                $status = map_locations_restore_mods($newid, $info, $restore);
            }
        } else {
            $status = false;
        }
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "map") . " \"" . format_string(stripslashes($map->name), true) . "\"</li>";
        }
        backup_flush(300);
    } else {
        $status = false;
    }
    return $status;
}
function webquestscorm_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Webquestscorm', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the webquestscorm record structure
        $webquestscorm->course = $restore->course_id;
        $webquestscorm->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $webquestscorm->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $webquestscorm->timeavailable = backup_todb($info['MOD']['#']['TIMEAVAILABLE']['0']['#']);
        $webquestscorm->timedue = backup_todb($info['MOD']['#']['TIMEDUE']['0']['#']);
        $webquestscorm->dueenable = backup_todb($info['MOD']['#']['DUEENABLE']['0']['#']);
        $webquestscorm->dueyear = backup_todb($info['MOD']['#']['DUEYEAR']['0']['#']);
        $webquestscorm->duemonth = backup_todb($info['MOD']['#']['DUEMONTH']['0']['#']);
        $webquestscorm->dueday = backup_todb($info['MOD']['#']['DUEDAY']['0']['#']);
        $webquestscorm->duehour = backup_todb($info['MOD']['#']['DUEHOUR']['0']['#']);
        $webquestscorm->dueminute = backup_todb($info['MOD']['#']['DUEMINUTE']['0']['#']);
        $webquestscorm->availableenable = backup_todb($info['MOD']['#']['AVAILABLEENABLE']['0']['#']);
        $webquestscorm->availableyear = backup_todb($info['MOD']['#']['AVAILABLEYEAR']['0']['#']);
        $webquestscorm->availablemonth = backup_todb($info['MOD']['#']['AVAILABLEMONTH']['0']['#']);
        $webquestscorm->availableday = backup_todb($info['MOD']['#']['AVAILABLEDAY']['0']['#']);
        $webquestscorm->availablehour = backup_todb($info['MOD']['#']['AVAILABLEHOUR']['0']['#']);
        $webquestscorm->availableminute = isset($info['MOD']['#']['AVAILABLEMINUTE']['0']['#']) ? backup_todb($info['MOD']['#']['TYPE']['0']['#']) : '';
        $webquestscorm->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
        $webquestscorm->preventlate = backup_todb($info['MOD']['#']['PREVENTLATE']['0']['#']);
        $webquestscorm->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
        $webquestscorm->emailteachers = backup_todb($info['MOD']['#']['EMAILTEACHERS']['0']['#']);
        $webquestscorm->template = backup_todb($info['MOD']['#']['TEMPLATE']['0']['#']);
        $webquestscorm->introduction = backup_todb($info['MOD']['#']['INTRODUCTION']['0']['#']);
        $webquestscorm->task = backup_todb($info['MOD']['#']['TASK']['0']['#']);
        $webquestscorm->process = backup_todb($info['MOD']['#']['PROCESS']['0']['#']);
        $webquestscorm->evaluation = backup_todb($info['MOD']['#']['EVALUATION']['0']['#']);
        $webquestscorm->conclusion = backup_todb($info['MOD']['#']['CONCLUSION']['0']['#']);
        $webquestscorm->credits = backup_todb($info['MOD']['#']['CREDITS']['0']['#']);
        $webquestscorm->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //We have to recode the grade field if it is <0 (scale)
        if ($webquestscorm->grade < 0) {
            $scale = backup_getid($restore->backup_unique_code, "scale", abs($webquestscorm->grade));
            if ($scale) {
                $webquestscorm->grade = -$scale->new_id;
            }
        }
        //The structure is equal to the db, so insert the webquestscorm
        $newid = insert_record("webquestscorm", $webquestscorm);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "webquestscorm") . " \"" . format_string(stripslashes($webquestscorm->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);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'webquestscorm', $mod->id)) {
                //Restore assignmet_submissions
                $status = webquestscorm_submissions_restore_mods($mod->id, $newid, $info, $restore) && $status;
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
Exemple #21
0
function hotpot_restore_mods($mod, $restore)
{
    //This function restores a single hotpot activity
    // This function is called by "restore_create_modules" (in "backup/restorelib.php")
    // which is called by "backup/restore_execute.html" (included by "backup/restore.php")
    // $mod is an object
    //     id           : id field in 'modtype' table
    //     modtype      : 'hotpot'
    // $restore is an object
    //     backup_unique_code : xxxxxxxxxx
    //     file         : '/full/path/to/backupfile.zip'
    //     mods         : an array of $modinfo's (see below)
    //     restoreto    : 0=existing course (replace), 1=existing course (append), 2=new course
    //     users        : 0=all, 1=course, 2=none
    //     logs         : 0=no, 1=yes
    //     user_files   : 0=no, 1=yes
    //     course_files : 0=no, 1=yes
    //     course_id    : id of course into which data is to be restored
    //     deleting     : true if 'restoreto'==0, otherwise false
    //     original_wwwroot : 'http://your.server.com/moodle'
    // $modinfo is an array
    //    'modname'    : array( 'restore'=> 0=no 1=yes, 'userinfo' => 0=no 1=yes)
    global $CFG;
    $status = true;
    // get course module data this hotpot activity
    $data = backup_getid($restore->backup_unique_code, 'hotpot', $mod->id);
    if ($data) {
        // $data is an object
        //    backup_code => xxxxxxxxxx,
        //    table_name  => 'hotpot',
        //    old_id      => xxx,
        //    new_id      => NULL,
        //    info        => xml tree array of info backed up for this hotpot activity
        $xml =& $data->info['MOD']['#'];
        $table = 'hotpot';
        $foreign_keys = array('course' => $restore->course_id);
        $more_restore = '';
        // print a message after each hotpot is backed up
        if (!defined('RESTORE_SILENTLY')) {
            $more_restore .= 'print "<li>".get_string("modulename", "hotpot")." &quot;".format_string(stripslashes($record->name),true)."&quot;</li>";';
        }
        $more_restore .= 'backup_flush(300);';
        if (function_exists('restore_userdata_selected')) {
            // Moodle >= 1.6
            $restore_userdata_selected = restore_userdata_selected($restore, 'hotpot', $mod->id);
        } else {
            // Moodle <= 1.5
            $restore_userdata_selected = $restore->mods['hotpot']->userinfo;
        }
        if ($restore_userdata_selected) {
            $has_details = false;
            if (isset($xml["ATTEMPT_DATA"]["0"]["#"]["ATTEMPT"]["0"]["#"]["DETAILS"]["0"]["#"])) {
                $details = trim($xml["ATTEMPT_DATA"]["0"]["#"]["ATTEMPT"]["0"]["#"]["DETAILS"]["0"]["#"]);
                if ($details != '' && $details != '<?xml version="1.0"?><hpjsresult><fields></fields></hpjsresult>') {
                    $has_details = true;
                }
            }
            if ($has_details && empty($xml["STRING_DATA"]) && empty($xml["QUESTION_DATA"])) {
                // HotPot v2.0.x (regenerate questions, responses and strings from attempt details)
                $more_restore .= '$status = hotpot_restore_attempts($restore, $status, $xml, $record, true);';
            } else {
                // HotPot v2.1+
                $more_restore .= '$status = hotpot_restore_strings($restore, $status, $xml, $record);';
                $more_restore .= '$status = hotpot_restore_questions($restore, $status, $xml, $record);';
                $more_restore .= '$status = hotpot_restore_attempts($restore, $status, $xml, $record);';
            }
        }
        // if necessary, adjust HotPot date/time fields and write to restorelog
        if ($restore->course_startdateoffset) {
            restore_log_date_changes('Hotpot', $restore, $xml, array('TIMEOPEN', 'TIMECLOSE', 'TIMECREATED', 'TIMEMODIFIED'));
        }
        $status = hotpot_restore_records($restore, $status, $xml, $table, $foreign_keys, $more_restore);
    }
    return $status;
}
Exemple #22
0
/**
 * This function creates all the gradebook data from xml
 */
function restore_create_gradebook($restore, $xml_file)
{
    global $CFG;
    $status = true;
    //Check it exists
    if (!file_exists($xml_file)) {
        return false;
    }
    // Get info from xml
    // info will contain the number of record to process
    $info = restore_read_xml_gradebook($restore, $xml_file);
    // If we have info, then process
    if (empty($info)) {
        return $status;
    }
    if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
        $restore_histories = true;
    } else {
        $restore_histories = false;
    }
    // make sure top course category exists
    $course_category = grade_category::fetch_course_category($restore->course_id);
    $course_category->load_grade_item();
    // we need to know if all grade items that were backed up are being restored
    // if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
    // i.e. the aggregated grades of that category
    $restoreall = true;
    // set to false if any grade_item is not selected/restored or already exist
    $importing = !empty($SESSION->restore->importing);
    if ($importing) {
        $restoreall = false;
    } else {
        $prev_grade_items = grade_item::fetch_all(array('courseid' => $restore->course_id));
        $prev_grade_cats = grade_category::fetch_all(array('courseid' => $restore->course_id));
        // if any categories already present, skip restore of categories from backup - course item or category already exist
        if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
            $restoreall = false;
        }
        unset($prev_grade_items);
        unset($prev_grade_cats);
        if ($restoreall) {
            if ($recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = {$restore->backup_unique_code}", "", "old_id")) {
                foreach ($recs as $rec) {
                    if ($data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id)) {
                        $info = $data->info;
                        // do not restore if this grade_item is a mod, and
                        $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
                        if ($itemtype == 'mod') {
                            $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
                            $itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
                            if (empty($restore->mods[$itemmodule]->granular)) {
                                continue;
                            } else {
                                if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
                                    continue;
                                }
                            }
                            // at least one activity should not be restored - do not restore categories and manual items at all
                            $restoreall = false;
                            break;
                        }
                    }
                }
            }
        }
    }
    // Start ul
    if (!defined('RESTORE_SILENTLY')) {
        echo '<ul>';
    }
    // array of restored categories - speedup ;-)
    $cached_categories = array();
    $outcomes = array();
    /// Process letters
    $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
    // respect current grade letters if defined
    if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeletters', 'grades') . '</li>';
        }
        // Fetch recordset_size records in each iteration
        $recs = get_records_select("backup_ids", "table_name = 'grade_letters' AND backup_code = {$restore->backup_unique_code}", "", "old_id");
        if ($recs) {
            foreach ($recs as $rec) {
                // Get the full record from backup_ids
                $data = backup_getid($restore->backup_unique_code, 'grade_letters', $rec->old_id);
                if ($data) {
                    $info = $data->info;
                    $dbrec = new object();
                    $dbrec->contextid = $context->id;
                    $dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
                    $dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
                    insert_record('grade_letters', $dbrec);
                }
            }
        }
    }
    /// Preprocess outcomes - do not store them yet!
    if ($status and !$importing and $restoreall) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeoutcomes', 'grades') . '</li>';
        }
        $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes' AND backup_code = '{$restore->backup_unique_code}'", "", "old_id");
        if ($recs) {
            foreach ($recs as $rec) {
                //Get the full record from backup_ids
                $data = backup_getid($restore->backup_unique_code, 'grade_outcomes', $rec->old_id);
                if ($data) {
                    $info = $data->info;
                    //first find out if outcome already exists
                    $shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#']);
                    if ($candidates = get_records_sql("SELECT *\n                                                             FROM {$CFG->prefix}grade_outcomes\n                                                            WHERE (courseid IS NULL OR courseid = {$restore->course_id})\n                                                                  AND shortname = '{$shortname}'\n                                                         ORDER BY courseid ASC, id ASC")) {
                        $grade_outcome = reset($candidates);
                        $outcomes[$rec->old_id] = $grade_outcome;
                        continue;
                    }
                    $dbrec = new object();
                    if (has_capability('moodle/grade:manageoutcomes', get_context_instance(CONTEXT_SYSTEM))) {
                        $oldoutcome = backup_todb($info['GRADE_OUTCOME']['#']['COURSEID']['0']['#']);
                        if (empty($oldoutcome)) {
                            //site wide
                            $dbrec->courseid = null;
                        } else {
                            //course only
                            $dbrec->courseid = $restore->course_id;
                        }
                    } else {
                        // no permission to add site outcomes
                        $dbrec->courseid = $restore->course_id;
                    }
                    //Get the fields
                    $dbrec->shortname = backup_todb($info['GRADE_OUTCOME']['#']['SHORTNAME']['0']['#'], false);
                    $dbrec->fullname = backup_todb($info['GRADE_OUTCOME']['#']['FULLNAME']['0']['#'], false);
                    $dbrec->scaleid = backup_todb($info['GRADE_OUTCOME']['#']['SCALEID']['0']['#'], false);
                    $dbrec->description = backup_todb($info['GRADE_OUTCOME']['#']['DESCRIPTION']['0']['#'], false);
                    $dbrec->timecreated = backup_todb($info['GRADE_OUTCOME']['#']['TIMECREATED']['0']['#'], false);
                    $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME']['#']['TIMEMODIFIED']['0']['#'], false);
                    $dbrec->usermodified = backup_todb($info['GRADE_OUTCOME']['#']['USERMODIFIED']['0']['#'], false);
                    //Need to recode the scaleid
                    if ($scale = backup_getid($restore->backup_unique_code, 'scale', $dbrec->scaleid)) {
                        $dbrec->scaleid = $scale->new_id;
                    }
                    //Need to recode the usermodified
                    if ($modifier = backup_getid($restore->backup_unique_code, 'user', $dbrec->usermodified)) {
                        $dbrec->usermodified = $modifier->new_id;
                    }
                    $grade_outcome = new grade_outcome($dbrec, false);
                    $outcomes[$rec->old_id] = $grade_outcome;
                }
            }
        }
    }
    /// Process grade items and grades
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('gradeitems', 'grades') . '</li>';
        }
        $counter = 0;
        //Fetch recordset_size records in each iteration
        $recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = '{$restore->backup_unique_code}'", "id", "old_id");
        if ($recs) {
            foreach ($recs as $rec) {
                //Get the full record from backup_ids
                $data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id);
                if ($data) {
                    $info = $data->info;
                    // first find out if category or normal item
                    $itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
                    if ($itemtype == 'course' or $itemtype == 'category') {
                        if (!$restoreall or $importing) {
                            continue;
                        }
                        $oldcat = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
                        if (!($cdata = backup_getid($restore->backup_unique_code, 'grade_categories', $oldcat))) {
                            continue;
                        }
                        $cinfo = $cdata->info;
                        unset($cdata);
                        if ($itemtype == 'course') {
                            $course_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
                            $course_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
                            $course_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
                            $course_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
                            $course_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
                            $course_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
                            $course_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
                            $course_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
                            $course_category->update('restore');
                            $status = backup_putid($restore->backup_unique_code, 'grade_categories', $oldcat, $course_category->id) && $status;
                            $cached_categories[$oldcat] = $course_category;
                            $grade_item = $course_category->get_grade_item();
                        } else {
                            $oldparent = backup_todb($cinfo['GRADE_CATEGORY']['#']['PARENT']['0']['#'], false);
                            if (empty($cached_categories[$oldparent])) {
                                debugging('parent not found ' . $oldparent);
                                continue;
                                // parent not found, sorry
                            }
                            $grade_category = new grade_category();
                            $grade_category->courseid = $restore->course_id;
                            $grade_category->parent = $cached_categories[$oldparent]->id;
                            $grade_category->fullname = backup_todb($cinfo['GRADE_CATEGORY']['#']['FULLNAME']['0']['#'], false);
                            $grade_category->aggregation = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATION']['0']['#'], false);
                            $grade_category->keephigh = backup_todb($cinfo['GRADE_CATEGORY']['#']['KEEPHIGH']['0']['#'], false);
                            $grade_category->droplow = backup_todb($cinfo['GRADE_CATEGORY']['#']['DROPLOW']['0']['#'], false);
                            $grade_category->aggregateonlygraded = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEONLYGRADED']['0']['#'], false);
                            $grade_category->aggregateoutcomes = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATEOUTCOMES']['0']['#'], false);
                            $grade_category->aggregatesubcats = backup_todb($cinfo['GRADE_CATEGORY']['#']['AGGREGATESUBCATS']['0']['#'], false);
                            $grade_category->timecreated = backup_todb($cinfo['GRADE_CATEGORY']['#']['TIMECREATED']['0']['#'], false);
                            $grade_category->insert('restore');
                            $status = backup_putid($restore->backup_unique_code, 'grade_categories', $oldcat, $grade_category->id) && $status;
                            $cached_categories[$oldcat] = $grade_category;
                            $grade_item = $grade_category->get_grade_item();
                            // creates grade_item too
                        }
                        unset($cinfo);
                        $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
                        if (grade_verify_idnumber($idnumber, $restore->course_id)) {
                            $grade_item->idnumber = $idnumber;
                        }
                        $grade_item->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
                        $grade_item->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
                        $grade_item->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
                        $grade_item->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
                        $grade_item->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
                        $grade_item->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
                        $grade_item->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
                        $grade_item->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
                        $grade_item->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
                        $grade_item->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
                        $grade_item->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
                        $grade_item->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
                        $grade_item->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
                        $grade_item->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
                        $grade_item->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
                        $grade_item->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
                        if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
                            $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
                            $grade_item->scaleid = $scale->new_id;
                        }
                        if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false)) {
                            $outcome = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'], false));
                            $grade_item->outcomeid = $outcome->new_id;
                        }
                        $grade_item->update('restore');
                        $status = backup_putid($restore->backup_unique_code, "grade_items", $rec->old_id, $grade_item->id) && $status;
                    } else {
                        if ($itemtype != 'mod' and (!$restoreall or $importing)) {
                            // not extra gradebook stuff if restoring individual activities or something already there
                            continue;
                        }
                        $dbrec = new object();
                        $dbrec->courseid = $restore->course_id;
                        $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
                        $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
                        if ($itemtype == 'mod') {
                            // iteminstance should point to new mod
                            $olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#'], false);
                            $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $olditeminstance);
                            $dbrec->iteminstance = $mod->new_id;
                            if (!($cm = get_coursemodule_from_instance($dbrec->itemmodule, $mod->new_id))) {
                                // item not restored - no item
                                continue;
                            }
                            // keep in sync with activity idnumber
                            $dbrec->idnumber = $cm->idnumber;
                        } else {
                            $idnumber = backup_todb($info['GRADE_ITEM']['#']['IDNUMBER']['0']['#'], false);
                            if (grade_verify_idnumber($idnumber, $restore->course_id)) {
                                //make sure the new idnumber is unique
                                $dbrec->idnumber = $idnumber;
                            }
                        }
                        $dbrec->itemname = backup_todb($info['GRADE_ITEM']['#']['ITEMNAME']['0']['#'], false);
                        $dbrec->itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#'], false);
                        $dbrec->itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#'], false);
                        $dbrec->itemnumber = backup_todb($info['GRADE_ITEM']['#']['ITEMNUMBER']['0']['#'], false);
                        $dbrec->iteminfo = backup_todb($info['GRADE_ITEM']['#']['ITEMINFO']['0']['#'], false);
                        $dbrec->gradetype = backup_todb($info['GRADE_ITEM']['#']['GRADETYPE']['0']['#'], false);
                        $dbrec->calculation = backup_todb($info['GRADE_ITEM']['#']['CALCULATION']['0']['#'], false);
                        $dbrec->grademax = backup_todb($info['GRADE_ITEM']['#']['GRADEMAX']['0']['#'], false);
                        $dbrec->grademin = backup_todb($info['GRADE_ITEM']['#']['GRADEMIN']['0']['#'], false);
                        $dbrec->gradepass = backup_todb($info['GRADE_ITEM']['#']['GRADEPASS']['0']['#'], false);
                        $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#'], false);
                        $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#'], false);
                        $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM']['#']['AGGREGATIONCOEF']['0']['#'], false);
                        $dbrec->display = backup_todb($info['GRADE_ITEM']['#']['DISPLAY']['0']['#'], false);
                        $dbrec->decimals = backup_todb($info['GRADE_ITEM']['#']['DECIMALS']['0']['#'], false);
                        $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#'], false);
                        $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#'], false);
                        $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#'], false);
                        $dbrec->timecreated = backup_todb($info['GRADE_ITEM']['#']['TIMECREATED']['0']['#'], false);
                        if (backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false)) {
                            $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM']['#']['SCALEID']['0']['#'], false));
                            $dbrec->scaleid = $scale->new_id;
                        }
                        if (backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#'])) {
                            $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
                            if (empty($outcomes[$oldoutcome])) {
                                continue;
                                // error!
                            }
                            if (empty($outcomes[$oldoutcome]->id)) {
                                $outcomes[$oldoutcome]->insert('restore');
                                $outcomes[$oldoutcome]->use_in($restore->course_id);
                                backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $outcomes[$oldoutcome]->id);
                            }
                            $dbrec->outcomeid = $outcomes[$oldoutcome]->id;
                        }
                        $grade_item = new grade_item($dbrec, false);
                        $grade_item->insert('restore');
                        if ($restoreall) {
                            // set original parent if restored
                            $oldcat = $info['GRADE_ITEM']['#']['CATEGORYID']['0']['#'];
                            if (!empty($cached_categories[$oldcat])) {
                                $grade_item->set_parent($cached_categories[$oldcat]->id);
                            }
                        }
                        $status = backup_putid($restore->backup_unique_code, "grade_items", $rec->old_id, $grade_item->id) && $status;
                    }
                    // no need to restore grades if user data is not selected or importing activities
                    if ($importing or $grade_item->itemtype == 'mod' and !restore_userdata_selected($restore, $grade_item->itemmodule, $olditeminstance)) {
                        // module instance not selected when restored using granular
                        // skip this item
                        continue;
                    }
                    /// now, restore grade_grades
                    if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
                        //Iterate over items
                        foreach ($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'] as $g_info) {
                            $grade = new grade_grade();
                            $grade->itemid = $grade_item->id;
                            $olduser = backup_todb($g_info['#']['USERID']['0']['#'], false);
                            $user = backup_getid($restore->backup_unique_code, "user", $olduser);
                            $grade->userid = $user->new_id;
                            $grade->rawgrade = backup_todb($g_info['#']['RAWGRADE']['0']['#'], false);
                            $grade->rawgrademax = backup_todb($g_info['#']['RAWGRADEMAX']['0']['#'], false);
                            $grade->rawgrademin = backup_todb($g_info['#']['RAWGRADEMIN']['0']['#'], false);
                            // need to find scaleid
                            if (backup_todb($g_info['#']['RAWSCALEID']['0']['#'])) {
                                $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($g_info['#']['RAWSCALEID']['0']['#'], false));
                                $grade->rawscaleid = $scale->new_id;
                            }
                            if (backup_todb($g_info['#']['USERMODIFIED']['0']['#'])) {
                                if ($modifier = backup_getid($restore->backup_unique_code, "user", backup_todb($g_info['#']['USERMODIFIED']['0']['#'], false))) {
                                    $grade->usermodified = $modifier->new_id;
                                }
                            }
                            $grade->finalgrade = backup_todb($g_info['#']['FINALGRADE']['0']['#'], false);
                            $grade->hidden = backup_todb($g_info['#']['HIDDEN']['0']['#'], false);
                            $grade->locked = backup_todb($g_info['#']['LOCKED']['0']['#'], false);
                            $grade->locktime = backup_todb($g_info['#']['LOCKTIME']['0']['#'], false);
                            $grade->exported = backup_todb($g_info['#']['EXPORTED']['0']['#'], false);
                            $grade->overridden = backup_todb($g_info['#']['OVERRIDDEN']['0']['#'], false);
                            $grade->excluded = backup_todb($g_info['#']['EXCLUDED']['0']['#'], false);
                            $grade->feedback = backup_todb($g_info['#']['FEEDBACK']['0']['#'], false);
                            $grade->feedbackformat = backup_todb($g_info['#']['FEEDBACKFORMAT']['0']['#'], false);
                            $grade->information = backup_todb($g_info['#']['INFORMATION']['0']['#'], false);
                            $grade->informationformat = backup_todb($g_info['#']['INFORMATIONFORMAT']['0']['#'], false);
                            $grade->timecreated = backup_todb($g_info['#']['TIMECREATED']['0']['#'], false);
                            $grade->timemodified = backup_todb($g_info['#']['TIMEMODIFIED']['0']['#'], false);
                            $grade->insert('restore');
                            backup_putid($restore->backup_unique_code, "grade_grades", backup_todb($g_info['#']['ID']['0']['#']), $grade->id);
                            $counter++;
                            if ($counter % 20 == 0) {
                                if (!defined('RESTORE_SILENTLY')) {
                                    echo ".";
                                    if ($counter % 400 == 0) {
                                        echo "<br />";
                                    }
                                }
                                backup_flush(300);
                            }
                        }
                    }
                }
            }
        }
    }
    /// add outcomes that are not used when doing full restore
    if ($status and $restoreall) {
        foreach ($outcomes as $oldoutcome => $grade_outcome) {
            if (empty($grade_outcome->id)) {
                $grade_outcome->insert('restore');
                $grade_outcome->use_in($restore->course_id);
                backup_putid($restore->backup_unique_code, "grade_outcomes", $oldoutcome, $grade_outcome->id);
            }
        }
    }
    if ($status and !$importing and $restore_histories) {
        /// following code is very inefficient
        $gchcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
        $gghcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
        $gihcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_items_history');
        $gohcount = count_records('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_outcomes_history');
        // Number of records to get in every chunk
        $recordset_size = 2;
        // process histories
        if ($gchcount && $status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('gradecategoryhistory', 'grades') . '</li>';
            }
            $counter = 0;
            while ($counter < $gchcount) {
                //Fetch recordset_size records in each iteration
                $recs = get_records_select("backup_ids", "table_name = 'grade_categories_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
                if ($recs) {
                    foreach ($recs as $rec) {
                        //Get the full record from backup_ids
                        $data = backup_getid($restore->backup_unique_code, 'grade_categories_history', $rec->old_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
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
                            if (empty($oldobj->new_id)) {
                                // if the old object is not being restored, can't restoring its history
                                $counter++;
                                continue;
                            }
                            $dbrec->oldid = $oldobj->new_id;
                            $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
                            $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
                            $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                            // loggeduser might not be restored, e.g. admin
                            if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                $dbrec->loggeduser = $oldobj->new_id;
                            }
                            // this item might not have a parent at all, do not skip it if no parent is specified
                            if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
                                $oldobj = backup_getid($restore->backup_unique_code, "grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
                                if (empty($oldobj->new_id)) {
                                    // if the parent category not restored
                                    $counter++;
                                    continue;
                                }
                            }
                            $dbrec->parent = $oldobj->new_id;
                            $dbrec->depth = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DEPTH']['0']['#']);
                            // path needs to be rebuilt
                            if ($path = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PATH']['0']['#'])) {
                                // to preserve the path and make it work, we need to replace the categories one by one
                                // we first get the list of categories in current path
                                if ($paths = explode("/", $path)) {
                                    $newpath = '';
                                    foreach ($paths as $catid) {
                                        if ($catid) {
                                            // find the new corresponding path
                                            $oldpath = backup_getid($restore->backup_unique_code, "grade_categories", $catid);
                                            $newpath .= "/{$oldpath->new_id}";
                                        }
                                    }
                                    $dbrec->path = $newpath;
                                }
                            }
                            $dbrec->fullname = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['FULLNAME']['0']['#']);
                            $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
                            $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
                            $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
                            $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
                            $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
                            $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
                            $dbrec->courseid = $restore->course_id;
                            insert_record('grade_categories_history', $dbrec);
                            unset($dbrec);
                        }
                        //Increment counters
                        $counter++;
                        //Do some output
                        if ($counter % 1 == 0) {
                            if (!defined('RESTORE_SILENTLY')) {
                                echo ".";
                                if ($counter % 20 == 0) {
                                    echo "<br />";
                                }
                            }
                            backup_flush(300);
                        }
                    }
                }
            }
        }
        // process histories
        if ($gghcount && $status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('gradegradeshistory', 'grades') . '</li>';
            }
            $counter = 0;
            while ($counter < $gghcount) {
                //Fetch recordset_size records in each iteration
                $recs = get_records_select("backup_ids", "table_name = 'grade_grades_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
                if ($recs) {
                    foreach ($recs as $rec) {
                        //Get the full record from backup_ids
                        $data = backup_getid($restore->backup_unique_code, 'grade_grades_history', $rec->old_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
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
                            if (empty($oldobj->new_id)) {
                                // if the old object is not being restored, can't restoring its history
                                $counter++;
                                continue;
                            }
                            $dbrec->oldid = $oldobj->new_id;
                            $dbrec->action = backup_todb($info['GRADE_GRADES_HISTORY']['#']['ACTION']['0']['#']);
                            $dbrec->source = backup_todb($info['GRADE_GRADES_HISTORY']['#']['SOURCE']['0']['#']);
                            $dbrec->timemodified = backup_todb($info['GRADE_GRADES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                            if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                $dbrec->loggeduser = $oldobj->new_id;
                            }
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
                            $dbrec->itemid = $oldobj->new_id;
                            if (empty($dbrec->itemid)) {
                                $counter++;
                                continue;
                                // grade item not being restored
                            }
                            $oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERID']['0']['#']));
                            $dbrec->userid = $oldobj->new_id;
                            $dbrec->rawgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADE']['0']['#']);
                            $dbrec->rawgrademax = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMAX']['0']['#']);
                            $dbrec->rawgrademin = backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWGRADEMIN']['0']['#']);
                            if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
                                $dbrec->usermodified = $oldobj->new_id;
                            }
                            if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
                                $scale = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
                                $dbrec->rawscaleid = $scale->new_id;
                            }
                            $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
                            $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
                            $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
                            $dbrec->locktime = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKTIME']['0']['#']);
                            $dbrec->exported = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXPORTED']['0']['#']);
                            $dbrec->overridden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['OVERRIDDEN']['0']['#']);
                            $dbrec->excluded = backup_todb($info['GRADE_GRADES_HISTORY']['#']['EXCLUDED']['0']['#']);
                            $dbrec->feedback = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACK']['0']['#']);
                            $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
                            $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
                            $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
                            insert_record('grade_grades_history', $dbrec);
                            unset($dbrec);
                        }
                        //Increment counters
                        $counter++;
                        //Do some output
                        if ($counter % 1 == 0) {
                            if (!defined('RESTORE_SILENTLY')) {
                                echo ".";
                                if ($counter % 20 == 0) {
                                    echo "<br />";
                                }
                            }
                            backup_flush(300);
                        }
                    }
                }
            }
        }
        // process histories
        if ($gihcount && $status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('gradeitemshistory', 'grades') . '</li>';
            }
            $counter = 0;
            while ($counter < $gihcount) {
                //Fetch recordset_size records in each iteration
                $recs = get_records_select("backup_ids", "table_name = 'grade_items_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
                if ($recs) {
                    foreach ($recs as $rec) {
                        //Get the full record from backup_ids
                        $data = backup_getid($restore->backup_unique_code, 'grade_items_history', $rec->old_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
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
                            if (empty($oldobj->new_id)) {
                                // if the old object is not being restored, can't restoring its history
                                $counter++;
                                continue;
                            }
                            $dbrec->oldid = $oldobj->new_id;
                            $dbrec->action = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ACTION']['0']['#']);
                            $dbrec->source = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SOURCE']['0']['#']);
                            $dbrec->timemodified = backup_todb($info['GRADE_ITEM_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                            if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                $dbrec->loggeduser = $oldobj->new_id;
                            }
                            $dbrec->courseid = $restore->course_id;
                            $oldobj = backup_getid($restore->backup_unique_code, 'grade_categories', backup_todb($info['GRADE_ITEM_HISTORY']['#']['CATEGORYID']['0']['#']));
                            $oldobj->categoryid = $category->new_id;
                            if (empty($oldobj->categoryid)) {
                                $counter++;
                                continue;
                                // category not restored
                            }
                            $dbrec->itemname = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
                            $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
                            $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
                            // code from grade_items restore
                            $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
                            // do not restore if this grade_item is a mod, and
                            if ($dbrec->itemtype == 'mod') {
                                if (!restore_userdata_selected($restore, $dbrec->itemmodule, $iteminstance)) {
                                    // module instance not selected when restored using granular
                                    // skip this item
                                    $counter++;
                                    continue;
                                }
                                // iteminstance should point to new mod
                                $mod = backup_getid($restore->backup_unique_code, $dbrec->itemmodule, $iteminstance);
                                $dbrec->iteminstance = $mod->new_id;
                            } else {
                                if ($dbrec->itemtype == 'category') {
                                    // the item instance should point to the new grade category
                                    // only proceed if we are restoring all grade items
                                    if ($restoreall) {
                                        $category = backup_getid($restore->backup_unique_code, 'grade_categories', $iteminstance);
                                        $dbrec->iteminstance = $category->new_id;
                                    } else {
                                        // otherwise we can safely ignore this grade item and subsequent
                                        // grade_raws, grade_finals etc
                                        continue;
                                    }
                                } elseif ($dbrec->itemtype == 'course') {
                                    // We don't restore course type to avoid duplicate course items
                                    if ($restoreall) {
                                        // TODO any special code needed here to restore course item without duplicating it?
                                        // find the course category with depth 1, and course id = current course id
                                        // this would have been already restored
                                        $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
                                        $dbrec->iteminstance = $cat->id;
                                    } else {
                                        $counter++;
                                        continue;
                                    }
                                }
                            }
                            $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
                            $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
                            $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
                            $dbrec->calculation = backup_todb($info['GRADE_ITEM_HISTORY']['#']['CALCULATION']['0']['#']);
                            $dbrec->gradetype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADETYPE']['0']['#']);
                            $dbrec->grademax = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMAX']['0']['#']);
                            $dbrec->grademin = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEMIN']['0']['#']);
                            if ($oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_ITEM_HISTORY']['#']['SCALEID']['0']['#']))) {
                                // scaleid is optional
                                $dbrec->scaleid = $oldobj->new_id;
                            }
                            if ($oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OUTCOMEID']['0']['#']))) {
                                // outcome is optional
                                $dbrec->outcomeid = $oldobj->new_id;
                            }
                            $dbrec->gradepass = backup_todb($info['GRADE_ITEM_HISTORY']['#']['GRADEPASS']['0']['#']);
                            $dbrec->multfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['MULTFACTOR']['0']['#']);
                            $dbrec->plusfactor = backup_todb($info['GRADE_ITEM_HISTORY']['#']['PLUSFACTOR']['0']['#']);
                            $dbrec->aggregationcoef = backup_todb($info['GRADE_ITEM_HISTORY']['#']['AGGREGATIONCOEF']['0']['#']);
                            $dbrec->sortorder = backup_todb($info['GRADE_ITEM_HISTORY']['#']['SORTORDER']['0']['#']);
                            $dbrec->display = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DISPLAY']['0']['#']);
                            $dbrec->decimals = backup_todb($info['GRADE_ITEM_HISTORY']['#']['DECIMALS']['0']['#']);
                            $dbrec->hidden = backup_todb($info['GRADE_ITEM_HISTORY']['#']['HIDDEN']['0']['#']);
                            $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
                            $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
                            $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
                            insert_record('grade_items_history', $dbrec);
                            unset($dbrec);
                        }
                        //Increment counters
                        $counter++;
                        //Do some output
                        if ($counter % 1 == 0) {
                            if (!defined('RESTORE_SILENTLY')) {
                                echo ".";
                                if ($counter % 20 == 0) {
                                    echo "<br />";
                                }
                            }
                            backup_flush(300);
                        }
                    }
                }
            }
        }
        // process histories
        if ($gohcount && $status) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('gradeoutcomeshistory', 'grades') . '</li>';
            }
            $counter = 0;
            while ($counter < $gohcount) {
                //Fetch recordset_size records in each iteration
                $recs = get_records_select("backup_ids", "table_name = 'grade_outcomes_history' AND backup_code = '{$restore->backup_unique_code}'", "old_id", "old_id", $counter, $recordset_size);
                if ($recs) {
                    foreach ($recs as $rec) {
                        //Get the full record from backup_ids
                        $data = backup_getid($restore->backup_unique_code, 'grade_outcomes_history', $rec->old_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
                            $oldobj = backup_getid($restore->backup_unique_code, "grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
                            if (empty($oldobj->new_id)) {
                                // if the old object is not being restored, can't restoring its history
                                $counter++;
                                continue;
                            }
                            $dbrec->oldid = $oldobj->new_id;
                            $dbrec->action = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['ACTION']['0']['#']);
                            $dbrec->source = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SOURCE']['0']['#']);
                            $dbrec->timemodified = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
                            if ($oldobj = backup_getid($restore->backup_unique_code, "user", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                $dbrec->loggeduser = $oldobj->new_id;
                            }
                            $dbrec->courseid = $restore->course_id;
                            $dbrec->shortname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SHORTNAME']['0']['#']);
                            $dbrec->fullname = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['FULLNAME']['0']['#']);
                            $oldobj = backup_getid($restore->backup_unique_code, "scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
                            $dbrec->scaleid = $oldobj->new_id;
                            $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
                            insert_record('grade_outcomes_history', $dbrec);
                            unset($dbrec);
                        }
                        //Increment counters
                        $counter++;
                        //Do some output
                        if ($counter % 1 == 0) {
                            if (!defined('RESTORE_SILENTLY')) {
                                echo ".";
                                if ($counter % 20 == 0) {
                                    echo "<br />";
                                }
                            }
                            backup_flush(300);
                        }
                    }
                }
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //End ul
        echo '</ul>';
    }
    return $status;
}
function assignment_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Assignment', $restore, $info['MOD']['#'], array('TIMEDUE', 'TIMEAVAILABLE'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the ASSIGNMENT record structure
        $assignment->course = $restore->course_id;
        $assignment->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $assignment->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
        $assignment->format = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
        $assignment->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
        $assignment->preventlate = backup_todb($info['MOD']['#']['PREVENTLATE']['0']['#']);
        $assignment->emailteachers = backup_todb($info['MOD']['#']['EMAILTEACHERS']['0']['#']);
        $assignment->var1 = backup_todb($info['MOD']['#']['VAR1']['0']['#']);
        $assignment->var2 = backup_todb($info['MOD']['#']['VAR2']['0']['#']);
        $assignment->var3 = backup_todb($info['MOD']['#']['VAR3']['0']['#']);
        $assignment->var4 = backup_todb($info['MOD']['#']['VAR4']['0']['#']);
        $assignment->var5 = backup_todb($info['MOD']['#']['VAR5']['0']['#']);
        $assignment->type = isset($info['MOD']['#']['TYPE']['0']['#']) ? backup_todb($info['MOD']['#']['TYPE']['0']['#']) : '';
        $assignment->assignmenttype = backup_todb($info['MOD']['#']['ASSIGNMENTTYPE']['0']['#']);
        $assignment->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
        $assignment->timedue = backup_todb($info['MOD']['#']['TIMEDUE']['0']['#']);
        $assignment->timeavailable = backup_todb($info['MOD']['#']['TIMEAVAILABLE']['0']['#']);
        $assignment->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $assignment->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //We have to recode the grade field if it is <0 (scale)
        if ($assignment->grade < 0) {
            $scale = backup_getid($restore->backup_unique_code, "scale", abs($assignment->grade));
            if ($scale) {
                $assignment->grade = -$scale->new_id;
            }
        }
        if (empty($assignment->assignmenttype)) {
            /// Pre 1.5 assignment
            if ($assignment->type == 1) {
                $assignment->assignmenttype = 'uploadsingle';
            } else {
                $assignment->assignmenttype = 'offline';
            }
        }
        // skip restore of plugins that are not installed
        static $plugins;
        if (!isset($plugins)) {
            $plugins = get_list_of_plugins('mod/assignment/type');
        }
        if (!in_array($assignment->assignmenttype, $plugins)) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li><strong>" . get_string("modulename", "assignment") . " \"" . format_string(stripslashes($assignment->name), true) . "\" - plugin '{$assignment->assignmenttype}' not available!</strong></li>";
            }
            return true;
            // do not fail the restore
        }
        //The structure is equal to the db, so insert the assignment
        $newid = insert_record("assignment", $assignment);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "assignment") . " \"" . format_string(stripslashes($assignment->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);
            // load up the subtype and see if it wants anything further restored.
            $class = 'assignment_' . $assignment->assignmenttype;
            require_once $CFG->dirroot . '/mod/assignment/lib.php';
            require_once $CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php';
            call_user_func(array($class, 'restore_one_mod'), $info, $restore, $assignment);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'assignment', $mod->id)) {
                //Restore assignmet_submissions
                $status = assignment_submissions_restore_mods($mod->id, $newid, $info, $restore, $assignment) && $status;
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function quiz_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //Hook to call Moodle < 1.5 Quiz Restore
    if ($restore->backup_version < 2005043000) {
        include_once "restorelibpre15.php";
        return quiz_restore_pre15_mods($mod, $restore);
    }
    //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('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the QUIZ record structure
        $quiz = new stdClass();
        $quiz->course = $restore->course_id;
        $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
        $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
        $quiz->optionflags = backup_todb($info['MOD']['#']['OPTIONFLAGS']['0']['#']);
        $quiz->penaltyscheme = backup_todb($info['MOD']['#']['PENALTYSCHEME']['0']['#']);
        $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
        $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
        $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
        $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
        $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
        $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
        $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
        $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
        $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
        $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
        $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
        $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
        $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
        $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
        $quiz->delay1 = isset($info['MOD']['#']['DELAY1']['0']['#']) ? backup_todb($info['MOD']['#']['DELAY1']['0']['#']) : '';
        $quiz->delay2 = isset($info['MOD']['#']['DELAY2']['0']['#']) ? backup_todb($info['MOD']['#']['DELAY2']['0']['#']) : '';
        //We have to recode the questions field (a list of questions id and pagebreaks)
        $quiz->questions = quiz_recode_layout($quiz->questions, $restore);
        //The structure is equal to the db, so insert the quiz
        $newid = insert_record("quiz", $quiz);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "quiz") . " \"" . format_string(stripslashes($quiz->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);
            //We have to restore the question_instances now (course level table)
            $status = quiz_question_instances_restore_mods($newid, $info, $restore);
            //We have to restore the feedback now (course level table)
            $status = quiz_feedback_restore_mods($newid, $info, $restore, $quiz);
            //We have to restore the question_versions now (course level table)
            $status = quiz_question_versions_restore_mods($newid, $info, $restore);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'quiz', $mod->id)) {
                //Restore quiz_attempts
                $status = quiz_attempts_restore_mods($newid, $info, $restore);
                if ($status) {
                    //Restore quiz_grades
                    $status = quiz_grades_restore_mods($newid, $info, $restore);
                }
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function quiz_restore_pre15_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Quiz', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the QUIZ record structure
        $quiz->course = $restore->course_id;
        $quiz->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $quiz->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        $quiz->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
        $quiz->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
        $quiz->attempts = backup_todb($info['MOD']['#']['ATTEMPTS_NUMBER']['0']['#']);
        $quiz->attemptonlast = backup_todb($info['MOD']['#']['ATTEMPTONLAST']['0']['#']);
        $quiz->feedback = backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']);
        $quiz->correctanswers = backup_todb($info['MOD']['#']['CORRECTANSWERS']['0']['#']);
        $quiz->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
        if (isset($info['MOD']['#']['DECIMALPOINTS']['0']['#'])) {
            //Only if it's set, to apply DB default else.
            $quiz->decimalpoints = backup_todb($info['MOD']['#']['DECIMALPOINTS']['0']['#']);
        }
        $quiz->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
        $quiz->questionsperpage = backup_todb($info['MOD']['#']['QUESTIONSPERPAGE']['0']['#']);
        $quiz->shufflequestions = backup_todb($info['MOD']['#']['SHUFFLEQUESTIONS']['0']['#']);
        $quiz->shuffleanswers = backup_todb($info['MOD']['#']['SHUFFLEANSWERS']['0']['#']);
        $quiz->questions = backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
        $quiz->sumgrades = backup_todb($info['MOD']['#']['SUMGRADES']['0']['#']);
        $quiz->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $quiz->timecreated = backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
        $quiz->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $quiz->timelimit = backup_todb($info['MOD']['#']['TIMELIMIT']['0']['#']);
        $quiz->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
        $quiz->subnet = backup_todb($info['MOD']['#']['SUBNET']['0']['#']);
        $quiz->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
        //We have to recode the questions field (a list of questions id)
        $newquestions = array();
        if ($questionsarr = explode(",", $quiz->questions)) {
            foreach ($questionsarr as $key => $value) {
                if ($question = backup_getid($restore->backup_unique_code, "question", $value)) {
                    $newquestions[] = $question->new_id;
                }
            }
        }
        $quiz->questions = implode(",", $newquestions);
        //Recalculate the questions field to include page breaks if necessary
        $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
        //Calculate the new review field contents (logic extracted from upgrade)
        $review = QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_RESPONSES + QUIZ_REVIEW_SCORES;
        if ($quiz->feedback) {
            $review += QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_FEEDBACK;
        }
        if ($quiz->correctanswers) {
            $review += QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_ANSWERS;
        }
        if ($quiz->review & 1) {
            $review += QUIZ_REVIEW_CLOSED;
        }
        if ($quiz->review & 2) {
            $review += QUIZ_REVIEW_OPEN;
        }
        $quiz->review = $review;
        //The structure is equal to the db, so insert the quiz
        $newid = insert_record("quiz", $quiz);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "quiz") . " \"" . format_string(stripslashes($quiz->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);
            //We have to restore the quiz_question_instances now (old quiz_question_grades, course level)
            $status = quiz_question_instances_restore_pre15_mods($newid, $info, $restore);
            //We have to restore the question_versions now (course level table)
            $status = quiz_question_versions_restore_pre15_mods($newid, $info, $restore);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'quiz', $mod->id)) {
                //Restore quiz_attempts
                $status = quiz_attempts_restore_pre15_mods($newid, $info, $restore, $quiz->questions);
                if ($status) {
                    //Restore quiz_grades
                    $status = quiz_grades_restore_pre15_mods($newid, $info, $restore);
                }
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function forum_restore_mods($mod, $restore)
{
    global $CFG, $DB;
    $status = true;
    //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']['#']) : '';
        $forum->completiondiscussions = isset($info['MOD']['#']['COMPLETIONDISCUSSIONS']['0']['#']) ? backup_todb($info['MOD']['#']['COMPLETIONDISCUSSIONS']['0']['#']) : 0;
        $forum->completionreplies = isset($info['MOD']['#']['COMPLETIONREPLIES']['0']['#']) ? backup_todb($info['MOD']['#']['COMPLETIONREPLIES']['0']['#']) : 0;
        $forum->completionposts = isset($info['MOD']['#']['COMPLETIONPOSTS']['0']['#']) ? backup_todb($info['MOD']['#']['COMPLETIONPOSTS']['0']['#']) : 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;
            }
        }
        $newid = $DB->insert_record("forum", $forum);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "forum") . " \"" . format_string($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' && !$DB->record_exists('forum_discussions', array('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->messageformat = $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) {
                    $DB->set_field('forum_posts', 'mailed', '1', array('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 = $DB->get_record('modules', array('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;
}
Exemple #27
0
function feedback_restore_mods($mod, $restore)
{
    global $CFG;
    // $allValues = array();
    // $allTrackings = array();
    $status = true;
    $restore_userdata = restore_userdata_selected($restore, 'feedback', $mod->id);
    //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;
        //check of older backupversion of feedback
        $version = intval(backup_todb($info['MOD']['#']['VERSION']['0']['#']));
        //Now, build the feedback record structure
        $feedback->course = $restore->course_id;
        $feedback->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $feedback->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
        $feedback->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
        $feedback->email_notification = backup_todb($info['MOD']['#']['EMAILNOTIFICATION']['0']['#']);
        $feedback->multiple_submit = backup_todb($info['MOD']['#']['MULTIPLESUBMIT']['0']['#']);
        $feedback->autonumbering = backup_todb($info['MOD']['#']['AUTONUMBERING']['0']['#']);
        $feedback->page_after_submit = backup_todb($info['MOD']['#']['PAGEAFTERSUB']['0']['#']);
        $feedback->site_after_submit = backup_todb($info['MOD']['#']['SITEAFTERSUB']['0']['#']);
        $feedback->publish_stats = backup_todb($info['MOD']['#']['PUBLISHSTATS']['0']['#']);
        $feedback->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
        $feedback->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
        $feedback->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        //The structure is equal to the db, so insert the feedback
        $newid = insert_record("feedback", $feedback);
        //create events
        // the open-event
        if ($feedback->timeopen > 0) {
            $event = NULL;
            $event->name = get_string('start', 'feedback') . ' ' . $feedback->name;
            $event->description = $feedback->summary;
            $event->courseid = $feedback->course;
            $event->groupid = 0;
            $event->userid = 0;
            $event->modulename = 'feedback';
            $event->instance = $newid;
            $event->eventtype = 'open';
            $event->timestart = $feedback->timeopen;
            $event->visible = instance_is_visible('feedback', $feedback);
            if ($feedback->timeclose > 0) {
                $event->timeduration = $feedback->timeclose - $feedback->timeopen;
            } else {
                $event->timeduration = 0;
            }
            add_event($event);
        }
        // the close-event
        if ($feedback->timeclose > 0) {
            $event = NULL;
            $event->name = get_string('stop', 'feedback') . ' ' . $feedback->name;
            $event->description = $feedback->summary;
            $event->courseid = $feedback->course;
            $event->groupid = 0;
            $event->userid = 0;
            $event->modulename = 'feedback';
            $event->instance = $newid;
            $event->eventtype = 'close';
            $event->timestart = $feedback->timeclose;
            $event->visible = instance_is_visible('feedback', $feedback);
            $event->timeduration = 0;
            add_event($event);
        }
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<ul><li>" . get_string("modulename", "feedback") . " \"" . $feedback->name . "\"<br />";
        }
        backup_flush(300);
        if ($newid) {
            //Now, build the feedback_item record structure
            $items = $info['MOD']['#']['ITEMS']['0']['#']['ITEM'];
            for ($i = 0; $i < sizeof($items); $i++) {
                $item_info = $items[$i];
                $item->feedback = $newid;
                $item->template = 0;
                $item->name = backup_todb($item_info['#']['NAME']['0']['#']);
                $item->presentation = backup_todb($item_info['#']['PRESENTATION']['0']['#']);
                $item->presentation = str_replace("\n", '', $item->presentation);
                if ($version >= 1) {
                    $item->typ = backup_todb($item_info['#']['TYP']['0']['#']);
                    $item->hasvalue = backup_todb($item_info['#']['HASVALUE']['0']['#']);
                    switch ($item->typ) {
                        case 'radio':
                            $item->typ = 'multichoice';
                            $item->presentation = 'r' . FEEDBACK_MULTICHOICERESTORE_TYPE_SEP . $item->presentation;
                            break;
                        case 'check':
                            $item->typ = 'multichoice';
                            $item->presentation = 'c' . FEEDBACK_MULTICHOICERESTORE_TYPE_SEP . $item->presentation;
                            break;
                        case 'dropdown':
                            $item->typ = 'multichoice';
                            $item->presentation = 'd' . FEEDBACK_MULTICHOICERESTORE_TYPE_SEP . $item->presentation;
                            break;
                        case 'radiorated':
                            $item->typ = 'multichoicerated';
                            $item->presentation = 'r' . FEEDBACK_MULTICHOICERESTORE_TYPE_SEP . $item->presentation;
                            break;
                        case 'dropdownrated':
                            $item->typ = 'multichoicerated';
                            $item->presentation = 'd' . FEEDBACK_MULTICHOICERESTORE_TYPE_SEP . $item->presentation;
                            break;
                    }
                } else {
                    $oldtyp = intval(backup_todb($item_info['#']['TYP']['0']['#']));
                    switch ($oldtyp) {
                        case 0:
                            $item->typ = 'label';
                            $item->hasvalue = 0;
                            break;
                        case 1:
                            $item->typ = 'textfield';
                            $item->hasvalue = 1;
                            break;
                        case 2:
                            $item->typ = 'textarea';
                            $item->hasvalue = 1;
                            break;
                        case 3:
                            $item->typ = 'radio';
                            $item->hasvalue = 1;
                            break;
                        case 4:
                            $item->typ = 'check';
                            $item->hasvalue = 1;
                            break;
                        case 5:
                            $item->typ = 'dropdown';
                            $item->hasvalue = 1;
                            break;
                    }
                }
                $item->position = backup_todb($item_info['#']['POSITION']['0']['#']);
                $item->required = backup_todb($item_info['#']['REQUIRED']['0']['#']);
                //put this new item into the database
                $newitemid = insert_record('feedback_item', $item);
                //Now check if want to restore user data and do it.
                if ($restore_userdata) {
                    $values = $item_info['#']['FBVALUES']['0']['#']['FBVALUE'];
                    for ($ii = 0; $ii < sizeof($values); $ii++) {
                        $value_info = $values[$ii];
                        $value = new object();
                        $value->id = '';
                        $value->item = $newitemid;
                        $value->completed = 0;
                        $value->tmp_completed = backup_todb($value_info['#']['COMPLETED']['0']['#']);
                        $value->value = backup_todb($value_info['#']['VAL']['0']['#']);
                        $value->value = addslashes($value->value);
                        $value->course_id = backup_todb($value_info['#']['COURSE_ID']['0']['#']);
                        //put this new value into the database
                        $newvalueid = insert_record('feedback_value', $value);
                        $value->id = $newvalueid;
                        // $allValues[] = $value;
                    }
                }
            }
            //Now check if want to restore user data again and do it.
            if ($restore_userdata) {
                //restore tracking-data
                $trackings = $info['MOD']['#']['TRACKINGS']['0']['#']['TRACKING'];
                for ($i = 0; $i < sizeof($trackings); $i++) {
                    $tracking_info = $trackings[$i];
                    $tracking = new object();
                    $tracking->id = '';
                    $tracking->userid = backup_todb($tracking_info['#']['USERID']['0']['#']);
                    //have to change later
                    $tracking->feedback = $newid;
                    $tracking->completed = backup_todb($tracking_info['#']['COMPLETED']['0']['#']);
                    //have to change later
                    $tracking->count = backup_todb($tracking_info['#']['COUNT']['0']['#']);
                    if ($tracking->userid > 0) {
                        //We have to recode the userid field
                        $user = backup_getid($restore->backup_unique_code, "user", $tracking->userid);
                        if ($user) {
                            $tracking->userid = $user->new_id;
                        }
                    }
                    //save the tracking
                    $newtrackingid = insert_record('feedback_tracking', $tracking);
                    $tracking->id = $newtrackingid;
                    // $allTrackings[] = $tracking;
                }
                //restore completeds
                $completeds = $info['MOD']['#']['COMPLETEDS']['0']['#']['COMPLETED'];
                for ($i = 0; $i < sizeof($completeds); $i++) {
                    $completed_info = $completeds[$i];
                    $completed = new object();
                    $completed->feedback = $newid;
                    $completed->userid = backup_todb($completed_info['#']['USERID']['0']['#']);
                    $completed->timemodified = backup_todb($completed_info['#']['TIMEMODIFIED']['0']['#']);
                    $completed->random_response = backup_todb($completed_info['#']['RANDOMRESPONSE']['0']['#']);
                    if (!($anonymous_response = backup_todb($completed_info['#']['ANONYMOUSRESPONSE']['0']['#']))) {
                        $anonymous_response = 1;
                    }
                    $completed->anonymous_response = $anonymous_response;
                    if ($completed->userid > 0) {
                        //We have to recode the userid field
                        $user = backup_getid($restore->backup_unique_code, "user", $completed->userid);
                        if ($user) {
                            $completed->userid = $user->new_id;
                        }
                    }
                    //later this have to be changed
                    $oldcompletedid = backup_todb($completed_info['#']['ID']['0']['#']);
                    //save the completed
                    $newcompletedid = insert_record('feedback_completed', $completed);
                    //the newcompletedid have to be changed at every values
                    $tochangevals = get_records('feedback_value', 'tmp_completed', $oldcompletedid);
                    if ($tochangevals) {
                        foreach ($tochangevals as $tmpVal) {
                            $tmpVal->completed = $newcompletedid;
                            $tmpVal->tmp_completed = 0;
                            update_record('feedback_value', $tmpVal);
                        }
                    }
                    //the newcompletedid have to be changed at every tracking
                    $tochangetracks = get_records('feedback_tracking', 'completed', $oldcompletedid);
                    if ($tochangetracks) {
                        foreach ($tochangetracks as $tmpTrack) {
                            $tmpTrack->completed = $newcompletedid;
                            $tmpTrack->tmp_completed = 0;
                            update_record('feedback_tracking', $tmpTrack);
                        }
                    }
                }
            }
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
        } else {
            $status = false;
        }
        if (!defined('RESTORE_SILENTLY')) {
            //Finalize ul
            echo "</ul>";
        }
    } else {
        $status = false;
    }
    return $status;
}
function exercise_submissions_restore($old_exercise_id, $new_exercise_id, $info, $restore)
{
    global $CFG;
    $status = true;
    //Get the submissions array (teacher submissions)
    $submissions = $info['MOD']['#']['SUBMISSIONS']['0']['#']['SUBMISSION'];
    //Iterate over submissions
    for ($i = 0; $i < sizeof($submissions); $i++) {
        $sub_info = $submissions[$i];
        //traverse_xmlize($sub_info);                                                         //Debug
        //print_object ($GLOBALS['traverse_array']);                                          //Debug
        //$GLOBALS['traverse_array']="";                                                      //Debug
        //We'll need this later!!
        $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
        $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
        //Now, build the exercise_SUBMISSIONS record structure
        $submission->exerciseid = $new_exercise_id;
        $submission->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
        $submission->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
        $submission->timecreated = backup_todb($sub_info['#']['TIMECREATED']['0']['#']);
        $submission->resubmit = backup_todb($sub_info['#']['RESUBMIT']['0']['#']);
        $submission->mailed = backup_todb($sub_info['#']['MAILED']['0']['#']);
        $submission->isexercise = backup_todb($sub_info['#']['ISEXERCISE']['0']['#']);
        $submission->late = backup_todb($sub_info['#']['LATE']['0']['#']);
        // always save the exercise descriptions and optionally the student submissions
        if ($submission->isexercise or restore_userdata_selected($restore, 'exercise', $old_exercise_id)) {
            //We have to recode the userid field
            $user = backup_getid($restore->backup_unique_code, "user", $olduserid);
            if ($user) {
                $submission->userid = $user->new_id;
            }
            //The structure is equal to the db, so insert the exercise_submission
            $newid = insert_record("exercise_submissions", $submission);
            //Do some output
            if (($i + 1) % 50 == 0) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo ".";
                    if (($i + 1) % 1000 == 0) {
                        echo "<br />";
                    }
                }
                backup_flush(300);
            }
            if ($newid) {
                //We have the newid, update backup_ids
                backup_putid($restore->backup_unique_code, "exercise_submissions", $oldid, $newid);
                //Now copy moddata associated files
                $status = exercise_restore_files($oldid, $newid, $restore);
                //Now we need to restore exercise_assessments (user level table)
                if ($status and restore_userdata_selected($restore, 'exercise', $old_exercise_id)) {
                    $status = exercise_assessments_restore($new_exercise_id, $newid, $sub_info, $restore);
                }
            } else {
                $status = false;
            }
        }
    }
    return $status;
}
function data_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    $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('Database', $restore, $info['MOD']['#'], array('TIMEAVAILABLEFROM', 'TIMEAVAILABLETO', 'TIMEVIEWFROM', 'TIMEVIEWTO'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        $database->course = $restore->course_id;
        $database->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $database->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
        // Only relevant for restoring backups from 1.6 in a 1.7 install.
        if (isset($info['MOD']['#']['RATINGS']['0']['#'])) {
            $database->ratings = backup_todb($info['MOD']['#']['RATINGS']['0']['#']);
        }
        $database->comments = backup_todb($info['MOD']['#']['COMMENTS']['0']['#']);
        $database->timeavailablefrom = backup_todb($info['MOD']['#']['TIMEAVAILABLEFROM']['0']['#']);
        $database->timeavailableto = backup_todb($info['MOD']['#']['TIMEAVAILABLETO']['0']['#']);
        $database->timeviewfrom = backup_todb($info['MOD']['#']['TIMEVIEWFROM']['0']['#']);
        $database->timeviewto = backup_todb($info['MOD']['#']['TIMEVIEWTO']['0']['#']);
        // Only relevant for restoring backups from 1.6 in a 1.7 install.
        if (isset($info['MOD']['#']['PARTICIPANTS']['0']['#'])) {
            $database->participants = backup_todb($info['MOD']['#']['PARTICIPANTS']['0']['#']);
        }
        $database->requiredentries = backup_todb($info['MOD']['#']['REQUIREDENTRIES']['0']['#']);
        $database->requiredentriestoview = backup_todb($info['MOD']['#']['REQUIREDENTRIESTOVIEW']['0']['#']);
        $database->maxentries = backup_todb($info['MOD']['#']['MAXENTRIES']['0']['#']);
        $database->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
        $database->singletemplate = backup_todb($info['MOD']['#']['SINGLETEMPLATE']['0']['#']);
        $database->listtemplate = backup_todb($info['MOD']['#']['LISTTEMPLATE']['0']['#']);
        $database->listtemplateheader = backup_todb($info['MOD']['#']['LISTTEMPLATEHEADER']['0']['#']);
        $database->listtemplatefooter = backup_todb($info['MOD']['#']['LISTTEMPLATEFOOTER']['0']['#']);
        $database->addtemplate = backup_todb($info['MOD']['#']['ADDTEMPLATE']['0']['#']);
        $database->rsstemplate = backup_todb($info['MOD']['#']['RSSTEMPLATE']['0']['#']);
        $database->rsstitletemplate = backup_todb($info['MOD']['#']['RSSTITLETEMPLATE']['0']['#']);
        $database->csstemplate = backup_todb($info['MOD']['#']['CSSTEMPLATE']['0']['#']);
        $database->jstemplate = backup_todb($info['MOD']['#']['JSTEMPLATE']['0']['#']);
        $database->asearchtemplate = backup_todb($info['MOD']['#']['ASEARCHTEMPLATE']['0']['#']);
        $database->approval = backup_todb($info['MOD']['#']['APPROVAL']['0']['#']);
        $database->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
        $database->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
        // Only relevant for restoring backups from 1.6 in a 1.7 install.
        if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
            $database->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
        }
        $database->defaultsort = backup_todb($info['MOD']['#']['DEFAULTSORT']['0']['#']);
        $database->defaultsortdir = backup_todb($info['MOD']['#']['DEFAULTSORTDIR']['0']['#']);
        $database->editany = backup_todb($info['MOD']['#']['EDITANY']['0']['#']);
        $database->notification = backup_todb($info['MOD']['#']['NOTIFICATION']['0']['#']);
        // We have to recode the scale field if it's <0 (positive is a grade, not a scale)
        if ($database->scale < 0) {
            $scale = backup_getid($restore->backup_unique_code, 'scale', abs($database->scale));
            if ($scale) {
                $database->scale = -$scale->new_id;
            }
        }
        $newid = insert_record('data', $database);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "data") . " \"" . format_string(stripslashes($database->name), true) . "\"</li>";
        }
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
            //Now check if want to restore user data and do it.
            if (function_exists('restore_userdata_selected')) {
                // Moodle 1.6
                $restore_userdata_selected = restore_userdata_selected($restore, 'data', $mod->id);
            } else {
                // Moodle 1.5
                $restore_userdata_selected = $restore->mods['data']->userinfo;
            }
            global $fieldids;
            //Restore data_fields first!!! need to hold an array of [oldid]=>newid due to double dependencies
            $status = $status and data_fields_restore_mods($mod->id, $newid, $info, $restore);
            // now use the new field in the defaultsort
            $newdefaultsort = empty($fieldids[$database->defaultsort]) ? 0 : $fieldids[$database->defaultsort];
            set_field('data', 'defaultsort', $newdefaultsort, 'id', $newid);
            if ($restore_userdata_selected) {
                $status = $status and data_records_restore_mods($mod->id, $newid, $info, $restore);
            }
            // If the backup contained $data->participants, $data->assesspublic
            // and $data->groupmode, we need to convert the data to use Roles.
            // It means the backup was made pre Moodle 1.7. We check the
            // backup_version to make sure.
            if (isset($database->participants) && isset($database->assesspublic)) {
                if (!($teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW))) {
                    notice('Default teacher role was not found. Roles and permissions ' . 'for your database modules 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 database modules 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 all your database modules will have to be manually set.');
                }
                require_once $CFG->dirroot . '/mod/data/lib.php';
                data_convert_to_roles($database, $teacherroles, $studentroles, $restore->mods['data']->instances[$mod->id]->restored_as_course_module);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}
function workshop_restore_mods($mod, $restore)
{
    global $CFG;
    $status = true;
    //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('Workshop', $restore, $info['MOD']['#'], array('SUBMISSIONSTART', 'ASSESSMENTSTART', 'SUBMISSIONEND', 'ASSESSMENTEND', 'RELEASEGRADES'));
        }
        //traverse_xmlize($info);                                                                     //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //Now, build the WORKSHOP record structure
        $workshop->course = $restore->course_id;
        $workshop->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
        $workshop->description = backup_todb($info['MOD']['#']['DESCRIPTION']['0']['#']);
        $workshop->wtype = backup_todb($info['MOD']['#']['WTYPE']['0']['#']);
        $workshop->nelements = backup_todb($info['MOD']['#']['NELEMENTS']['0']['#']);
        $workshop->nattachments = backup_todb($info['MOD']['#']['NATTACHMENTS']['0']['#']);
        $workshop->phase = backup_todb($info['MOD']['#']['PHASE']['0']['#']);
        $workshop->format = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
        $workshop->gradingstrategy = backup_todb($info['MOD']['#']['GRADINGSTRATEGY']['0']['#']);
        $workshop->resubmit = backup_todb($info['MOD']['#']['RESUBMIT']['0']['#']);
        $workshop->agreeassessments = backup_todb($info['MOD']['#']['AGREEASSESSMENTS']['0']['#']);
        $workshop->hidegrades = backup_todb($info['MOD']['#']['HIDEGRADES']['0']['#']);
        $workshop->anonymous = backup_todb($info['MOD']['#']['ANONYMOUS']['0']['#']);
        $workshop->includeself = backup_todb($info['MOD']['#']['INCLUDESELF']['0']['#']);
        $workshop->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
        $workshop->submissionstart = backup_todb($info['MOD']['#']['SUBMISSIONSTART']['0']['#']);
        $workshop->assessmentstart = backup_todb($info['MOD']['#']['ASSESSMENTSTART']['0']['#']);
        $workshop->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
        $workshop->submissionend = backup_todb($info['MOD']['#']['SUBMISSIONEND']['0']['#']);
        $workshop->assessmentend = backup_todb($info['MOD']['#']['ASSESSMENTEND']['0']['#']);
        $workshop->releasegrades = backup_todb($info['MOD']['#']['RELEASEGRADES']['0']['#']);
        $workshop->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
        $workshop->gradinggrade = backup_todb($info['MOD']['#']['GRADINGGRADE']['0']['#']);
        $workshop->ntassessments = backup_todb($info['MOD']['#']['NTASSESSMENTS']['0']['#']);
        $workshop->assessmentcomps = backup_todb($info['MOD']['#']['ASSESSMENTCOMPS']['0']['#']);
        $workshop->nsassessments = backup_todb($info['MOD']['#']['NSASSESSMENTS']['0']['#']);
        $workshop->overallocation = backup_todb($info['MOD']['#']['OVERALLOCATION']['0']['#']);
        $workshop->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
        $workshop->teacherweight = backup_todb($info['MOD']['#']['TEACHERWEIGHT']['0']['#']);
        $workshop->showleaguetable = backup_todb($info['MOD']['#']['SHOWLEAGUETABLE']['0']['#']);
        $workshop->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
        $workshop->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
        //If we have retrieved workshop->phase, it's a pre 1.5 backup, so we have to do
        //some conversions before inserting to DB. Upwards compatibility :-)
        if (isset($info['MOD']['#']['PHASE']['0']['#'])) {
            //It's a pre-15 backup file
            //Adjust the wtype field (mimetised from the upgrade script)
            $workshop->wtype = 0;
            if ($workshop->includeself || $workshop->ntassessments) {
                $workshop->wtype = 1;
                // 3 phases with grading grades
            } else {
                if ($workshop->nsassessments) {
                    $workshop->wtype = 2;
                    // 5 phases with grading grades
                }
            }
            //Now, adjust phases time limits (mimetised from the upgrade script too)
            $early = 0;
            $late = 0;
            $now = time();
            if ($now < $workshop->deadline) {
                $late = $workshop->deadline;
            } else {
                $early = $workshop->deadline;
            }
            if ($workshop->phase > 1) {
                $workshop->submissionstart = $early;
            } else {
                $workshop->submissionstart = $late;
            }
            if ($workshop->phase > 2) {
                $workshop->assessmentstart = $early;
            } else {
                $workshop->assessmentstart = $late;
            }
            if ($workshop->phase > 3) {
                $workshop->submissionend = $early;
            } else {
                $workshop->submissionend = $late;
            }
            if ($workshop->phase > 4) {
                $workshop->assessmentend = $early;
            } else {
                $workshop->assessmentend = $late;
            }
            if ($workshop->phase > 5) {
                $workshop->releasegrades = $now;
            } else {
                $workshop->releasegrades = $now + 4 * 7 * 24 * 60 * 60;
                //Grades will be available in 4 weeks
            }
        }
        //The structure is equal to the db, so insert the workshop
        $newid = insert_record("workshop", $workshop);
        //Do some output
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("modulename", "workshop") . " \"" . format_string(stripslashes($workshop->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);
            //We have to restore the workshop_elements table now (course level table)
            $status = workshop_elements_restore_mods($newid, $info, $restore);
            //Now check if want to restore user data and do it.
            if (restore_userdata_selected($restore, 'workshop', $mod->id)) {
                //Restore workshop_submissions
                $status = workshop_submissions_restore_mods($mod->id, $newid, $info, $restore);
            }
        } else {
            $status = false;
        }
    } else {
        $status = false;
    }
    return $status;
}