getRegistrationRecords method

Future<List<RegistrationRecordDto>> getRegistrationRecords()

Fetches registration records including class, mentors, and cadre roles.

Parses the registration table containing historical enrollment status and assigned tutors for each semester.

Implementation

Future<List<RegistrationRecordDto>> getRegistrationRecords() async {
  final response = await _studentQueryDio.get('QryRegist.jsp');
  final document = parse(response.data);

  final table = document.querySelector('table');
  if (table == null) return [];

  final semesterPattern = RegExp(r'(\d+)\s*-\s*(\d+)');

  final results = <RegistrationRecordDto>[];
  for (final row in table.querySelectorAll('tr').skip(1)) {
    final cells = row.querySelectorAll('th, td');
    if (cells.length < 7) continue;

    final semesterContainer = cells[0].querySelector('div') ?? cells[0];
    final semesterText = semesterContainer.nodes
        .where((node) => node.nodeType == Node.TEXT_NODE)
        .firstOrNull
        ?.text;
    final semesterMatch = semesterPattern.firstMatch(semesterText ?? '');
    if (semesterMatch == null) continue;

    final semester = (
      year: int.parse(semesterMatch.group(1)!),
      term: int.parse(semesterMatch.group(2)!),
    );
    final className = _parseCellText(cells[1]);
    final enrollmentStatus = _parseEnrollmentStatus(_parseCellText(cells[2]));
    final registered = cells[3].text.contains('※');
    final graduated = cells[4].text.contains('※');

    final tutors = cells[5].querySelectorAll('a').map((a) {
      final href = Uri.tryParse(a.attributes['href'] ?? '');
      final id = href?.queryParameters['code'];
      return (id: id, name: _parseCellText(a));
    }).toList();

    final cadreContainer = cells[6].querySelector('div') ?? cells[6];
    final classCadres = cadreContainer.nodes
        .where((node) => node.nodeType == Node.TEXT_NODE)
        .map((node) => node.text?.trim() ?? '')
        .where((text) => text.isNotEmpty)
        .toList();

    results.add((
      semester: semester,
      className: className,
      enrollmentStatus: enrollmentStatus,
      registered: registered,
      graduated: graduated,
      tutors: tutors,
      classCadres: classCadres,
    ));
  }

  return results;
}