LMS & eLearning

Snippet: Award Course Points When a LearnDash Essay Is Graded

Give learners bonus course points when an instructor grades their essay: how LearnDash totals course points, the course_points user meta it adds, the learndash_essay_all_quiz_data_updated hook and a verified snippet.

Snippet: Award Course Points When a LearnDash Essay Is Graded

Course points in LearnDash are earned by completing courses that have a points value, and they can gate access to other courses. Essay questions are graded by hand and carry quiz points, which are a different currency. Sites that want to reward strong written work with course points, for example so a learner who scores well on a reflective essay qualifies for an advanced course sooner, need a small bridge between the two. LearnDash 4.18.0.1 sums a bonus from user meta into every learner's course points total and fires an action when an essay grade is saved, which is all the bridge needs.

How course points are totalled

learndash_get_user_course_points( $user_id ) in includes/ld-users.php adds up two things: the course_points post meta of every course the user has completed, found through their course_completed_ user meta keys, and the value of a course_points user meta on the learner, formatted with learndash_format_course_points(). That user meta is the field an administrator edits on the user's profile as a manual adjustment, and it is exactly where a bonus belongs: it is additive, it survives course completions, and every access check that compares course points reads the total through this function.

The grading hook

When an instructor grades an essay from the essay edit screen, the quick edit on the Essays list, or the REST API, LearnDash updates the attempt's score and then fires do_action( 'learndash_essay_all_quiz_data_updated', $quiz_id, $question_id, $updated_scoring, $essay ). $updated_scoring has updated_question_score, the points now awarded for the essay, plus points_awarded_difference and score_difference describing how much the grade changed, which is what makes re-grading safe: a grade raised from 6 to 8 reports a difference of 2, and a grade lowered reports a negative number. $essay is the essay post, whose author is the learner.

The snippet

<?php
/**
 * Award one course point for every two essay points, adjusting on re-grade.
 */
add_action(
    'learndash_essay_all_quiz_data_updated',
    function ( $quiz_id, $question_id, $updated_scoring, $essay ) {
        if ( ! $essay instanceof WP_Post || ! isset( $updated_scoring['points_awarded_difference'] ) ) {
            return;
        }

        $user_id    = (int) $essay->post_author;
        $difference = (float) $updated_scoring['points_awarded_difference'];

        if ( ! $user_id || 0.0 === $difference ) {
            return;
        }

        // Conversion rate: essay points to course points.
        $bonus = $difference / 2;

        $current = (float) learndash_format_course_points( get_user_meta( $user_id, 'course_points', true ) );
        $new     = max( 0, $current + $bonus );

        update_user_meta( $user_id, 'course_points', learndash_format_course_points( (string) $new ) );

        // Keep a per-essay record so the award can be audited or reversed.
        update_post_meta( $essay->ID, 'wwc_course_points_awarded', (float) get_post_meta( $essay->ID, 'wwc_course_points_awarded', true ) + $bonus );
    },
    10,
    4
);

Working from the difference rather than the new score means grading the same essay twice does not double the award, and lowering a grade takes points back. The total is floored at zero because a learner cannot owe points. learndash_format_course_points() applies the site's decimal setting for points, so the stored value matches what the profile field would hold.

Choosing a rule

A fixed conversion is the simplest. Other common rules, each a small change to the $bonus line:

  • Pass threshold. Award a flat 5 points when updated_question_score reaches the essay's maximum, and 0 otherwise, using the question's points from the quiz settings. Track whether the flat award was already given in the essay meta so re-grades do not repeat it.
  • Per-quiz rate. Read a rate from quiz post meta so some quizzes are worth more.
  • Only on first grade. Skip when the essay meta already records an award, for sites that do not want re-grades to move course points.

Bulk grading and duplicate events

The Essays list lets instructors grade several essays in a row, and each save fires the hook once with its own difference, so the snippet handles bulk sessions correctly without extra work. The REST API path fires the same hook. What the hook does not fire for is an essay deleted outright; if you delete essays after grading, remove the matching bonus first using the per-essay meta the snippet keeps, or accept that deleted essays leave their points behind.

Where learners see the result

The profile shortcode and block show the course points total, and courses with a points requirement show the shortfall on their page. The number updates on the next page load after grading. If you want the learner told, hook the same action and send an email, or combine it with our group hooks guide to move the learner into an advanced group when their total crosses a line.

Interaction with quiz points

The essay's quiz points still count toward the quiz score and toward the quiz's own points total, unchanged. Course points are separate, so a leaderboard based on quiz points is unaffected, and a course that requires a quiz pass still requires it. The essay grading flow and its statuses are described in our guide to essay questions and manual grading, and the hook's siblings in the assignment and essay hooks reference.

Testing

  1. Note a learner's course points on their profile, then submit an essay as that learner on a quiz where the essay is worth 10 points.
  2. Grade it 8: the profile shows 4 more course points and the essay post carries the award meta.
  3. Re-grade to 6: the profile drops by 1 point; re-grade to 8 again: back up by 1.
  4. Open a course that requires more points than the learner has and confirm the shortfall message reflects the new total.

When to get help

A conversion rule is a short task. Point economies with leaderboards, badges and redemption, or reconciling points after a bulk re-grade, are projects our LearnDash services team takes on, and a LearnDash developer can add an admin report of awarded bonuses from the per-essay meta.

Frequently asked questions

How does LearnDash calculate a learner's course points?

learndash_get_user_course_points() sums the points of completed courses and adds the learner's course_points user meta, which is the manual adjustment field on their profile.

Which hook fires when an essay is graded?

learndash_essay_all_quiz_data_updated, from the essay edit screen, the Essays list quick grade and the REST API, with the quiz, question, scoring changes and the essay post.

What happens if an essay is re-graded?

The hook reports the difference in points, so the snippet adjusts the bonus up or down rather than awarding again.

Are quiz points and course points the same thing?

No. Essay points belong to the quiz score; course points gate course access and are what this snippet adds to.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.