Commit 23c1bb9e authored by Yechang's avatar Yechang
Browse files

feat: create grade record

parent fea29a7c
Loading
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
/*
  Warnings:

  - A unique constraint covering the columns `[gradeGroupId]` on the table `Problem` will be added. If there are existing duplicate values, this will fail.

*/
-- AlterTable
ALTER TABLE `Problem` ADD COLUMN `gradeGroupId` INTEGER NULL;

-- CreateIndex
CREATE UNIQUE INDEX `Problem_gradeGroupId_key` ON `Problem`(`gradeGroupId`);

-- AddForeignKey
ALTER TABLE `Problem` ADD CONSTRAINT `Problem_gradeGroupId_fkey` FOREIGN KEY (`gradeGroupId`) REFERENCES `GradeGroup`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
+21 −0
Original line number Diff line number Diff line
/*
  Warnings:

  - You are about to drop the column `feedbackId` on the `Task` table. All the data in the column will be lost.
  - You are about to drop the `TaskLog` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE `Task` DROP FOREIGN KEY `Task_feedbackId_fkey`;

-- DropForeignKey
ALTER TABLE `TaskLog` DROP FOREIGN KEY `TaskLog_taskId_fkey`;

-- AlterTable
ALTER TABLE `Task` DROP COLUMN `feedbackId`;

-- DropTable
DROP TABLE `TaskLog`;

-- AddForeignKey
ALTER TABLE `Task` ADD CONSTRAINT `Task_submissionId_fkey` FOREIGN KEY (`submissionId`) REFERENCES `Submission`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
+7 −15
Original line number Diff line number Diff line
@@ -187,6 +187,9 @@ model Problem {
  penaltyAfter Int?

  submissions Submission[]

  gradeGroup   GradeGroup? @relation(fields: [gradeGroupId], references: [id])
  gradeGroupId Int?        @unique
}

model Submission {
@@ -205,6 +208,7 @@ model Submission {

  // feedback
  feedback SubmissionFeedback?
  tasks    Task[]
}

model SubmissionFeedback {
@@ -213,8 +217,6 @@ model SubmissionFeedback {
  updatedAt  DateTime  @updatedAt
  releasedAt DateTime?

  tasks Task[]

  isValid Boolean @default(true)

  scorer  User? @relation(fields: [scoreId], references: [id])
@@ -255,6 +257,8 @@ model GradeGroup {

  entry   GradeEntry @relation(fields: [entryId], references: [id])
  entryId Int

  problem Problem?
}

enum GradeGroupStrategy {
@@ -297,20 +301,8 @@ model Task {
  // output
  result String? @db.Text

  feedback     SubmissionFeedback? @relation(fields: [feedbackId], references: [id])
  feedbackId   Int?
  submission   Submission @relation(fields: [submissionId], references: [id])
  submissionId Int
  TaskLog      TaskLog[]
}

model TaskLog {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())

  data String @db.Text

  task   Task @relation(fields: [taskId], references: [id])
  taskId Int
}

enum TaskStatus {
+5 −1
Original line number Diff line number Diff line
import { dev } from "$app/environment";
import { createLogger } from "bunyan"

export const log = createLogger({ name: 'lms' });
export const log = createLogger({
    name: 'lms',
    level: dev ? "debug" : "info"
});
+41 −44
Original line number Diff line number Diff line
@@ -75,7 +75,10 @@ export const PUT = async ({ request, params }) => {
        error(404)
    }

    const task = await db.task.findUnique({ where: { id: taskId } })
    const task = await db.task.findUnique({
        where: { id: taskId },
        include: { submission: { include: { problem: true } } }
    })
    if (_.isNil(task)) {
        error(404)
    }
@@ -122,13 +125,6 @@ export const PUT = async ({ request, params }) => {
                            value: res.compile.message
                        }
                    ] as z.infer<typeof contentSchema>,
                    tasks: {
                        connect: [
                            {
                                id: task.id
                            }
                        ]
                    },
                    submissionId: task.submissionId,
                    isValid: false
                }
@@ -155,13 +151,6 @@ export const PUT = async ({ request, params }) => {
                            value: 0
                        },
                    ] as z.infer<typeof contentSchema>,
                    tasks: {
                        connect: [
                            {
                                id: task.id
                            }
                        ]
                    },
                    submissionId: task.submissionId,
                    isValid: false
                }
@@ -171,7 +160,21 @@ export const PUT = async ({ request, params }) => {

        const penalty = await fixAndCalculatePenalty(task.submissionId)
        const score = _.isNil(penalty) ? -1 : (_.max([res.score - penalty, 0]) || 0)
        const feedback = await db.submissionFeedback.create({
        const feedback = await db.$transaction(async (tx) => {
            if (score >= 0) {
                await db.gradeRecord.create({
                    data: {
                        grade: score,
                        source: {
                            type: "submission",
                            id: task.submissionId
                        },
                        studentId: task.submission.submitterId,
                        groupId: task.submission.problem.gradeGroupId,
                    }
                })
            }
            return await db.submissionFeedback.create({
                data: {
                    score,
                    description: res.status,
@@ -193,17 +196,11 @@ export const PUT = async ({ request, params }) => {
                            })
                        }
                    ] as z.infer<typeof contentSchema>,
                tasks: {
                    connect: [
                        {
                            id: task.id
                        }
                    ]
                },
                    submissionId: task.submissionId,
                    isValid: true
                }
            })
        })

    }
    return new Response(null, { status: 201 })
Loading