Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPPIA-1289: Collect grade boundaries and display correct message on feedback result #1297

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions app/src/main/java/org/digitalcampus/mobile/quiz/Quiz.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.util.Log;

import org.digitalcampus.mobile.quiz.model.GradeBoundary;
import org.digitalcampus.mobile.quiz.model.QuizQuestion;
import org.digitalcampus.mobile.quiz.model.Response;
import org.digitalcampus.mobile.quiz.model.questiontypes.Description;
Expand All @@ -39,11 +40,14 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Quiz implements Serializable {

Expand Down Expand Up @@ -543,6 +547,16 @@ private String propsSerializedGetString(String key, String defaultValue){
return defaultValue;
}

private JSONArray propsSerializedGetJSONArray(String key) {
try {
JSONObject json = new JSONObject(propsSerialized);
return json.isNull(key) ? null : json.getJSONArray(key);
} catch (JSONException jsone) {
Log.d(TAG, "Error getting JSONArray from propsSerialized " + key);
}
return null;
}

public int getMaxAttempts() { return maxattempts; }
public boolean limitAttempts(){ return maxattempts > 0; }
private void setMaxAttempts(int maxAttempts) { this.maxattempts = maxAttempts; }
Expand All @@ -555,4 +569,66 @@ public void updateResponsesAfterLanguageChange(String previousLang, String newLa
question.updateUserResponsesLang(previousLang, newLang);
}
}

private List<GradeBoundary> getGradeBoundaries() {
ArrayList<GradeBoundary> gradeBoundaries = new ArrayList<>();
JSONArray gradeBoundariesJSON = propsSerializedGetJSONArray("grade_boundaries");

try {
if(gradeBoundariesJSON != null) {
for (int i = 0; i < gradeBoundariesJSON.length(); i++) {
JSONObject gradeBoundary = gradeBoundariesJSON.getJSONObject(i);
Iterator<String> keys = gradeBoundary.keys();
while (keys.hasNext()) {
String key = keys.next();
int grade = Integer.parseInt(key);
String message = gradeBoundary.getString(key);
gradeBoundaries.add(new GradeBoundary(grade, message));
}
}
Collections.sort(gradeBoundaries, GradeBoundary.sorByGradeDescending);
}
} catch (JSONException e) {
e.printStackTrace();
}

return gradeBoundaries;
}

// Get the feedback message associated to a grade. The learner must get a grade
// that is greater or equal than the defined grade boundaries
public String getFeedbackMessageBasedOnQuizGrade(float quizGrade) {
List<GradeBoundary> gradeBoundaries = this.getGradeBoundaries();
for(GradeBoundary gradeBoundary : gradeBoundaries) {
if(quizGrade >= gradeBoundary.getGrade()){
return cleanFeedbackMessage(gradeBoundary.getMessage());
}
}
return null;
}

// Replace placeholders in the form of {{property_name}} by the value of property_name
private String cleanFeedbackMessage(String feedbackMessage) {
Pattern p = Pattern.compile("\\{\\{(\\w+)\\}\\}");
Matcher m = p.matcher(feedbackMessage);
while (m.find()) {
String placeholder = m.group();
String propertyName = placeholder.replaceAll("[{}]", "");
String propertyValue = getPropertyValue(propertyName);
if(propertyValue != null) {
feedbackMessage = feedbackMessage.replace(placeholder, propertyValue);
}
}
return feedbackMessage;
}

// Get a property´s value by its name, or null if the property name is not included.
private String getPropertyValue(String propertyName) {
switch (propertyName) {
case "user_score": return String.valueOf(this.userscore);
case "max_score": return String.valueOf(this.maxscore);
case "score_percentage": return String.valueOf(this.userscore * 100 / this.maxscore);
default: return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.digitalcampus.mobile.quiz.model;

import java.util.Comparator;

public class GradeBoundary {
private int grade;
private String message;

public GradeBoundary(int grade, String message) {
this.grade = grade;
this.message = message;
}

public int getGrade() {
return grade;
}

public String getMessage() {
return message;
}

public static Comparator<GradeBoundary> sorByGradeDescending = new Comparator<GradeBoundary>() {
@Override
public int compare(GradeBoundary gradeBoundary1, GradeBoundary gradeBoundary2) {
return gradeBoundary2.grade - gradeBoundary1.grade;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,9 @@ public void onClick(View v) {
startMediaPlayerWithFile(mediaFileName);
}
}

public float getPercentScore() {
quiz.mark(prefLang);
return quiz.getUserscore() * 100 / quiz.getMaxscore();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ void loadInitialInfo(ViewGroup infoContainer) {

@Override
void showResultsInfo() {
String feedbackMessage = this.quiz.getFeedbackMessageBasedOnQuizGrade(this.getPercentScore());
TextView title = getView().findViewById(R.id.quiz_results_score);
title.setText(R.string.widget_feedback_submit_title);
if(feedbackMessage != null && !feedbackMessage.isEmpty()) {
title.setText(feedbackMessage);
} else {
title.setText(R.string.widget_feedback_submit_title);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ void showBaselineResultMessage() {
baselineText.setVisibility(View.VISIBLE);
}

private float getPercentScore() {
quiz.mark(prefLang);
return quiz.getUserscore() * 100 / quiz.getMaxscore();
}


@Override
public boolean getActivityCompleted() {
Expand Down