From 2c545c26b465fe40dc308c321a11f5515cebc4c7 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sun, 28 Jan 2018 22:22:11 +0100
Subject: [PATCH] Fix exam mode

---
 entities/assessment.js           | 11 ++++-------
 models/assessment.js             |  6 ++----
 public/javascripts/assessment.js | 17 ++++++++++-------
 3 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/entities/assessment.js b/entities/assessment.js
index a8a781b..b4e3092 100644
--- a/entities/assessment.js
+++ b/entities/assessment.js
@@ -127,7 +127,6 @@ const AssessmentEntity =
 
 	startSession: function(aid, number, password, callback)
 	{
-		// TODO: security, do not re-do tasks if already done
 		db.assessments.update(
 			{ _id: aid },
 			{ $push: { papers: {
@@ -154,13 +153,11 @@ const AssessmentEntity =
 			(err,a) => {
 				if (!!err || !a)
 					return cb(err,a);
-				for (let p of a.papers)
+				let papIdx = a.papers.findIndex( item => { return item.number == number; });
+				for (let i of a.papers[papIdx].inputs)
 				{
-					for (let i of p.inputs)
-					{
-						if (i.index == idx)
-							return cb(null,true);
-					}
+					if (i.index == idx)
+						return cb(null,true);
 				}
 				cb(null,false);
 			}
diff --git a/models/assessment.js b/models/assessment.js
index a1a41e2..9269aee 100644
--- a/models/assessment.js
+++ b/models/assessment.js
@@ -74,8 +74,8 @@ const AssessmentModel =
 					return cb(err,null);
 				if (!!paper)
 					return cb(null,{paper:paper,questions:questions});
-				AssessmentEntity.startSession(aid, number, password, (err2,ret) => {
-					const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough...
+				const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough...
+				AssessmentEntity.startSession(aid, number, pwd, (err2,ret) => {
 					cb(err2, {
 						questions: questions,
 						password: pwd,
@@ -87,7 +87,6 @@ const AssessmentModel =
 
 	newAnswer: function(aid, number, password, input, cb)
 	{
-		console.log(JSON.stringify(input));
 		// Check that student hasn't already answered
 		AssessmentEntity.hasInput(aid, number, password, input.index, (err,ret) => {
 			if (!!err)
@@ -95,7 +94,6 @@ const AssessmentModel =
 			if (!!ret)
 				return cb({errmsg:"Question already answered"},null);
 			AssessmentEntity.setInput(aid, number, password, input, (err2,ret2) => {
-				console.log(JSON.stringify(ret2));
 				if (!!err2 || !ret2)
 					return cb(err2,ret2);
 				return cb(null,ret2);
diff --git a/public/javascripts/assessment.js b/public/javascripts/assessment.js
index 5fe24cd..1d62894 100644
--- a/public/javascripts/assessment.js
+++ b/public/javascripts/assessment.js
@@ -193,14 +193,15 @@ new Vue({
 				},
 				// stage 2
 				sendAnswer: function(realIndex) {
-					console.log(realIndex);
-					if (assessment.index == assessment.questions.length - 1)
-						this.$emit("gameover");
-					else
-						assessment.index++;
-					this.$forceUpdate(); //TODO: shouldn't be required
+					let gotoNext = () => {
+						if (assessment.index == assessment.questions.length - 1)
+							this.$emit("gameover");
+						else
+							assessment.index++;
+						this.$forceUpdate(); //TODO: shouldn't be required
+					};
 					if (assessment.mode == "open")
-						return; //only local
+						return gotoNext(); //only local
 					let answerData = {
 						aid: assessment._id,
 						answer: JSON.stringify({
@@ -220,6 +221,8 @@ new Vue({
 						success: ret => {
 							if (!!ret.errmsg)
 								return this.$emit("warning", ret.errmsg);
+							else
+								gotoNext();
 							//socket.emit(message.newAnswer, answer);
 						},
 					});
-- 
2.44.0