From 0f7c311e1051606dccb35c7697e6cc505b0e51e3 Mon Sep 17 00:00:00 2001 From: Nya Date: Tue, 2 Feb 2021 16:20:04 +0100 Subject: [PATCH] feat: #105 Add logic for determining content mastery --- .../analytics/dao/WordAssessmentEventDao.java | 7 +++- .../provider/WordAssessmentEventProvider.java | 21 +++++++++- .../analytics/utils/EventProviderUtil.java | 39 +++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ai/elimu/analytics/dao/WordAssessmentEventDao.java b/app/src/main/java/ai/elimu/analytics/dao/WordAssessmentEventDao.java index 979b734..c362a3f 100644 --- a/app/src/main/java/ai/elimu/analytics/dao/WordAssessmentEventDao.java +++ b/app/src/main/java/ai/elimu/analytics/dao/WordAssessmentEventDao.java @@ -19,6 +19,9 @@ public interface WordAssessmentEventDao { @Query("SELECT * FROM WordAssessmentEvent ORDER BY time ASC") List loadAllOrderedByTimeAsc(); - @Query("SELECT * FROM WordAssessmentEvent ORDER BY time ASC") - Cursor loadAllOrderedByTimeAscAsCursor(); + @Query("SELECT * FROM WordAssessmentEvent ORDER BY time DESC") + Cursor loadAllOrderedByTimeDesc(); + + @Query("SELECT * FROM WordAssessmentEvent WHERE wordId = :wordId ORDER BY time DESC") + Cursor loadAllOrderedByTimeDesc(Long wordId); } diff --git a/app/src/main/java/ai/elimu/analytics/provider/WordAssessmentEventProvider.java b/app/src/main/java/ai/elimu/analytics/provider/WordAssessmentEventProvider.java index 0a59cb3..dbf7a34 100644 --- a/app/src/main/java/ai/elimu/analytics/provider/WordAssessmentEventProvider.java +++ b/app/src/main/java/ai/elimu/analytics/provider/WordAssessmentEventProvider.java @@ -8,6 +8,8 @@ import android.net.Uri; import android.util.Log; +import java.util.List; + import ai.elimu.analytics.BuildConfig; import ai.elimu.analytics.dao.WordAssessmentEventDao; import ai.elimu.analytics.db.RoomDb; @@ -17,10 +19,12 @@ public class WordAssessmentEventProvider extends ContentProvider { private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider.word_assessment_event_provider"; private static final String TABLE = "events"; private static final int CODE_EVENTS = 1; + private static final int CODE_EVENTS_BY_WORD_ID = 2; private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); static { MATCHER.addURI(AUTHORITY, TABLE, CODE_EVENTS); + MATCHER.addURI(AUTHORITY, TABLE + "/by-word-id/#", CODE_EVENTS_BY_WORD_ID); } @Override @@ -55,7 +59,22 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel // Get the Room Cursor RoomDb roomDb = RoomDb.getDatabase(context); WordAssessmentEventDao wordAssessmentEventDao = roomDb.wordAssessmentEventDao(); - Cursor cursor = wordAssessmentEventDao.loadAllOrderedByTimeAscAsCursor(); + Cursor cursor = wordAssessmentEventDao.loadAllOrderedByTimeDesc(); + Log.i(getClass().getName(), "cursor: " + cursor); + cursor.setNotificationUri(context.getContentResolver(), uri); + return cursor; + } else if (code == CODE_EVENTS_BY_WORD_ID) { + // Extract the Word ID from the URI + List pathSegments = uri.getPathSegments(); + Log.i(getClass().getName(), "pathSegments: " + pathSegments); + String wordIdAsString = pathSegments.get(2); + Long wordId = Long.valueOf(wordIdAsString); + Log.i(getClass().getName(), "wordId: " + wordId); + + // Get the Room Cursor + RoomDb roomDb = RoomDb.getDatabase(context); + WordAssessmentEventDao wordAssessmentEventDao = roomDb.wordAssessmentEventDao(); + Cursor cursor = wordAssessmentEventDao.loadAllOrderedByTimeDesc(wordId); Log.i(getClass().getName(), "cursor: " + cursor); cursor.setNotificationUri(context.getContentResolver(), uri); return cursor; diff --git a/utils/src/main/java/ai/elimu/analytics/utils/EventProviderUtil.java b/utils/src/main/java/ai/elimu/analytics/utils/EventProviderUtil.java index d1afa35..9f3aeb1 100644 --- a/utils/src/main/java/ai/elimu/analytics/utils/EventProviderUtil.java +++ b/utils/src/main/java/ai/elimu/analytics/utils/EventProviderUtil.java @@ -18,6 +18,7 @@ import ai.elimu.model.v2.gson.analytics.WordAssessmentEventGson; import ai.elimu.model.v2.gson.analytics.WordLearningEventGson; import ai.elimu.model.v2.gson.content.LetterGson; +import ai.elimu.model.v2.gson.content.WordGson; public class EventProviderUtil { @@ -210,4 +211,42 @@ public static List getWordAssessmentEventGsons(Context return wordAssessmentEventGsons; } + + public static List getWordAssessmentEventGsonsByWord(WordGson wordGson, Context context, String analyticsApplicationId) { + Log.i(EventProviderUtil.class.getName(), "getWordAssessmentEventGsonsByWord"); + + List wordAssessmentEventGsons = new ArrayList<>(); + + Uri wordAssessmentEventsUri = Uri.parse("content://" + analyticsApplicationId + ".provider.word_assessment_event_provider/events/by-word-id/" + wordGson.getId()); + Log.i(EventProviderUtil.class.getName(), "wordAssessmentEventsUri: " + wordAssessmentEventsUri); + Cursor wordAssessmentEventsCursor = context.getContentResolver().query(wordAssessmentEventsUri, null, null, null, null); + Log.i(EventProviderUtil.class.getName(), "wordAssessmentEventsCursor: " + wordAssessmentEventsCursor); + if (wordAssessmentEventsCursor == null) { + Log.e(EventProviderUtil.class.getName(), "wordAssessmentEventsCursor == null"); + Toast.makeText(context, "wordAssessmentEventsCursor == null", Toast.LENGTH_LONG).show(); + } else { + Log.i(EventProviderUtil.class.getName(), "wordAssessmentEventsCursor.getCount(): " + wordAssessmentEventsCursor.getCount()); + if (wordAssessmentEventsCursor.getCount() == 0) { + Log.e(EventProviderUtil.class.getName(), "wordAssessmentEventsCursor.getCount() == 0"); + } else { + boolean isLast = false; + while (!isLast) { + wordAssessmentEventsCursor.moveToNext(); + + // Convert from Room to Gson + WordAssessmentEventGson wordAssessmentEventGson = CursorToWordAssessmentEventGsonConverter.getWordAssessmentEventGson(wordAssessmentEventsCursor); + + wordAssessmentEventGsons.add(wordAssessmentEventGson); + + isLast = wordAssessmentEventsCursor.isLast(); + } + + wordAssessmentEventsCursor.close(); + Log.i(EventProviderUtil.class.getName(), "wordAssessmentEventsCursor.isClosed(): " + wordAssessmentEventsCursor.isClosed()); + } + } + Log.i(EventProviderUtil.class.getName(), "wordAssessmentEventGsons.size(): " + wordAssessmentEventGsons.size()); + + return wordAssessmentEventGsons; + } }