Skip to content

Commit

Permalink
OPPIA-1369, OPPIA-1379: Fixes in utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoMoreta committed Sep 20, 2023
1 parent e67624e commit 82598d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
28 changes: 16 additions & 12 deletions app/src/main/java/org/digitalcampus/oppia/utils/CourseUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ object CourseUtils {
val coursesServerResponse = Gson().fromJson(
coursesCachedStr, CoursesServerResponse::class.java)
for (course in courses) {
checkCourseStatuses(course, coursesServerResponse.courses, fromTimestamp)
coursesServerResponse.courses?.let {
checkCourseStatuses(course, it, fromTimestamp)
}
}
} else {
for (course in courses) {
Expand All @@ -55,7 +57,7 @@ object CourseUtils {
if (fromTimestamp != null && courseServer.version <= fromTimestamp) {
continue
}
if (TextUtilsJava.equals(course.shortname, courseServer.shortname)) {
if (TextUtilsJava.equals(course.getShortname(), courseServer.shortname)) {
val toUpdate = course.versionId < courseServer.version
course.isToUpdate = toUpdate
course.isToDelete = false
Expand All @@ -77,9 +79,11 @@ object CourseUtils {
coursesCachedStr, CoursesServerResponse::class.java)
val coursesServer = coursesServerResponse.courses
notInstalledCourses = ArrayList()
for (courseServer in coursesServer) {
if (!isCourseInstalled(courseServer, coursesInstalled)) {
notInstalledCourses.add(courseServer)
if (coursesServer != null) {
for (courseServer in coursesServer) {
if (!isCourseInstalled(courseServer, coursesInstalled)) {
notInstalledCourses.add(courseServer)
}
}
}
}
Expand All @@ -88,7 +92,7 @@ object CourseUtils {

private fun isCourseInstalled(courseServer: CourseServer, coursesInstalled: List<Course>): Boolean {
for (course in coursesInstalled) {
if (TextUtilsJava.equals(course.shortname, courseServer.shortname)) {
if (TextUtilsJava.equals(course.getShortname(), courseServer.shortname)) {
return true
}
}
Expand All @@ -100,10 +104,10 @@ object CourseUtils {
val db = DbHelper.getInstance(ctx)
val activities = course.getActivities(course.courseId.toLong())
for (a in activities) {
val langs = course.langs as ArrayList<Lang>
val langs = course.getLangs() as ArrayList<Lang>
var wordCount = 0
for (l in langs) {
var langContents = a.getFileContents(course.location, l.language) ?: continue
var langContents = a.getFileContents(course.getLocation(), l.language) ?: continue
// strip out all html tags from string (not needed for search nor wordcount)
langContents = langContents.replace("<.*?>".toRegex(), "").trim()
val langWordCount = langContents.split("\\s+".toRegex()).size
Expand All @@ -130,8 +134,8 @@ object CourseUtils {
if (coursesCachedStr != null) {
val coursesServerResponse = Gson().fromJson(
coursesCachedStr, CoursesServerResponse::class.java)
for (courseServer in coursesServerResponse.courses) {
if (TextUtilsJava.equals(courseServer.shortname, course.shortname)) {
for (courseServer in coursesServerResponse.courses!!) {
if (TextUtilsJava.equals(courseServer.shortname, course.getShortname())) {
return courseServer.hasStatus(Course.STATUS_READ_ONLY)
}
}
Expand All @@ -147,8 +151,8 @@ object CourseUtils {
val coursesServerResponse = Gson().fromJson(
coursesCachedStr, CoursesServerResponse::class.java)
if (coursesServerResponse != null) {
for (courseServer in coursesServerResponse.courses) {
if (TextUtilsJava.equals(courseServer.shortname, course.shortname)) {
for (courseServer in coursesServerResponse.courses!!) {
if (TextUtilsJava.equals(courseServer.shortname, course.getShortname())) {
refreshCachedStatus(course, courseServer.status)
refreshCachedCohorts(course, courseServer.isRestricted, courseServer.restrictedCohorts)
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/digitalcampus/oppia/utils/SearchUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ object SearchUtils {

db.beginTransaction()
for (a in activities) {
val langs = course.langs
val langs = course.getLangs()
val fileContents = StringBuilder()
for (l in langs) {
var langContents = a.getFileContents(course.location, l.language) ?: continue
var langContents = a.getFileContents(course.getLocation(), l.language) ?: continue
// strip out all html tags from string (not needed for search)
langContents = langContents.replace("<.*?>".toRegex(), "").trim()
fileContents.append(langContents)
}
val act = db.getActivityByDigest(a.digest)
if (act != null && fileContents.isNotEmpty()) {
db.insertActivityIntoSearchTable(course.titleJSONString,
course.getSection(a.sectionId).titleJSONString,
a.titleJSONString,
db.insertActivityIntoSearchTable(course.getTitleJSONString(),
course.getSection(a.sectionId)?.getTitleJSONString(),
a.getTitleJSONString(),
act.dbId,
fileContents.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:
private val courseTitles = ArrayList<Lang>()
private val courseGamification = ArrayList<GamificationEvent>()
private val courseLangs = ArrayList<Lang>()
private val courseBaseline = ArrayList<Activity?>()
private val sections = ArrayList<Section?>()
private val courseMetaPages = ArrayList<CourseMetaPage?>()
private val courseBaseline = ArrayList<Activity>()
private val sections = ArrayList<Section>()
private val courseMetaPages = ArrayList<CourseMetaPage>()
override val courseMedia: MutableList<Media> = ArrayList()

//Vars for traversing the tree
Expand Down Expand Up @@ -169,40 +169,40 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:

override fun endElement(aUri: String, aLocalName: String, aQName: String) {
if (NODE_SECTION == aQName) {
currentSection?.setTitles(sectTitles)
sectTitles?.let { currentSection?.setTitles(it) }
if (currentSection!!.isProtectedByPassword) {
currentSection?.isUnlocked = db.sectionUnlocked(courseId, currentSection!!.order, userId)
}
sections.add(currentSection)
sections.add(currentSection!!)
parentElements.pop()
} else if (NODE_TITLE == aQName) {
if (chars!!.isEmpty()) return
if (NODE_SECTION == parentElements.peek()) {
sectTitles?.add(Lang(currentLang, chars.toString()))
sectTitles?.add(Lang(currentLang!!, chars.toString()))
} else if (NODE_ACTIVITY == parentElements.peek()) {
actTitles?.add(Lang(currentLang, chars.toString()))
actTitles?.add(Lang(currentLang!!, chars.toString()))
} else if (NODE_META == parentElements.peek()) {
courseTitles.add(Lang(if (currentLang == null) App.DEFAULT_LANG else currentLang, chars.toString()))
courseTitles.add(Lang(currentLang ?: App.DEFAULT_LANG, chars.toString()))
} else if (NODE_PAGE == parentElements.peek()) {
pageTitles?.add(Lang(if (currentLang == null) App.DEFAULT_LANG else currentLang, chars.toString()))
pageTitles?.add(Lang(currentLang ?: App.DEFAULT_LANG, chars.toString()))
}
} else if (NODE_LOCATION == aQName) {
if (chars!!.isEmpty()) return
if (NODE_ACTIVITY == parentElements.peek()) {
actLocations?.add(Lang(currentLang, chars.toString()))
actLocations?.add(Lang(currentLang!!, chars.toString()))
} else if (NODE_PAGE == parentElements.peek()) {
pageLocations?.add(Lang(currentLang, chars.toString()))
pageLocations?.add(Lang(currentLang!!, chars.toString()))
}
} else if (NODE_CONTENT == aQName) {
if (chars!!.isNotEmpty() && NODE_ACTIVITY == parentElements.peek()) {
actContents?.add(Lang(currentLang, chars.toString()))
actContents?.add(Lang(currentLang!!, chars.toString()))
}
} else if (NODE_DESCRIPTION == aQName) {
if (chars!!.isEmpty()) return
if (NODE_ACTIVITY == parentElements.peek()) {
actDescriptions?.add(Lang(currentLang, chars.toString()))
actDescriptions?.add(Lang(currentLang!!, chars.toString()))
} else if (NODE_META == parentElements.peek()) {
courseDescriptions.add(Lang(if (currentLang == null) App.DEFAULT_LANG else currentLang, chars.toString()))
courseDescriptions.add(Lang(currentLang ?: App.DEFAULT_LANG, chars.toString()))
}
} else if (NODE_VERSIONID == aQName) {
if (chars!!.isEmpty()) return
Expand All @@ -218,21 +218,21 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:
courseSequencingMode = chars.toString()
}
} else if (NODE_ACTIVITY == aQName) {
currentActivity?.setTitles(actTitles)
currentActivity?.setDescriptions(actDescriptions)
currentActivity?.setLocations(actLocations)
currentActivity?.setContents(actContents)
currentActivity?.media = actMedia
currentActivity?.setGamificationEvents(actGamification)
currentActivity?.setTitles(actTitles!!)
currentActivity?.setDescriptions(actDescriptions!!)
currentActivity?.setLocations(actLocations!!)
currentActivity?.setContents(actContents!!)
currentActivity?.media = actMedia!!
currentActivity?.setGamificationEvents(actGamification!!)
parentElements.pop()
if (NODE_SECTION == parentElements.peek()) {
currentActivity?.sectionId = currentSection!!.order
currentActivity?.completed = db.activityCompleted(courseId.toInt(), currentActivity?.digest, userId)
currentSection?.addActivity(currentActivity)
currentSection?.addActivity(currentActivity!!)
} else if (NODE_META == parentElements.peek()) {
currentActivity?.sectionId = 0
currentActivity?.isAttempted = db.activityAttempted(courseId, currentActivity?.digest, userId)
courseBaseline.add(currentActivity)
courseBaseline.add(currentActivity!!)
}
} else if (NODE_MEDIA == aQName) {
parentElements.pop()
Expand All @@ -257,7 +257,7 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:
}
}
}
courseMetaPages.add(currentPage)
courseMetaPages.add(currentPage!!)
parentElements.pop()
} else if (NODE_GAMIFICATION == aQName) {
parentElements.pop()
Expand All @@ -284,7 +284,7 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:
c.imageFile = courseImage
c.priority = coursePriority
c.setTitles(courseTitles)
c.langs = courseLangs
c.setLangs(courseLangs)
c.setDescriptions(courseDescriptions)
c.baselineActivities = courseBaseline
c.media = courseMedia
Expand All @@ -295,7 +295,7 @@ internal class CourseXMLHandler(private val courseId: Long, private val userId:
&& ((courseSequencingMode == Course.SEQUENCING_MODE_COURSE)
|| (courseSequencingMode == Course.SEQUENCING_MODE_SECTION)
|| (courseSequencingMode == Course.SEQUENCING_MODE_NONE))) {
c.sequencingMode = courseSequencingMode
c.sequencingMode = courseSequencingMode!!
}
return c
}
Expand Down

0 comments on commit 82598d9

Please sign in to comment.