getStudents method

Future<List<StudentDto>> getStudents(
  1. ISchoolCourseDto course
)

Fetches the list of students enrolled in the specified course.

Returns student information (ID and name) for all students enrolled in the given course.

The course should be obtained from getCourseList.

System accounts (e.g., "istudyoaa") are automatically filtered out.

Throws an Exception if no student data exists.

Implementation

Future<List<StudentDto>> getStudents(ISchoolCourseDto course) async {
  await _selectCourse(course);

  final response = await _iSchoolPlusDio.get('learn_ranking.php');

  // Parse the HTML and extract the table of student rankings
  final document = parse(response.data);
  final studyRankingsTable = document.querySelector('.content>.data2 tbody');
  if (studyRankingsTable == null) {
    throw Exception(
      'No student data found for course ${course.courseNumber}.',
    );
  }

  // Extract second column from each row for student ID and name
  // Example cell: "111360109 (何承軒)"
  final students = studyRankingsTable.children
      .map((row) => row.children[1].children.first.text)
      .toList();
  if (students.isEmpty) {
    throw Exception('No students found for course ${course.courseNumber}.');
  }

  return students
      .map((student) {
        final parts = student.split(' (');
        final id = parts[0];
        final name = parts[1].replaceAll(')', '').trim();

        return (
          id: id.isEmpty ? null : id,
          name: name.isEmpty ? null : name,
        );
      })
      .where(
        (student) => student.id != 'istudyoaa', // Filter out system account
      )
      .toList();
}