Skip to content

Commit

Permalink
OPPIA-1643: Avoid having duplicate trackers by comparing the existing…
Browse files Browse the repository at this point in the history
… trackers UUIDs
  • Loading branch information
AlbertoMoreta committed Jul 11, 2024
1 parent 3a9bb34 commit 5020cc5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void installDownloadedCourse(Context ctx, String filename, String

long userId = db.getUserId(SessionManager.getUsername(ctx));
db.resetCourse(courseId, userId);
db.insertTrackers(ctxr.getTrackers(courseId, userId));
db.insertTrackers(ctxr.getTrackers(ctx, courseId, userId));
db.insertQuizAttempts(ctxr.getQuizAttempts(courseId, userId));

listener.onInstallProgress(60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected EntityResult<List<Course>> doInBackground(List<Course>... params) {
try {
String responseString = response.body().string();
ctxr = new CourseTrackerXMLReader(responseString);
List<TrackerLog> trackers = ctxr.getTrackers(course.getCourseId(), userId);
List<TrackerLog> trackers = ctxr.getTrackers(ctx, course.getCourseId(), userId);
List<QuizAttempt> quizAttempts = ctxr.getQuizAttempts(course.getCourseId(), userId);

if (singleCourseUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ protected void upgradeV17() {

if (courseId != -1) {
db.insertActivities(c.getActivities(courseId));
db.insertTrackers(ctxr.getTrackers(courseId, 0));
db.insertTrackers(ctxr.getTrackers(ctx, courseId, 0));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@

package org.digitalcampus.oppia.utils.xmlreaders;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.digitalcampus.oppia.database.DbHelper;
import org.digitalcampus.oppia.exception.InvalidXMLException;
import org.digitalcampus.oppia.gamification.Gamification;
import org.digitalcampus.oppia.model.QuizAttempt;
Expand All @@ -36,6 +42,7 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -54,6 +61,7 @@ public class CourseTrackerXMLReader {
private static final String NODE_PASSED = "passed";
private static final String NODE_EVENT = "event";
private static final String NODE_POINTS = "points";
private static final String NODE_UUID = "uuid";

private Document document;

Expand Down Expand Up @@ -81,14 +89,20 @@ public CourseTrackerXMLReader(String xmlContent) throws InvalidXMLException {
}


public List<TrackerLog> getTrackers(long courseId, long userId) {
public List<TrackerLog> getTrackers(Context ctx, long courseId, long userId) {
List<TrackerLog> trackers = new ArrayList<>();
if (this.document == null) {
return trackers;
}
List<String> uuidList = getExistingTrackersIds(ctx, userId);
NodeList actTrackers = document.getFirstChild().getChildNodes();
for (int i = 0; i < actTrackers.getLength(); i++) {
NamedNodeMap attrs = actTrackers.item(i).getAttributes();
String uuid = attrs.getNamedItem(NODE_UUID).getTextContent();
if (uuidList.contains(uuid)) {
continue;
}

String digest = attrs.getNamedItem(NODE_DIGEST).getTextContent();
String submittedDateString = attrs.getNamedItem(NODE_SUBMITTEDDATE).getTextContent();
DateTime sdt = DateUtils.DATETIME_FORMAT.parseDateTime(submittedDateString);
Expand Down Expand Up @@ -136,6 +150,24 @@ public List<TrackerLog> getTrackers(long courseId, long userId) {
return trackers;
}

private List<String> getExistingTrackersIds(Context ctx, long userId) {
DbHelper db = DbHelper.getInstance(ctx);
List<TrackerLog> userTrackers = db.getTrackers(userId, false);
List<String> uuidList = userTrackers.stream().map(t -> {
if (t.getContent() != null) {
JsonElement data = new Gson().fromJson(t.getContent(), JsonObject.class).get("data");
if (data != null) {
JsonElement uuid = new Gson().fromJson(data.getAsString(), JsonObject.class).get("uuid");
if (uuid != null) {
return uuid.getAsString();
}
}
}
return "";
}).collect(Collectors.toList());
return uuidList;
}

public List<QuizAttempt> getQuizAttempts(long courseId, long userId) {
ArrayList<QuizAttempt> quizAttempts = new ArrayList<>();
if (this.document == null) {
Expand Down

0 comments on commit 5020cc5

Please sign in to comment.