summaryrefslogtreecommitdiff
path: root/src/study/StudyRecord.cpp
blob: 330f5726641713ff6d22eefe3edd46d481b69d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "StudyRecord.h"

#include "StudySettings.h"
#include "../utils/TimeProvider.h"

ostream& operator<<(ostream& os, const StudyRecord& study)
{
    if(study.date <= QDateTime::currentDateTime())
    {
        const char* dateStr = study.date.toString(Qt::ISODate).toStdString().c_str();
        os << "(" << dateStr <<
            ", g" << study.grade << ", e" << study.easiness << ", " <<
            "i" << study.interval << ")";
    }
    else
        os << "(New card)";
    return os;
}

// Create "new" study record
StudyRecord::StudyRecord():
    date(QDateTime()),
    level(New),
    interval(0),
    grade(Unknown),
    easiness(StudySettings::inst()->initEasiness),
    recallTime(0),
    answerTime(0)
    {
    }

StudyRecord::StudyRecord(int level, int grade, double easiness, double interval):
    date(QDateTime()),
    recallTime(0),
    answerTime(0)
{
    this->level = level;
    this->interval = interval;
    this->grade = grade;
    this->easiness = easiness;
}

bool StudyRecord::operator==(const StudyRecord& aOther) const
    {
    return level == aOther.level &&
        date == aOther.date &&
        interval == aOther.interval &&
        grade == aOther.grade &&
        easiness == aOther.easiness;
    }

bool StudyRecord::timeTriggered() const
{
    if(date.isNull())
        return false;
    QDateTime nextRepetition = date.addSecs( (int)(interval * 60*60*24) );
    return nextRepetition <= TimeProvider::get();
}

int StudyRecord::getSecsToNextRepetition() const
{
    if(date.isNull())
        return 0;
    QDateTime nextRepetition = date.addSecs( (int)(interval * 60*60*24) );
    return TimeProvider::get().secsTo(nextRepetition);
}

bool StudyRecord::isOneDayOld() const
{
    return date.secsTo(TimeProvider::get()) / (60*60*24.) >=
            StudySettings::inst()->nextDayInterval;
}

bool StudyRecord::isLearning() const
{
    return (level == ShortLearning || level == LongLearning) &&
            !isOneDayOld();
}

int StudyRecord::getScheduledTodayReviews() const
{
    switch(level)
    {
    case New:
        return 3;
    case ShortLearning:
        return 2;
    case LongLearning:
        return 1;
    default:
        return 0;
    }
}

bool StudyRecord::isReviewedToday() const
{
    QDateTime recShiftedDate = shiftedDate(date);
    QDateTime curShiftedDate = shiftedDate(QDateTime::currentDateTime());
    return recShiftedDate.date() == curShiftedDate.date();
}

bool StudyRecord::isActivatedToday() const
{
    if(date.isNull())
        return false;
    QDateTime nextRepetition = date.addSecs( (int)(interval * 60*60*24) );
    QDateTime shiftedNextRep = shiftedDate(nextRepetition);
    QDateTime curShiftedDate = shiftedDate(QDateTime::currentDateTime());
    return shiftedNextRep.date() <= curShiftedDate.date();
}

QDateTime StudyRecord::shiftedDate(const QDateTime& aDate)
{
    return aDate.addSecs(-60 * 60 * StudySettings::inst()->dayShift);
}

void StudyRecord::setRecallTime(double time)
{
    recallTime = time;
    if(recallTime > MaxAnswerTime)
        recallTime = MaxAnswerTime;
}

void StudyRecord::setAnswerTime(double time)
{
    answerTime = time;
    if(answerTime > MaxAnswerTime)
        answerTime = MaxAnswerTime;
}