Tracking progress

Storing the progress

The Klub plugin has built-in support for reading/writing the tracked progress into a field on the user blueprint.

site/blueprints/users/member.yml
# other settings

fields:
  progress: fields/klub/progress

  # other fields
You can adjust the name of the field with the config option klub.progress.field.

Seen this, done that

The Klub plugin ships with a simplistic progress-tracking system. It is implemented to store a list with page ID/UUID keys and nullable boolean values.

You can use that to have the member add (or reset) their progress on single pages. Let's assume we are building a course platform with multiple lessons in each course. With the help of the Klub plugin, you can track if a user completed a lesson by making the "next"-button trigger the klub/progress/add-endpoint.

Calling the klub/progress/removeThe endpoint will remove that one lesson (not all) from the completed lessons list.

You can use $page->hasProgress() or $user->hasProgress($page) to query if the current page has been marked as completed.

site/snippets/lesson-remove-next.php
<!-- form with POST to prevent browser URL preloading -->
<?php
// only show reset if progress has been tracked
if ($page->hasProgress()) { ?>
<form method="POST" action="<?= site()->url() ?>/klub/progress/remove">
    <input type="hidden" name="redirect" value="<?= $page->url() ?>">
    <input type="hidden" name="id" value="<?= $page->uuid() ?>">
    <input type="hidden" name="token" value="<?= csrf() ?>">
    <button type="submit">Reset progress</button>
</form>
<?php } ?>
<form method="POST" action="<?= site()->url() ?>/klub/progress/add">
    <?php
    // find next lesson...
    $next = $page->nextListed();
    if (! $next) {
        // or next course
        $next = $page->parent()->nextListed();
    }
    if (! $next) {
        // or home
        $next = site()->homePage();
    }
    ?>
    <input type="hidden" name="redirect" value="<?= $next->url() ?>">
    <input type="hidden" name="id" value="<?= $page->uuid() ?>">
    <input type="hidden" name="token" value="<?= csrf() ?>">
    <button type="submit">☑︎ &nbsp;Complete and Next</button>
</form>

Alternatives to klub()->progress($page) would be...

klub()->progress($page)
kirby()->user()->hasProgress($page)
$page->hasProgress(kirby()->user())
$page->hasProgress()
Kirby Klub is not affiliated with the developers of Kirby CMS. We are merely standing on the shoulder of giants.
© 2024 Bruno Meilick All rights reserved.