<?php //$Id: backup_form.html,v 1.29.2.5 2009/11/21 11:53:08 stronk7 Exp $
    //This page prints the backup form to select everything

    //Check login   
    require_login();

    //Init backupuserinfo
    $backupuserinfo = false;

    if (!empty($course->id)) {
        if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $course->id))) {
            if (empty($to)) {
                error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
            } else {
                if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $to))) {
                    error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
                }
            }
        }
        $backupuserinfo = has_capability('moodle/backup:userinfo', get_context_instance(CONTEXT_COURSE, $course->id));
    } else {
        if (!has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
            error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
        }
        $backupuserinfo = has_capability('moodle/backup:userinfo', get_context_instance(CONTEXT_SYSTEM));
    }

    //Check site
    if (!$site = get_site()) {
        error("Site not found!");
    }

    //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";
            $modbackupone = $modname.'_backup_one_mod';
            $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++;
                   // check that there are instances and we can back them up individually
                   if (count_records('course_modules','course',$course->id,'module',$mod->id) && function_exists($modbackupone)) {
                       $var = 'exists_one_'.$modname;
                       $$var = true;
                   }
               }
            }
            //Check data
            //Check module info
            $var = "backup_".$modname;
            $$var = optional_param( $var,1);
            //Check include user info
            $var = "backup_user_info_".$modname;
            $$var = optional_param( $var,1);
        }
    }

    //Check other parameters
    $backup_metacourse = optional_param('backup_metacourse',1);
    $backup_users = optional_param('backup_users',1);
    $backup_logs = optional_param('backup_logs',0);
    $backup_user_files = optional_param('backup_user_files',1);
    $backup_course_files = optional_param('backup_course_files',1);
    $backup_messages = optional_param('backup_messages',1);

    if ($count == 0) {
        notice("No backupable modules are installed!");
    }

?>

<script type="text/javascript">
<!--
function selectItemInMenuByName(formId, menuName, selectIndex ) {
    myForm = document.getElementById(formId)
    for (i=0,n=myForm.elements.length;i<n;i++) {
        myLen = menuName.length;
        myName = myForm.elements[i].name;
        myType = myForm.elements[i].type;
        if (myName.substring(0,myLen) == menuName && myType == "select-one") {
            myForm.elements[i].options[selectIndex].selected = true;
        }
    }
}

function selectItemInRadioByName(formId, radioName, selectIndex ) {
    myForm = document.getElementById(formId)
    for (i=0,n=myForm.elements.length;i<n;i++) {
        myLen = radioName.length;
        myName = myForm.elements[i].name;
        myType = myForm.elements[i].type;
        if (myName.substring(0,myLen) == radioName && myType == "radio") {
            myRadioGroup = myForm.elements[myName];
            myRadioGroup[selectIndex].checked = true;
        }
    }
}

function selectItemInCheckboxByName(formId, checkName, checked ) {
    myForm = document.getElementById(formId);
    for (i=0,n=myForm.elements.length;i<n;i++) {
        myLen = checkName.length;
        myName = myForm.elements[i].name;
        myType = myForm.elements[i].type;
        if (myName.substring(0,myLen) == checkName && myType == "checkbox") {
            myForm.elements[i].checked = checked;
        }
    }
}
-->
</script>
<form id="form1" method="post" action="backup.php">
<table cellpadding="5" style="margin-left:auto;margin-right:auto;">
<?php
    echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';

/// Acummulator for hidden options and proper XHTML output
    $hidden_options = '';
    //Now, check modules and info and show posibilities
    if ($allmods = get_records("modules") ) {
        //Print option to select/deselect everything with 1 click.
        echo "<tr>";
        echo "<td align=\"right\">";
        echo '<b>'.get_string("include").":</b>";
        echo "</td><td>";
        echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', true);\">". get_string("all")."</a>/";
        echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_', false);\">".  get_string("none")."</a>";
        echo "</td>";
        if (empty($to) and $backupuserinfo) {
            echo "<td align=\"right\">";
            echo '<b>&nbsp;</b>';
            echo "</td><td>";
            echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', true);\">". get_string("all")."</a>/";
            echo "<a href=\"javascript:void(0);\" onclick=\"selectItemInCheckboxByName('form1', 'backup_user_info_', false);\">". get_string("none")."</a>";
            echo "</td>";
        } else {
            echo "<td colspan=\"2\">&nbsp;</td>";
        }
        echo "</tr>";
        echo "<tr><td colspan=\"4\"><hr /></td></tr>";
        $currentrow = 0;
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modbackup = $modname."_backup_mods";
            //If exists the lib & function
            $var = "exists_".$modname;
            if (isset($$var) && $$var) {
                //Print the full tr
                echo "<tr class=\"r".$currentrow."\">";
                echo "  <td align=\"right\">&nbsp;";
                echo "</td><td>\n";
                $backup_options[0] = get_string("no"); 
                $backup_options[1] = get_string("yes");
                $var = "backup_".$modname;
                //choose_from_menu($backup_options, $var, $$var, "");
                //Print the checkbox
                print_checkbox($var, $$var, $$var, get_string("modulenameplural",$modname),'','selectItemInCheckboxByName(\'form1\',\'backup_'.$modname.'\',this.checked)');
                echo "</td><td align=\"right\">&nbsp;";
                echo "</td><td>";
                $var = "backup_user_info_".$modname;
                if (empty($to) and $backupuserinfo) {
                    $backup_user_options[0] = get_string("withoutuserdata");
                    $backup_user_options[1] = get_string("withuserdata");
                    //choose_from_menu($backup_user_options, $var, $$var, "");
                    print_checkbox($var, $$var, $$var, get_string("userdata"),'','selectItemInCheckboxByName(\'form1\',\'backup_user_info_'.$modname.'\',this.checked)');
                } else {
                    echo '<input type="hidden" name="'.$var.'" value="0" />';
                }
                echo "</td></tr>\n"; 
                // if we have the backup_one_mod function, loop here.
                $var = 'exists_one_'.$modname;
                if (!empty($$var)) {
                    echo '<tr><td></td><td colspan="3"><table class="backup-form-instances">';
                    $instances = get_all_instances_in_course($modname,$course);
                    foreach ($instances as $instance) {
                        echo '<tr><td>';
                        $var = 'backup_'.$modname.'_instance_'.$instance->id;
                        $$var = optional_param($var,1);
                        print_checkbox($var,$$var,$$var,$instance->name,$instance->name,'this.form.elements[\'backup_'.$modname.'\'].checked=1;');
                        echo '</td><td align="right">&nbsp;';
                        $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
                        $$var = optional_param($var,1);
                        if (empty($to) and $backupuserinfo) {
                            print_checkbox($var,$$var,$$var,get_string('userdata'),'','this.form.elements[\'backup_user_info_'.$modname.'\'].checked=1;');
                        } else {
                            echo '<input type="hidden" name="'.$var.'" value="0" />';
                        }
                        echo '</td></tr>';
                    }
                    echo '</table></td></tr>';
                }
              
            }
        }
        //Line
        echo "<tr><td colspan=\"4\"><hr /></td></tr>\n";

        if (empty($to) && !empty($course->metacourse)) {
            //Now print the Metacourse tr
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
            echo get_string ("metacourse").":";
            echo "</b></td><td colspan=\"2\">";
            $meta_options[0] = get_string("no"); 
            $meta_options[1] = get_string("yes"); 
            choose_from_menu($meta_options, "backup_metacourse", $backup_metacourse, "");
            echo "</td></tr>";
        }
        else {
            $hidden_options .= '<input type="hidden" name="backup_metacourse" value="0" />';
        }

        if (empty($to) and $backupuserinfo) {
            //Now print the Users tr
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
            echo get_string("users").":";
            echo "</b></td><td colspan=\"2\">";
            if (has_capability('moodle/site:backup', get_context_instance(CONTEXT_SYSTEM))) {
                $user_options[0] = get_string("all");
            }
            $user_options[1] = get_string("course");
            $user_options[2] = get_string("none");
            choose_from_menu($user_options, "backup_users", $backup_users, "");
            echo "</td></tr>";
        }
        else {
            $hidden_options .= '<input type="hidden" name="backup_users" value="2" />';
        }

        if (empty($to) and $backupuserinfo) {
            //Now print the Logs tr
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
            echo get_string("logs").":";                                               
            echo "</b></td><td colspan=\"2\">";
            $log_options[0] = get_string("no");
            $log_options[1] = get_string("yes"); 
            choose_from_menu($log_options, "backup_logs", $backup_logs, ""); 
            echo "</td></tr>";
        }
        else {
            $hidden_options .= '<input type="hidden" name="backup_logs" value="0" />';
        }

        if (empty($to) and $backupuserinfo) {
            //Now print the User Files tr
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
            echo get_string ("userfiles").":";
            echo "</b></td><td colspan=\"2\">";
            $user_file_options[0] = get_string("no"); 
            $user_file_options[1] = get_string("yes"); 
            choose_from_menu($user_file_options, "backup_user_files", $backup_user_files, "");
            echo "</td></tr>";
        }
        else {
            $hidden_options .= '<input type="hidden" name="backup_user_files" value="0" />';
        }
        //Now print the Course Files tr
        echo "<tr>";
        echo "<td align=\"right\" colspan=\"2\"><b>";
        echo get_string ("coursefiles").":";
        echo "</b></td><td colspan=\"2\">";
        $course_file_options[0] = get_string("no");
        $course_file_options[1] = get_string("yes");
        choose_from_menu($course_file_options, "backup_course_files", $backup_course_files, "");
        echo "</td></tr>";

        if (empty($to) and $backupuserinfo and $course->id == SITEID) {
            //If we are in a SITEID backup print the Messages tr
            echo "<tr>";
            echo "<td align=\"right\" colspan=\"2\"><b>";
            echo get_string ('messages','message').":";
            echo "</b></td><td colspan=\"2\">";
            $mess_options[0] = get_string("no"); 
            $mess_options[1] = get_string("yes"); 
            choose_from_menu($mess_options, "backup_messages", $backup_messages, "");
            echo "</td></tr>";
        }
        else {
            $hidden_options .= '<input type="hidden" name="backup_messages" value="0" />';
        }
    }

    $backup_unique_code = time();

    //Calculate the backup string

    //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 backup unique code to allow simultaneus backups (to define
    //the temp-directory name and records in backup temp tables
?>
</table>
<?php
/// Print captured hidden options, now that we have closed the table
    echo $hidden_options;
?>

<div style="text-align:center;margin-left:auto;margin-right:auto">
<input type="hidden" name="id"     value="<?php  p($id) ?>" />
<input type="hidden" name="to"     value="<?php p($to) ?>" />
<input type="hidden" name="backup_unique_code" value="<?php p($backup_unique_code); ?>" />
<input type="hidden" name="backup_name" value="<?php p($backup_name); ?>" />
<input type="hidden" name="launch" value="check" />
<input type="submit" value="<?php  print_string("continue") ?>" />
<input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
</div>
</form>
