161 lines
9.3 KiB
PHP
161 lines
9.3 KiB
PHP
<?php
|
|
function kc_data_page() {
|
|
global $wpdb;
|
|
$prefix = $wpdb->prefix;
|
|
|
|
// Only allow WP user with ID 1 to access this page
|
|
$current = wp_get_current_user();
|
|
if (!$current || intval($current->ID) !== 1) {
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<h2>Datenverwaltung</h2>';
|
|
echo '<div class="notice notice-error">Nur der Hauptadministrator (User ID 1) kann die Testdatenverwaltung verwenden.</div>';
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
kc_admin_tabs('kc_wahlen');
|
|
|
|
echo '<div class="kc-admin-table-wrap">';
|
|
echo '<h2 style="margin-top:0;">Datenverwaltung / Testdaten</h2>';
|
|
|
|
// 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 '<div class="notice notice-error">Ungültiger Request (Nonce).</div>';
|
|
} 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 '<div class="notice notice-success">Zufalls-Testdaten erzeugt: '.intval($created['wahlen']).' Wahl(en), '.intval($created['workshops']).' Workshops, '.intval($created['ww']).' Zuweisungen, '.intval($created['teilnehmer']).' Teilnehmer.</div>';
|
|
}
|
|
|
|
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 '<div class="notice notice-success">Plugin-Daten zurückgesetzt: Tabellen wurden gelöscht und neu erstellt.</div>';
|
|
} else {
|
|
echo '<div class="notice notice-error">Reset fehlgeschlagen: Install/Uninstall Funktionen nicht verfügbar.</div>';
|
|
}
|
|
}
|
|
|
|
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 '<div class="notice notice-success">Alle Testdaten (Präfix "TEST - ") wurden entfernt.</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Form with actions and generation options
|
|
$nonce = wp_create_nonce('kc_data_action');
|
|
echo '<form method="post" style="max-width:800px;margin-top:12px;">';
|
|
echo '<p>Mit diesen Schaltern kannst du in der Testumgebung automatisch Beispiel-Daten erzeugen oder wieder komplett entfernen. Die Aktionen sind reversibel und zielen nur auf Einträge mit dem Präfix "TEST - ".</p>';
|
|
echo '<input type="hidden" name="kc_data_nonce" value="'.esc_attr($nonce).'">';
|
|
|
|
echo '<h3>Generierungs-Optionen</h3>';
|
|
echo '<table style="width:100%;max-width:600px">';
|
|
echo '<tr><td style="width:40%;padding:6px 8px;">Anzahl Workshops</td><td><input type="number" name="num_workshops" value="3" min="1" style="width:120px;padding:6px;"></td></tr>';
|
|
echo '<tr><td style="padding:6px 8px;">Teilnehmer pro Phase</td><td><input type="number" name="num_part_per_phase" value="10" min="1" style="width:120px;padding:6px;"></td></tr>';
|
|
echo '<tr><td style="padding:6px 8px;">Phasen (Einheiten)</td><td><input type="number" name="phases" value="2" min="1" max="4" style="width:120px;padding:6px;"></td></tr>';
|
|
echo '<tr><td style="padding:6px 8px;">Min. Workshop-Kapazität</td><td><input type="number" name="min_capacity" value="4" min="1" style="width:120px;padding:6px;"></td></tr>';
|
|
echo '<tr><td style="padding:6px 8px;">Max. Workshop-Kapazität</td><td><input type="number" name="max_capacity" value="8" min="1" style="width:120px;padding:6px;"></td></tr>';
|
|
echo '</table>';
|
|
|
|
echo '<p style="margin-top:12px;"><button class="kc-btn" name="kc_generate_testdata">Testdaten erzeugen</button> ';
|
|
echo '<button class="kc-btn del" name="kc_clear_testdata" onclick="return confirm(\'Sicher? Alle Testdaten löschen?\')">Testdaten entfernen</button> ';
|
|
echo '<button class="kc-btn del" name="kc_reset_plugin" onclick="return confirm(\'Achtung: ALLE Plugin-Daten löschen und Tabellen neu erstellen? Dies ist unwiderruflich.\')">Reset Plugin (Tabellen neu erstellen)</button></p>';
|
|
echo '</form>';
|
|
|
|
echo '</div>';
|
|
}
|
|
?>
|