exportResult static method

List<int> exportResult(
  1. List<Student> students,
  2. List<School> schools
)

Implementation

static List<int> exportResult(List<Student> students, List<School> schools) {
  Map<int, String> indexes = {
    0: "Nom",
    1: "Voeu",
    2: "Pays",
    3: "Etablissement",
    4: "Département",
    5: "Interclassement",
    6: "Nb ECTS",
    7: "Niveau Anglais",
    8: "Absences",
    9: "Nbre de Places",
    10: "Commentaires",
    11: "Commentaires post-jury"
  };

  // Create separate lists for different sheets
  List<(int, int, Choice)> allChoices = [];
  List<(int, int, Choice)> acceptedChoices = [];
  List<(int, int, Choice)> rejectedChoices = [];
  List<(int, int, Choice)> noResponseChoices = [];

  // First pass - identify students with responses and collect all choices
  Map<int, bool> studentHasResponse = {};
  Map<int, bool> studentAllChoicesRejected = {}; // New map to track fully rejected students

  for (var s in students) {
    // Initialize tracking variables
    studentHasResponse[s.id] = false;
    studentAllChoicesRejected[s.id] = true; // Assume all choices rejected until proven otherwise

    for (var c in s.choices.entries) {
      // Add to all choices list
      allChoices.add((s.id, c.key, c.value));

      // Check acceptance status
      if (s.accepted != null && s.accepted!.school.id == c.value.school.id) {
        acceptedChoices.add((s.id, c.key, c.value));
        studentHasResponse[s.id] = true;
        studentAllChoicesRejected[s.id] = false; // At least one choice is accepted
      }
      // Check rejection status
      else if (s.refused.any((choice) => choice.school.id == c.value.school.id)) {
        rejectedChoices.add((s.id, c.key, c.value));
        studentHasResponse[s.id] = true;
        // Note: We don't set studentAllChoicesRejected to false here
        // because we're checking if ALL choices are rejected
      } else {
        studentAllChoicesRejected[s.id] = false; // Found a choice that's not rejected
      }
    }

    // If the student has all choices rejected but has choices,
    // confirm it by checking the get_second_tour() method
    if ((studentAllChoicesRejected[s.id] ?? false) && s.choices.isNotEmpty) {
      studentAllChoicesRejected[s.id] = s.get_second_tour();
    }
  }

  // Second pass - add students to noResponseChoices (now includes second tour students)
  for (var s in students) {
    if ((studentHasResponse[s.id] == false && s.choices.isNotEmpty) ||
        (studentAllChoicesRejected[s.id] == true && s.choices.isNotEmpty)) {
      // This student has NO responses OR all choices rejected - add ALL their choices
      for (var c in s.choices.entries) {
        noResponseChoices.add((s.id, c.key, c.value));
      }
    }
  }

  // Sort all lists by interranking
  allChoices.sort((a, b) => b.$3.interranking.compareTo(a.$3.interranking));
  acceptedChoices.sort((a, b) => b.$3.interranking.compareTo(a.$3.interranking));
  rejectedChoices.sort((a, b) => b.$3.interranking.compareTo(a.$3.interranking));
  noResponseChoices.sort((a, b) => b.$3.interranking.compareTo(a.$3.interranking));

  // Create Excel workbook
  Excel exportedExcel = Excel.createExcel();

  // Get default sheet name
  String defaultSheetName = exportedExcel.getDefaultSheet() ?? "Sheet1";

  // Create additional sheets by copying the default one
  exportedExcel.copy(defaultSheetName, "Choix acceptés");
  exportedExcel.copy(defaultSheetName, "Choix refusés");
  exportedExcel.copy(defaultSheetName, "Second Tour"); // Changed name

  // Rename default sheet and get sheet references
  exportedExcel.rename(defaultSheetName, "Tous les choix");
  Sheet allSheet = exportedExcel.sheets["Tous les choix"]!;
  Sheet acceptedSheet = exportedExcel.sheets["Choix acceptés"]!;
  Sheet rejectedSheet = exportedExcel.sheets["Choix refusés"]!;
  Sheet secondTourSheet = exportedExcel.sheets["Second Tour"]!; // Reference to new sheet

  // Clear copied content
  //acceptedSheet.clear();
  //rejectedSheet.clear();
  //noResponseSheet.clear();

  // Add headers to each sheet
  setupSheetHeaders(allSheet, indexes);
  setupSheetHeaders(acceptedSheet, indexes);
  setupSheetHeaders(rejectedSheet, indexes);
  setupSheetHeaders(secondTourSheet, indexes); // Add headers to new sheet

  // Fill each sheet with data
  fillSheetWithChoices(allSheet, allChoices, students, true);
  fillSheetWithChoices(acceptedSheet, acceptedChoices, students, false);
  fillSheetWithChoices(rejectedSheet, rejectedChoices, students, false);
  fillSheetWithChoices(secondTourSheet, noResponseChoices, students, false); // Fill new sheet

  return exportedExcel.save() ?? [];
}