Add attendance from new student module

This commit is contained in:
Mikołaj Pich 2018-12-12 20:46:26 +01:00
parent cdc97c81b9
commit 424f7e2b80
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
11 changed files with 209 additions and 12 deletions

View file

@ -139,11 +139,11 @@ class Api {
fun getSemesters() = if (useNewStudent) studentStart.getSemesters() else snpStart.getSemesters()
fun getAttendance(startDate: LocalDate, endDate: LocalDate? = null) = snp.getAttendance(startDate, endDate)
fun getAttendance(startDate: LocalDate, endDate: LocalDate? = null) = if (useNewStudent) student.getAttendance(startDate, endDate) else snp.getAttendance(startDate, endDate)
fun getAttendanceSummary(subjectId: Int? = null) = snp.getAttendanceSummary(subjectId)
fun getAttendanceSummary(subjectId: Int? = -1) = if (useNewStudent) student.getAttendanceSummary(subjectId) else snp.getAttendanceSummary(subjectId)
fun getSubjects() = snp.getSubjects()
fun getSubjects() = if (useNewStudent) student.getSubjects() else snp.getSubjects()
fun getExams(startDate: LocalDate, endDate: LocalDate? = null) = if (useNewStudent) student.getExams(startDate, endDate) else snp.getExams(startDate, endDate)

View file

@ -1,14 +1,18 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
import pl.droidsonroids.jspoon.annotation.Selector
import java.util.*
class Attendance {
@SerializedName("IdPoraLekcji")
var number: Int = 0
@SerializedName("Data")
lateinit var date: Date
@SerializedName("PrzedmiotNazwa")
@Selector("span", defValue = "null")
lateinit var subject: String
@ -18,6 +22,9 @@ class Attendance {
@Selector("div", attr = "class")
lateinit var type: String // do not use
@SerializedName("IdKategoria")
var categoryId: Int = -1
var presence: Boolean = false
var absence: Boolean = false
@ -39,4 +46,14 @@ class Attendance {
const val EXCUSED_LATENESS = "x-sp-spr"
const val EXEMPTION = "x-sp-zwolnienie"
}
enum class Category(val id: Int, val title: String) {
PRESENCE(1, "Obecność"),
ABSENCE_UNEXCUSED(2, "Nieobecność nieusprawiedliwiona"),
ABSENCE_EXCUSED(3, "Nieobecność usprawiedliwiona"),
UNEXCUSED_LATENESS(4, "Spóźnienie nieusprawiedliwione"),
EXCUSED_LATENESS(5, "Spóźnienie usprawiedliwione"),
ABSENCE_FOR_SCHOOL_REASONS(6, "Nieobecność z przyczyn szkolnych"),
EXEMPTION(7, "Zwolnienie")
}
}

View file

@ -0,0 +1,13 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
import java.util.*
data class AttendanceRequest(
@SerializedName("data")
val date: Date,
@SerializedName("idTypWpisuFrekwencji")
val typeId: Int = -1
)

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
import pl.droidsonroids.jspoon.annotation.Format
import pl.droidsonroids.jspoon.annotation.Selector
import java.util.*
@ -22,6 +23,9 @@ class AttendanceResponse {
var lessons: List<Attendance> = emptyList()
}
@SerializedName("Frekwencje")
var lessons: List<Attendance> = emptyList()
@Selector("#idPrzedmiot option")
var subjects: List<Subject> = emptyList()

View file

@ -0,0 +1,8 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
data class AttendanceSummaryRequest(
@SerializedName("idPrzedmiot")
val id: Int?
)

View file

@ -0,0 +1,60 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
class AttendanceSummaryResponse {
@SerializedName("Podsumowanie")
var percentage: Double = .0
@SerializedName("Statystyki")
var items: List<Summary> = emptyList()
data class Summary(
@SerializedName("Id")
val id: Int,
@SerializedName("NazwaTypuFrekwencji")
val type: String,
@SerializedName("Wrzesien")
val september: Int,
@SerializedName("Pazdziernik")
val october: Int,
@SerializedName("Listopad")
val november: Int,
@SerializedName("Grudzien")
val december: Int,
@SerializedName("Styczen")
val january: Int,
@SerializedName("Luty")
val february: Int,
@SerializedName("Marzec")
val march: Int,
@SerializedName("Kwiecien")
val april: Int,
@SerializedName("Maj")
val may: Int,
@SerializedName("Czerwiec")
val june: Int,
@SerializedName("Lipiec")
val july: Int,
@SerializedName("Sierpien")
val august: Int,
@SerializedName("Razem")
val total: Int
)
}

View file

@ -1,12 +1,15 @@
package io.github.wulkanowy.api.attendance
import com.google.gson.annotations.SerializedName
import pl.droidsonroids.jspoon.annotation.Selector
data class Subject(
@SerializedName("Nazwa")
@Selector("option")
var name: String = "Wszystkie",
@SerializedName("Id")
@Selector("option", attr = "value")
var value: Int = -1
)

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.api.repository
import io.github.wulkanowy.api.attendance.*
import io.github.wulkanowy.api.exams.Exam
import io.github.wulkanowy.api.exams.ExamRequest
import io.github.wulkanowy.api.getGradeShortValue
@ -15,12 +16,61 @@ import io.github.wulkanowy.api.timetable.TimetableResponse
import io.github.wulkanowy.api.toDate
import io.github.wulkanowy.api.toFormat
import io.github.wulkanowy.api.toLocalDate
import io.reactivex.Observable
import io.reactivex.Single
import org.jsoup.Jsoup
import org.threeten.bp.LocalDate
import org.threeten.bp.Month
class StudentRepository(private val api: StudentService) {
// private val times by lazy { api.getUserCache().map { it.data?.times } }
fun getAttendance(startDate: LocalDate, endDate: LocalDate? = null): Single<List<Attendance>> {
val end = endDate ?: startDate.plusDays(4)
return api.getAttendance(AttendanceRequest(startDate.toDate())).map { it.data?.lessons }
.flatMapObservable { Observable.fromIterable(it) }
.map { a ->
// .flatMap { a ->
// times.flatMapObservable { times ->
// Observable.fromIterable(times.filter { time -> time.id == a.categoryId })
// }.map {
a.apply {
presence = a.categoryId == Attendance.Category.PRESENCE.id || a.categoryId == Attendance.Category.ABSENCE_FOR_SCHOOL_REASONS.id
absence = a.categoryId == Attendance.Category.ABSENCE_UNEXCUSED.id || a.categoryId == Attendance.Category.ABSENCE_EXCUSED.id
lateness = a.categoryId == Attendance.Category.EXCUSED_LATENESS.id || a.categoryId == Attendance.Category.UNEXCUSED_LATENESS.id
excused = a.categoryId == Attendance.Category.ABSENCE_EXCUSED.id || a.categoryId == Attendance.Category.EXCUSED_LATENESS.id
exemption = a.categoryId == Attendance.Category.EXEMPTION.id
name = Attendance.Category.values().single { category -> category.id == categoryId }.title
}
// }
}.filter {
it.date.toLocalDate() >= startDate && it.date.toLocalDate() <= end
}.toList().map { it.sortedWith(compareBy({ it.date }, { it.number })) }
}
fun getAttendanceSummary(subjectId: Int?): Single<List<AttendanceSummary>> {
return api.getAttendanceStatistics(AttendanceSummaryRequest(subjectId)).map { it.data?.items }.map {
listOf(
AttendanceSummary(Month.SEPTEMBER, it[0].september, it[1].september, it[2].september, it[3].september, it[4].september, it[5].september, it[6].september),
AttendanceSummary(Month.OCTOBER, it[0].october, it[1].october, it[2].october, it[3].october, it[4].october, it[5].october, it[6].october),
AttendanceSummary(Month.NOVEMBER, it[0].november, it[1].november, it[2].november, it[3].november, it[4].november, it[5].november, it[6].november),
AttendanceSummary(Month.DECEMBER, it[0].december, it[1].december, it[2].december, it[3].december, it[4].december, it[5].december, it[6].december),
AttendanceSummary(Month.JANUARY, it[0].january, it[1].january, it[2].january, it[3].january, it[4].january, it[5].january, it[6].january),
AttendanceSummary(Month.FEBRUARY, it[0].february, it[1].february, it[2].february, it[3].february, it[4].february, it[5].february, it[6].february),
AttendanceSummary(Month.MARCH, it[0].march, it[1].march, it[2].march, it[3].march, it[4].march, it[5].march, it[6].march),
AttendanceSummary(Month.APRIL, it[0].april, it[1].april, it[2].april, it[3].april, it[4].april, it[5].april, it[6].april),
AttendanceSummary(Month.MAY, it[0].may, it[1].may, it[2].may, it[3].may, it[4].may, it[5].may, it[6].may),
AttendanceSummary(Month.JUNE, it[0].june, it[1].june, it[2].june, it[3].june, it[4].june, it[5].june, it[6].june)
).filterNot { it.absence == 0 && it.absenceExcused == 0 && it.absenceForSchoolReasons == 0 && it.exemption == 0 && it.lateness == 0 && it.latenessExcused == 0 && it.presence == 0 }
}
}
fun getSubjects(): Single<List<Subject>> {
return api.getAttendanceSubjects().map { it.data }
}
fun getExams(startDate: LocalDate, endDate: LocalDate? = null): Single<List<Exam>> {
val end = endDate ?: startDate.plusDays(4)
return api.getExams(ExamRequest(startDate.toDate(), startDate.year)).map { res ->

View file

@ -1,6 +1,7 @@
package io.github.wulkanowy.api.service
import io.github.wulkanowy.api.ApiResponse
import io.github.wulkanowy.api.attendance.*
import io.github.wulkanowy.api.exams.ExamRequest
import io.github.wulkanowy.api.exams.ExamResponse
import io.github.wulkanowy.api.grades.GradeRequest
@ -11,6 +12,7 @@ import io.github.wulkanowy.api.mobile.Device
import io.github.wulkanowy.api.notes.NotesResponse
import io.github.wulkanowy.api.register.Diary
import io.github.wulkanowy.api.register.HomepageResponse
import io.github.wulkanowy.api.timetable.CacheResponse
import io.github.wulkanowy.api.timetable.TimetableRequest
import io.github.wulkanowy.api.timetable.TimetableResponse
import io.reactivex.Single
@ -21,7 +23,7 @@ import retrofit2.http.Url
interface StudentService {
@POST("UczenCache.mvc/Get")
fun getUserCache()
fun getUserCache(): Single<ApiResponse<CacheResponse>>
@POST
fun getSchoolInfo(@Url url: String): Single<ApiResponse<List<Diary>>>
@ -33,13 +35,13 @@ interface StudentService {
fun getGrades(@Body gradeRequest: GradeRequest): Single<ApiResponse<GradesResponse>>
@POST("Frekwencja.mvc/Get")
fun getAttendance()
fun getAttendance(@Body attendanceRequest: AttendanceRequest): Single<ApiResponse<AttendanceResponse>>
@POST("FrekwencjaStatystyki.mvc/Get")
fun getAttendanceStatistics()
fun getAttendanceStatistics(@Body attendanceSummaryRequest: AttendanceSummaryRequest): Single<ApiResponse<AttendanceSummaryResponse>>
@POST("FrekwencjaStatystykiPrzedmioty.mvc/Get")
fun getAttendanceSubjects()
fun getAttendanceSubjects(): Single<ApiResponse<List<Subject>>>
@POST("EgzaminyZewnetrzne.mvc/Get")
fun getExternalExaminations()

View file

@ -0,0 +1,34 @@
package io.github.wulkanowy.api.timetable
import com.google.gson.annotations.SerializedName
import java.util.*
class CacheResponse {
@SerializedName("poryLekcji")
lateinit var times: List<Time>
class Time {
@SerializedName("Id")
var id: Int = 0
@SerializedName("Numer")
var number: Int = 0
@SerializedName("Poczatek")
lateinit var start: Date
@SerializedName("Koniec")
lateinit var end: Date
@SerializedName("DataModyfikacji")
lateinit var modified: Date
@SerializedName("IdJednostkaSprawozdawcza")
var organizationUnitId: Int = 0
@SerializedName("Nazwa")
lateinit var name: String
}
}

View file

@ -71,7 +71,7 @@ class ApiRemoteTest : BaseTest() {
assertEquals("Jan Kowalski", studentName)
assertEquals("123456", schoolSymbol)
assertEquals(1, studentId)
// assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName)
assertEquals("Publiczna szkoła Wulkanowego nr 1 w fakelog.cf", schoolName)
}
}
@ -188,7 +188,9 @@ class ApiRemoteTest : BaseTest() {
exams.subscribe(examsObserver)
examsObserver.assertComplete()
examsObserver.values()[0][0].run {
val values = examsObserver.values()[0]
values[0].run {
assertEquals(getDate(2018, 5, 7), date)
assertEquals(getDate(1970, 1, 1), entryDate)
assertEquals("Matematyka", subject)
@ -202,12 +204,14 @@ class ApiRemoteTest : BaseTest() {
@Test
fun homeworkTest() {
val homework = api.getHomework(getLocalDate(2017, 10, 24))
val homework = api.getHomework(getLocalDate(2018, 9, 11))
val homeworkObserver = TestObserver<List<Homework>>()
homework.subscribe(homeworkObserver)
homeworkObserver.assertComplete()
homeworkObserver.values()[0][1].run {
val values = homeworkObserver.values()[0]
values[1].run {
assertEquals(getDate(2017, 10, 24), date)
assertEquals(getDate(2017, 10, 18), entryDate)
assertEquals("Metodologia programowania", subject)
@ -224,7 +228,9 @@ class ApiRemoteTest : BaseTest() {
notes.subscribe(notesObserver)
notesObserver.assertComplete()
notesObserver.values()[0][0].run {
val values = notesObserver.values()[0]
values[0].run {
assertEquals(getDate(2018, 1, 16), date)
assertEquals("Lech Wałęsa", teacher)
assertEquals("", teacherSymbol)