Now, this was OK for small courses with very few numbers of students, but even more than a handful it became very tedious.
So, to automate this a little, first I needed to find the mdl_context for the single student role I'd just added:
moodle=# SELECT * FROM mdl_context WHERE contextlevel = 50 AND instanceid = 21;
id | contextlevel | instanceid | path | depth
------+--------------+------------+-----------+-------
2578 | 50 | 21 | /1/3/2578 | 3
(1 row)
This gives us the the contextid (2578) for the course (21) that we just restored.
So, let's have a look at the student role assignment we just did manually:
moodle=# SELECT * FROM mdl_role_assignments WHERE contextid = 2578;
id | roleid | contextid | userid | timemodified | modifierid | component | itemid | sortorder
----+--------+-----------+--------+--------------+------------+-----------+--------+-----------
59 | 5 | 2578 | 1277 | 1442877255 | 8 | | 0 | 0
(1 row)
This shows us the student with userid (1277) and a student roleid (5) which we need to add for all the other participants who are students in this course. So, let's get the list of all participants enrolled in this course.
First we need to see what enrolments/methods have been used:
moodle=# SELECT id, enrol, courseid FROM mdl_enrol WHERE courseid = 21;
id | enrol | courseid
----+--------+----------
24 | manual | 21
(1 row)
Luckily, only a manual enrolment with id (24).
Now let's get the list of participants with this enrolid (24):
moodle=# SELECT * FROM mdl_user_enroments WHERE enrolid = 24;
That's great.
Now, let's insert records into mdl_role_assignments like the first one we created manually for all users (participants) except the first user we did manually, and filter the participants on a known value (we don't want to assign a student role to teachers or tutors) by joining the participants list against the user table and filtering on - in this case - the username with a known domain suffix):
moodle=# INSERT INTO mdl_role_assignments (roleid, contextid, userid, timemodified, modifierid) SELECT 5, 2578, ue.userid, 1442877255, 8 FROM mdl_user_enrolments ue LEFT JOIN mdl_user u ON ue.userid = u.id WHERE enrolid = 24 AND userid <> 1277 AND u.username LIKE '%@students.mydomain.edu.au';
INSERT 0 166
Now we have 167 participants, all with the "student" role id (5) in our mdl_role_assignments table, for the same manual enrolment event, against the correct mdl_context access context, and now we can see all the grades for all our (real) students in the Grades / Grader Report.
Done!