added assorbimento corso
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
require_once(__DIR__ . '/../auth_check.php');
|
||||
require_once(__DIR__ . '/../../class/db-functions.php');
|
||||
require_once(__DIR__ . '/../../include/training_coverage.php');
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
@@ -24,16 +25,58 @@ $is_mandatory = isset($_POST['is_mandatory']) && (int)$_POST['is_mandatory'] ===
|
||||
$freq = ($freqRaw === '' || $freqRaw === null) ? null : max(0, (int)$freqRaw);
|
||||
$rem = ($remRaw === '' || $remRaw === null) ? 30 : max(0, (int)$remRaw);
|
||||
|
||||
/* Coperture: corsi assorbiti da questo */
|
||||
$coveredIds = isset($_POST['covered_ids']) ? (array)$_POST['covered_ids'] : [];
|
||||
$coveredIds = array_values(array_unique(array_filter(array_map('intval', $coveredIds), function ($v) {
|
||||
return $v > 0;
|
||||
})));
|
||||
|
||||
if ($name === '') {
|
||||
echo json_encode(['success' => false, 'message' => 'Il nome del corso è obbligatorio.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Riscrive le coperture del corso: cancella le vecchie e inserisce le nuove,
|
||||
* scartando auto-copertura e relazioni che creerebbero un ciclo.
|
||||
*/
|
||||
function saveCoverage(PDO $pdo, int $topicId, array $coveredIds): array
|
||||
{
|
||||
$skipped = [];
|
||||
|
||||
$pdo->prepare("DELETE FROM training_topic_coverage WHERE topic_id = :tid")
|
||||
->execute(['tid' => $topicId]);
|
||||
|
||||
if (!$coveredIds) return $skipped;
|
||||
|
||||
$ins = $pdo->prepare("
|
||||
INSERT IGNORE INTO training_topic_coverage
|
||||
(topic_id, covered_topic_id, inherit_expiry, created_at)
|
||||
VALUES (:tid, :cid, 'own', NOW())
|
||||
");
|
||||
|
||||
foreach ($coveredIds as $cid) {
|
||||
if ($cid === $topicId) {
|
||||
continue;
|
||||
}
|
||||
if (tc_wouldCreateCycle($pdo, $topicId, $cid)) {
|
||||
$skipped[] = $cid;
|
||||
continue;
|
||||
}
|
||||
$ins->execute(['tid' => $topicId, 'cid' => $cid]);
|
||||
}
|
||||
|
||||
return $skipped;
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
if ($id > 0) {
|
||||
$check = $pdo->prepare("SELECT COUNT(*) FROM training_topics WHERE name = :name AND id <> :id");
|
||||
$check->execute(['name' => $name, 'id' => $id]);
|
||||
if ((int)$check->fetchColumn() > 0) {
|
||||
$pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'message' => 'Esiste già un altro corso con questo nome.']);
|
||||
exit;
|
||||
}
|
||||
@@ -61,13 +104,27 @@ try {
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $id]);
|
||||
$skipped = saveCoverage($pdo, $id, $coveredIds);
|
||||
$pdo->commit();
|
||||
|
||||
$msg = $skipped
|
||||
? 'Corso aggiornato. Alcune coperture sono state ignorate perché avrebbero creato un riferimento circolare.'
|
||||
: null;
|
||||
|
||||
echo json_encode(array_filter([
|
||||
'success' => true,
|
||||
'id' => $id,
|
||||
'message' => $msg,
|
||||
], function ($v) {
|
||||
return $v !== null;
|
||||
}));
|
||||
exit;
|
||||
}
|
||||
|
||||
$check = $pdo->prepare("SELECT COUNT(*) FROM training_topics WHERE name = :name");
|
||||
$check->execute(['name' => $name]);
|
||||
if ((int)$check->fetchColumn() > 0) {
|
||||
$pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'message' => 'Esiste già un corso con questo nome.']);
|
||||
exit;
|
||||
}
|
||||
@@ -88,7 +145,14 @@ try {
|
||||
'is_mandatory' => $is_mandatory,
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => (int)$pdo->lastInsertId()]);
|
||||
$newId = (int)$pdo->lastInsertId();
|
||||
saveCoverage($pdo, $newId, $coveredIds);
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(['success' => true, 'id' => $newId]);
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user