prefix;
// Only allow WP user with ID 1 to access this page
$current = wp_get_current_user();
if (!$current || intval($current->ID) !== 1) {
echo '
';
echo '
Datenverwaltung / Testdaten
';
// Handle actions (with nonce)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($_POST['kc_data_nonce']) || !wp_verify_nonce($_POST['kc_data_nonce'], 'kc_data_action')) {
echo '
Ungültiger Request (Nonce).
';
} else {
if (isset($_POST['kc_generate_testdata'])) {
// Read generation options from the form (with sensible defaults)
$num_workshops = max(1, intval($_POST['num_workshops'] ?? 3));
$num_part_per_phase = max(1, intval($_POST['num_part_per_phase'] ?? 10));
$phases = max(1, min(4, intval($_POST['phases'] ?? 2)));
$min_cap = max(1, intval($_POST['min_capacity'] ?? 4));
$max_cap = max($min_cap, intval($_POST['max_capacity'] ?? 8));
$created = ['wahlen'=>0, 'workshops'=>0, 'ww'=>0, 'teilnehmer'=>0];
// Create a random Wahl
$wahl_name = 'TEST - Zufallswahl ' . time();
$wpdb->insert("{$prefix}kc_wahlen", [
'name' => $wahl_name,
'beschreibung' => 'Automatisch erzeugte zufällige Testdaten',
'anzahl_einheiten' => $phases,
'freigegeben' => 1,
'deleted' => 0
]);
$wahl_id = intval($wpdb->insert_id);
if ($wahl_id) $created['wahlen']++;
// Create randomized workshops and map to the Wahl for each phase
$ws_ids = [];
for ($i=0; $i<$num_workshops; $i++) {
$cap = rand($min_cap, $max_cap);
$wn = 'TEST - WS '.($i+1).' #'.rand(1000,9999);
$wpdb->insert("{$prefix}kc_workshops", [
'name' => $wn,
'beschreibung' => 'Auto-generated',
'max_teilnehmer' => $cap
]);
$wid = intval($wpdb->insert_id);
if ($wid) {
$ws_ids[] = $wid;
$created['workshops']++;
for ($ph=1; $ph<=$phases; $ph++) {
$wpdb->insert("{$prefix}kc_wahl_workshops", ['wahl_id'=>$wahl_id, 'workshop_id'=>$wid, 'phase'=>$ph]);
$created['ww']++;
}
}
}
// Create randomized participants with random wishes
$firsts = ['Lukas','Mia','Jonas','Emma','Noah','Hannah','Paul','Lea','Tim','Lina','Max','Sara'];
$lasts = ['Müller','Schmidt','Schneider','Fischer','Weber','Mayer','Wagner','Becker','Hoffmann','Schulz'];
for ($n=0; $n<$num_part_per_phase; $n++) {
for ($ph=1; $ph<=$phases; $ph++) {
$fn = $firsts[array_rand($firsts)].rand(1,999);
$ln = $lasts[array_rand($lasts)];
// pick three wishes (distinct if possible)
$w1 = $ws_ids[array_rand($ws_ids)];
$w2 = $ws_ids[array_rand($ws_ids)];
$w3 = $ws_ids[array_rand($ws_ids)];
$wpdb->insert("{$prefix}kc_teilnehmer", [
'vorname' => $fn,
'nachname' => $ln,
'wahl_id' => $wahl_id,
'phase' => $ph,
'wunsch1' => intval($w1),
'wunsch2' => intval($w2),
'wunsch3' => intval($w3),
'deleted' => 0
]);
if (intval($wpdb->insert_id)) $created['teilnehmer']++;
}
}
echo '
Zufalls-Testdaten erzeugt: '.intval($created['wahlen']).' Wahl(en), '.intval($created['workshops']).' Workshops, '.intval($created['ww']).' Zuweisungen, '.intval($created['teilnehmer']).' Teilnehmer.
';
}
if (isset($_POST['kc_reset_plugin'])) {
// Reset plugin DB: uninstall tables, then recreate
if (function_exists('kc_uninstall_tables') && function_exists('kc_install_tables')) {
kc_uninstall_tables();
kc_install_tables();
echo '
Plugin-Daten zurückgesetzt: Tabellen wurden gelöscht und neu erstellt.
';
} else {
echo '
Reset fehlgeschlagen: Install/Uninstall Funktionen nicht verfügbar.
';
}
}
if (isset($_POST['kc_clear_testdata'])) {
// Remove everything that has names starting with 'TEST - '
// Find workshop ids
$test_ws = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$prefix}kc_workshops WHERE name LIKE %s", 'TEST - %'));
if (!empty($test_ws)) {
foreach($test_ws as $tw) {
$wpdb->delete("{$prefix}kc_workshop_teamer", ['workshop_id'=>$tw]);
}
$wpdb->query("DELETE FROM {$prefix}kc_wahl_workshops WHERE workshop_id IN (".implode(',', array_map('intval', $test_ws)).")");
$wpdb->query("DELETE FROM {$prefix}kc_workshops WHERE id IN (".implode(',', array_map('intval', $test_ws)).")");
}
// Find test wahlen
$test_wahlen = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$prefix}kc_wahlen WHERE name LIKE %s", 'TEST - %'));
if (!empty($test_wahlen)) {
// delete related teilnehmer and zuteilung and force zuteilung
$ids = implode(',', array_map('intval', $test_wahlen));
$wpdb->query("DELETE FROM {$prefix}kc_zuteilung WHERE wahl_id IN (".$ids.")");
$wpdb->query("DELETE FROM {$prefix}kc_force_zuteilung WHERE wahl_id IN (".$ids.")");
$wpdb->query("DELETE FROM {$prefix}kc_teilnehmer WHERE wahl_id IN (".$ids.")");
$wpdb->query("DELETE FROM {$prefix}kc_wahl_workshops WHERE wahl_id IN (".$ids.")");
$wpdb->query("DELETE FROM {$prefix}kc_wahlen WHERE id IN (".$ids.")");
}
echo '
Alle Testdaten (Präfix "TEST - ") wurden entfernt.
';
}
}
}
// Form with actions and generation options
$nonce = wp_create_nonce('kc_data_action');
echo '
';
echo '
';
}
?>