getCourseList method

Future<List<ISchoolCourseDto>> getCourseList()

Fetches the list of courses available on iSchool+ for the current user.

Returns course references that can be passed to getStudents, getMaterials, and getMaterial. Not all courses from the course system will be present — internships and newly added courses may not appear until they are set up on I-School Plus.

The returned list preserves the order from the I-School Plus sidebar.

Implementation

Future<List<ISchoolCourseDto>> getCourseList() async {
  final response = await _iSchoolPlusDio.get('mooc_sysbar.php');

  final document = parse(response.data);
  final courseSelect = document.getElementById('selcourse');
  if (courseSelect == null) return [];

  // Options may be inside <optgroup> elements, so use querySelectorAll.
  // Example option: <option value="10099386">1141_智慧財產權_352902</option>
  final options = courseSelect.querySelectorAll('option');

  final courses = <ISchoolCourseDto>[];
  for (final option in options) {
    final internalId = option.attributes['value'];
    if (internalId == null || internalId.isEmpty) continue;

    // Extract course number from the end of the option text
    final text = option.text;
    final underscoreIdx = text.lastIndexOf('_');
    if (underscoreIdx == -1) continue;
    final courseNumber = text.substring(underscoreIdx + 1).trim();
    if (courseNumber.isEmpty) continue;

    courses.add((courseNumber: courseNumber, internalId: internalId));
  }

  return courses;
}