diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java index f6bb60d4bd594ade7a56073e3f06128f5415d6fd..893bd56b76bd5b9d23120d7574a3822848614b56 100644 --- a/src/main/java/de/thm/arsnova/controller/SessionController.java +++ b/src/main/java/de/thm/arsnova/controller/SessionController.java @@ -17,15 +17,12 @@ */ package de.thm.arsnova.controller; -import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.servlet.http.HttpServletResponse; -import net.sf.json.JSONObject; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -43,6 +40,7 @@ import de.thm.arsnova.connector.model.Course; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; import de.thm.arsnova.entities.transport.ImportExportSession; +import de.thm.arsnova.entities.transport.LearningProgressValues; import de.thm.arsnova.exceptions.UnauthorizedException; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; @@ -233,7 +231,7 @@ public class SessionController extends AbstractController { } @RequestMapping(value = "/{sessionkey}/learningprogress", method = RequestMethod.GET) - public int learningProgress( + public LearningProgressValues learningProgress( @PathVariable final String sessionkey, @RequestParam(value = "type", defaultValue = "questions") final String progressType, final HttpServletResponse response @@ -242,16 +240,12 @@ public class SessionController extends AbstractController { } @RequestMapping(value = "/{sessionkey}/mylearningprogress", method = RequestMethod.GET) - public JSONObject myLearningProgress( + public LearningProgressValues myLearningProgress( @PathVariable final String sessionkey, @RequestParam(value = "type", defaultValue = "questions") final String progressType, final HttpServletResponse response ) { - final SimpleEntry<Integer, Integer> result = sessionService.getMyLearningProgress(sessionkey, progressType); - final JSONObject json = new JSONObject(); - json.put("myprogress", result.getKey()); - json.put("courseprogress", result.getValue()); - return json; + return sessionService.getMyLearningProgress(sessionkey, progressType); } /* internal redirections */ diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index 57aa2c48b9d8db74ec85fcacc801fd0a9611a3b4..326dbec1395dcc7faa4346454982cae4b45732f9 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -1728,7 +1728,7 @@ public class CouchDBDao implements IDatabaseDao { CourseScore courseScore = new CourseScore(); // no results found - if (maximumValueResult.isEmpty() || answerSumResult.isEmpty()) { + if (maximumValueResult.isEmpty() && answerSumResult.isEmpty()) { return courseScore; } diff --git a/src/main/java/de/thm/arsnova/domain/LearningProgress.java b/src/main/java/de/thm/arsnova/domain/LearningProgress.java index e8ccfbfedc80275bfaedfff91cf00cfbab1aa76b..3cb8d49c6ef47b7ce6abf07a10bece72b62b2d3f 100644 --- a/src/main/java/de/thm/arsnova/domain/LearningProgress.java +++ b/src/main/java/de/thm/arsnova/domain/LearningProgress.java @@ -17,14 +17,13 @@ */ package de.thm.arsnova.domain; -import java.util.AbstractMap.SimpleEntry; - import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +import de.thm.arsnova.entities.transport.LearningProgressValues; public interface LearningProgress { - public int getCourseProgress(Session session); + public LearningProgressValues getCourseProgress(Session session); - public SimpleEntry<Integer,Integer> getMyProgress(Session session, User user); + public LearningProgressValues getMyProgress(Session session, User user); } diff --git a/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java b/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java index c7168bc0c05dee16423a4e4d7a847aae24db3eb2..3ccdb813355eec6137bd613ca51d46658c542061 100644 --- a/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java +++ b/src/main/java/de/thm/arsnova/domain/PointBasedLearningProgress.java @@ -17,12 +17,10 @@ */ package de.thm.arsnova.domain; -import java.util.AbstractMap; -import java.util.AbstractMap.SimpleEntry; - import de.thm.arsnova.dao.IDatabaseDao; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +import de.thm.arsnova.entities.transport.LearningProgressValues; public class PointBasedLearningProgress implements LearningProgress { @@ -33,9 +31,12 @@ public class PointBasedLearningProgress implements LearningProgress { } @Override - public int getCourseProgress(Session session) { + public LearningProgressValues getCourseProgress(Session session) { CourseScore courseScore = databaseDao.getLearningProgress(session); - return calculateCourseScore(courseScore); + LearningProgressValues lpv = new LearningProgressValues(); + lpv.setCourseProgress(calculateCourseScore(courseScore)); + lpv.setNumQuestions(courseScore.getQuestionCount()); + return lpv; } private int calculateCourseScore(CourseScore courseScore) { @@ -51,20 +52,24 @@ public class PointBasedLearningProgress implements LearningProgress { } @Override - public SimpleEntry<Integer, Integer> getMyProgress(Session session, User user) { + public LearningProgressValues getMyProgress(Session session, User user) { CourseScore courseScore = databaseDao.getLearningProgress(session); int courseProgress = calculateCourseScore(courseScore); final double courseMaximumValue = courseScore.getMaximumScore(); final double userTotalValue = courseScore.getTotalUserScore(user); + LearningProgressValues lpv = new LearningProgressValues(); + lpv.setCourseProgress(courseProgress); + lpv.setNumQuestions(courseScore.getQuestionCount()); if (courseMaximumValue == 0) { - return new AbstractMap.SimpleEntry<Integer, Integer>(0, courseProgress); + return lpv; } final double myProgress = userTotalValue / courseMaximumValue; final int myLearningProgress = (int)Math.min(100, Math.round(myProgress*100)); + lpv.setMyProgress(myLearningProgress); - return new AbstractMap.SimpleEntry<Integer, Integer>(myLearningProgress, courseProgress); + return lpv; } } diff --git a/src/main/java/de/thm/arsnova/domain/QuestionBasedLearningProgress.java b/src/main/java/de/thm/arsnova/domain/QuestionBasedLearningProgress.java index d1ea2d45103cf5e3eee2e8ea8e82368a37ab8f3c..65220ea05c31ee40d234062d0057325e9db8f1a0 100644 --- a/src/main/java/de/thm/arsnova/domain/QuestionBasedLearningProgress.java +++ b/src/main/java/de/thm/arsnova/domain/QuestionBasedLearningProgress.java @@ -17,12 +17,10 @@ */ package de.thm.arsnova.domain; -import java.util.AbstractMap; -import java.util.AbstractMap.SimpleEntry; - import de.thm.arsnova.dao.IDatabaseDao; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +import de.thm.arsnova.entities.transport.LearningProgressValues; public class QuestionBasedLearningProgress implements LearningProgress { @@ -33,9 +31,12 @@ public class QuestionBasedLearningProgress implements LearningProgress { } @Override - public int getCourseProgress(Session session) { + public LearningProgressValues getCourseProgress(Session session) { CourseScore courseScore = databaseDao.getLearningProgress(session); - return calculateCourseProgress(courseScore); + LearningProgressValues lpv = new LearningProgressValues(); + lpv.setCourseProgress(calculateCourseProgress(courseScore)); + lpv.setNumQuestions(courseScore.getQuestionCount()); + return lpv; } private int calculateCourseProgress(CourseScore courseScore) { @@ -68,7 +69,7 @@ public class QuestionBasedLearningProgress implements LearningProgress { } @Override - public SimpleEntry<Integer, Integer> getMyProgress(Session session, User user) { + public LearningProgressValues getMyProgress(Session session, User user) { CourseScore courseScore = databaseDao.getLearningProgress(session); int courseProgress = calculateCourseProgress(courseScore); @@ -77,7 +78,11 @@ public class QuestionBasedLearningProgress implements LearningProgress { final double myLearningProgress = numQuestionsCorrect / (double)(courseScore.getQuestionCount()); // calculate percent, cap results to 100 final int percentage = (int) Math.min(100, Math.round(myLearningProgress*100)); - return new AbstractMap.SimpleEntry<Integer, Integer>(percentage, courseProgress); + LearningProgressValues lpv = new LearningProgressValues(); + lpv.setCourseProgress(courseProgress); + lpv.setMyProgress(percentage); + lpv.setNumQuestions(courseScore.getQuestionCount()); + return lpv; } private int numQuestionsCorrectForUser(User user, CourseScore courseScore) { diff --git a/src/main/java/de/thm/arsnova/entities/transport/LearningProgressValues.java b/src/main/java/de/thm/arsnova/entities/transport/LearningProgressValues.java new file mode 100644 index 0000000000000000000000000000000000000000..4bdf46f33b847d80adbe29df01aaa97f03d3656c --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/transport/LearningProgressValues.java @@ -0,0 +1,87 @@ +/* + * This file is part of ARSnova Backend. + * Copyright (C) 2012-2015 The ARSnova Team + * + * ARSnova Backend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova Backend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.entities.transport; + +public class LearningProgressValues { + + private int courseProgress; + + private int myProgress; + + private int numQuestions; + + public int getCourseProgress() { + return courseProgress; + } + + public void setCourseProgress(int courseProgress) { + this.courseProgress = courseProgress; + } + + public int getMyProgress() { + return myProgress; + } + + public void setMyProgress(int myProgress) { + this.myProgress = myProgress; + } + + public int getNumQuestions() { + return numQuestions; + } + + public void setNumQuestions(int numQuestions) { + this.numQuestions = numQuestions; + } + + @Override + public int hashCode() { + // auto generated! + final int prime = 31; + int result = 1; + result = prime * result + courseProgress; + result = prime * result + myProgress; + result = prime * result + numQuestions; + return result; + } + + @Override + public boolean equals(Object obj) { + // auto generated! + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LearningProgressValues other = (LearningProgressValues) obj; + if (courseProgress != other.courseProgress) { + return false; + } + if (myProgress != other.myProgress) { + return false; + } + if (numQuestions != other.numQuestions) { + return false; + } + return true; + } +} diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java index 9104ddfbe6b94ec4defb9e82809cf57d3991a5f9..1661b0e113879bd742bfb8186b39609e3aaaabba 100644 --- a/src/main/java/de/thm/arsnova/services/ISessionService.java +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -17,7 +17,6 @@ */ package de.thm.arsnova.services; -import java.util.AbstractMap.SimpleEntry; import java.util.List; import java.util.UUID; @@ -26,6 +25,7 @@ import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.transport.ImportExportSession; +import de.thm.arsnova.entities.transport.LearningProgressValues; public interface ISessionService { Session getSession(String keyword); @@ -56,9 +56,9 @@ public interface ISessionService { void deleteSession(String sessionkey); - int getLearningProgress(String sessionkey, String progressType); + LearningProgressValues getLearningProgress(String sessionkey, String progressType); - SimpleEntry<Integer, Integer> getMyLearningProgress(String sessionkey, String progressType); + LearningProgressValues getMyLearningProgress(String sessionkey, String progressType); List<SessionInfo> getMySessionsInfo(); diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 9bbe32c64993445dae036c32d8a1bbe053adfcb7..2f8211bd58b5f751d64de0fe4c89f7482bbf776c 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -42,6 +42,7 @@ import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.SessionInfo; import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.transport.ImportExportSession; +import de.thm.arsnova.entities.transport.LearningProgressValues; import de.thm.arsnova.events.StatusSessionEvent; import de.thm.arsnova.exceptions.BadRequestException; import de.thm.arsnova.exceptions.ForbiddenException; @@ -310,7 +311,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe @Override @PreAuthorize("isAuthenticated()") - public int getLearningProgress(final String sessionkey, final String progressType) { + public LearningProgressValues getLearningProgress(final String sessionkey, final String progressType) { final Session session = databaseDao.getSessionFromKeyword(sessionkey); LearningProgress learningProgress = learningProgressFactory.createFromType(progressType); return learningProgress.getCourseProgress(session); @@ -318,7 +319,7 @@ public class SessionService implements ISessionService, ApplicationEventPublishe @Override @PreAuthorize("isAuthenticated()") - public SimpleEntry<Integer, Integer> getMyLearningProgress(final String sessionkey, final String progressType) { + public LearningProgressValues getMyLearningProgress(final String sessionkey, final String progressType) { final Session session = databaseDao.getSessionFromKeyword(sessionkey); final User user = userService.getCurrentUser(); LearningProgress learningProgress = learningProgressFactory.createFromType(progressType); diff --git a/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java b/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java index 941d50c98875957cc873b8a4a0dae18e68699f3b..01148489ad4030f58cab6124a29e48b59c1d9bca 100644 --- a/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java +++ b/src/test/java/de/thm/arsnova/domain/QuestionBasedLearningProgressTest.java @@ -20,13 +20,12 @@ package de.thm.arsnova.domain; import static org.mockito.Mockito.*; import static org.junit.Assert.*; -import java.util.AbstractMap.SimpleEntry; - import org.junit.Test; import de.thm.arsnova.dao.IDatabaseDao; import de.thm.arsnova.entities.TestUser; import de.thm.arsnova.entities.User; +import de.thm.arsnova.entities.transport.LearningProgressValues; public class QuestionBasedLearningProgressTest { @@ -46,8 +45,11 @@ public class QuestionBasedLearningProgressTest { when(db.getLearningProgress(null)).thenReturn(courseScore); LearningProgress lp = new QuestionBasedLearningProgress(db); - SimpleEntry<Integer, Integer> expected = new SimpleEntry<Integer, Integer>(0, 0); - SimpleEntry<Integer, Integer> actual = lp.getMyProgress(null, user); + LearningProgressValues expected = new LearningProgressValues(); + expected.setCourseProgress(0); + expected.setMyProgress(0); + expected.setNumQuestions(0); + LearningProgressValues actual = lp.getMyProgress(null, user); assertEquals(expected, actual); } @@ -65,8 +67,11 @@ public class QuestionBasedLearningProgressTest { when(db.getLearningProgress(null)).thenReturn(courseScore); LearningProgress lp = new QuestionBasedLearningProgress(db); - SimpleEntry<Integer, Integer> expected = new SimpleEntry<Integer, Integer>(100, 100); - SimpleEntry<Integer, Integer> actual = lp.getMyProgress(null, user); + LearningProgressValues expected = new LearningProgressValues(); + expected.setCourseProgress(100); + expected.setMyProgress(100); + expected.setNumQuestions(1); + LearningProgressValues actual = lp.getMyProgress(null, user); assertEquals(expected, actual); }