-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation_guide_exam_quiz.txt
More file actions
305 lines (246 loc) · 9.12 KB
/
implementation_guide_exam_quiz.txt
File metadata and controls
305 lines (246 loc) · 9.12 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Implementation Guide for Exam and Quiz Functionality
## Overview
This guide outlines the step-by-step process to implement the exam and quiz functionality in the FreshHub project. The exams and quizzes will include multiple-choice and fill-in-the-blank questions, fetched from the backend (Supabase). The implementation will include a timer for each test and use a sleek UI for interaction.
---
## Step 1: Database Design
### Table: `courses`
- (Already exists in Supabase)
- **Columns** (relevant):
- `id` (UUID, Primary Key): Unique identifier for each course.
### Table: `exams`
- **Purpose**: Store metadata for exams (mid/final) for each course.
- **Columns**:
- `id` (UUID, Primary Key): Unique identifier for each exam.
- `course_id` (UUID, Foreign Key): References `courses.id`.
- `type` (ENUM): Type of exam (`mid`, `final`).
- `title` (TEXT): Title of the exam.
- `description` (TEXT): Description of the exam.
- `duration` (INTEGER): Duration of the exam in minutes.
- `created_at` (TIMESTAMP): Timestamp of when the exam was created.
### Table: `quizzes`
- **Purpose**: Store metadata for quizzes for each course.
- **Columns**:
- `id` (UUID, Primary Key): Unique identifier for each quiz.
- `course_id` (UUID, Foreign Key): References `courses.id`.
- `title` (TEXT): Title of the quiz.
- `description` (TEXT): Description of the quiz.
- `duration` (INTEGER): Duration of the quiz in minutes (optional).
- `created_at` (TIMESTAMP): Timestamp of when the quiz was created.
### Table: `exam_questions`
- **Purpose**: Store questions for exams.
- **Columns**:
- `id` (UUID, Primary Key): Unique identifier for each question.
- `exam_id` (UUID, Foreign Key): References `exams.id`.
- `type` (ENUM): Type of question (`multiple_choice`, `fill_in_blank`).
- `question` (TEXT): The question text.
- `options` (JSONB): Options for multiple-choice questions (nullable for fill-in-the-blank).
- `answer` (TEXT): Correct answer.
- `created_at` (TIMESTAMP): Timestamp of when the question was created.
### Table: `quiz_questions`
- **Purpose**: Store questions for quizzes.
- **Columns**:
- `id` (UUID, Primary Key): Unique identifier for each question.
- `quiz_id` (UUID, Foreign Key): References `quizzes.id`.
- `type` (ENUM): Type of question (`multiple_choice`, `fill_in_blank`).
- `question` (TEXT): The question text.
- `options` (JSONB): Options for multiple-choice questions (nullable for fill-in-the-blank).
- `answer` (TEXT): Correct answer.
- `created_at` (TIMESTAMP): Timestamp of when the question was created.
---
## Step 2: Backend Implementation
### 1. **Fetch Questions from Supabase**
- Create a new route in the backend: `routes/exams.js`.
- Add a function to fetch questions and exam metadata from Supabase:
```javascript
const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
async function getExamData(examId) {
const { data: exam, error: examError } = await supabase
.from('exams')
.select('*')
.eq('id', examId)
.single();
if (examError) throw new Error(examError.message);
const { data: questions, error: questionsError } = await supabase
.from('exam_questions')
.select('*')
.eq('exam_id', examId);
if (questionsError) throw new Error(questionsError.message);
return { exam, questions };
}
module.exports = { getExamData };
```
### 2. **API Endpoint**
- Add an endpoint to serve exam data to the frontend:
```javascript
const express = require('express');
const { getExamData } = require('../controllers/exams');
const router = express.Router();
router.get('/:examId', async (req, res) => {
try {
const data = await getExamData(req.params.examId);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;
```
---
## Step 3: Frontend Implementation
### 1. **Routing**
- Add a new route in `src/routes/course/[course]/exam/[id]/+page.svelte` for the exam page.
- Use the `id` parameter to fetch exam data from the backend.
### 2. **Components**
- **ExamPage.svelte**: Main page for displaying the exam.
- Fetch exam data using `load` function.
- Pass data to child components.
- **QuestionCard.svelte**: Component for rendering individual questions.
- Props:
- `type`: Type of question.
- `question`: Question text.
- `options`: Options for multiple-choice questions.
- `onAnswer`: Callback for when the user selects an answer.
- **Timer.svelte**: Timer component.
- Props:
- `duration`: Duration of the exam.
- `onTimeUp`: Callback for when the timer ends.
### 3. **State Management**
- Use Svelte stores to manage state:
- `currentQuestionIndex`: Tracks the current question.
- `answers`: Stores user answers.
- `timeRemaining`: Tracks remaining time.
### 4. **UI Design**
- Use Tailwind CSS for styling.
- Example layout:
```html
<div class="exam-container">
<Timer duration={exam.duration} onTimeUp={handleTimeUp} />
<QuestionCard
type={questions[currentQuestionIndex].type}
question={questions[currentQuestionIndex].question}
options={questions[currentQuestionIndex].options}
onAnswer={handleAnswer}
/>
<button on:click={goToNextQuestion}>Next</button>
</div>
```
---
## Step 4: Timer Functionality
### Timer Logic
- Use a Svelte store to track time remaining.
- Example:
```javascript
import { writable } from 'svelte/store';
export const timeRemaining = writable(duration * 60); // duration in seconds
const interval = setInterval(() => {
timeRemaining.update((time) => {
if (time <= 0) {
clearInterval(interval);
return 0;
}
return time - 1;
});
}, 1000);
```
---
## Step 5: Submission and Results
### Submission
- Add a `submitExam` function to send user answers to the backend:
```javascript
async function submitExam(examId, answers) {
const response = await fetch(`/api/exams/${examId}/submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ answers }),
});
if (!response.ok) {
throw new Error('Failed to submit exam');
}
return await response.json();
}
```
### Results Page
- Create a new route: `src/routes/exam/[id]/results/+page.svelte`.
- Display the user's score and correct answers.
---
## Step 6: Testing
### Manual Testing
- Test the following scenarios:
- Timer ends before submission.
- User submits answers successfully.
- User navigates between questions.
### Automated Testing
- Write unit tests for:
- Timer logic.
- Question rendering.
- Answer submission.
---
## Routing and Backend Process for Exams
#### Frontend Routing
1. **Starting Point**:
- Users can access exams from:
- `/[course]`: The main course page.
- `/[course]/[resource]`: A specific resource page within the course.
- Clicking an exam will navigate to `/course/exam/[id]`.
2. **Route Structure**:
- Add a new route: `src/routes/course/exam/[id]/+page.svelte`.
- Use the `id` parameter to fetch exam data from the backend.
3. **Navigation Example**:
- From `/[course]`:
```svelte
<a href={`/course/exam/${exam.id}`}>Start Exam</a>
```
- From `/[course]/[resource]`:
```svelte
<a href={`/course/exam/${exam.id}`}>Start Exam</a>
```
#### Backend Process
1. **API Endpoint**:
- Add a new endpoint in the backend to fetch exam data:
```javascript
const express = require('express');
const { getExamData } = require('../controllers/exams');
const router = express.Router();
router.get('/:examId', async (req, res) => {
try {
const data = await getExamData(req.params.examId);
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;
```
2. **Controller Logic**:
- Fetch exam and question data from Supabase:
```javascript
const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
async function getExamData(examId) {
const { data: exam, error: examError } = await supabase
.from('exams')
.select('*')
.eq('id', examId)
.single();
if (examError) throw new Error(examError.message);
const { data: questions, error: questionsError } = await supabase
.from('exam_questions')
.select('*')
.eq('exam_id', examId);
if (questionsError) throw new Error(questionsError.message);
return { exam, questions };
}
module.exports = { getExamData };
```
3. **Best Practices**:
- **Validation**:
- Validate `examId` to ensure it exists and belongs to the correct course.
- **Error Handling**:
- Return meaningful error messages for missing or invalid data.
- **Caching**:
- Use caching (e.g., Redis) to reduce database load for frequently accessed exams.
- **Security**:
- Ensure only authorized users can access the exam data.
- Use JWT or session-based authentication to verify user identity.
---