Skip to content

Commit

Permalink
questions now contains positive and negative marks
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinkatyal13 committed Apr 22, 2020
1 parent 0ca5241 commit b49f633
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion framework/serializers/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function (included = [], type, config) {
}

const options = {
attributes: ['title', 'description', 'positiveWeight' ,'negativeWeight', 'question'],
attributes: ['title', 'description', 'question'],
meta: {
pagination: function (record) {
return record.pagination
Expand Down
15 changes: 13 additions & 2 deletions framework/serializers/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ module.exports = function (included = [], type, config) {
}

const options = {
attributes: ['title', 'description', 'explanation','difficulty' ,'user', 'choices', 'tags','createdBy'],
attributes: [
'title',
'description',
'explanation',
'difficulty',
'positiveScore',
'negativeScore',
'user',
'choices',
'tags',
'createdBy'
],
meta: {
pagination: function (record) {
return record.pagination
Expand All @@ -37,7 +48,7 @@ module.exports = function (included = [], type, config) {
},
choices: {
ref: 'id',
attributes: ['title', 'description', 'positiveWeight' ,'negativeWeight', 'question'],
attributes: ['title', 'description', 'question'],
included: included.includes('choices')
},
tags: {
Expand Down
25 changes: 25 additions & 0 deletions migrations/20200422121249-addScoresInQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('questions', 'positiveScore', {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 10
}),
queryInterface.addColumn('questions', 'negativeScore', {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
})
])
},

down: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.removeColumn('questions', 'positiveScore'),
queryInterface.removeColumn('questions', 'negativeScore')
])
}
};
10 changes: 0 additions & 10 deletions models/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ module.exports = (sequelize, DataTypes) => {
description: {
type: DataTypes.STRING,
allowNull: true
},
positiveWeight: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 1,
},
negativeWeight: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0,
}
}, {});
choices.associate = function(models) {
Expand Down
10 changes: 10 additions & 0 deletions models/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.ARRAY(DataTypes.BIGINT),
allowNull: false,
defaultValue: []
},
positiveScore: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 10
},
negativeScore: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
paranoid: true
Expand Down
2 changes: 1 addition & 1 deletion routes/api/quiz/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class QuizController extends BaseController {
return ;
}

const { score, correctlyAnswered, incorrectlyAnswered } = U.getScore(markedChoices, U.parseIntArray(question.correctAnswers), question.choices)
const { score, correctlyAnswered, incorrectlyAnswered } = U.getScore(markedChoices, U.parseIntArray(question.correctAnswers), question)

return {
id: markedQuestion.id,
Expand Down
9 changes: 3 additions & 6 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const isContainedIn = (a, b) => R.intersection(a, b).length === a.length
markedChoices = Array of ids
correctChoices = Array of Sequelize model instances of correctChoices
*/
const getScore = (markedChoices, correctChoiceIds, possibleChoices) => {
const getScore = (markedChoices, correctChoiceIds, question) => {
const possibleChoices = question.choices
// const correctChoiceIds = correctChoices.map(_ => _.id)

// is the id in the correctChoiceIds
Expand All @@ -35,11 +36,7 @@ const getScore = (markedChoices, correctChoiceIds, possibleChoices) => {
const correctlyAnswered = possibleChoices.filter(choice => R.contains(choice.id, correctlyAnsweredIds))
const incorrectlyAnswered = possibleChoices.filter(choice => R.contains(choice.id, incorrectlyAnsweredIds))

let score = correctlyAnswered.reduce((acc, c) => acc + c.positiveWeight, 0)
score -= incorrectlyAnswered.reduce((acc, c) => acc + c.negativeWeight, 0)

// scale score out of 10
score *= 10
const score = R.equals(new Set(correctChoiceIds), new Set(correctlyAnsweredIds)) ? question.positiveScore : -question.negativeScore

return {
score,
Expand Down

0 comments on commit b49f633

Please sign in to comment.