Ejemplo n.º 1
0
function schedule_inc_backup_course_configure($course, $starttime = 0)
{
    global $CFG;
    $status = true;
    backup_add_to_log($starttime, $course->id, "    checking parameters", 'incrementalbackup');
    //Check the required variable
    if (empty($course->id)) {
        $status = false;
    }
    //Get scheduled backup preferences
    $incremental_config = backup_get_config();
    //Checks backup_config pairs exist
    if ($status) {
        if (!isset($incremental_config->backup_inc_modules)) {
            $incremental_config->backup_inc_modules = 1;
        }
        if (!isset($incremental_config->backup_inc_withuserdata)) {
            $incremental_config->backup_inc_withuserdata = 1;
        }
        if (!isset($incremental_config->backup_inc_metacourse)) {
            $incremental_config->backup_inc_metacourse = 1;
        }
        if (!isset($incremental_config->backup_inc_users)) {
            $incremental_config->backup_inc_users = 1;
        }
        if (!isset($incremental_config->backup_inc_logs)) {
            $incremental_config->backup_inc_logs = 0;
        }
        if (!isset($incremental_config->backup_inc_userfiles)) {
            $incremental_config->backup_inc_userfiles = 1;
        }
        if (!isset($incremental_config->backup_inc_coursefiles)) {
            $incremental_config->backup_inc_coursefiles = 1;
        }
        if (!isset($incremental_config->backup_inc_sitefiles)) {
            $incremental_config->backup_inc_sitefiles = 1;
        }
        if (!isset($incremental_config->backup_inc_messages)) {
            $incremental_config->backup_inc_messages = 0;
        }
        if (!isset($incremental_config->backup_inc_weekdays)) {
            $incremental_config->backup_inc_weekdays = "0000000";
        }
        if (!isset($incremental_config->backup_inc_hour)) {
            $incremental_config->backup_inc_hour = 00;
        }
        if (!isset($incremental_config->backup_inc_minute)) {
            $incremental_config->backup_inc_minute = 00;
        }
        if (!isset($incremental_config->backup_inc_destination)) {
            $incremental_config->backup_inc_destination = "";
        }
        if (!isset($incremental_config->backup_inc_keep)) {
            $incremental_config->backup_inc_keep = 1;
        }
        if (!isset($incremental_config->backup_sche_incrementals)) {
            $incremental_config->backup_sche_incrementals = 0;
        }
        if (!isset($incremental_config->backup_inc_privatedata)) {
            $incremental_config->backup_inc_privatedata = 0;
        }
    }
    if ($status) {
        //Checks for the required files/functions to backup every mod
        //And check if there is data about it
        //use the configured modules to backup.
        $count = 0;
        if (!empty($incremental_config->backup_inc_modules)) {
            $moduleselect = 'id=' . str_replace(',', ' OR id=', $incremental_config->backup_inc_modules);
            if ($allmods = get_records_select("modules", $moduleselect)) {
                foreach ($allmods as $mod) {
                    $modname = $mod->name;
                    $modfile = "{$CFG->dirroot}/mod/{$modname}/backuplib.php";
                    $modbackup = $modname . "_backup_mods";
                    $modcheckbackup = $modname . "_check_backup_mods";
                    if (file_exists($modfile)) {
                        include_once $modfile;
                        if (function_exists($modbackup) and function_exists($modcheckbackup)) {
                            $var = "exists_" . $modname;
                            ${$var} = true;
                            $count++;
                            // PENNY NOTES: I have moved from here to the closing brace inside
                            // by two sets of ifs()
                            // to avoid the backup failing on a non existant backup.
                            // If the file/function/whatever doesn't exist, we don't want to set this
                            // this module in backup preferences at all.
                            //Check data
                            //Check module info
                            $var = "backup_" . $modname;
                            if (!isset(${$var})) {
                                ${$var} = $incremental_config->backup_inc_modules;
                            }
                            //Now stores all the mods preferences into an array into preferences
                            $preferences->mods[$modname]->backup = ${$var};
                            //Check include user info
                            $var = "backup_user_info_" . $modname;
                            if (!isset(${$var})) {
                                ${$var} = $incremental_config->backup_inc_withuserdata;
                            }
                            //Now stores all the mods preferences into an array into preferences
                            $preferences->mods[$modname]->userinfo = ${$var};
                            //And the name of the mod
                            $preferences->mods[$modname]->name = $modname;
                        }
                    }
                }
            }
        }
        // now set instances
        if ($coursemods = get_course_mods($course->id)) {
            foreach ($coursemods as $mod) {
                if (array_key_exists($mod->modname, $preferences->mods)) {
                    // we are to backup this module
                    if (empty($preferences->mods[$mod->modname]->instances)) {
                        $preferences->mods[$mod->modname]->instances = array();
                        // avoid warnings
                    }
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->backup = $preferences->mods[$mod->modname]->backup;
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->userinfo = $preferences->mods[$mod->modname]->userinfo;
                    // there isn't really a nice way to do this...
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->name = get_field($mod->modname, 'name', 'id', $mod->instance);
                }
            }
        }
        // finally, clean all the $preferences->mods[] not having instances. Nothing to backup about them
        if (!empty($preferences->mods)) {
            foreach ($preferences->mods as $modname => $mod) {
                if (!isset($mod->instances)) {
                    unset($preferences->mods[$modname]);
                }
            }
        }
    }
    //Convert other parameters
    if ($status) {
        $preferences->backup_metacourse = $incremental_config->backup_inc_metacourse;
        $preferences->backup_users = $incremental_config->backup_inc_users;
        $preferences->backup_logs = $incremental_config->backup_inc_logs;
        $preferences->backup_user_files = $incremental_config->backup_inc_userfiles;
        $preferences->backup_course_files = $incremental_config->backup_inc_coursefiles;
        $preferences->backup_site_files = $incremental_config->backup_inc_sitefiles;
        $preferences->backup_messages = $incremental_config->backup_inc_messages;
        $preferences->backup_gradebook_history = 1;
        $preferences->backup_course = $course->id;
        $preferences->backup_private_user_data = $incremental_config->backup_inc_privatedata;
        if (!empty($incremental_config->backup_inc_destination)) {
            if (!check_dir_exists($incremental_config->backup_inc_destination . '/' . $course->id, true, true)) {
                error('failed to create backup destination directory');
            } else {
                $preferences->backup_destination = $incremental_config->backup_inc_destination . '/' . $course->id;
            }
        }
        $preferences->backup_keep = $incremental_config->backup_inc_keep;
        $preferences->backup_sche_incrementals = $incremental_config->backup_sche_incrementals;
    }
    //Calculate various backup preferences
    if ($status) {
        backup_add_to_log($starttime, $course->id, "    calculating backup name", 'incrementalbackup');
        //Calculate the backup file name
        $backup_name = backup_get_zipfile_name($course);
        //Calculate the string to match the keep preference
        $keep_name = backup_get_keep_name($course);
        //Set them
        $preferences->backup_name = $backup_name;
        $preferences->keep_name = $keep_name;
        //Roleasignments
        $roles = get_records('role', '', '', 'sortorder');
        foreach ($roles as $role) {
            $preferences->backuproleassignments[$role->id] = $role;
        }
        //Another Info
        backup_add_static_preferences($preferences);
    }
    //Calculate the backup unique code to allow simultaneus backups (to define
    //the temp-directory name and records in backup temp tables
    if ($status) {
        $backup_unique_code = time();
        $preferences->backup_unique_code = $backup_unique_code;
    }
    //Calculate necesary info to backup modules
    if ($status) {
        backup_add_to_log($starttime, $course->id, "    calculating modules data", 'incrementalbackup');
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $modbackup = $modname . "_backup_mods";
                //If exists the lib & function
                $var = "exists_" . $modname;
                if (isset(${$var}) && ${$var}) {
                    //Add hidden fields
                    $var = "backup_" . $modname;
                    //Only if selected
                    if (${$var} == 1) {
                        $var = "backup_user_info_" . $modname;
                        //Call the check function to show more info
                        $modcheckbackup = $modname . "_check_backup_mods";
                        backup_add_to_log($starttime, $course->id, "      {$modname}", 'incrementalbackup');
                        $modcheckbackup($course->id, ${$var}, $backup_unique_code);
                    }
                }
            }
        }
    }
    //Now calculate the users
    if ($status) {
        backup_add_to_log($starttime, $course->id, "    calculating users", 'incrementalbackup');
        //Decide about include users with messages, based on SITEID
        if ($preferences->backup_messages && $preferences->backup_course == SITEID) {
            $include_message_users = true;
        } else {
            $include_message_users = false;
        }
        //Decide about include users with blogs, based on SITEID
        if (isset($preferences->backup_blogs) && isset($preferences->backup_course) && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            $include_blog_users = true;
        } else {
            $include_blog_users = false;
        }
        user_check_backup($course->id, $backup_unique_code, $preferences->backup_users, $include_message_users, $include_blog_users);
    }
    //Now calculate the logs
    if ($status) {
        if ($preferences->backup_logs) {
            backup_add_to_log($starttime, $course->id, "    calculating logs", 'incrementalbackup');
            log_check_backup($course->id);
        }
    }
    //Now calculate the userfiles
    if ($status) {
        if ($preferences->backup_user_files) {
            backup_add_to_log($starttime, $course->id, "    calculating user files", 'incrementalbackup');
            user_files_check_backup($course->id, $preferences->backup_unique_code);
        }
    }
    //Now calculate the coursefiles
    if ($status) {
        if ($preferences->backup_course_files) {
            backup_add_to_log($starttime, $course->id, "    calculating course files", 'incrementalbackup');
            course_files_check_backup($course->id, $preferences->backup_unique_code);
        }
    }
    //Now calculate the sitefiles
    if ($status) {
        if ($preferences->backup_site_files) {
            backup_add_to_log($starttime, $course->id, "    calculating site files", 'incrementalbackup');
            site_files_check_backup($course->id, $preferences->backup_unique_code);
        }
    }
    //If everything is ok, return calculated preferences
    if ($status) {
        $status = $preferences;
    }
    return $status;
}
Ejemplo n.º 2
0
/**
* Function to generate the $preferences variable that
* backup uses.  This will back up all modules and instances in a course.
*
* @param object $course course object
* @param array $prefs can contain:
        backup_metacourse
        backup_users
        backup_logs
        backup_user_files
        backup_course_files
        backup_site_files
        backup_messages
* and if not provided, they will not be included.
*/
function backup_generate_preferences_artificially($course, $prefs)
{
    global $CFG, $DB;
    $preferences = new StdClass();
    $preferences->backup_unique_code = time();
    $preferences->backup_users = isset($prefs['backup_users']) ? $prefs['backup_users'] : 0;
    $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
    $count = 0;
    if ($allmods = $DB->get_records("modules")) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = "{$CFG->dirroot}/mod/{$modname}/backuplib.php";
            $modbackup = $modname . "_backup_mods";
            $modbackupone = $modname . "_backup_one_mod";
            $modcheckbackup = $modname . "_check_backup_mods";
            if (!file_exists($modfile)) {
                continue;
            }
            include_once $modfile;
            if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
                continue;
            }
            $modcheckbackup($course->id, $preferences->backup_users, $preferences->backup_unique_code);
            $var = "exists_" . $modname;
            $preferences->{$var} = true;
            $count++;
            // check that there are instances and we can back them up individually
            if (!$DB->count_records('course_modules', array('course' => $course->id), array('module' => $mod->id)) || !function_exists($modbackupone)) {
                continue;
            }
            $var = 'exists_one_' . $modname;
            $preferences->{$var} = true;
            $varname = $modname . '_instances';
            $preferences->{$varname} = get_all_instances_in_course($modname, $course, NULL, true);
            foreach ($preferences->{$varname} as $instance) {
                $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
                $var = 'backup_' . $modname . '_instance_' . $instance->id;
                $preferences->{$var} = true;
                $preferences->mods[$modname]->instances[$instance->id]->backup = true;
                $var = 'backup_user_info_' . $modname . '_instance_' . $instance->id;
                $preferences->{$var} = true;
                $preferences->mods[$modname]->instances[$instance->id]->userinfo = true;
                $var = 'backup_' . $modname . '_instances';
                $preferences->{$var} = 1;
                // we need this later to determine what to display in modcheckbackup.
            }
            //Check data
            //Check module info
            $preferences->mods[$modname]->name = $modname;
            $var = "backup_" . $modname;
            $preferences->{$var} = true;
            $preferences->mods[$modname]->backup = true;
            //Check include user info
            $var = "backup_user_info_" . $modname;
            $preferences->{$var} = true;
            $preferences->mods[$modname]->userinfo = true;
        }
    }
    //Check other parameters
    $preferences->backup_metacourse = isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0;
    $preferences->backup_logs = isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0;
    $preferences->backup_user_files = isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0;
    $preferences->backup_course_files = isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0;
    $preferences->backup_site_files = isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0;
    $preferences->backup_messages = isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0;
    $preferences->backup_gradebook_history = isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0;
    $preferences->backup_blogs = isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0;
    $preferences->backup_course = $course->id;
    //Check users
    user_check_backup($course->id, $preferences->backup_unique_code, $preferences->backup_users, $preferences->backup_messages, $preferences->backup_blogs);
    //Check logs
    log_check_backup($course->id);
    //Check user files
    user_files_check_backup($course->id, $preferences->backup_unique_code);
    //Check course files
    course_files_check_backup($course->id, $preferences->backup_unique_code);
    //Check site files
    site_files_check_backup($course->id, $preferences->backup_unique_code);
    //Role assignments
    $roles = get_records('role', '', '', 'sortorder');
    foreach ($roles as $role) {
        $preferences->backuproleassignments[$role->id] = $role;
    }
    backup_add_static_preferences($preferences);
    return $preferences;
}
Ejemplo n.º 3
0
function schedule_backup_course_configure($course, $starttime = 0)
{
    global $CFG;
    $status = true;
    schedule_backup_log($starttime, $course->id, "    checking parameters");
    //Check the required variable
    if (empty($course->id)) {
        $status = false;
    }
    //Get scheduled backup preferences
    $backup_config = backup_get_config();
    //Checks backup_config pairs exist
    if ($status) {
        if (!isset($backup_config->backup_sche_modules)) {
            $backup_config->backup_sche_modules = 1;
        }
        if (!isset($backup_config->backup_sche_withuserdata)) {
            $backup_config->backup_sche_withuserdata = 1;
        }
        if (!isset($backup_config->backup_sche_metacourse)) {
            $backup_config->backup_sche_metacourse = 1;
        }
        if (!isset($backup_config->backup_sche_users)) {
            $backup_config->backup_sche_users = 1;
        }
        if (!isset($backup_config->backup_sche_logs)) {
            $backup_config->backup_sche_logs = 0;
        }
        if (!isset($backup_config->backup_sche_userfiles)) {
            $backup_config->backup_sche_userfiles = 1;
        }
        if (!isset($backup_config->backup_sche_coursefiles)) {
            $backup_config->backup_sche_coursefiles = 1;
        }
        if (!isset($backup_config->backup_sche_messages)) {
            $backup_config->backup_sche_messages = 0;
        }
        if (!isset($backup_config->backup_sche_active)) {
            $backup_config->backup_sche_active = 0;
        }
        if (!isset($backup_config->backup_sche_weekdays)) {
            $backup_config->backup_sche_weekdays = "0000000";
        }
        if (!isset($backup_config->backup_sche_hour)) {
            $backup_config->backup_sche_hour = 00;
        }
        if (!isset($backup_config->backup_sche_minute)) {
            $backup_config->backup_sche_minute = 00;
        }
        if (!isset($backup_config->backup_sche_destination)) {
            $backup_config->backup_sche_destination = "";
        }
        if (!isset($backup_config->backup_sche_keep)) {
            $backup_config->backup_sche_keep = 1;
        }
    }
    if ($status) {
        //Checks for the required files/functions to backup every mod
        //And check if there is data about it
        $count = 0;
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $modfile = "{$CFG->dirroot}/mod/{$modname}/backuplib.php";
                $modbackup = $modname . "_backup_mods";
                $modcheckbackup = $modname . "_check_backup_mods";
                if (file_exists($modfile)) {
                    include_once $modfile;
                    if (function_exists($modbackup) and function_exists($modcheckbackup)) {
                        $var = "exists_" . $modname;
                        ${$var} = true;
                        $count++;
                        // PENNY NOTES: I have moved from here to the closing brace inside
                        // by two sets of ifs()
                        // to avoid the backup failing on a non existant backup.
                        // If the file/function/whatever doesn't exist, we don't want to set this
                        // this module in backup preferences at all.
                        //Check data
                        //Check module info
                        $var = "backup_" . $modname;
                        if (!isset(${$var})) {
                            ${$var} = $backup_config->backup_sche_modules;
                        }
                        //Now stores all the mods preferences into an array into preferences
                        $preferences->mods[$modname]->backup = ${$var};
                        //Check include user info
                        $var = "backup_user_info_" . $modname;
                        if (!isset(${$var})) {
                            ${$var} = $backup_config->backup_sche_withuserdata;
                        }
                        //Now stores all the mods preferences into an array into preferences
                        $preferences->mods[$modname]->userinfo = ${$var};
                        //And the name of the mod
                        $preferences->mods[$modname]->name = $modname;
                    }
                }
            }
        }
        // now set instances
        if ($coursemods = get_course_mods($course->id)) {
            foreach ($coursemods as $mod) {
                if (array_key_exists($mod->modname, $preferences->mods)) {
                    // we are to backup this module
                    if (empty($preferences->mods[$mod->modname]->instances)) {
                        $preferences->mods[$mod->modname]->instances = array();
                        // avoid warnings
                    }
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->backup = $preferences->mods[$mod->modname]->backup;
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->userinfo = $preferences->mods[$mod->modname]->userinfo;
                    // there isn't really a nice way to do this...
                    $preferences->mods[$mod->modname]->instances[$mod->instance]->name = get_field($mod->modname, 'name', 'id', $mod->instance);
                }
            }
        }
        // finally, clean all the $preferences->mods[] not having instances. Nothing to backup about them
        foreach ($preferences->mods as $modname => $mod) {
            if (!isset($mod->instances)) {
                unset($preferences->mods[$modname]);
            }
        }
    }
    //Convert other parameters
    if ($status) {
        $preferences->backup_metacourse = $backup_config->backup_sche_metacourse;
        $preferences->backup_users = $backup_config->backup_sche_users;
        $preferences->backup_logs = $backup_config->backup_sche_logs;
        $preferences->backup_user_files = $backup_config->backup_sche_userfiles;
        $preferences->backup_course_files = $backup_config->backup_sche_coursefiles;
        $preferences->backup_messages = $backup_config->backup_sche_messages;
        $preferences->backup_course = $course->id;
        $preferences->backup_destination = $backup_config->backup_sche_destination;
        $preferences->backup_keep = $backup_config->backup_sche_keep;
    }
    //Calculate the backup string
    if ($status) {
        schedule_backup_log($starttime, $course->id, "    calculating backup name");
        //Calculate the backup word
        //Take off some characters in the filename !!
        $takeoff = array(" ", ":", "/", "\\", "|");
        $backup_word = str_replace($takeoff, "_", moodle_strtolower(get_string("backupfilename")));
        //If non-translated, use "backup"
        if (substr($backup_word, 0, 1) == "[") {
            $backup_word = "backup";
        }
        //Calculate the date format string
        $backup_date_format = str_replace(" ", "_", get_string("backupnameformat"));
        //If non-translated, use "%Y%m%d-%H%M"
        if (substr($backup_date_format, 0, 1) == "[") {
            $backup_date_format = "%%Y%%m%%d-%%H%%M";
        }
        //Calculate the shortname
        $backup_shortname = clean_filename($course->shortname);
        if (empty($backup_shortname) or $backup_shortname == '_') {
            $backup_shortname = $course->id;
        }
        //Calculate the final backup filename
        //The backup word
        $backup_name = $backup_word . "-";
        //The shortname
        $backup_name .= moodle_strtolower($backup_shortname) . "-";
        //The date format
        $backup_name .= userdate(time(), $backup_date_format, 99, false);
        //The extension
        $backup_name .= ".zip";
        //And finally, clean everything
        $backup_name = clean_filename($backup_name);
        //Calculate the string to match the keep preference
        $keep_name = $backup_word . "-";
        //The shortname
        $keep_name .= moodle_strtolower($backup_shortname) . "-";
        //And finally, clean everything
        $keep_name = clean_filename($keep_name);
        $preferences->backup_name = $backup_name;
        $preferences->keep_name = $keep_name;
    }
    //Calculate the backup unique code to allow simultaneus backups (to define
    //the temp-directory name and records in backup temp tables
    if ($status) {
        $backup_unique_code = time();
        $preferences->backup_unique_code = $backup_unique_code;
    }
    //Calculate necesary info to backup modules
    if ($status) {
        schedule_backup_log($starttime, $course->id, "    calculating modules data");
        if ($allmods = get_records("modules")) {
            foreach ($allmods as $mod) {
                $modname = $mod->name;
                $modbackup = $modname . "_backup_mods";
                //If exists the lib & function
                $var = "exists_" . $modname;
                if (isset(${$var}) && ${$var}) {
                    //Add hidden fields
                    $var = "backup_" . $modname;
                    //Only if selected
                    if (${$var} == 1) {
                        $var = "backup_user_info_" . $modname;
                        //Call the check function to show more info
                        $modcheckbackup = $modname . "_check_backup_mods";
                        schedule_backup_log($starttime, $course->id, "      {$modname}");
                        $modcheckbackup($course->id, ${$var}, $backup_unique_code);
                    }
                }
            }
        }
    }
    //Now calculate the users
    if ($status) {
        schedule_backup_log($starttime, $course->id, "    calculating users");
        //Decide about include users with messages, based on SITEID
        if ($preferences->backup_messages && $preferences->backup_course == SITEID) {
            $include_message_users = true;
        } else {
            $include_message_users = false;
        }
        user_check_backup($course->id, $backup_unique_code, $preferences->backup_users, $include_message_users);
    }
    //Now calculate the logs
    if ($status) {
        if ($preferences->backup_logs) {
            schedule_backup_log($starttime, $course->id, "    calculating logs");
            log_check_backup($course->id);
        }
    }
    //Now calculate the userfiles
    if ($status) {
        if ($preferences->backup_user_files) {
            schedule_backup_log($starttime, $course->id, "    calculating user files");
            user_files_check_backup($course->id, $preferences->backup_unique_code);
        }
    }
    //Now calculate the coursefiles
    if ($status) {
        if ($preferences->backup_course_files) {
            schedule_backup_log($starttime, $course->id, "    calculating course files");
            course_files_check_backup($course->id, $preferences->backup_unique_code);
        }
    }
    //If everything is ok, return calculated preferences
    if ($status) {
        $status = $preferences;
    }
    return $status;
}