Files
ASPIC/agents_competences.php
T

153 lines
5.7 KiB
PHP

<?php
/**
* ASPIC — agents_competences.php
*
* Endpoint du BackOffice (11 septembre 2026) — attribution des compétences
* aux collaborateurs (`ASP_AgentsCompetences`). Séparé de `competences.php`
* (le catalogue lui-même) : ici on ne fait que relier un `AGN_Incr` à un
* `CMP_Code` déjà existant, jamais créer une compétence à la volée.
*
* GET : liste toutes les attributions, avec le prénom du collaborateur et
* le libellé de la compétence (jointure, pour affichage direct).
* POST { "AGC_AgentIncr": 13, "AGC_CompetenceCode": "SITE_WEB" } : attribue.
* DELETE { "AGC_AgentIncr": 13, "AGC_CompetenceCode": "SITE_WEB" } : retire.
*
* À chaque attribution/retrait réussi, `ASP_Agents.AGN_Competences` (champ
* texte dénormalisé) est intégralement recalculé et écrasé à partir de
* l'état courant de `ASP_AgentsCompetences`/`ASP_Competences` — jamais un
* ajout incrémental (contrairement à AGN_Experience) puisqu'il doit
* refléter l'attribution actuelle, pas un historique cumulatif.
*/
session_start();
header('Content-Type: application/json; charset=utf-8');
require __DIR__ . '/db.php';
/**
* Recalcule et écrase AGN_Competences pour un agent à partir de l'état
* courant de la table de liaison — lu ensuite par mission_agent.php et le
* dialogue de test, au même titre qu'AGN_Identite/AGN_Experience.
*/
function recalculerCompetencesAgent(PDO $pdo, int $agentIncr): void
{
$stmt = $pdo->prepare(
'SELECT c.CMP_Libelle, c.CMP_Description
FROM ASP_AgentsCompetences ac
JOIN ASP_Competences c ON c.CMP_Code = ac.AGC_CompetenceCode
WHERE ac.AGC_AgentIncr = ?
ORDER BY c.CMP_Libelle'
);
$stmt->execute([$agentIncr]);
$texte = implode("\n", array_map(
fn(array $c) => $c['CMP_Description']
? "- {$c['CMP_Libelle']} : {$c['CMP_Description']}"
: "- {$c['CMP_Libelle']}",
$stmt->fetchAll()
));
$pdo->prepare('UPDATE ASP_Agents SET AGN_Competences = ? WHERE AGN_Incr = ?')
->execute([$texte !== '' ? $texte : null, $agentIncr]);
}
if (empty($_SESSION['USR_Code'])) {
http_response_code(401);
echo json_encode(['error' => 'Non identifié — veuillez vous connecter.']);
exit;
}
try {
$pdo = getDbConnection();
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Connexion base de données impossible']);
exit;
}
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$lignes = $pdo->query(
'SELECT ac.AGC_Incr, ac.AGC_AgentIncr, ac.AGC_CompetenceCode, ac.AGC_DateAttribution,
a.AGN_Prenom, c.CMP_Libelle
FROM ASP_AgentsCompetences ac
JOIN ASP_Agents a ON a.AGN_Incr = ac.AGC_AgentIncr
JOIN ASP_Competences c ON c.CMP_Code = ac.AGC_CompetenceCode
ORDER BY a.AGN_Prenom, c.CMP_Libelle'
)->fetchAll();
echo json_encode(['attributions' => $lignes]);
exit;
}
if ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true) ?? [];
$agentIncr = (int) ($data['AGC_AgentIncr'] ?? 0);
$competenceCode = trim((string) ($data['AGC_CompetenceCode'] ?? ''));
if ($agentIncr <= 0 || $competenceCode === '') {
http_response_code(400);
echo json_encode(['error' => 'AGC_AgentIncr et AGC_CompetenceCode sont obligatoires.']);
exit;
}
try {
$verifAgent = $pdo->prepare('SELECT 1 FROM ASP_Agents WHERE AGN_Incr = ?');
$verifAgent->execute([$agentIncr]);
if (!$verifAgent->fetch()) {
http_response_code(404);
echo json_encode(['error' => 'Collaborateur introuvable']);
exit;
}
$verifComp = $pdo->prepare('SELECT 1 FROM ASP_Competences WHERE CMP_Code = ?');
$verifComp->execute([$competenceCode]);
if (!$verifComp->fetch()) {
http_response_code(404);
echo json_encode(['error' => 'Compétence introuvable']);
exit;
}
$stmt = $pdo->prepare('SELECT 1 FROM ASP_AgentsCompetences WHERE AGC_AgentIncr = ? AND AGC_CompetenceCode = ?');
$stmt->execute([$agentIncr, $competenceCode]);
if ($stmt->fetch()) {
http_response_code(409);
echo json_encode(['error' => 'Ce collaborateur a déjà cette compétence.']);
exit;
}
$pdo->prepare('INSERT INTO ASP_AgentsCompetences (AGC_AgentIncr, AGC_CompetenceCode) VALUES (?, ?)')
->execute([$agentIncr, $competenceCode]);
recalculerCompetencesAgent($pdo, $agentIncr);
echo json_encode(['success' => true]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Erreur base de données : ' . $e->getMessage()]);
}
exit;
}
if ($method === 'DELETE') {
$data = json_decode(file_get_contents('php://input'), true) ?? [];
$agentIncr = (int) ($data['AGC_AgentIncr'] ?? 0);
$competenceCode = trim((string) ($data['AGC_CompetenceCode'] ?? ''));
if ($agentIncr <= 0 || $competenceCode === '') {
http_response_code(400);
echo json_encode(['error' => 'AGC_AgentIncr et AGC_CompetenceCode sont obligatoires.']);
exit;
}
try {
$stmt = $pdo->prepare('DELETE FROM ASP_AgentsCompetences WHERE AGC_AgentIncr = ? AND AGC_CompetenceCode = ?');
$stmt->execute([$agentIncr, $competenceCode]);
recalculerCompetencesAgent($pdo, $agentIncr);
echo json_encode(['success' => true]);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Erreur base de données : ' . $e->getMessage()]);
}
exit;
}
http_response_code(405);
echo json_encode(['error' => 'Méthode non autorisée']);