<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
<title>Bulletin - Statistiques Matière</title>
<style type="text/css">
@media print {
  .noprint { display: none !important; visibility: hidden !important; }
  .page-break { display: block; page-break-after: always; }
  @page { size: 210mm 297mm; margin: 10mm 10mm 10mm 10mm; }
  body { font-size: 11pt; }
  .stat-table { width: 100% !important; }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Times New Roman", Times, serif;
  background: #f5f6fa;
  color: #1a1a2e;
  font-size: 13px;
}

/* ── Top toolbar ── */
.toolbar {
  background: linear-gradient(135deg, #1565c0 0%, #1976d2 60%, #1e88e5 100%);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  box-shadow: 0 3px 12px rgba(21,101,192,0.35);
}
.toolbar-title {
  color: #fff;
  font-size: 17px;
  font-weight: bold;
  letter-spacing: 1px;
  white-space: nowrap;
  text-transform: uppercase;
  font-family: "Times New Roman", Times, serif;
}
.toolbar label {
  color: #bbdefb;
  font-size: 12px;
  font-weight: bold;
  letter-spacing: 0.5px;
  margin-right: 4px;
}
.toolbar select {
  border: 1px solid #90caf9;
  background: #1565c0;
  color: #e3f2fd;
  padding: 6px 10px;
  border-radius: 4px;
  font-family: "Times New Roman", Times, serif;
  font-size: 12px;
  cursor: pointer;
  min-width: 120px;
  transition: border-color 0.2s;
}
.toolbar select:focus { border-color: #fff; outline: none; }
.toolbar select:disabled { opacity: 0.6; cursor: wait; }

.sort-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #bbdefb;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}
.sort-toggle input[type="checkbox"] {
  width: 15px;
  height: 15px;
  cursor: pointer;
  accent-color: #fff;
}

.btn-submit {
  background: #fff;
  color: #1565c0;
  border: none;
  padding: 7px 20px;
  border-radius: 4px;
  font-family: "Times New Roman", Times, serif;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
  letter-spacing: 0.5px;
  transition: background 0.2s, transform 0.1s;
}
.btn-submit:hover { background: #e3f2fd; transform: translateY(-1px); }

.btn-print {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 4px;
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color 0.2s;
  display: flex;
  align-items: center;
}
.btn-print:hover { border-color: #fff; }
.btn-print img { display: block; }

/* ── Report header ── */
.report-header {
  background: #fff;
  border-bottom: 3px solid #e2b96f;
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.report-title {
  font-size: 18px;
  font-weight: bold;
  color: #1a1a2e;
  letter-spacing: 0.5px;
}
.report-meta {
  font-size: 12px;
  color: #555;
}
.report-meta span {
  display: inline-block;
  margin-left: 16px;
  padding: 3px 10px;
  background: #f0f4ff;
  border-radius: 12px;
  border: 1px solid #d0d8ee;
}
.sorted-badge {
  display: inline-block;
  margin-left: 10px;
  padding: 2px 10px;
  background: #e8f5e9;
  color: #2e7d32;
  border-radius: 12px;
  border: 1px solid #c8e6c9;
  font-size: 11px;
  font-weight: bold;
  vertical-align: middle;
}

/* ── Table wrapper ── */
.table-wrapper {
  padding: 20px 24px;
  overflow-x: auto;
}

/* ── Stats table ── */
.stat-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
  border-radius: 8px;
  overflow: hidden;
}
.stat-table thead tr {
  background: linear-gradient(90deg, #1565c0 0%, #1e88e5 100%);
  color: #e8eef5;
}
.stat-table thead th {
  padding: 11px 14px;
  font-size: 12px;
  font-weight: bold;
  letter-spacing: 0.5px;
  text-align: center;
  border-right: 1px solid rgba(255,255,255,0.15);
  white-space: nowrap;
}
.stat-table thead th:first-child {
  text-align: left;
  padding-left: 16px;
  min-width: 200px;
}
.stat-table thead th.avg-col {
  background: rgba(255,255,255,0.18);
  color: #fff;
}
.stat-table tbody tr {
  border-bottom: 1px solid #e8edf5;
  transition: background 0.15s;
}
.stat-table tbody tr:hover { background: #f4f7ff; }
.stat-table tbody tr:nth-child(even) { background: #fafbfd; }
.stat-table tbody tr:nth-child(even):hover { background: #f0f4ff; }
.stat-table tbody td {
  padding: 9px 14px;
  font-size: 12.5px;
  text-align: center;
  border-right: 1px solid #e8edf5;
}
.stat-table tbody td:first-child {
  text-align: left;
  padding-left: 16px;
  font-weight: 500;
  color: #1a1a2e;
}
.stat-table tbody td.avg-col { font-weight: bold; font-size: 13px; }

/* Grade coloring */
.grade-high  { color: #1a7f37; font-weight: bold; }
.grade-mid   { color: #7d6608; }
.grade-low   { color: #c0392b; font-weight: bold; }
.grade-empty { color: #aaa; font-style: italic; }

/* tfoot */
.stat-table tfoot tr {
  background: linear-gradient(90deg, #1565c0 0%, #1976d2 100%);
  color: #e8eef5;
}
.stat-table tfoot td {
  padding: 10px 14px;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  border-right: 1px solid rgba(255,255,255,0.15);
}
.stat-table tfoot td:first-child {
  text-align: left;
  padding-left: 16px;
  font-size: 13px;
  letter-spacing: 0.5px;
  color: #fff;
}
.stat-table tfoot td.avg-col { color: #fff; font-size: 14px; }

/* ── Graph panel ── */
.graph-panel {
  margin: 0 24px 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.08);
  overflow: hidden;
}
.graph-panel-header {
  background: linear-gradient(90deg, #1565c0, #1e88e5);
  padding: 10px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.graph-panel-header span { color: #fff; font-weight: bold; font-size: 13px; letter-spacing: 0.5px; }
.btn-close-graph {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  color: #fff;
  cursor: pointer;
  border-radius: 4px;
  padding: 3px 10px;
  font-size: 12px;
  transition: background 0.2s;
}
.btn-close-graph:hover { background: rgba(255,255,255,0.2); }
.graph-panel-body { padding: 16px; text-align: center; }

/* Rank badge */
.rank {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 50%;
  background: #e8edf5;
  color: #1a1a2e;
  font-size: 11px;
  font-weight: bold;
  margin-right: 6px;
}
.rank-gold   { background: #fff3cd; color: #7d6608; }
.rank-silver { background: #f0f0f0; color: #555; }
.rank-bronze { background: #fce8d0; color: #7d4a1a; }

/* ── PRINT ── */
@media print {
  .noprint, .noprint * {
    display: none !important;
    visibility: hidden !important;
    height: 0 !important;
    overflow: hidden !important;
  }
}
</style>
</head>

<body>

<?php
/* ══════════════════════════════════════════════════════════════
   AJAX endpoint — returns <option> list for a given classID
   Call: ?cycle=X&ajax_subs=1&classID=Y
══════════════════════════════════════════════════════════════ */
if (isset($_GET['ajax_subs'])) {
    $cycle = $_GET['cycle'];
    include '_serveur.php';
    $ajaxClassID = intval($_GET['classID']);
    $sql = 'SELECT subSubjects.id, subSubjects.name AS subname, subjects.name AS subjectname
            FROM subSubjects
            JOIN subjects       ON subSubjects.subjectID  = subjects.id
            JOIN gradesSubjects ON subSubjects.id         = gradesSubjects.subSubjectID
            JOIN classes        ON gradesSubjects.classID = classes.id
            WHERE classes.id = "'.$ajaxClassID.'"
            ORDER BY subjects.id, subSubjects.id';
    $r = mysqli_query($link, $sql);
    while ($d = mysqli_fetch_assoc($r)) {
        $val = $d['id'].';'.htmlspecialchars($d['subname']);
        $lbl = htmlspecialchars($d['subjectname']).' - '.htmlspecialchars($d['subname']);
        echo '<option value="'.$val.'">'.$lbl.'</option>';
    }
    exit;
}
?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

<?php
echo "<div class='noprint'>";
include 'header.php';

$cycle = $_GET['cycle'];
include '_serveur.php';
?>
<input name="cycle" type="hidden" value="<?php $cycle=$_REQUEST['cycle']; echo $cycle; ?>">

<?php
/* ═══════════════════════════════════════════════════════
   TOOLBAR
═══════════════════════════════════════════════════════ */
$sql = 'SELECT * FROM classes WHERE cycleID="'.$cycle.'" ORDER BY id DESC';
if (!mysqli_query($link, $sql)) {
    echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
$req = mysqli_query($link, $sql);

/* Determine which class is currently selected (POST or first in list) */
$postedClaID = '';
if (isset($_POST['cla'])) {
    $parts = explode(';', $_POST['cla']);
    $postedClaID = $parts[0];
}

$allClasses   = [];
$firstClassID = null;
while ($data = mysqli_fetch_array($req)) {
    $allClasses[] = ['id' => $data['id'], 'name' => strtoupper($data['name'])];
    if ($firstClassID === null) $firstClassID = $data['id'];
}

/* The class to use for the initial subject dropdown */
$activeClassID = ($postedClaID !== '') ? intval($postedClaID) : intval($firstClassID);

echo '<div class="toolbar noprint">';
echo '<span class="toolbar-title">Statistiques &mdash; Matière</span>';

echo '<label>Classe :</label>';
echo '<select name="cla" id="sel_classe">';
foreach ($allClasses as $cl) {
    $selected = ($cl['id'] == $activeClassID) ? ' selected="selected"' : '';
    echo '<option value="'.$cl['id'].';'.$cl['name'].'"'.$selected.'>'.$cl['name'].'</option>';
}
echo '</select>';

/* Subject dropdown — populated server-side for the active class,
   then refreshed via JS when the user changes class */
$sql1 = 'SELECT subSubjects.id, subSubjects.name AS subname, subjects.name AS subjectname
         FROM subSubjects
         JOIN subjects       ON subSubjects.subjectID  = subjects.id
         JOIN gradesSubjects ON subSubjects.id         = gradesSubjects.subSubjectID
         JOIN classes        ON gradesSubjects.classID = classes.id
         WHERE classes.id = "'.$activeClassID.'"
         ORDER BY subjects.id, subSubjects.id';
if (!mysqli_query($link, $sql1)) {
    echo "ERROR: Could not execute $sql1. " . mysqli_error($link);
}
$req1 = mysqli_query($link, $sql1);

/* Remember which sub was posted so we can re-select it */
$postedSubID = '';
if (isset($_POST['sub'])) {
    $ps = explode(';', $_POST['sub']);
    $postedSubID = $ps[0];
}

echo '<label>Matière :</label>';
echo '<select name="sub" id="sel_sub">';
while ($data1 = mysqli_fetch_assoc($req1)) {
    $val      = $data1['id'].';'.htmlspecialchars($data1['subname']);
    $lbl      = htmlspecialchars($data1['subjectname']).' - '.htmlspecialchars($data1['subname']);
    $selected = ($data1['id'] == $postedSubID) ? ' selected="selected"' : '';
    echo '<option value="'.$val.'"'.$selected.'>'.$lbl.'</option>';
}
echo '</select>';

/* Sort checkbox */
$sortChecked = (isset($_POST['sortrank']) && $_POST['sortrank'] == '1') ? ' checked="checked"' : '';
echo '<label class="sort-toggle">';
echo '<input type="checkbox" name="sortrank" value="1"'.$sortChecked.' id="chk_sort"> Trier par rang ↓</label>';

echo '<input name="chercher" type="submit" class="btn-submit" value="Afficher" />';
echo '</form>';
echo '<button type="button" class="btn-print" onclick="window.print()" title="Imprimer">
        <img src="imprimante.png" height="28" width="36" alt="Imprimer" />
      </button>';
echo '</div>';   /* end toolbar */
echo '</div>';   /* end noprint wrapper */

/* ══════════════════════════════════════════════════════════════
   JavaScript — refresh subject list when class changes
══════════════════════════════════════════════════════════════ */
?>
<script>
(function () {
    var selClasse = document.getElementById('sel_classe');
    var selSub    = document.getElementById('sel_sub');
    var cycle     = <?php echo json_encode($cycle); ?>;

    if (!selClasse || !selSub) return;

    selClasse.addEventListener('change', function () {
        var raw     = this.value;                  /* "classID;CLASSNAME" */
        var classID = raw.split(';')[0];

        selSub.disabled = true;
        selSub.innerHTML = '<option>Chargement…</option>';

        var url = window.location.pathname + '?cycle=' + encodeURIComponent(cycle)
                + '&ajax_subs=1&classID=' + encodeURIComponent(classID);

        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onload = function () {
            if (xhr.status === 200) {
                selSub.innerHTML = xhr.responseText;
            } else {
                selSub.innerHTML = '<option>Erreur de chargement</option>';
            }
            selSub.disabled = false;
        };
        xhr.onerror = function () {
            selSub.innerHTML = '<option>Erreur réseau</option>';
            selSub.disabled = false;
        };
        xhr.send();
    });
})();
</script>

<?php
/* ═══════════════════════════════════════════════════════
   RESULTS
═══════════════════════════════════════════════════════ */
if (isset($_POST['chercher'])) {

    $cla1       = $_POST['cla'];
    $cellcla    = explode(";", $cla1);
    $classeID   = $cellcla[0];
    $classe     = $cellcla[1];

    $sub1         = $_POST['sub'];
    $cellsub      = explode(";", $sub1);
    $subSubjectID = $cellsub[0];
    $matiere      = $cellsub[1];

    $sortByRank = (isset($_POST['sortrank']) && $_POST['sortrank'] == '1');

    /* ── Report header ── */
    echo '<div class="report-header">';
    echo '  <span class="report-title">'.htmlspecialchars($classe).' &mdash; '.htmlspecialchars($matiere);
    if ($sortByRank) echo ' <span class="sorted-badge">&#x25BC; Trié par rang</span>';
    echo '  </span>';
    echo '  <span class="report-meta"><span>Date : '.date('d/m/Y').'</span></span>';
    echo '</div>';

    /* ── Fetch semesters that actually have grades for this class+subject ── */
    $sql = 'SELECT DISTINCT semesters.id, semesters.name
            FROM semesters
            JOIN classesSemesters ON semesters.id      = classesSemesters.semesterID
            JOIN grades           ON grades.semesterID = semesters.id
                                 AND grades.classID    = classesSemesters.classID
            WHERE classesSemesters.classID  = "'.$classeID.'"
              AND grades.subSubjectID       = "'.$subSubjectID.'"
            ORDER BY semesters.id ASC';
    if (!mysqli_query($link, $sql)) echo "ERROR: " . mysqli_error($link);
    $reqSem = mysqli_query($link, $sql);
    $sem    = []; $semid = []; $k = 0;
    while ($data = mysqli_fetch_array($reqSem)) {
        $sem[$k]   = $data[1];
        $semid[$k] = $data[0];
        $k++;
    }
    $nbSem = $k;

    /* ── Fetch student grades ── */
    $sql = 'SELECT * FROM classes
            LEFT JOIN students          ON students.classID          = classes.id
            LEFT JOIN grades            ON students.id               = grades.studentID
                                       AND students.classID          = grades.classID
            LEFT JOIN gradesSubjects    ON grades.subSubjectID       = gradesSubjects.subSubjectID
                                       AND grades.classID            = gradesSubjects.classID
            LEFT JOIN subSubjects       ON grades.subSubjectID       = subSubjects.id
            LEFT JOIN subjects          ON subSubjects.subjectID     = subjects.id
            LEFT JOIN classesSemesters  ON classesSemesters.classId  = classes.id
                                       AND grades.semesterID         = classesSemesters.semesterID
            LEFT JOIN semesters         ON semesters.id              = classesSemesters.semesterID
            LEFT JOIN users             ON users.id                  = students.userID
            WHERE classes.id         = "'.$classeID.'"
              AND grades.subSubjectID = "'.$subSubjectID.'"
            ORDER BY students.firstName, students.lastName, students.id,
                     semesters.id, subjects.id, subSubjects.id, subjects.ordering';

    if (!mysqli_query($link, $sql)) echo "ERROR: " . mysqli_error($link);
    $req = mysqli_query($link, $sql);

    $studentid  = []; $prenom = []; $famille = []; $pere = [];
    $grade = []; $discipcoef = []; $discipid = []; $discip = [];
    $matid = []; $mat = []; $ctrlid = []; $ctrl = [];
    $i = 0;
    while ($data = mysqli_fetch_array($req)) {
        $studentid[$i]  = $data[3];
        $prenom[$i]     = $data[5];
        $famille[$i]    = $data[6];
        $pere[$i]       = $data[33];
        $grade[$i]      = $data[14];
        $discipcoef[$i] = $data[17];
        $discipid[$i]   = $data[19];
        $discip[$i]     = $data[21];
        $matid[$i]      = $data[22];
        $mat[$i]        = $data[23];
        $ctrlid[$i]     = $data[29];
        $ctrl[$i]       = $data[30];
        $i++;
    }
    $totalRows = $i;

    /* ══════════════════════════════════════════════════════
       PASS 1 — group rows into per-student / per-semester buckets
    ══════════════════════════════════════════════════════ */
    $students     = [];
    $studentOrder = [];
    for ($i = 0; $i < $totalRows; $i++) {
        $sid = $studentid[$i];
        if (!isset($students[$sid])) {
            $students[$sid] = [
                'prenom'  => $prenom[$i],
                'famille' => $famille[$i],
                'pere'    => $pere[$i],
                'sems'    => []
            ];
            $studentOrder[] = $sid;
        }
        $thisSemID = $ctrlid[$i];   /* semesters.id for this row */
        if (!isset($students[$sid]['sems'][$thisSemID])) {
            $students[$sid]['sems'][$thisSemID] = ['sumGrade' => 0, 'sumCoef' => 0];
        }
        if ($grade[$i] != -1) {
            $students[$sid]['sems'][$thisSemID]['sumGrade'] += $grade[$i];
            $students[$sid]['sems'][$thisSemID]['sumCoef']  += $discipcoef[$i];
        }
    }

    /* ══════════════════════════════════════════════════════
       PASS 2 — compute final average per student
    ══════════════════════════════════════════════════════ */
    $classSumAvg = 0; $classCount = 0; $allAvg = [];

    foreach ($studentOrder as $sid) {
        $moyensumat = 0; $conter = 0;
        for ($s = 0; $s < $nbSem; $s++) {
            $thisSemID = $semid[$s];
            if (isset($students[$sid]['sems'][$thisSemID])) {
                $d = $students[$sid]['sems'][$thisSemID];
                if ($d['sumCoef'] > 0) {
                    $semMoy = round($d['sumGrade'] * 20 / $d['sumCoef'], 1);
                    if ($semMoy > 0) { $moyensumat += $semMoy; $conter++; }
                }
            }
        }
        $avg = ($conter > 0) ? round($moyensumat / $conter, 1) : 0;
        $students[$sid]['avg'] = $avg;
        if ($avg > 0) { $classSumAvg += $avg; $classCount++; $allAvg[] = $avg; }
    }

    /* ══════════════════════════════════════════════════════
       PASS 3 — sort if requested
    ══════════════════════════════════════════════════════ */
    if ($sortByRank) {
        usort($studentOrder, function($a, $b) use ($students) {
            return $students[$b]['avg'] <=> $students[$a]['avg'];
        });
    }

    /* ══════════════════════════════════════════════════════
       RENDER TABLE
    ══════════════════════════════════════════════════════ */
    echo '<div class="table-wrapper">';
    echo '<table class="stat-table">';

    /* thead */
    echo '<thead><tr>';
    echo '<th style="text-align:left;">Élève</th>';
    for ($s = 0; $s < $nbSem; $s++) {
        echo '<th>'.htmlspecialchars($sem[$s]).'</th>';
    }
    echo '<th class="avg-col">Moyenne</th>';
    echo '</tr></thead>';

    /* tbody */
    echo '<tbody>';
    $num = 1; $donn = []; $texte = []; $num1 = 0;

    foreach ($studentOrder as $sid) {
        $stu = $students[$sid];
        $rankClass = ($num == 1) ? 'rank-gold' : (($num == 2) ? 'rank-silver' : (($num == 3) ? 'rank-bronze' : ''));
        echo '<tr>';
        echo '<td><span class="rank '.$rankClass.'">'.$num.'</span>'.
             htmlspecialchars($stu['prenom']).' '.
             htmlspecialchars($stu['pere']).' '.
             htmlspecialchars($stu['famille']).'</td>';
        $num++;

        /* Per-semester cells */
        for ($s = 0; $s < $nbSem; $s++) {
            $thisSemID = $semid[$s];
            echo '<td>';
            if (isset($stu['sems'][$thisSemID]) && $stu['sems'][$thisSemID]['sumCoef'] > 0) {
                $d      = $stu['sems'][$thisSemID];
                $semMoy = round($d['sumGrade'] * 20 / $d['sumCoef'], 1);
                if ($semMoy > 0) {
                    $cls = ($semMoy >= 14) ? 'grade-high' : (($semMoy >= 10) ? 'grade-mid' : 'grade-low');
                    echo '<b class="'.$cls.'">'.$semMoy.'</b>';
                } else {
                    echo '<span class="grade-empty">—</span>';
                }
            } else {
                echo '<span class="grade-empty">—</span>';
            }
            echo '</td>';
        }

        /* Final average cell */
        $avg = $stu['avg'];
        echo '<td class="avg-col">';
        if ($avg > 0) {
            $cls = ($avg >= 14) ? 'grade-high' : (($avg >= 10) ? 'grade-mid' : 'grade-low');
            echo '<span class="'.$cls.'">'.$avg.'</span>';
        } else {
            echo '<span class="grade-empty">—</span>';
        }
        echo '</td>';
        echo '</tr>';

        $texte[$num1] = $stu['prenom'].' '.$stu['famille'];
        $donn[$num1]  = $avg;
        $num1++;
    }
    echo '</tbody>';

    /* tfoot */
    $classAvg = ($classCount > 0) ? round($classSumAvg / $classCount, 2) : 0;
    $minAvg   = !empty($allAvg) ? min($allAvg) : 0;
    $maxAvg   = !empty($allAvg) ? max($allAvg) : 0;

    echo '<tfoot>';
    echo '<tr>';
    echo '<td>Moyenne de la classe ('.$classCount.' élève'.($classCount > 1 ? 's' : '').')</td>';
    for ($s = 0; $s < $nbSem; $s++) echo '<td>—</td>';
    echo '<td class="avg-col">'.$classAvg.'</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td style="color:#a0b4cc;">Min / Max</td>';
    for ($s = 0; $s < $nbSem; $s++) echo '<td style="color:#a0b4cc;">—</td>';
    echo '<td style="color:#a0b4cc; font-size:12px;">'.$minAvg.' / '.$maxAvg.'</td>';
    echo '</tr>';
    echo '</tfoot>';

    echo '</table>';
    echo '</div>'; /* end table-wrapper */

    /* Histogram */
    $titre = "Statistiques Des Moyennes Finals &mdash; ".htmlspecialchars($classe)." &mdash; ".htmlspecialchars($matiere);
    echo '<div class="graph-panel noprint">';
    echo '  <div class="graph-panel-header">';
    echo '    <span>Distribution des moyennes</span>';
    echo '    <button type="button" class="btn-close-graph" onclick="this.closest(\'.graph-panel\').style.display=\'none\'">✕ Fermer</button>';
    echo '  </div>';
    echo '  <div class="graph-panel-body">';
    include("histogrammenotes.php");
    histo(1400, 450, $donn, $texte, $titre, $classAvg);
    echo '  </div>';
    echo '</div>';
}
?>
</body>
</html>
