Fix query

This commit is contained in:
jay-tux 2025-04-29 13:36:09 +02:00
parent 88449e9580
commit 6928da272f
Signed by: jay-tux
GPG Key ID: 84302006B056926E
1 changed files with 30 additions and 31 deletions

View File

@ -729,48 +729,42 @@ class PeerEvaluationState(val evaluation: PeerEvaluation) {
val contents = RawDbState { loadContents() } val contents = RawDbState { loadContents() }
private fun Transaction.loadContents(): List<GroupEntry> { private fun Transaction.loadContents(): List<GroupEntry> {
val found = (Groups leftJoin PeerEvaluationContents).selectAll().where { return evaluation.edition.groups.map { group ->
(Groups.editionId eq evaluation.edition.id) and val globalNotes = PeerEvaluationContents.selectAll()
(PeerEvaluationContents.peerEvaluationId eq evaluation.id) .where { PeerEvaluationContents.groupId eq group.id }.firstOrNull()?.let {
}.associate { gc -> it[PeerEvaluationContents.content]
val group = Group[gc[Groups.id]]
val content = gc[PeerEvaluationContents.content] ?: ""
val students = group.students.map { student1 ->
val others = group.students.map { student2 ->
val eval = StudentToStudentEvaluation.selectAll().where {
StudentToStudentEvaluation.peerEvaluationId eq evaluation.id and
(StudentToStudentEvaluation.studentIdFrom eq student1.id) and
(StudentToStudentEvaluation.studentIdTo eq student2.id)
}.firstOrNull()
student2 to eval?.let {
Student2StudentEntry(
it[StudentToStudentEvaluation.grade], it[StudentToStudentEvaluation.note]
)
} }
}.sortedBy { it.first.name }
val global = StudentToGroupEvaluation.selectAll().where { val students = group.students.map { from ->
StudentToGroupEvaluation.peerEvaluationId eq evaluation.id and val s2g = StudentToGroupEvaluation.selectAll().where {
(StudentToGroupEvaluation.studentId eq student1.id) (StudentToGroupEvaluation.peerEvaluationId eq evaluation.id) and
(StudentToGroupEvaluation.studentId eq from.id)
}.firstOrNull()?.let { }.firstOrNull()?.let {
Student2StudentEntry(it[StudentToGroupEvaluation.grade], it[StudentToGroupEvaluation.note]) Student2StudentEntry(it[StudentToGroupEvaluation.grade], it[StudentToGroupEvaluation.note])
} }
StudentEntry(student1, global, others) val others = group.students.map { other ->
}.sortedBy { it.student.name } // enforce synchronized order val eval = StudentToStudentEvaluation.selectAll().where {
(StudentToStudentEvaluation.peerEvaluationId eq evaluation.id) and
group to GroupEntry(group, content, students) (StudentToStudentEvaluation.studentIdFrom eq from.id) and
(StudentToStudentEvaluation.studentIdTo eq other.id)
}.firstOrNull()?.let {
Student2StudentEntry(it[StudentToStudentEvaluation.grade], it[StudentToStudentEvaluation.note])
} }
return editionCourse.second.groups.map { other to eval
found[it] ?: GroupEntry( }
it, "",
it.students.map { s1 -> StudentEntry(s1, null, it.students.map { s2 -> s2 to null }) } StudentEntry(from, s2g, others)
) }
GroupEntry(group, globalNotes ?: "", students)
} }
} }
fun upsertGroupFeedback(group: Group, feedback: String) { fun upsertGroupFeedback(group: Group, feedback: String) {
transaction { transaction {
println("Upserting group-level notes for ${group.name} (${evaluation.name})")
PeerEvaluationContents.upsert { PeerEvaluationContents.upsert {
it[peerEvaluationId] = evaluation.id it[peerEvaluationId] = evaluation.id
it[groupId] = group.id it[groupId] = group.id
@ -783,6 +777,7 @@ class PeerEvaluationState(val evaluation: PeerEvaluation) {
fun upsertIndividualFeedback(from: Student, to: Student?, grade: String, feedback: String) { fun upsertIndividualFeedback(from: Student, to: Student?, grade: String, feedback: String) {
transaction { transaction {
to?.let { to?.let {
println("Upserting individual feedback for ${from.name} -> ${to.name} (${evaluation.name})")
StudentToStudentEvaluation.upsert { StudentToStudentEvaluation.upsert {
it[peerEvaluationId] = evaluation.id it[peerEvaluationId] = evaluation.id
it[studentIdFrom] = from.id it[studentIdFrom] = from.id
@ -796,6 +791,10 @@ class PeerEvaluationState(val evaluation: PeerEvaluation) {
it[this.grade] = grade it[this.grade] = grade
it[this.note] = feedback it[this.note] = feedback
} }
if(to == null) {
println("Upserting group-level feedback for ${from.name} ${from.id.value} (${evaluation.name} ${evaluation.id.value})")
}
} }
contents.refresh() contents.refresh()
} }