Gerald Q. Maguire

Inactivating users by program

Blog Post created by Gerald Q. Maguire on Aug 14, 2018

In my recent efforts to migrate data into Canvas from another reporting system one of the things that I found useful was to enroll all the users into the course and set their state as active, but for students who should not have access to the course (because they have taken a leave, quit, have an administrative hold on course participation, ...) - after adding their data to the gradebook - simply set their state to inactive (deactivate the student).

The code below shows how to do this in python:

def deactivate_user(course_id, enrollment_id):     global Verbose_Flag     #Request Method: POST     # DELETE /api/v1/courses/:course_id/enrollments/:id     # Scope: url:DELETE|/api/v1/courses/:course_id/enrollments/:id     # Conclude, deactivate, or delete an enrollment. If the task argument isn't given, the enrollment will be concluded.      url = baseUrl + '%s/enrollments/%s/?task=deactivate' % (course_id, enrollment_id)     if Verbose_Flag:        print("url: " + url)      r = requests.delete(url, headers = header)     if Verbose_Flag:            write_to_log("result of put of reactivate: " + r.text)     if r.status_code == requests.codes.ok:        page_response=r.json()        if Verbose_Flag:               print("deactivated {0} in course {1}".format(enrollment_id, course_id))        return True     else:        if r.status_code == 404: # "404 Not Found"               write_to_log("status code: " + str(r.status_code))        else:               write_to_log("status code: " + str(r.status_code))     return False 

One surprise in doing the above is that one has to use the student enrollment_id and not their user_id. One can find this enrollment_id with:

def active_user_in_course_by_user_id(course_id, user_id):        # Use the Canvas API to get the list of users enrolled in this course        #GET /api/v1/courses/:course_id/enrollments        # user_id string     Filter by user_id (only valid for course or section enrollment queries). If set to the current user's id, this is a way to determine if the user has any enrollments in the course or section, independent of whether the user has permission to view other people on the roster.         url = baseUrl + '%s/enrollments' % (course_id)        if Verbose_Flag:               print("url: " + url)         extra_parameters={'user_id': user_id,                          'enrollment_type[]': 'StudentEnrollment,TeacherEnrollment,TaEnrollment,DesignerEnrollment,ObserverEnrollment',                          'state[]': 'active'        }        print("extra_parameters={}".format(extra_parameters))        r = requests.get(url, params=extra_parameters, headers = header)        if Verbose_Flag:               write_to_log("result of getting enrollments: " + r.text)         if r.status_code == requests.codes.ok:               page_response=r.json()               return page_response        return None

Outcomes