summaryrefslogtreecommitdiff
path: root/src/study/StudyRecord.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/study/StudyRecord.cpp')
-rw-r--r--src/study/StudyRecord.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/study/StudyRecord.cpp b/src/study/StudyRecord.cpp
new file mode 100644
index 0000000..330f572
--- /dev/null
+++ b/src/study/StudyRecord.cpp
@@ -0,0 +1,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;
+}