extractAssignment function

Future<Assignment> extractAssignment(
  1. BeautifulSoup soup,
  2. AssignmentRef ref,
  3. Student student
)

Implementation

Future<Assignment> extractAssignment(
    BeautifulSoup soup, AssignmentRef ref, Student student) async {
  Bs4Element? weirdElement = soup.find('*', id: 'm_Content_ExerciseFilePnl');
  List<File> testFiles = [];
  List<Bs4Element> infoTableRows = soup
      .find('*', id: 'm_Content_registerAfl_pa')!
      .children[weirdElement != null ? 1 : 0]
      .children[0]
      .children;
  String title = soup.find('*', id: 'm_Content_NameLbl')!.text;
  String testFilesText = infoTableRows[1].children[0].text;
  bool hasTestFiles = testFilesText.startsWith("Opgavebeskrivelse");
  if (hasTestFiles) {
    var entries = infoTableRows[1].children[1].children;
    for (var entry in entries) {
      if (entry.name == "a") {
        testFiles
            .add(File(href: entry.getAttrValue("href")!, name: entry.text));
      }
    }
  }
  String note = infoTableRows[hasTestFiles ? 2 : 1].children[1].text;

  Bs4Element teamElement =
      infoTableRows[hasTestFiles ? 3 : 2].children[1].children[0];
  var teamId = teamElement.getAttrValue("data-lectiocontextcard")!;
  var teamContext = (await student.context.get(teamId)) as TeamContext;
  Team team = Team(
      name: teamElement.text, id: teamId, displayName: teamContext.subject);
  String grading = soup.find('*', id: 'm_Content_gradeScaleIdLbl')!.text;
  Bs4Element responsibleElement =
      infoTableRows[hasTestFiles ? 5 : 4].children[1].children[0];
  MetaDataEntry responsible = MetaDataEntry(
      name: responsibleElement.text,
      id: responsibleElement.getAttrValue("data-lectiocontextcard")!);
  double hours = double.parse(soup
      .find('*', id: 'm_Content_WeightLbl')!
      .text
      .split(" ")
      .elementAt(0)
      .replaceAll(",", "."));
  DateTime deadline = deadlineFormat
      .parse(infoTableRows[hasTestFiles ? 7 : 6].children[1].text);
  List<Bs4Element> entryRows =
      soup.find('*', id: 'm_Content_RecipientGV')!.children[0].children;
  entryRows.removeAt(0);
  List<AssignmentEntry> entries = [];
  for (var row in entryRows) {
    var extracted = extractAssignmentEntry(row);
    if (extracted != null) {
      entries.add(extracted);
    }
  }
  String grade = "";
  double absence = 0.0;
  String gradeNote = "";
  List<Bs4Element> deliveredBy = soup
          .find('table', id: 'm_Content_StudentGV')
          ?.children
          .elementAtOrNull(0)
          ?.children ??
      [];
  if (deliveredBy.length >= 2) {
    Bs4Element studentRow = deliveredBy.elementAt(1);

    Bs4Element? absenceElement = studentRow.children.elementAtOrNull(3);
    Bs4Element? gradeElement = studentRow.children.elementAtOrNull(5);
    Bs4Element? gradeNoteElement = studentRow.children.elementAtOrNull(6);
    grade = gradeElement?.text.trim() ?? "";
    gradeNote = gradeNoteElement?.text.trim() ?? "";
    String? rightSideAbsence =
        absenceElement?.text.split("/").elementAtOrNull(1);
    String? percentageString =
        rightSideAbsence?.split(":").last.replaceAll("%", "");
    absence = double.parse(percentageString ?? "100");
  }

  return Assignment(
      id: ref.id,
      title: title,
      note: note,
      team: team,
      grading: grading,
      responsible: responsible,
      hours: hours,
      deadline: deadline,
      entries: entries,
      testFiles: testFiles,
      grade: grade,
      gradeNote: gradeNote,
      absence: absence);
}