Problem/Motivation

If a user is enrolled in a course, but does not have a tracked role in that course, an error is thrown by the getCourseCompletionPercentage function:

Drupal\moodle_rest\Services\MoodleRestException: User is not enrolled in this course in Drupal\moodle_rest\Services\MoodleRest->requestFunction() (line 159 of /app/web/modules/contrib/moodle_rest/src/Services/MoodleRest.ph

This can occur if the user has either no role at all or, for example, has the Course Creator role only.

Steps to reproduce

  1. Enrol a user in a course using whatever method.
  2. Via the Moodle Site Administration, remove all course roles from that user, eg remove Student.
  3. Execute the getCourseCompletionPercentage REST function.

No roles may be an edge case, but from some old discussion it does appear to be a supported state, even in current versions (tested with v4.5.2).
Not having the Student role seems a more likely scenario. The Moodle function core_completion_get_course_completion_status checks if a user is 'tracked' (see Moodle source) and throws an exception if not.

Proposed resolution

Add usernotenroled as a possible error code that will trigger a NULL return.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

gaddman created an issue. See original summary.

gaddman’s picture

Title: Error if user has no role in course » Error if user has no tracked role in course
Issue summary: View changes
gaddman’s picture

Status: Active » Needs review

MR7 working OK for me, no more failures when pulling course info from Moodle.

ekes made their first commit to this issue’s fork.

  • ekes committed e2af59a1 on 1.1.x authored by gaddman
    Issue #3523307 by gaddman: Error if user has no tracked role in course
    
ekes’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.