getPortfolio method

Future<Portfolio> getPortfolio()

Returns a list of User's registered courses along with their profile tools.

Implementation

Future<Portfolio> getPortfolio() async {
  List<Course> courses = [];
  List<Tool> tools = [];
  List<Tool> profileTools = [];
  Course course;
  Tool tool;
  Iterable<XmlElement> nodes;
  final response = await _client.post(
    Uri.https(
      'eclass.$instituteId.gr',
      '/modules/mobile/mportfolio.php',
    ),
    body: {
      'token': _token ?? '',
    },
  );
  if (response.statusCode != 200 || response.body == 'EXPIRED') {
    throw Exception("Failed to get portfolio's courses and tools.");
  }
  final decodedResponse = XmlDocument.parse(response.body);
  nodes = decodedResponse.findAllElements('course');
  for (final node in nodes) {
    course = Course(
      code: node.attributes[0].value,
      title: node.attributes[1].value,
      description: node.attributes[2].value,
    );
    courses.add(course);
  }
  nodes = decodedResponse.findAllElements('tool');
  for (final node in nodes) {
    tool = Tool(
      name: node.attributes[0].value,
      link: node.attributes[1].value,
      redirect: node.attributes[2].value,
      type: node.attributes[3].value,
      active: node.attributes[4].value,
    );
    if (node.attributes[3].value == "coursesubscribe") {
      tools.add(tool);
    } else {
      profileTools.add(tool);
    }
  }
  return Portfolio(
    courses: courses,
    tools: tools,
    profileTools: profileTools,
  );
}