Skip to content
Snippets Groups Projects
Commit 7da5c9bb authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Merge branch '2.x'

parents 91d30dfb 732ccd6d
No related merge requests found
...@@ -48,7 +48,7 @@ public class ScoreBasedScoreCalculator extends VariantScoreCalculator { ...@@ -48,7 +48,7 @@ public class ScoreBasedScoreCalculator extends VariantScoreCalculator {
if (courseMaximumValue == 0 || numUsers == 0) { if (courseMaximumValue == 0 || numUsers == 0) {
return 0; return 0;
} }
final double courseAverageValue = userTotalValue / numUsers; final double courseAverageValue = (double) userTotalValue / numUsers;
final double courseProgress = courseAverageValue / courseMaximumValue; final double courseProgress = courseAverageValue / courseMaximumValue;
return (int) Math.min(100, Math.round(courseProgress * 100)); return (int) Math.min(100, Math.round(courseProgress * 100));
} }
......
...@@ -81,7 +81,7 @@ public class ImageUtils { ...@@ -81,7 +81,7 @@ public class ImageUtils {
if (urlParts.length > 0) { if (urlParts.length > 0) {
final String extension = urlParts[urlParts.length - 1]; final String extension = urlParts[urlParts.length - 1];
return "data:image/" + extension + ";base64," + Base64.encodeBase64String(convertFileToByteArray(imageUrl)); return IMAGE_PREFIX_START + extension + IMAGE_PREFIX_MIDDLE + Base64.encodeBase64String(convertFileToByteArray(imageUrl));
} }
return null; return null;
...@@ -165,8 +165,9 @@ public class ImageUtils { ...@@ -165,8 +165,9 @@ public class ImageUtils {
final String base64String = imgInfo[1]; final String base64String = imgInfo[1];
byte[] imageData = Base64.decodeBase64(base64String); byte[] imageData = Base64.decodeBase64(base64String);
try { try (final ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData)); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
BufferedImage originalImage = ImageIO.read(bais);
BufferedImage newImage = new BufferedImage(width, height, originalImage.getType()); BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
Graphics2D g = newImage.createGraphics(); Graphics2D g = newImage.createGraphics();
...@@ -186,17 +187,15 @@ public class ImageUtils { ...@@ -186,17 +187,15 @@ public class ImageUtils {
g.dispose(); g.dispose();
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
result.append("data:image/"); result.append(IMAGE_PREFIX_START);
result.append(extension); result.append(extension);
result.append(";base64,"); result.append(IMAGE_PREFIX_MIDDLE);
ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(newImage, extension, baos);
ImageIO.write(newImage, extension, output);
output.flush(); baos.flush();
output.close();
result.append(Base64.encodeBase64String(output.toByteArray())); result.append(Base64.encodeBase64String(baos.toByteArray()));
return result.toString(); return result.toString();
} catch (IOException e) { } catch (IOException e) {
...@@ -230,11 +229,8 @@ public class ImageUtils { ...@@ -230,11 +229,8 @@ public class ImageUtils {
* @return The <code>byte[]</code> of the image on success, otherwise <code>null</code>. * @return The <code>byte[]</code> of the image on success, otherwise <code>null</code>.
*/ */
byte[] convertFileToByteArray(final String imageUrl) { byte[] convertFileToByteArray(final String imageUrl) {
try { try (final InputStream is = new URL(imageUrl).openStream();
final URL url = new URL(imageUrl); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final InputStream is = url.openStream();
final byte[] byteChunk = new byte[CHUNK_SIZE]; final byte[] byteChunk = new byte[CHUNK_SIZE];
int n; int n;
...@@ -243,10 +239,8 @@ public class ImageUtils { ...@@ -243,10 +239,8 @@ public class ImageUtils {
} }
baos.flush(); baos.flush();
baos.close();
return baos.toByteArray(); return baos.toByteArray();
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getLocalizedMessage()); logger.error(e.getLocalizedMessage());
} }
......
...@@ -60,27 +60,30 @@ public class ScoreBasedScoreCalculatorTest { ...@@ -60,27 +60,30 @@ public class ScoreBasedScoreCalculatorTest {
@Test @Test
public void shouldFilterBasedOnQuestionVariant() { public void shouldFilterBasedOnQuestionVariant() {
// Total of 300 Points
String q1 = this.addQuestion("lecture", 100); String q1 = this.addQuestion("lecture", 100);
String q2 = this.addQuestion("preparation", 100); String q2 = this.addQuestion("lecture", 100);
String q3 = this.addQuestion("lecture", 100);
User u1 = new TestUser("user1"); User u1 = new TestUser("user1");
User u2 = new TestUser("user2"); User u2 = new TestUser("user2");
// first question is answered correctly, second one is not User u3 = new TestUser("user3");
// Both users achieve 200 points
this.addAnswer(q1, u1, 100); this.addAnswer(q1, u1, 100);
this.addAnswer(q1, u2, 100); this.addAnswer(q1, u2, 100);
this.addAnswer(q1, u3, 0);
this.addAnswer(q2, u1, 0); this.addAnswer(q2, u1, 0);
this.addAnswer(q2, u2, 0); this.addAnswer(q2, u2, 100);
this.addAnswer(q2, u3, 0);
this.addAnswer(q3, u1, 100);
this.addAnswer(q3, u2, 100);
this.addAnswer(q3, u3, 0);
lp.setQuestionVariant("lecture"); lp.setQuestionVariant("lecture");
ScoreStatistics lectureProgress = lp.getCourseProgress(null); ScoreStatistics u1LectureProgress = lp.getMyProgress(null, u1);
ScoreStatistics myLectureProgress = lp.getMyProgress(null, u1); // (500/3) / 300 ~= 0,56.
lp.setQuestionVariant("preparation"); assertEquals(56, u1LectureProgress.getCourseProgress());
ScoreStatistics prepProgress = lp.getCourseProgress(null); // 200 / 300 ~= 0,67.
ScoreStatistics myPrepProgress = lp.getMyProgress(null, u1); assertEquals(67, u1LectureProgress.getMyProgress());
assertEquals(100, lectureProgress.getCourseProgress());
assertEquals(100, myLectureProgress.getMyProgress());
assertEquals(0, prepProgress.getCourseProgress());
assertEquals(0, myPrepProgress.getMyProgress());
} }
@Test @Test
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment