def test_enrollments_for_section_id(self): canvas = Enrollments() enrollments = canvas.get_enrollments_for_section_by_sis_id( "2013-autumn-PHYS-248-A--") self.assertEqual(len(enrollments), 2, "Has 2 canvas enrollments") students = canvas.get_enrollments_for_section_by_sis_id( "2013-autumn-PHYS-248-A--", {"role": "student"}) self.assertEqual(len(students), 1, "Has 1 student enrollments") enr = students[0] self.assertEqual(enr.current_score, 77.76) self.assertEqual(enr.current_grade, None) self.assertEqual(enr.final_score, 53.37) self.assertEqual(enr.final_grade, None) self.assertEqual(enr.unposted_current_score, 58.32) self.assertEqual(enr.unposted_final_score, 55.37) self.assertEqual(enr.unposted_current_grade, None) self.assertEqual(enr.unposted_final_grade, None) self.assertEqual(enr.override_score, 80.0) self.assertEqual(enr.override_grade, None) self.assertEquals(enr.sis_course_id, "2013-autumn-PHYS-248-A") self.assertEquals(enr.sws_course_id(), "2013,autumn,PHYS,248/A") self.assertEquals(enr.sis_section_id, "2013-autumn-PHYS-248-A--") self.assertEquals(enr.sws_section_id(), "2013,autumn,PHYS,248/A")
def test_enroll_user(self, mock_create): mock_create.return_value = None canvas = Enrollments() canvas.enroll_user("862539", "12345", "Student") mock_create.assert_called_with( '/api/v1/courses/862539/enrollments', { 'enrollment': {'user_id': '12345', 'type': 'Student'}})
def get_enrollments_for_course_by_sis_id(course_sis_id): canvas = Enrollments(per_page=200) enrollments = [] for enrollment in canvas.get_enrollments_for_course_by_sis_id( course_sis_id, {'state': [ENROLLMENT_ACTIVE]}): # Ignore the Canvas preview 'user' if 'StudentViewEnrollment' != enrollment.role: enrollments.append(enrollment) return enrollments
def get_sis_enrollments_for_user_in_course(user_sis_id, course_sis_id): canvas = Enrollments() enrollments = [] for enrollment in canvas.get_enrollments_for_course_by_sis_id( course_sis_id, {'user_id': canvas.sis_user_id(user_sis_id)}): try: valid_academic_section_sis_id(enrollment.sis_section_id) enrollments.append(enrollment) except CoursePolicyException: continue return enrollments
def test_enrollments_for_course_id(self): canvas = Enrollments() enrollments = canvas.get_enrollments_for_course_by_sis_id( "2013-autumn-PHYS-248-A") self.assertEqual(len(enrollments), 2, "Has 2 canvas enrollments") students = canvas.get_enrollments_for_course_by_sis_id( "2013-autumn-PHYS-248-A", {"role": "student"}) self.assertEqual(len(students), 1, "Has 1 student enrollments")
def test_enroll_user_in_course(self, mock_create): mock_create.return_value = None canvas = Enrollments() canvas.enroll_user_in_course("862539", "12345", "Student", course_section_id="99999", role_id="1111", status="active") mock_create.assert_called_with( '/api/v1/courses/862539/enrollments', { 'enrollment': {'user_id': '12345', 'type': 'Student', 'enrollment_state': 'active', 'course_section_id': '99999', 'role_id': '1111'}})
def test_pending_enrollments(self): canvas = Enrollments() enrollments = canvas.get_enrollments_for_course("862539") self.assertEquals(len(enrollments), 1, "Has 1 canvas enrollment") enrollment = enrollments[0] self.assertEquals(enrollment.name, "James Average", "Name") self.assertEquals( enrollment.sortable_name, "Average, James", "Sortable Name") self.assertEquals(enrollment.login_id, None) self.assertEquals( enrollment.status, CanvasEnrollment.STATUS_INVITED, "Status")
def get_active_sis_enrollments_for_user(user_sis_id, roles=[]): canvas = Enrollments(per_page=100) params = {'state': [ENROLLMENT_ACTIVE]} if len(roles): params['type'] = roles enrollments = [] for enrollment in canvas.get_enrollments_for_regid(user_sis_id, params): try: valid_academic_section_sis_id(enrollment.sis_section_id) enrollments.append(enrollment) except CoursePolicyException: continue return enrollments
def enroll_course_user(**kwargs): params = { 'role_id': kwargs.get('role_id'), 'limit_privileges_to_course_section': kwargs.get('section_only'), 'notify': kwargs.get('notify_users'), 'enrollment_state': 'active'} if int(kwargs.get('section_id', 0)) > 0: params['course_section_id'] = kwargs.get('section_id') return Enrollments(as_user=kwargs.get('as_user')).enroll_user( kwargs.get('course_id'), kwargs.get('user_id'), kwargs.get('role_type'), params=params)
def test_enrollments_by_regid(self): canvas = Enrollments() enrollments = canvas.get_enrollments_for_regid( "9136CCB8F66711D5BE060004AC494FFE", include_courses=True) self.assertEquals(len(enrollments), 2, "Has 2 canvas enrollments") enrollment = enrollments[0] self.assertEquals(enrollment.course_url, "https://test.canvas.edu/courses/149650") self.assertEquals(enrollment.sis_course_id, "2013-spring-PHYS-121-A") self.assertEquals(enrollment.sws_course_id(), "2013,spring,PHYS,121/A") self.assertEquals(enrollment.sis_user_id, "9136CCB8F66711D5BE060004AC494FFE") self.assertEquals(enrollment.course_name, "MECHANICS") self.assertIsNotNone(enrollment.course) stu_enrollment = enrollments[1] self.assertEquals( stu_enrollment.grade_html_url, "https://test.instructure.com/courses/862539/grades/12345") self.assertEquals(stu_enrollment.current_score, 23.0, "Current score") self.assertEquals(stu_enrollment.login_id, "javerage", "Login ID") self.assertEquals( stu_enrollment.sis_user_id, "9136CCB8F66711D5BE060004AC494FFE", "SIS User ID") self.assertEquals(stu_enrollment.name, "JAMES AVERAGE STUDENT", "Name") self.assertEquals( enrollment.sortable_name, "STUDENT, JAMES AVERAGE", "Sortable Name") self.assertEquals( str(stu_enrollment.last_activity_at), "2012-08-18 23:08:51-06:00", "Last activity datetime") self.assertEquals( stu_enrollment.total_activity_time, 100, "Total activity time") self.assertEquals( stu_enrollment.status, CanvasEnrollment.STATUS_ACTIVE, "Status")
def grades_for_section(section, instructor): canvas = Enrollments() # Use the canvas section resource, since individual sections # may be cross-listed to other courses if len(section.linked_section_urls): enrollments = [] for url in section.linked_section_urls: linked_section = section_from_url(url) sis_id = linked_section.canvas_section_sis_id() enrollments.extend( canvas.get_enrollments_for_section_by_sis_id(sis_id)) else: if section.is_independent_study: section.independent_study_instructor_regid = instructor.uwregid sis_id = section.canvas_section_sis_id() enrollments = canvas.get_enrollments_for_section_by_sis_id(sis_id) courses = {} grade_data = {"grades": [], "muted_assignments": []} for enrollment in enrollments: if enrollment.role == enrollment.STUDENT: grade_data["grades"].append(enrollment.json_data()) courses[enrollment.course_id] = True canvas = Assignments() for course_id in courses.keys(): for assignment in canvas.get_assignments(course_id): if assignment_muted(assignment): assignment_data = {"course_id": course_id, "assignment_id": assignment.assignment_id, "name": assignment.name} grade_data["muted_assignments"].append(assignment_data) return grade_data
def get_enrollments_for_regid(self, regid): deprecation("Use uw_canvas.enrollments.get_enrollments_for_regid") from uw_canvas.enrollments import Enrollments return Enrollments().get_enrollments_for_regid(regid)
def get_enrollments_for_course(course_id, user_id): return Enrollments().get_enrollments_for_course( course_id, params={'user_id': user_id})