Skip to content

Commit

Permalink
Merge pull request #9 from vinayakjaas/quiz_portal
Browse files Browse the repository at this point in the history
Add Backend code for Quiz Portal
  • Loading branch information
rks-031 authored Jul 22, 2024
2 parents 5ca929f + d39b554 commit f8275c2
Show file tree
Hide file tree
Showing 5 changed files with 1,482 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Quiz_Portal/Backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
77 changes: 77 additions & 0 deletions Quiz_Portal/Backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const TinCan = require('tincanjs');

const app = express();
const port = process.env.PORT || 5000;

app.use(bodyParser.json());

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});

let lrs;
try {
lrs = new TinCan.LRS({
endpoint: process.env.LRS_ENDPOINT,
username: process.env.LRS_KEY,
password: process.env.LRS_SECRET,
allowFail: false
});
console.log("LRS object setup successfully.");
} catch (ex) {
console.error("Failed to setup LRS object: ", ex);
process.exit(1);
}

app.post('/api/quiz-result', (req, res) => {
const { learner, quizResult } = req.body;

const statement = new TinCan.Statement({
actor: {
mbox: `mailto:${learner.email}`,
name: learner.name,
},
verb: {
id: "http://adlnet.gov/expapi/verbs/completed",
display: { "en-US": "completed" }
},
object: {
id: "http://example.com/activities/quiz",
definition: {
name: { "en-US": "Quiz" },
description: { "en-US": "A quiz to test the learner's knowledge." }
}
},
result: {
score: {
raw: quizResult.correctPoints,
max: quizResult.totalPoints
},
success: quizResult.numberOfCorrectAnswers > 0,
completion: true
}
});

lrs.saveStatement(statement, {
callback: function (err, xhr) {
if (err !== null) {
console.error("Failed to save statement: ", err);
console.error("Error details:", xhr.responseText);
res.status(500).send("Failed to save statement");
} else {
console.log("Statement saved successfully");
res.status(200).send("Statement saved successfully");
}
}
});
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
11 changes: 11 additions & 0 deletions Quiz_Portal/Backend/models/Quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');

const QuizSchema = new mongoose.Schema({
question: String,
type: { type: String, enum: ['multiple-choice', 'fill-in-the-blank'], required: true },
options: [String], // Only for multiple-choice questions
correctAnswer: String, // Correct option or answer
points: { type: Number, default: 4 }
});

const Quiz = mongoose.model('Quiz', QuizSchema);
Loading

0 comments on commit f8275c2

Please sign in to comment.