Search code examples
moodlemoodle-api

Moodle best way to get information about user points in course and the whole points in the course


I am seeking assistance in determining a user's overall score and the maximum achievable score in a course. My goal is to calculate the percentage of total points the user has achieved throughout the entire course. Additionally, I am interested in the ability to set and retrieve a predefined threshold, representing a specific percentage of points, through an API.

I initially attempted to use the core_completion_get_course_completion_status function. However, I encountered an error for some courses, indicating that completion criteria were not set. Subsequently, I considered downloading all activities in the course and retrieving user results from each. However, the volume of data obtained led me to reconsider this approach as it seems overly complex.

I am in search of a simpler method to achieve these goals. If you have any suggestions or insights, I would greatly appreciate your assistance.

Thank you.


Solution

  • I've solved the problem by using method gradereport_user_get_grade_items. From the response provided I managed to get a list of objects and iterate over it until I find an item with property: itemtype set to course and gradeisoverridden set to false. From this item I can get:

    • graderaw - raw value of the grade
    • gradeformatted - string representing formatted grade
    • grademin - minimal grade that can be obtained
    • grademax - maximum grade that can be obtained

    To the request I must add such headers:

    • wstoken - access token (with needed permissions)
    • wsfunction - moodle method (gradereport_user_get_grade_items)
    • moodlewsrestformat - optional (json, xml - default)
    • courseid - ID of the course
    • userid - ID of the user for whom the grade is being searched

    Curl example:

    curl --location '<YOUR_URL>/webservice/rest/server.php' \
    --form 'wstoken="<YOUR_TOKEN>"' \
    --form 'wsfunction="gradereport_user_get_grade_items"' \
    --form 'moodlewsrestformat="json"' \
    --form 'courseid="<YOUR_COURSE_ID>"' \
    --form 'userid="<YOUR_USER_ID>"'