|
| 1 | +package plexams |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/obcode/plexams.go/graph/model" |
| 9 | + "github.com/rs/zerolog/log" |
| 10 | +) |
| 11 | + |
| 12 | +type ExportNtas struct { |
| 13 | + Name string `json:"name"` |
| 14 | + Duration int `json:"duration"` |
| 15 | +} |
| 16 | + |
| 17 | +type ExportPlannedRooms struct { |
| 18 | + MainExamer string `json:"mainExamer"` |
| 19 | + Module string `json:"module"` |
| 20 | + Room string `json:"room"` |
| 21 | + Date string `json:"date"` |
| 22 | + Starttime string `json:"starttime"` |
| 23 | + NumberOfStudents int `json:"numberOfStudents"` |
| 24 | + Duration int `json:"duration"` |
| 25 | + MaxDuration int `json:"maxDuration"` |
| 26 | + Invigilator string `json:"invigilator"` |
| 27 | + Ntas []ExportNtas `json:"ntas"` |
| 28 | +} |
| 29 | + |
| 30 | +func (p *Plexams) ExportPlannedRooms(jsonfile string) error { |
| 31 | + ctx := context.Background() |
| 32 | + plannedExams, err := p.PlannedExams(ctx) |
| 33 | + if err != nil { |
| 34 | + log.Error().Err(err).Msg("cannot get planned exams") |
| 35 | + } |
| 36 | + |
| 37 | + exportPlannedRooms := make([]*ExportPlannedRooms, 0) |
| 38 | + for _, exam := range plannedExams { |
| 39 | + if exam.Constraints != nil && exam.Constraints.NotPlannedByMe { |
| 40 | + continue |
| 41 | + } |
| 42 | + starttime := p.getSlotTime(exam.PlanEntry.DayNumber, exam.PlanEntry.SlotNumber) |
| 43 | + |
| 44 | + plannedRoomsMap := make(map[string][]*model.PlannedRoom) |
| 45 | + for _, plannedRoom := range exam.PlannedRooms { |
| 46 | + if _, ok := plannedRoomsMap[plannedRoom.RoomName]; !ok { |
| 47 | + plannedRoomsMap[plannedRoom.RoomName] = make([]*model.PlannedRoom, 0) |
| 48 | + } |
| 49 | + plannedRoomsMap[plannedRoom.RoomName] = append(plannedRoomsMap[plannedRoom.RoomName], plannedRoom) |
| 50 | + } |
| 51 | + |
| 52 | + for roomName, plannedRooms := range plannedRoomsMap { |
| 53 | + invigilator, err := p.GetInvigilatorInSlot(ctx, roomName, exam.PlanEntry.DayNumber, exam.PlanEntry.SlotNumber) |
| 54 | + if err != nil { |
| 55 | + log.Error().Err(err).Int("ancode", exam.Ancode).Str("room", roomName). |
| 56 | + Msg("cannot get invigilator for room") |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + numberOfStudents := 0 |
| 61 | + maxDuration := 0 |
| 62 | + ntas := make([]ExportNtas, 0) |
| 63 | + |
| 64 | + for _, plannedRoom := range plannedRooms { |
| 65 | + numberOfStudents += len(plannedRoom.StudentsInRoom) |
| 66 | + if plannedRoom.Duration > maxDuration { |
| 67 | + maxDuration = plannedRoom.Duration |
| 68 | + } |
| 69 | + if plannedRoom.NtaMtknr != nil && *plannedRoom.NtaMtknr != "" { |
| 70 | + nta, err := p.NtaByMtknr(ctx, *plannedRoom.NtaMtknr) |
| 71 | + if err != nil { |
| 72 | + log.Error().Err(err).Str("mtknr", *plannedRoom.NtaMtknr).Msg("cannot get nta") |
| 73 | + return err |
| 74 | + } |
| 75 | + ntas = append(ntas, ExportNtas{ |
| 76 | + Name: nta.Name, |
| 77 | + Duration: plannedRoom.Duration, |
| 78 | + }) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + exportPlannedRooms = append(exportPlannedRooms, &ExportPlannedRooms{ |
| 83 | + MainExamer: exam.ZpaExam.MainExamer, |
| 84 | + Module: exam.ZpaExam.Module, |
| 85 | + Room: roomName, |
| 86 | + Date: starttime.Local().Format("02.01.2006"), |
| 87 | + Starttime: starttime.Local().Format("15:04"), |
| 88 | + NumberOfStudents: numberOfStudents, |
| 89 | + Duration: exam.ZpaExam.Duration, |
| 90 | + MaxDuration: maxDuration, |
| 91 | + Invigilator: invigilator.Shortname, |
| 92 | + Ntas: ntas, |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + file, err := os.Create(jsonfile) |
| 98 | + if err != nil { |
| 99 | + log.Error().Err(err).Msg("cannot create JSON file") |
| 100 | + return err |
| 101 | + } |
| 102 | + defer file.Close() |
| 103 | + |
| 104 | + encoder := json.NewEncoder(file) |
| 105 | + encoder.SetIndent("", " ") |
| 106 | + if err := encoder.Encode(exportPlannedRooms); err != nil { |
| 107 | + log.Error().Err(err).Msg("cannot encode JSON") |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
0 commit comments