Compare commits
6 Commits
develop
...
bbfa923c9d
| Author | SHA1 | Date | |
|---|---|---|---|
| bbfa923c9d | |||
| 8827e5b324 | |||
| f1fb4cd4ea | |||
| a827275995 | |||
| cd5a0f72de | |||
| 8029ebfb27 |
@@ -98,6 +98,7 @@ jobs:
|
|||||||
--exclude Notes \
|
--exclude Notes \
|
||||||
--exclude "*.yml" \
|
--exclude "*.yml" \
|
||||||
--exclude "*.yaml" \
|
--exclude "*.yaml" \
|
||||||
|
--exclude "*.md" \
|
||||||
.
|
.
|
||||||
|
|
||||||
# Choose target path: prefer absolute plugin path; otherwise derive from HOST_WP_ROOT
|
# Choose target path: prefer absolute plugin path; otherwise derive from HOST_WP_ROOT
|
||||||
|
|||||||
@@ -1,103 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
// CSV Export Handler: früh ausführen, damit kein HTML in der CSV landet
|
|
||||||
if (isset($_GET['export_teilnehmer_csv']) && current_user_can('manage_options')) {
|
|
||||||
global $wpdb;
|
|
||||||
$prefix = $wpdb->prefix;
|
|
||||||
|
|
||||||
$all_wahlen = $wpdb->get_results("SELECT id, name FROM {$prefix}kc_wahlen WHERE deleted=0 ORDER BY id DESC");
|
|
||||||
$all_workshops = $wpdb->get_results("SELECT id, name FROM {$prefix}kc_workshops ORDER BY name");
|
|
||||||
|
|
||||||
$wahl_name_map = [];
|
|
||||||
if (!empty($all_wahlen)) {
|
|
||||||
foreach ($all_wahlen as $w) {
|
|
||||||
$wahl_name_map[intval($w->id)] = $w->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$workshops_map = [];
|
|
||||||
if (!empty($all_workshops)) {
|
|
||||||
foreach ($all_workshops as $ws) {
|
|
||||||
$workshops_map[intval($ws->id)] = $ws->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$export_wahl_id = isset($_GET['wahl_id']) ? intval($_GET['wahl_id']) : 0;
|
|
||||||
$export_phase = isset($_GET['phase']) ? intval($_GET['phase']) : 0;
|
|
||||||
|
|
||||||
$sql = "SELECT * FROM {$prefix}kc_teilnehmer";
|
|
||||||
if ($export_wahl_id > 0 && $export_phase > 0) {
|
|
||||||
$sql .= $wpdb->prepare(" WHERE wahl_id=%d AND phase=%d", $export_wahl_id, $export_phase);
|
|
||||||
} elseif ($export_wahl_id > 0) {
|
|
||||||
$sql .= $wpdb->prepare(" WHERE wahl_id=%d", $export_wahl_id);
|
|
||||||
} elseif ($export_phase > 0) {
|
|
||||||
$sql .= $wpdb->prepare(" WHERE phase=%d", $export_phase);
|
|
||||||
}
|
|
||||||
$sql .= " ORDER BY wahl_id, phase, nachname, vorname";
|
|
||||||
$export_rows = $wpdb->get_results($sql);
|
|
||||||
|
|
||||||
$filename_suffix = '_alle';
|
|
||||||
if ($export_wahl_id > 0) {
|
|
||||||
$wahl_name_for_file = isset($wahl_name_map[$export_wahl_id]) ? $wahl_name_map[$export_wahl_id] : ('wahl_'.$export_wahl_id);
|
|
||||||
$wahl_slug = sanitize_title($wahl_name_for_file);
|
|
||||||
if ($wahl_slug === '') {
|
|
||||||
$wahl_slug = 'wahl_'.$export_wahl_id;
|
|
||||||
}
|
|
||||||
$filename_suffix = '_'.$wahl_slug;
|
|
||||||
if ($export_phase > 0) {
|
|
||||||
$filename_suffix .= '_phase_'.$export_phase;
|
|
||||||
}
|
|
||||||
} elseif ($export_phase > 0) {
|
|
||||||
$filename_suffix = '_phase_'.$export_phase;
|
|
||||||
}
|
|
||||||
|
|
||||||
header('Content-Type: text/csv; charset=utf-8');
|
|
||||||
header('Content-Disposition: attachment; filename="kc_teilnehmer'.$filename_suffix.'.csv"');
|
|
||||||
echo "\xEF\xBB\xBF";
|
|
||||||
|
|
||||||
$output = fopen('php://output', 'w');
|
|
||||||
$delimiter = ';';
|
|
||||||
fputcsv($output, ['Vorname','Nachname','Wahl','Phase','Wunsch 1','Wunsch 2','Wunsch 3'], $delimiter);
|
|
||||||
|
|
||||||
$csv_clean = function($value) {
|
|
||||||
$text = html_entity_decode((string)$value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
||||||
$text = wp_strip_all_tags($text, true);
|
|
||||||
$text = preg_replace('/\s+/u', ' ', $text);
|
|
||||||
return trim($text);
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach ($export_rows as $tn) {
|
|
||||||
$w1_id = intval($tn->wunsch1);
|
|
||||||
$w2_id = intval($tn->wunsch2);
|
|
||||||
$w3_id = intval($tn->wunsch3);
|
|
||||||
|
|
||||||
$w1_disp = $w1_id && isset($workshops_map[$w1_id]) ? $workshops_map[$w1_id] : ($w1_id ? strval($w1_id) : '');
|
|
||||||
$w2_disp = $w2_id && isset($workshops_map[$w2_id]) ? $workshops_map[$w2_id] : ($w2_id ? strval($w2_id) : '');
|
|
||||||
$w3_disp = $w3_id && isset($workshops_map[$w3_id]) ? $workshops_map[$w3_id] : ($w3_id ? strval($w3_id) : '');
|
|
||||||
|
|
||||||
$wahl_name = isset($wahl_name_map[intval($tn->wahl_id)]) ? $wahl_name_map[intval($tn->wahl_id)] : '';
|
|
||||||
|
|
||||||
$vorname = $csv_clean($tn->vorname);
|
|
||||||
$nachname = $csv_clean($tn->nachname);
|
|
||||||
$wahl_name_clean = $csv_clean($wahl_name);
|
|
||||||
$w1_clean = $csv_clean($w1_disp);
|
|
||||||
$w2_clean = $csv_clean($w2_disp);
|
|
||||||
$w3_clean = $csv_clean($w3_disp);
|
|
||||||
|
|
||||||
fputcsv($output, [
|
|
||||||
$vorname,
|
|
||||||
$nachname,
|
|
||||||
$wahl_name_clean,
|
|
||||||
intval($tn->phase),
|
|
||||||
$w1_clean,
|
|
||||||
$w2_clean,
|
|
||||||
$w3_clean
|
|
||||||
], $delimiter);
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose($output);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
function kc_teilnehmer_page() {
|
function kc_teilnehmer_page() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$prefix = $wpdb->prefix;
|
$prefix = $wpdb->prefix;
|
||||||
@@ -106,14 +7,6 @@ function kc_teilnehmer_page() {
|
|||||||
$all_wahlen = $wpdb->get_results("SELECT id, name, anzahl_einheiten FROM {$prefix}kc_wahlen WHERE deleted=0 ORDER BY id DESC");
|
$all_wahlen = $wpdb->get_results("SELECT id, name, anzahl_einheiten FROM {$prefix}kc_wahlen WHERE deleted=0 ORDER BY id DESC");
|
||||||
$all_workshops = $wpdb->get_results("SELECT id, name FROM {$prefix}kc_workshops ORDER BY name");
|
$all_workshops = $wpdb->get_results("SELECT id, name FROM {$prefix}kc_workshops ORDER BY name");
|
||||||
|
|
||||||
// Map of wahl id => name for quick lookup
|
|
||||||
$wahl_name_map = [];
|
|
||||||
if (!empty($all_wahlen)) {
|
|
||||||
foreach ($all_wahlen as $w) {
|
|
||||||
$wahl_name_map[intval($w->id)] = $w->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map of workshop id => name for quick lookup in overview
|
// Map of workshop id => name for quick lookup in overview
|
||||||
$workshops_map = [];
|
$workshops_map = [];
|
||||||
if (!empty($all_workshops)) {
|
if (!empty($all_workshops)) {
|
||||||
@@ -329,10 +222,13 @@ function kc_teilnehmer_page() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Übersicht
|
// Übersicht
|
||||||
// Map für Wahl-ID => Name ist bereits oben aufgebaut
|
// Map für Wahl-ID => Name
|
||||||
|
$wahl_name_map = [];
|
||||||
|
foreach($all_wahlen as $w) {
|
||||||
|
$wahl_name_map[intval($w->id)] = $w->name;
|
||||||
|
}
|
||||||
echo '<div class="kc-admin-table-wrap">';
|
echo '<div class="kc-admin-table-wrap">';
|
||||||
echo '<h2 style="margin-top:0;">Alle Teilnehmer</h2>';
|
echo '<h2 style="margin-top:0;">Alle Teilnehmer</h2>';
|
||||||
echo '<a id="kc-export-current-view" class="kc-btn" style="float:right;margin-right:8px;margin-bottom:12px;background:#2da66a;color:#fff;" href="?page=kc_teilnehmer&export_teilnehmer_csv=1">Teilnehmer-Export (Ansicht)</a>';
|
|
||||||
echo '<a class="kc-btn" style="float:right;margin-bottom:12px;" href="?page=kc_teilnehmer&new=1">+ Neuer Teilnehmer</a>';
|
echo '<a class="kc-btn" style="float:right;margin-bottom:12px;" href="?page=kc_teilnehmer&new=1">+ Neuer Teilnehmer</a>';
|
||||||
|
|
||||||
// Wahl-Filter-Buttons sortiert nach KC1, KC2, KC3, dann Rest
|
// Wahl-Filter-Buttons sortiert nach KC1, KC2, KC3, dann Rest
|
||||||
@@ -375,19 +271,11 @@ function kc_teilnehmer_page() {
|
|||||||
|
|
||||||
foreach ($gruppen as $wid => $phasen) {
|
foreach ($gruppen as $wid => $phasen) {
|
||||||
$wahl_disp = isset($wahl_name_map[$wid]) ? esc_html($wahl_name_map[$wid]) : $wid;
|
$wahl_disp = isset($wahl_name_map[$wid]) ? esc_html($wahl_name_map[$wid]) : $wid;
|
||||||
$csv_wahl_url = add_query_arg([
|
|
||||||
'page' => 'kc_teilnehmer',
|
|
||||||
'export_teilnehmer_csv' => 1,
|
|
||||||
'wahl_id' => intval($wid)
|
|
||||||
], admin_url('admin.php'));
|
|
||||||
// Gesamtanzahl Teilnehmer für diese Wahl berechnen
|
// Gesamtanzahl Teilnehmer für diese Wahl berechnen
|
||||||
$gesamt = 0;
|
$gesamt = 0;
|
||||||
foreach ($phasen as $tns) $gesamt += count($tns);
|
foreach ($phasen as $tns) $gesamt += count($tns);
|
||||||
echo '<details style="margin:10px 0 18px 0;border:1px solid #eaeaea;border-radius:6px;padding:8px;">';
|
echo '<details style="margin:10px 0 18px 0;border:1px solid #eaeaea;border-radius:6px;padding:8px;">';
|
||||||
echo '<summary style="font-weight:700;cursor:pointer;">'. $wahl_disp . ' <span style="color:#555;font-weight:600;">(' . $gesamt . ' TN)</span></summary>';
|
echo '<summary style="font-weight:700;cursor:pointer;">'. $wahl_disp . ' <span style="color:#555;font-weight:600;">(' . $gesamt . ' TN)</span></summary>';
|
||||||
echo '<div style="text-align:right;margin:8px 0 4px 0;">';
|
|
||||||
echo '<a class="kc-btn" style="background:#2da66a;color:#fff;" href="'.esc_url($csv_wahl_url).'">Teilnehmer-Export dieser Wahl</a>';
|
|
||||||
echo '</div>';
|
|
||||||
foreach ($phasen as $phase => $tns) {
|
foreach ($phasen as $phase => $tns) {
|
||||||
echo '<details style="margin:10px 0 10px 0;border:1px solid #f0f0f0;border-radius:6px;padding:8px;">';
|
echo '<details style="margin:10px 0 10px 0;border:1px solid #f0f0f0;border-radius:6px;padding:8px;">';
|
||||||
echo '<summary style="font-weight:600;">Phase '.intval($phase).' <span style="color:#555;font-weight:600;">('.count($tns).' TN)</span></summary>';
|
echo '<summary style="font-weight:600;">Phase '.intval($phase).' <span style="color:#555;font-weight:600;">('.count($tns).' TN)</span></summary>';
|
||||||
@@ -428,20 +316,11 @@ function kc_teilnehmer_page() {
|
|||||||
var rows = Array.prototype.slice.call(document.querySelectorAll(".kc-admin-table tbody tr"));
|
var rows = Array.prototype.slice.call(document.querySelectorAll(".kc-admin-table tbody tr"));
|
||||||
var status = document.getElementById("kc-wahl-filter-count");
|
var status = document.getElementById("kc-wahl-filter-count");
|
||||||
var phaseRow = document.getElementById("kc-phase-filter-row");
|
var phaseRow = document.getElementById("kc-phase-filter-row");
|
||||||
var exportBtn = document.getElementById("kc-export-current-view");
|
|
||||||
// Wahl-Map für Phasen
|
// Wahl-Map für Phasen
|
||||||
var wahlMap = '.json_encode($wahl_map, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP).';
|
var wahlMap = '.json_encode($wahl_map, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP).';
|
||||||
var currentWahl = "";
|
var currentWahl = "";
|
||||||
var currentPhase = "";
|
var currentPhase = "";
|
||||||
|
|
||||||
function updateExportButton() {
|
|
||||||
if (!exportBtn) return;
|
|
||||||
var href = "?page=kc_teilnehmer&export_teilnehmer_csv=1";
|
|
||||||
if (currentWahl) href += "&wahl_id=" + encodeURIComponent(currentWahl);
|
|
||||||
if (currentPhase) href += "&phase=" + encodeURIComponent(currentPhase);
|
|
||||||
exportBtn.setAttribute("href", href);
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderPhaseButtons(wahlId) {
|
function renderPhaseButtons(wahlId) {
|
||||||
phaseRow.innerHTML = "";
|
phaseRow.innerHTML = "";
|
||||||
if (!wahlId || !wahlMap[wahlId]) { phaseRow.style.display = "none"; currentPhase = ""; return; }
|
if (!wahlId || !wahlMap[wahlId]) { phaseRow.style.display = "none"; currentPhase = ""; return; }
|
||||||
@@ -487,7 +366,6 @@ function kc_teilnehmer_page() {
|
|||||||
if (show) shown++;
|
if (show) shown++;
|
||||||
});
|
});
|
||||||
if (status) status.textContent = (wahlId ? (shown+" angezeigt") : "Alle anzeigen");
|
if (status) status.textContent = (wahlId ? (shown+" angezeigt") : "Alle anzeigen");
|
||||||
updateExportButton();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function attachPhaseEvents() {
|
function attachPhaseEvents() {
|
||||||
@@ -518,7 +396,6 @@ function kc_teilnehmer_page() {
|
|||||||
if(btns.length) btns[0].classList.add("active");
|
if(btns.length) btns[0].classList.add("active");
|
||||||
renderPhaseButtons("");
|
renderPhaseButtons("");
|
||||||
applyFilter("", "");
|
applyFilter("", "");
|
||||||
updateExportButton();
|
|
||||||
})();
|
})();
|
||||||
</script>';
|
</script>';
|
||||||
echo '</div>';
|
echo '</div>';
|
||||||
|
|||||||
@@ -360,9 +360,9 @@ function kc_wahlen_page() {
|
|||||||
echo '<td class="kc-actions">';
|
echo '<td class="kc-actions">';
|
||||||
echo '<a class="kc-btn" href="?page=kc_wahlen&zuweisen='.intval($wahl->id).'">Workshops zuweisen</a>';
|
echo '<a class="kc-btn" href="?page=kc_wahlen&zuweisen='.intval($wahl->id).'">Workshops zuweisen</a>';
|
||||||
echo '<a class="kc-btn" href="?page=kc_wahlen&edit_wahl='.intval($wahl->id).'">Bearbeiten</a>';
|
echo '<a class="kc-btn" href="?page=kc_wahlen&edit_wahl='.intval($wahl->id).'">Bearbeiten</a>';
|
||||||
|
echo '<a class="kc-btn" href="?page=kc_wahlen&show_zuteilung='.intval($wahl->id).'">Zuteilung anzeigen</a>';
|
||||||
$nonce = wp_create_nonce('kc_run_zuteilung_' . intval($wahl->id));
|
$nonce = wp_create_nonce('kc_run_zuteilung_' . intval($wahl->id));
|
||||||
echo '<a class="kc-btn" href="?page=kc_wahlen&run_zuteilung='.intval($wahl->id).'&_wpnonce='.$nonce.'" onclick="return confirm(\'Zuteilung wirklich starten? Dies überschreibt vorhandene Zuteilungen.\');">Zuteilung starten</a>';
|
echo '<a class="kc-btn" href="?page=kc_wahlen&run_zuteilung='.intval($wahl->id).'&_wpnonce='.$nonce.'" onclick="return confirm(\'Zuteilung wirklich starten? Dies überschreibt vorhandene Zuteilungen.\');">Zuteilung starten</a>';
|
||||||
echo '<a class="kc-btn" href="?page=kc_wahlen&show_zuteilung='.intval($wahl->id).'">Zuteilung anzeigen</a>';
|
|
||||||
echo '<a href="?page=kc_wahlen&delete_wahl='.intval($wahl->id).'" class="kc-btn del" onclick="return confirm(\'Wirklich löschen?\');">Löschen</a>';
|
echo '<a href="?page=kc_wahlen&delete_wahl='.intval($wahl->id).'" class="kc-btn del" onclick="return confirm(\'Wirklich löschen?\');">Löschen</a>';
|
||||||
echo '</td>';
|
echo '</td>';
|
||||||
echo '</tr>';
|
echo '</tr>';
|
||||||
|
|||||||
1483
includes/api.php
1483
includes/api.php
File diff suppressed because it is too large
Load Diff
@@ -49,20 +49,6 @@ function kc_run_zuteilung($wahl_id) {
|
|||||||
// 2) Kopie der Kapazitäten für diese Phase (werden während Zuordnung reduziert)
|
// 2) Kopie der Kapazitäten für diese Phase (werden während Zuordnung reduziert)
|
||||||
$caps = $workshop_caps;
|
$caps = $workshop_caps;
|
||||||
|
|
||||||
// Minimalanzahlen einmalig laden
|
|
||||||
$ws_ids_in_wahl = array_keys($workshops);
|
|
||||||
$min_map = [];
|
|
||||||
if (!empty($ws_ids_in_wahl)) {
|
|
||||||
$placeholders = implode(',', array_fill(0, count($ws_ids_in_wahl), '%d'));
|
|
||||||
$min_rows = $wpdb->get_results($wpdb->prepare(
|
|
||||||
"SELECT id, min_teilnehmer FROM {$prefix}kc_workshops WHERE id IN ($placeholders)",
|
|
||||||
$ws_ids_in_wahl
|
|
||||||
));
|
|
||||||
foreach($min_rows as $r) {
|
|
||||||
$min_map[intval($r->id)] = intval($r->min_teilnehmer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3) Force-Zuteilungen anwenden (nur für diese Phase)
|
// 3) Force-Zuteilungen anwenden (nur für diese Phase)
|
||||||
foreach($forces_all as $f) {
|
foreach($forces_all as $f) {
|
||||||
if(intval($f->phase) !== $phase) continue;
|
if(intval($f->phase) !== $phase) continue;
|
||||||
@@ -107,7 +93,7 @@ function kc_run_zuteilung($wahl_id) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 5) Wunschrunden 1..3 (OHNE Minimalanzahl-Prüfung - volle Priorität!)
|
// 5) Wunschrunden 1..3
|
||||||
for($wunsch=1;$wunsch<=3;$wunsch++) {
|
for($wunsch=1;$wunsch<=3;$wunsch++) {
|
||||||
$not_assigned = [];
|
$not_assigned = [];
|
||||||
foreach($remaining as $tn) {
|
foreach($remaining as $tn) {
|
||||||
@@ -124,7 +110,8 @@ function kc_run_zuteilung($wahl_id) {
|
|||||||
if(count($remaining) > 1) shuffle($remaining);
|
if(count($remaining) > 1) shuffle($remaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6) Restliche zufällig verteilen (OHNE Minimalanzahl-Prüfung)
|
// 6) Restliche zufällig verteilen (auf alle freie Workshops)
|
||||||
|
$freie = array_keys(array_filter($caps, function($c){return $c>0;}));
|
||||||
foreach($remaining as $tn) {
|
foreach($remaining as $tn) {
|
||||||
$freie = array_keys(array_filter($caps, function($c){return $c>0;}));
|
$freie = array_keys(array_filter($caps, function($c){return $c>0;}));
|
||||||
if(count($freie) === 0) {
|
if(count($freie) === 0) {
|
||||||
@@ -144,178 +131,87 @@ function kc_run_zuteilung($wahl_id) {
|
|||||||
$assign($tn, $ws_id, 99);
|
$assign($tn, $ws_id, 99);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6.5 ITERATIVE MINIMALANZAHL-SICHERUNG: Bis alle erfüllt sind
|
// 6.5 Consolidate workshops that did not reach their minimal Teilnehmerzahl
|
||||||
|
// Load minimal requirements for workshops in this Wahl
|
||||||
$ws_ids_in_wahl = array_keys($workshops);
|
$ws_ids_in_wahl = array_keys($workshops);
|
||||||
$iteration = 0;
|
if (!empty($ws_ids_in_wahl)) {
|
||||||
$max_iterations = 10;
|
$placeholders = implode(',', array_fill(0, count($ws_ids_in_wahl), '%d'));
|
||||||
|
$min_rows = $wpdb->get_results($wpdb->prepare("SELECT id, min_teilnehmer FROM {$prefix}kc_workshops WHERE id IN ($placeholders)", $ws_ids_in_wahl));
|
||||||
|
$min_map = [];
|
||||||
|
foreach($min_rows as $r) $min_map[intval($r->id)] = intval($r->min_teilnehmer);
|
||||||
|
|
||||||
while($iteration < $max_iterations) {
|
// Count current assignments per workshop
|
||||||
$iteration++;
|
$assigned_counts_raw = $wpdb->get_results($wpdb->prepare("SELECT workshop_id, COUNT(*) AS cnt FROM {$prefix}kc_zuteilung WHERE wahl_id=%d AND phase=%d AND workshop_id IS NOT NULL GROUP BY workshop_id", $wahl_id, $phase), ARRAY_A);
|
||||||
error_log("Phase $phase: Minimalanzahl-Check Iteration $iteration");
|
$assigned_counts = [];
|
||||||
|
foreach($assigned_counts_raw as $ar) $assigned_counts[intval($ar['workshop_id'])] = intval($ar['cnt']);
|
||||||
|
|
||||||
if (!empty($ws_ids_in_wahl)) {
|
// Find failing workshops (assigned >0 but < min)
|
||||||
// Aktuelle Belegung zählen
|
$failing = [];
|
||||||
$assigned_counts_raw = $wpdb->get_results($wpdb->prepare(
|
foreach($ws_ids_in_wahl as $wsid) {
|
||||||
"SELECT workshop_id, COUNT(*) AS cnt
|
$min_req = intval($min_map[$wsid] ?? 0);
|
||||||
FROM {$prefix}kc_zuteilung
|
$cnt = intval($assigned_counts[$wsid] ?? 0);
|
||||||
WHERE wahl_id=%d AND phase=%d AND workshop_id IS NOT NULL
|
if ($cnt > 0 && $min_req > 0 && $cnt < $min_req) {
|
||||||
GROUP BY workshop_id",
|
$failing[] = $wsid;
|
||||||
$wahl_id, $phase
|
|
||||||
), 'ARRAY_A');
|
|
||||||
|
|
||||||
$assigned_counts = [];
|
|
||||||
foreach($assigned_counts_raw as $ar) {
|
|
||||||
$assigned_counts[intval($ar['workshop_id'])] = intval($ar['cnt']);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Unterbesetzte Workshops finden (NUR die mit Teilnehmern, aber < min)
|
if (!empty($failing)) {
|
||||||
$failing = [];
|
// collect participants from failing workshops
|
||||||
foreach($ws_ids_in_wahl as $wsid) {
|
|
||||||
$min_req = $min_map[$wsid] ?? 0;
|
|
||||||
$cnt = $assigned_counts[$wsid] ?? 0;
|
|
||||||
if ($min_req > 0 && $cnt > 0 && $cnt < $min_req) {
|
|
||||||
$failing[] = $wsid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($failing)) {
|
|
||||||
// Alle erfüllt!
|
|
||||||
error_log("Phase $phase: ✓ Alle Workshops erfüllen ihre Minimalanzahl (Iteration $iteration)");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
error_log("Phase $phase [Iter $iteration]: $" . count($failing) . " Workshops UNTER Mindestanzahl → umverteilen");
|
|
||||||
|
|
||||||
// Alle TN aus unterbesetzten Workshops sammeln
|
|
||||||
$to_reassign = [];
|
$to_reassign = [];
|
||||||
foreach($failing as $fw) {
|
foreach($failing as $fw) {
|
||||||
$rows = $wpdb->get_results($wpdb->prepare(
|
$rows = $wpdb->get_results($wpdb->prepare("SELECT teilnehmer_id FROM {$prefix}kc_zuteilung WHERE wahl_id=%d AND phase=%d AND workshop_id=%d", $wahl_id, $phase, $fw));
|
||||||
"SELECT teilnehmer_id FROM {$prefix}kc_zuteilung
|
foreach($rows as $r) $to_reassign[] = intval($r->teilnehmer_id);
|
||||||
WHERE wahl_id=%d AND phase=%d AND workshop_id=%d",
|
|
||||||
$wahl_id, $phase, $fw
|
|
||||||
));
|
|
||||||
foreach($rows as $r) {
|
|
||||||
$to_reassign[] = intval($r->teilnehmer_id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($to_reassign)) {
|
if (!empty($to_reassign)) {
|
||||||
// Keine TN zu umverteilen
|
// remove those assignments
|
||||||
error_log("Phase $phase [Iter $iteration]: Keine Teilnehmer in unterbesetzten Workshops gefunden");
|
$fw_placeholders = implode(',', array_map('intval', $failing));
|
||||||
break;
|
$wpdb->query("DELETE FROM {$prefix}kc_zuteilung WHERE wahl_id=".intval($wahl_id)." AND phase=".intval($phase)." AND workshop_id IN ($fw_placeholders)");
|
||||||
}
|
|
||||||
|
|
||||||
error_log("Phase $phase [Iter $iteration]: " . count($to_reassign) . " Teilnehmer werden entfernt und neu verteilt");
|
// free capacity for the failing workshops
|
||||||
|
foreach($failing as $fw) {
|
||||||
// Alle Zuweisungen aus unterbesetzten Workshops löschen
|
$freed = intval($assigned_counts[$fw] ?? 0);
|
||||||
$fw_list = implode(',', array_map('intval', $failing));
|
if (!isset($caps[$fw])) $caps[$fw] = 0;
|
||||||
$wpdb->query("DELETE FROM {$prefix}kc_zuteilung
|
$caps[$fw] += $freed;
|
||||||
WHERE wahl_id = " . intval($wahl_id) . "
|
|
||||||
AND phase = " . intval($phase) . "
|
|
||||||
AND workshop_id IN ($fw_list)");
|
|
||||||
|
|
||||||
// Kapazitäten wieder freigeben
|
|
||||||
foreach($failing as $fw) {
|
|
||||||
$freed = $assigned_counts[$fw] ?? 0;
|
|
||||||
$caps[$fw] = $workshop_caps[$fw]; // Auf max zurücksetzen
|
|
||||||
}
|
|
||||||
|
|
||||||
// "Sichere" Workshops für Umverteilung: Kein Min ODER Min erfüllt (OHNE underbesetzte)
|
|
||||||
$safe_workshops = [];
|
|
||||||
foreach($ws_ids_in_wahl as $wsid) {
|
|
||||||
if (in_array($wsid, $failing)) continue; // Unterbesetzte raus
|
|
||||||
$min_req = $min_map[$wsid] ?? 0;
|
|
||||||
$cnt = $assigned_counts[$wsid] ?? 0;
|
|
||||||
|
|
||||||
// Sicher wenn:
|
|
||||||
// A) Kein Minimum UND noch Platz, ODER
|
|
||||||
// B) Min > 0 UND Min schon erreicht UND noch Platz, ODER
|
|
||||||
// C) Min > 0 UND alles folgenden TN passen rein (reicht für Min)
|
|
||||||
$remaining_tns_to_reassign = count($to_reassign);
|
|
||||||
$cap_left = $caps[$wsid] ?? 0;
|
|
||||||
|
|
||||||
$can_fit = false;
|
|
||||||
if ($min_req == 0) {
|
|
||||||
$can_fit = $cap_left > 0;
|
|
||||||
} else {
|
|
||||||
// Wenn Min schon erfüllt, kann man noch hinzufügen
|
|
||||||
$current_cnt = $assigned_counts[$wsid] ?? 0;
|
|
||||||
if ($current_cnt >= $min_req && $cap_left > 0) {
|
|
||||||
$can_fit = true;
|
|
||||||
}
|
|
||||||
// ODER: Wenn noch Platz für den Rest + Min
|
|
||||||
elseif ($cap_left >= $min_req) {
|
|
||||||
$can_fit = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($can_fit) {
|
// Try to reassign each participant preferring their wishes 1..3
|
||||||
$safe_workshops[] = $wsid;
|
foreach($to_reassign as $tid) {
|
||||||
}
|
$tn = isset($all_teilnehmer_by_id[$tid]) ? $all_teilnehmer_by_id[$tid] : $wpdb->get_row($wpdb->prepare("SELECT * FROM {$prefix}kc_teilnehmer WHERE id=%d", $tid));
|
||||||
}
|
if (!$tn) continue;
|
||||||
|
$reassigned = false;
|
||||||
error_log("Phase $phase [Iter $iteration]: " . count($safe_workshops) . " sichere Workshops verfügbar");
|
for($w=1;$w<=3;$w++) {
|
||||||
|
$choice = intval($tn->{"wunsch$w"});
|
||||||
// Teilnehmer auf sichere Workshops neu verteilen
|
if ($choice > 0 && isset($caps[$choice]) && $caps[$choice] > 0) {
|
||||||
foreach($to_reassign as $tid) {
|
$assigned = $assign($tn, $choice, $w);
|
||||||
$tn = $all_teilnehmer_by_id[$tid] ?? $wpdb->get_row($wpdb->prepare(
|
if ($assigned) { $reassigned = true; break; }
|
||||||
"SELECT * FROM {$prefix}kc_teilnehmer WHERE id = %d", $tid
|
|
||||||
));
|
|
||||||
if (!$tn) continue;
|
|
||||||
|
|
||||||
$reassigned = false;
|
|
||||||
|
|
||||||
// Zuerst Wünsche auf sichere Workshops
|
|
||||||
for($w = 1; $w <= 3; $w++) {
|
|
||||||
$choice = intval($tn->{"wunsch$w"});
|
|
||||||
if ($choice > 0 && in_array($choice, $safe_workshops) && isset($caps[$choice]) && $caps[$choice] > 0) {
|
|
||||||
if ($assign($tn, $choice, $w)) {
|
|
||||||
$reassigned = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if ($reassigned) continue;
|
||||||
|
|
||||||
if ($reassigned) continue;
|
// otherwise assign to any workshop with free capacity
|
||||||
|
$available = array_keys(array_filter($caps, function($c){return $c>0;}));
|
||||||
// Zufällig auf sichere Workshops
|
|
||||||
if (!empty($safe_workshops)) {
|
|
||||||
$available = [];
|
|
||||||
foreach($safe_workshops as $wsid) {
|
|
||||||
if (isset($caps[$wsid]) && $caps[$wsid] > 0) {
|
|
||||||
$available[] = $wsid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!empty($available)) {
|
if (!empty($available)) {
|
||||||
$target = $available[array_rand($available)];
|
$target = $available[array_rand($available)];
|
||||||
if ($assign($tn, $target, 99)) {
|
$assign($tn, $target, 99);
|
||||||
$reassigned = true;
|
continue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Falls immer noch nichts frei → unzugewiesen
|
// lastly, mark as unassigned
|
||||||
if (!$reassigned) {
|
|
||||||
$wpdb->insert("{$prefix}kc_zuteilung", [
|
$wpdb->insert("{$prefix}kc_zuteilung", [
|
||||||
'wahl_id' => $wahl_id,
|
'wahl_id' => $wahl_id,
|
||||||
'teilnehmer_id' => $tn->id,
|
'teilnehmer_id' => $tn->id,
|
||||||
'vorname' => $tn->vorname,
|
'vorname' => $tn->vorname,
|
||||||
'nachname' => $tn->nachname,
|
'nachname' => $tn->nachname,
|
||||||
'phase' => $phase,
|
'phase' => $phase,
|
||||||
'workshop_id' => null,
|
'workshop_id' => null,
|
||||||
'wunsch_rang' => -1
|
'wunsch_rang' => -1
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($iteration >= $max_iterations) {
|
|
||||||
error_log("Phase $phase: WARNUNG - Max Iterationen ($max_iterations) erreicht. Es könnten noch unterbesetzte Workshops existieren.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 7) Kapazitätsprüfung (Debug / Log)
|
// 7) Kapazitätsprüfung (Debug / Log)
|
||||||
foreach($caps as $wsid=>$capleft) {
|
foreach($caps as $wsid=>$capleft) {
|
||||||
if($capleft < 0) {
|
if($capleft < 0) {
|
||||||
|
|||||||
@@ -77,5 +77,4 @@ require_once plugin_dir_path(__FILE__).'includes/frontend-form.php';
|
|||||||
require_once plugin_dir_path(__FILE__).'includes/frontend-ergebnis.php';
|
require_once plugin_dir_path(__FILE__).'includes/frontend-ergebnis.php';
|
||||||
require_once plugin_dir_path(__FILE__).'includes/zuteilungslogik.php';
|
require_once plugin_dir_path(__FILE__).'includes/zuteilungslogik.php';
|
||||||
require_once plugin_dir_path(__FILE__).'includes/admin-data.php';
|
require_once plugin_dir_path(__FILE__).'includes/admin-data.php';
|
||||||
require_once plugin_dir_path(__FILE__).'includes/api.php';
|
|
||||||
require_once plugin_dir_path(__FILE__).'install.php';
|
require_once plugin_dir_path(__FILE__).'install.php';
|
||||||
Reference in New Issue
Block a user