Recording reading
tilttek/reading-questsA reading quest counts what phil/reading-progress measures: the furthest percent a reader reached in a document, and
the two events of a reading, started and completed. The library turns each step of reading into activities of
tilttek/activity, which every quest, goal or badge then reads the same way.
Books
A Book is a document as the quests see it: the identifier of its Document, its printed page count when it has one,
and up to 15 facts — a genre, a game system, a level, where the author comes from, whether it is a rules
supplement. Facts are named like an activity context entry (lower-case letters, digits and _, a letter first) and
hold a boolean, an integer or a string of at most 100 bytes. The name unit is reserved for the library.
use Tilttek\ReadingQuests\Book;
new Book('core-rulebook', 320, ['system' => 'dnd5e', 'author_region' => 'qc', 'supplement' => false]);
The facts are copied into the context of every activity of the book, so a quest step selects books by them. They describe the book, never the reader.
A BookShelfInterface finds a book by the identifier of its document. InMemoryBookShelf holds up to 100 000 books,
built from Books or from plain values with fromArray(), which refuses a key it does not know:
core-rulebook: { pages: 320, facts: { system: dnd5e, supplement: false } }
monster-manual: { pages: 352, facts: { system: dnd5e, supplement: true } }
novella: { facts: { genre: fantasy } } # no page count: measure it in minutes
The measure
The site picks one ReadingMeasure and keeps it:
| Measure | What it counts |
|---|---|
ReadingMeasure::pages() | The printed pages; every book counted needs a page count. |
ReadingMeasure::minutes($charactersPerMinute) | The length of the document divided by a reading speed, from 1 to 100 000 characters a minute, 1 000 by default, rounded up. |
The amount reached is the share of the furthest percent, rounded down: a book of 200 pages read to 37 % has given 74 pages. Once the reading is complete — at 100 %, or at the lower completion percent the page sets — the whole book counts, so the pages after the last read block are not lost.
The amount never comes from the time a page stays on screen. A tab left open gains nothing; a chapter read offline gains it all once its position arrives. Going back to reread gains nothing, since the furthest percent never goes back.
The activities
ReadingActivities::between() takes the book, the document, the reader, the progress before the step and the update
Progress::record() gave, and returns the activities of the step, in this order:
| Verb | When | Quantity | Context |
|---|---|---|---|
book.started | The update holds ReadingStarted | 1 | The book's facts |
book.read | The amount reached grew | The pages or minutes gained | The book's facts and unit, pages or minutes |
book.finished | The update holds ReadingCompleted | 1 | The book's facts |
Every activity has the reader as member, book:<identifier> as object, and is dated at the event or, for an amount, at
the moment of the position. The reader identifier given to Progress::record() is a resource reference, member:44.
Keys
Each activity has an idempotency key built from a SHA-256 digest of the reader, the book and the reading number,
followed by :started, :finished or :read:<amount reached>. The same step given twice gives the same keys, and the
journal counts it once: a request retried, a task run again or two tabs sending the same position count nothing twice.
Reading a book again
A reading has a number, from 1 to 1 000. When the application lets a reader read a book again from a fresh
Progress::none(), it passes the next number: the new reading counts again, started, pages and finished, under keys of
its own.
The recorder
ReadingRecorder does both in one call: it finds the book on the shelf, records the position on the progress, records
the activities in the journal, and returns a ReadingUpdate with the progress update, the journal's entries and the
amount newly counted.
$result = $recorder->record($before, $document, 'member:44', $position, $at, completionPercent: 95, reading: 1);
$row->progress = $result->update->progress->toString(); // store it after the journal recorded the activities
foreach ($result->update->events as $event) {
$dispatcher->dispatch($event);
}
$result->gained; // pages or minutes counted for the first time
The journal is written before anything is stored. When it refuses an activity — a position dated outside its window, thirty days before its clock by default —, the progress is not stored, and the same call made again counts nothing twice.
One progress at a time
The amount gained is read from the progress before the step. Two positions of the same reader in the same book recorded at once from the same stored progress would each count from it: the application serialises the writes of one progress — a row lock, or a lock per reader and document —, as it does already so that one position does not overwrite the other.