Recording reading
tilttek/reading-quests-bundleTilttek\ReadingQuestsBundle\Application\ReadingTracker records where a reader is in a book. The application calls it
when the reader's place arrives — a controller that receives the block and the offset, a message handler for positions
sent offline.
$update = $tracker->record('member:44', $document, new Position('chapter-3', 120));
$update->gained; // the pages or minutes newly counted
$update->update->progress; // the progress now stored
$update->appended; // the entries of the activity journal
What one call does
- The reader is checked: a resource reference such as
member:44, of at most 128 bytes. - The latest reading of the book by the reader is read, or the first one when the reader never opened it.
ReadingRecorderrecords the position on that progress, at the clock's time, with the completion percent of the configuration, and records its activities in the journal oftilttek/activity-bundle:book.started,book.readwith the pages or minutes gained,book.finished.- The reading is stored at its new progress, at the version it was read at.
ReadingStartedandReadingCompletedare dispatched (Events).
A document the shelf does not hold throws UnknownBook, a block the document does not hold InvalidPosition, before
anything is recorded.
The time
The time of a position is the clock's — the clock service of FrameworkBundle —, never one the browser sends: a reader
cannot date their reading in the past to fill a year's goal.
Writers at once
Two tabs, or a retried request, may send positions of the same reading at once. The journal is written first: its
activities carry idempotency keys of the reader, the book, the reading and the amount reached, so the same step counts
once. The reading is then written where it is still at the version it was read at; the second writer meets
ReadingConflict and stores nothing. The application catches it and records the position again, or tells the reader
to reload: nothing is counted twice either way.
try {
$tracker->record($reader, $document, $position);
} catch (ReadingConflict) {
$tracker->record($reader, $document, $position);
}
Reading a book again
Once a book is finished, readAgain($reader, $book) starts its next reading, not started, under the next number: its
positions count again, a second book.finished included. A reader reads one book at most 1 000 times. A book not
finished, or never opened, throws InvalidReadingQuest.
current($reader, $book) gives the latest reading — its number, its progress, its version —, the first one not started
when the reader never opened the book: what a page shows to resume reading with Progress::resume().
Quests
The quest steps of tilttek/reading-quests read the activities the tracker records: ReadingSteps::start(),
finish() and read() select books by their facts. The quests themselves are kept by tilttek/quests-bundle.