Skip to main content

Connecting Moodle

The Moodle capa lets agents read and work in your Moodle site. It needs Moodle 4.1 LTS or newer. Agents use one dedicated service account, so oc8 sees and can do only what that account may.

Budget about 15 minutes. You do the Moodle side once, then the oc8 side.

What it does​

Reading. Agents can look at:

  • courses and course contents;
  • participants and users;
  • grades and activity completion;
  • assignments and submissions, including who hasn't submitted;
  • forums and their discussions.

Acting. Agents can:

  • create and update courses and users;
  • enrol and unenrol users;
  • grade a submission with feedback;
  • send messages;
  • post to forums.

Every action runs through oc8 guardrails and approvals. See Guardrails below.

Before you start​

  • Moodle 4.1 LTS or newer.
  • Admin rights in Moodle.
  • A site that oc8 can reach over https. Plain http is accepted only for localhost and host.docker.internal.

Moodle side​

Do these steps in Moodle as an administrator.

1. Enable web services​

Go to Site administration → General → Advanced features. Tick Enable web services and save.

2. Enable the REST protocol​

Go to Site administration → Server → Web services → Manage protocols. Enable REST protocol.

3. Create a dedicated user​

Go to Site administration → Users → Accounts → Add a new user. Create a user such as oc8-service. Use a strong password. Nobody needs to sign in as this user.

4. Give the user a role​

oc8 can use only what this user may do in Moodle. Give the user a role with the capabilities you want agents to have.

The simplest choice is the Manager role at system level:

  1. Go to Site administration → Users → Permissions → Assign system roles.
  2. Open Manager and add oc8-service.
  3. Go to Site administration → Users → Permissions → Define roles, open Manager and allow webservice/rest:use.

If you prefer a narrower role, create one with only the capabilities you need, plus webservice/rest:use.

note

The service user does not need to be enrolled in courses. oc8 asks Moodle for the courses the user can see without being enrolled, which a Manager can.

5. Create the external service​

  1. Go to Site administration → Server → Web services → External services.

  2. Under Custom services, click Add.

  3. Name it oc8, tick Enabled and Authorised users only, and save.

  4. Click Functions next to the service, then Add functions, and add all of these:

    core_completion_get_activities_completion_status
    core_course_create_courses
    core_course_get_contents
    core_course_get_courses_by_field
    core_course_search_courses
    core_course_update_courses
    core_enrol_get_enrolled_users
    core_message_send_instant_messages
    core_user_create_users
    core_user_get_users_by_field
    core_user_update_users
    core_webservice_get_site_info
    enrol_manual_enrol_users
    enrol_manual_unenrol_users
    gradereport_user_get_grade_items
    mod_assign_get_assignments
    mod_assign_get_submission_status
    mod_assign_get_submissions
    mod_assign_save_grade
    mod_forum_add_discussion
    mod_forum_add_discussion_post
    mod_forum_get_discussion_posts
    mod_forum_get_forum_discussions
  5. Go back to External services, click Authorised users next to oc8, and add oc8-service.

If you leave out a function, the tool that needs it stops working. You can add it later. The site-info tool tells you which ones are missing (see Troubleshooting).

6. Create a token​

  1. Go to Site administration → Server → Web services → Manage tokens.
  2. Click Create token.
  3. Choose the user oc8-service and the service oc8, then save.
  4. Copy the token. You enter it in oc8 in the next section.

oc8 side​

  1. Go to Capas → Moodle → Set up.
  2. Pick a Department. Agents in that department use the connection automatically.
  3. For Moodle site, create a Moodle site credential. Enter the site URL and the web service token. See Credentials.
  4. Click Save & test.

The card then shows Connected, and the connection lists 19 tools. Connected means only that oc8 started the Moodle bridge. It does not yet prove that the token works or that the oc8 service has every function. To confirm both, ask an agent to run site info (moodle_site_info) and check that missing_functions is empty. See Check the connection.

The URL must be exactly Moodle's own web address (its wwwroot), for example https://lms.example.org. Don't add /login or any other path. A different address, such as one that redirects to the real site, fails.

Then allow the tools for the team under Departments → Integrations & Permissions. See Capas and Departments.

Guardrails​

Each guardrail is a ready-made set of tools. Pick the one that fits the team. Reading is always free. Every change waits for a person to approve it, except in Read only, which cannot change anything.

GuardrailWhat it allows
Read onlyLooks at courses, participants, grades, submissions and forums. Changes nothing and contacts nobody.
Assist with approval (recommended)Reads freely. Every change waits for approval: grades, enrolments, accounts, courses, messages and forum posts.
Teaching assistantReviews submissions and progress and enters grades with feedback after approval. Cannot enrol, change accounts or courses, or send messages and forum posts.
Administration with approvalCreates and updates courses and accounts and manages enrolments, each after approval. Moodle may send its own account and welcome emails. Never grades, messages or posts to a forum.
Communication with approvalSends Moodle messages and forum posts, such as reminders, announcements and answers, each only after approval. Changes no grades, enrolments, accounts or courses.

Actions that reach people outside oc8​

Three tools put text in front of a learner. oc8 marks them outward and irreversible, because a message cannot be recalled. An agent may use each outward tool only once per person in a task, so put every recipient of a reminder in one message.

ToolWho is reached
Grade a submission with feedbackMoodle may notify the student, depending on the assignment's own "Notify student" setting.
Send a messageThe recipients get a direct message.
Post to a forumSubscribers are emailed.

Two more tools can make Moodle send its own system email. oc8 marks them irreversible, so the approval asks for confirmation, but not outward, so one task can create many accounts or enrol a person in several courses:

ToolWhat Moodle may send
Create a userMoodle emails the generated password to the new user.
Enrol a userMoodle sends the course welcome message, if the course sends one.

Approve these with care.

Unenrolling a user is destructive: Moodle can drop the user's course data.

Troubleshooting​

Errors reach the agent and show in the run. Find the text in the table.

Error textCauseFix
The Moodle token is invalid or expired. Create a new token for the oc8 service user.The token is wrong, was deleted or has expired.In Moodle, create a new token under Manage tokens and update the credential in oc8.
The external service does not allow function. Add it to the oc8 service in Moodle.The function is missing from the oc8 service, or the user isn't authorised for it.Add the function under External services → Functions. Check that oc8-service is under Authorised users.
The Moodle service user lacks permission for this: …The service user's role doesn't allow this action.Give the role the capability named in the message, or use a broader role such as Manager. Check that webservice/rest:use is allowed.
Moodle rejected the request (code): …Moodle refused the call for another reason, for example a missing or invalid value.Read the message after the colon. It comes straight from Moodle.
Moodle at URL redirected the request … Use Moodle's own web address (its wwwroot).The URL is not Moodle's wwwroot, for example http instead of https, or a missing www.Enter the address exactly as Moodle is configured.
Moodle at URL is not reachable (timed out after 20 s).The site is slow, down, or blocked by a firewall.Check that the site is up and that oc8 can reach it.
Moodle at URL is not reachable (…)The connection failed, for example a DNS or certificate error.Check the address, DNS and the site's certificate.
Moodle at URL did not answer with JSON.The address is not a Moodle site, or the REST protocol is off.Check the address. Enable REST under Manage protocols.
Moodle must be reached over https, not URL.The URL uses plain http.Use an https address. Plain http works only for localhost and host.docker.internal.

Check the connection​

Ask an agent to run the site info tool. It returns the site name, the Moodle version and the service user. It also returns missing_functions: a list of functions that the oc8 service lacks, each with the tool it disables. If the list is not empty, add those functions to the service in Moodle. If your Moodle is older than 4.1, it also returns a version warning.

Limits​

  • Lists return 50 items by default, up to 200. When a list is cut off, the tool says so, and the agent can ask for the next page.
  • Course search covers the first 200 matches.
  • Forum listing covers the 200 most recent discussions. Older ones aren't listed.
  • A message goes to at most 50 users at once.
  • Grading a submission needs the assignment's course as well as the assignment. oc8 refuses a grade above the assignment's maximum, and it refuses assignments graded on a scale: grade those in Moodle.