# Connect Moodle for theory courses

Connecting your Moodle site lets PiGi create a Moodle account for each student, enrol them on the Moodle course linked to their PiGi course, and send them straight into it signed in. You need a Moodle site you administer, web services switched on, the User key authentication plugin, and a web-service token.

Source: https://help.pigi.io/guides/integrations/lms/moodle

## Quick answers

**What does connecting Moodle do?**
When a student is enrolled on a scheduled run of a PiGi course that links a Moodle
course, PiGi finds their Moodle account by email address (or creates one) and enrols
them on that Moodle course. The student then opens Moodle from their PiGi portal
already signed in.

**Where do I connect Moodle?**
Company → Settings → Integrations, in the **Learning Management Systems (LMS)**
section. Open the **Moodle** tile, click **Add credentials**, enter the **Moodle URL**
and **Web-service token**, and click **Save**.

**Which Moodle web-service functions does the token need?**
All five: `core_user_get_users_by_field` (find the student),
`core_user_create_users` (create a missing student), `core_user_update_users` (turn
off the change-password prompt), `enrol_manual_enrol_users` (enrol them) and
`auth_userkey_request_login_url` (sign them in from PiGi).

**Where does `auth_userkey_request_login_url` come from?**
From the free **User key authentication** plugin (`auth_userkey`). Install it, enable
it, and set its mapping field to email address. Without it, enrolment still works but
every sign-in from PiGi fails.

**Do students get a Moodle password?**
No. PiGi creates the account with a random password and Moodle sends no email. The
student reaches Moodle through the course button in their PiGi portal.

**I linked Moodle after students were already enrolled. How do I add them?**
Open the student's course, go to the **Learning** tab and click **Register in
Moodle**. A student who opens Moodle from their portal is also registered at that
moment.

**Why did the token field go blank after I saved?**
The token is never shown again. Leave it blank when you change only the URL and the
stored token is kept.

**Which link should I paste on the course?**
The Moodle course page link, ending in `course/view.php?id=` and a number. PiGi reads
the number to know which Moodle course to enrol into.

## What this does

Moodle hosts your theory material; PiGi keeps the student record. The connection
saves you from creating Moodle accounts and enrolments by hand. Moodle itself is free
open-source software — costs depend on how you host it (see moodle.org or your
hosting provider).

## Before you start

- The **admin** role in PiGi.
- Site administrator access to your Moodle site, including permission to install
  plugins.
- The PiGi course already created, with its matching Moodle course already built.
- Every student needs an email address in PiGi; it is how Moodle accounts are matched.

## Set up Moodle

Menu names below are from the Moodle documentation for current versions.

1. **Enable web services:** Site administration → Advanced features → tick **Enable
   web services** → **Save changes**.
2. **Enable REST:** Site administration → Server → Web services → Manage protocols →
   enable **REST protocol**.
3. **Enable manual enrolments:** make sure **Manual enrolments** is enabled in Manage
   enrol plugins, and on each Moodle course (Participants → Enrolment methods, eye
   icon open).
4. **Install User key authentication** (`auth_userkey`) from the Moodle plugins
   directory, enable it under Site administration → Plugins → Authentication → Manage
   authentication, and set its **mapping field** to email address.
5. **Create a dedicated user**, for example "PiGi integration", and give it a
   site-level role holding every capability the five functions need. Moodle lists
   the required capabilities beside each function when you add it; a site-level
   Manager role normally covers them. The user also needs `webservice/rest:use` and
   `auth/userkey:generatekey`.
6. **Create the service:** Site administration → Server → Web services → External
   services → **Add**. Name it, tick **Enabled** and **Authorised users only**, then
   **Add service**.
7. Click **Add functions** and add the five functions listed in Quick answers.
8. Click **Authorised users** for the service and add your dedicated user.
9. **Create the token:** Site administration → Server → Web services → Manage tokens →
   **Add**. Pick the dedicated user and your service, save, and copy the token.

## Connect it to PiGi

1. Open **Company → Settings → Integrations** and click the **Moodle** tile.
2. Click **Add credentials** (marked **Needed**).
3. Enter **Moodle URL** — your site's address, for example
   `https://learn.examplecompany.com` — and paste the **Web-service token**.
4. Click **Save**. The toast reads "Moodle settings saved" and the sheet shows
   **Connected** and "Credentials saved".
5. Open each PiGi course's **Details** tab. Under **Learning platforms**, click
   **Add**, choose **Platform** → **Moodle**, paste the **Link**, click **Done**, then
   **Save changes**.

## Test it

1. Enrol a test student (for example Example Person, person@examplecompany.com) on a
   scheduled run of the linked course.
2. Open that student's course and its **Learning** tab. Within a minute the Moodle row
   should show **Registered**; if not, click **Register in Moodle**.
3. In Moodle, check the student appears on the course participants list.
4. Sign in to the portal as the student and click the **Moodle** button on the
   course. Moodle should open signed in on that course.

## Troubleshooting

| What you see | What to do |
|---|---|
| "Moodle URL/token not configured for this school." | Add the URL and token in the Moodle tile. |
| "Couldn't read a course id from …" | Replace the course link with one ending `course/view.php?id=` and a number. |
| "Student has no email to match in Moodle." | Add an email address to the student. |
| "Moodle rejected `auth_userkey_request_login_url`…" | Install and enable User key authentication and add the function to your service. |
| "Couldn't sign you in to Moodle" (student) | Usually the same cause. The student can use **Open Moodle anyway** in the meantime. |
| **Failed** with "Invalid token" | Re-create the token in Manage tokens and save it again in PiGi. |
| **Failed** with an access or capability error | Give the dedicated user the missing capability, or add the missing function to the service. |

## Known limitations

- PiGi enrols students automatically only when they join a scheduled run. Adding a
  Moodle link to a course does not register students already on it — use **Register
  in Moodle**.
- Students are enrolled with Moodle's standard Student role.
- A Moodle failure never blocks the PiGi enrolment; it shows on the **Learning** tab.
- There is no **Test connection** for Moodle. Test with a real enrolment.
- Removing a student in PiGi does not unenrol them from Moodle.

## Sources

- [Using web services — Moodle docs](https://docs.moodle.org/en/Using_web_services)
- [Manual enrolment — Moodle docs](https://docs.moodle.org/en/Manual_enrolment)
- [Site security settings (usernames) — Moodle docs](https://docs.moodle.org/en/Site_policies)
- [enrol/manual:enrol capability — Moodle docs](https://docs.moodle.org/en/Capabilities/enrol/manual:enrol)
- [User key authentication plugin — Moodle plugins directory](https://marketplace.moodle.com/plugins/auth_userkey)
- [auth_userkey README — GitHub](https://github.com/catalyst/moodle-auth_userkey)
