Skip to content

Commit dea6bd9

Browse files
committed
feat: validate student regs
1 parent cde3ae1 commit dea6bd9

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

cmd/validate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
conflicts --- check conflicts for each student
2020
constraints --- check if constraints hold
2121
preplanned-exahm-rooms --- validate exahm pre-planned rooms
22+
studentregs --- check for students with registrations in diffenrent programs
2223
db --- data base entries
2324
rooms --- check room constraints
2425
zpa --- check if the plan on ZPA is the same here
@@ -51,6 +52,9 @@ var (
5152
case "constraints":
5253
validations = append(validations, plexams.ValidateConstraints)
5354

55+
case "studentregs":
56+
validations = append(validations, plexams.ValidateStudentRegs)
57+
5458
case "db":
5559
validations = append(validations, plexams.ValidateDB)
5660

plexams/ics.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func (p *Plexams) ExportICS(program string, filename string) error {
3939
return err
4040
}
4141
vevent.SetStartAt(*starttime)
42-
vevent.SetDuration(time.Duration(exam.ZpaExam.Duration) * time.Minute)
42+
err = vevent.SetDuration(time.Duration(exam.ZpaExam.Duration) * time.Minute)
43+
if err != nil {
44+
return err
45+
}
4346
}
4447

4548
file, err := os.Create(filename)

plexams/validate_studentregs.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package plexams
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
set "github.com/deckarep/golang-set/v2"
10+
"github.com/logrusorgru/aurora"
11+
"github.com/rs/zerolog/log"
12+
"github.com/theckman/yacspin"
13+
)
14+
15+
// TODO: all planned_rooms okay? especially after moving an exam? check room -> slot -> ancode sameslot?
16+
func (p *Plexams) ValidateStudentRegs() error {
17+
ctx := context.Background()
18+
cfg := yacspin.Config{
19+
Frequency: 100 * time.Millisecond,
20+
CharSet: yacspin.CharSets[69],
21+
Suffix: aurora.Sprintf(aurora.Cyan(" validating student regs")),
22+
SuffixAutoColon: true,
23+
StopCharacter: "✓",
24+
StopColors: []string{"fgGreen"},
25+
StopFailMessage: "error",
26+
StopFailCharacter: "✗",
27+
StopFailColors: []string{"fgRed"},
28+
}
29+
30+
spinner, err := yacspin.New(cfg)
31+
if err != nil {
32+
log.Debug().Err(err).Msg("cannot create spinner")
33+
}
34+
err = spinner.Start()
35+
if err != nil {
36+
log.Debug().Err(err).Msg("cannot start spinner")
37+
}
38+
39+
validationMessages := make([]string, 0)
40+
41+
studentRegs, err := p.dbClient.StudentRegsPerStudentPlanned(ctx)
42+
if err != nil {
43+
log.Error().Err(err).Msg("cannot get student regs")
44+
}
45+
46+
spinner.Message(aurora.Sprintf(aurora.Yellow(" validating only regs from one program per student")))
47+
for _, studentReg := range studentRegs {
48+
programs := set.NewSet[string]()
49+
for _, reg := range studentReg.RegsWithProgram {
50+
programs.Add(reg.Program)
51+
}
52+
if programs.Cardinality() > 1 {
53+
var sb strings.Builder
54+
for _, reg := range studentReg.RegsWithProgram {
55+
zpaExam, err := p.dbClient.GetZpaExamByAncode(ctx, reg.Reg)
56+
if err != nil {
57+
log.Error().Err(err).Int("ancode", reg.Reg).
58+
Msg("cannot get zpa exam for student reg")
59+
continue
60+
}
61+
sb.WriteString(fmt.Sprintf("%s/%d: %s (%s)\n", reg.Program, zpaExam.AnCode, zpaExam.Module, zpaExam.MainExamer))
62+
}
63+
64+
validationMessages = append(validationMessages, aurora.Sprintf(
65+
aurora.Red("regs from more than one program for student %s (%s/%s): %v\n%s"),
66+
aurora.Magenta(studentReg.Name),
67+
aurora.Cyan(studentReg.Program), aurora.Cyan(studentReg.Mtknr),
68+
aurora.Yellow(programs.ToSlice()),
69+
aurora.Yellow(sb.String()),
70+
))
71+
}
72+
}
73+
74+
if len(validationMessages) > 0 {
75+
spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems"),
76+
len(validationMessages)))
77+
err = spinner.StopFail()
78+
if err != nil {
79+
log.Debug().Err(err).Msg("cannot stop spinner")
80+
}
81+
for _, msg := range validationMessages {
82+
fmt.Printf("%s\n", msg)
83+
}
84+
85+
} else {
86+
spinner.StopMessage(aurora.Sprintf(aurora.Green("%d student registrations are okay"),
87+
len(studentRegs)))
88+
err = spinner.Stop()
89+
if err != nil {
90+
log.Debug().Err(err).Msg("cannot stop spinner")
91+
}
92+
}
93+
94+
return nil
95+
}

0 commit comments

Comments
 (0)