I am building scripts that use the Instructure Canvas REST API to maintain courses I teach.
I have sections for days of the week (like Monday, Tuesday etc).
I want to move a user (student) from one section to another, like Monday to Tuesday.
I'm not sure how to remove them from the section so I can add them to another section. I cannot find the syntax in the Instructure documentation.
It can be easily performed on the web GUI interface, but I want to script it as there will be bulk changes.
I can add a user to a section using this API call (in Python syntax):
#POST /api/v1/sections/:section_id/enrollments
url = "{0}/sections/{1}/enrollments".format(baseUrl,section_id)
extra_parameters={
'enrollment[user_id]': user_id,
'enrollment[type]': 'StudentEnrollment',
'enrollment[enrollment_state]': 'active'
}
But this just add the student to an additional section, so I needed to have removed the previous section first.
I tried guessing at the syntax:
# Use the Canvas API to get the remove a member from a named section in a course
#DELETE /api/v1/section/:section_id/enrollments
url = "{0}/sections/{1}/enrollments".format(baseUrl,section_id)
extra_parameters={
'enrollment[user_id]': user_id
}
But that does not work and gives me a 404. I could delete the whole section, but it would take many hundreds of enrollments to put everyone back who had not changed!
I managed to get this working after some further understanding of the Instructure Enrollment API specification. The wording there is very subtle.
I had imagined that there was a one-to-one mapping between enrollments and users, but this is not the case. A user can be enrolled many times in different sections in a course. Thus it is the enrollment_id
that is removed from a course and not a user_id
from a section.
The code fragment I needed was:
#DELETE /api/v1/courses/:course_id/enrollments/:id
url = "{0}/courses/{1}/enrollments/{2}".format(baseUrl,course_id,enrollment_id)