-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizModel.js
74 lines (68 loc) · 1.58 KB
/
QuizModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const mongoose = require('mongoose');
const { Schema } = mongoose;
const studentMarksSchema = new Schema({
user_id: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
mark: {
type: Number,
required: true
}
}, { _id: false });
const questionsSchema = new Schema({
sort: {
type: Number,
required: true
},
question: {
type: String,
required: true
},
answers: {
type: [String],
required: true
},
imgURL: {
type: String,
required: false
},
correctAnswer: {
type: String,
required: true
}
}, { _id: false });
const quizSchema = new Schema({
lesson_id: {
type: Schema.Types.ObjectId,
ref: 'Lesson',
required: true
},
course_id: {
type: Schema.Types.ObjectId,
ref: 'Course',
required: true
},
studentMarks: [studentMarksSchema],
questions: {
type: [questionsSchema],
},
full_mark: {
type: Number,
required: true
},
created_at: {
type: Date,
default: Date.now
}
});
quizSchema.virtual('averageMarks').get(function () {
if (this.studentMarks.length === 0) return 0;
const totalMarks = this.studentMarks.reduce((acc, curr) => acc + curr.mark, 0);
return totalMarks / this.studentMarks.length;
});
quizSchema.set('toJSON', { virtuals: true });
quizSchema.set('toObject', { virtuals: true });
const Quiz = mongoose.model('Quiz', quizSchema);
module.exports = Quiz;