def load_assignments(course: Course, assignments: List[Dict]) -> List[Assignment]: """ Load a list of assignments via the Canvas API. Parameters ---------- course: Course A Course SDK object assignments: List[Dict] A list of JSON-like assignment objects in a form suitable for submission to the Canvas assignment creation endpoint. Returns ------- List[Assignment] A list of Canvas SDK Assignment objects representing the created assignments """ logger.info("Creating %s assignments via Canvas API", len(assignments)) result: List[Assignment] = [] for assignment in assignments: result.append(course.create_assignment(assignment)) logger.info("Successfully created %s assignments", len(assignments)) return result
def get_parent(self, **kwargs): """ Return the object that spawned this content migration. :rtype: :class:`canvasapi.group.Account`, or :class:`canvasapi.course.Course`, or :class:`canvasapi.course.Group`, or :class:`canvasapi.course.User` """ from canvasapi.account import Account from canvasapi.course import Course from canvasapi.group import Group from canvasapi.user import User response = self._requester.request( "GET", "{}s/{}".format(self._parent_type, self._parent_id), _kwargs=combine_kwargs(**kwargs), ) if self._parent_type == "group": return Group(self._requester, response.json()) elif self._parent_type == "course": return Course(self._requester, response.json()) elif self._parent_type == "account": return Account(self._requester, response.json()) elif self._parent_type == "user": return User(self._requester, response.json())
def get_course(self, course, use_sis_id=False, **kwargs): """ Retrieve a course by its ID. :calls: `GET /courses/:id \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.show>`_ :param course: The object or ID of the course to retrieve. :type course: int, str or :class:`canvasapi.course.Course` :param use_sis_id: Whether or not course_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.course.Course` """ if use_sis_id: course_id = course uri_str = 'courses/sis_course_id:{}' else: course_id = obj_or_id(course, "course", (Course, )) uri_str = 'courses/{}' response = self.__requester.request('GET', uri_str.format(course_id), _kwargs=combine_kwargs(**kwargs)) return Course(self.__requester, response.json())
def load_sections(course: Course, sections: List[Dict]) -> List[Section]: """ Load a list of sections for a course via the Canvas API. Parameters ---------- course: Course A Course SDK object sections: List[Dict] A list of JSON-like section objects in a form suitable for submission to the Canvas section creation endpoint. Returns ------- List[Section] A list of Canvas SDK Section objects representing the created sections """ logger.info("Creating %s sections via Canvas API", len(sections)) result: List[Section] = [] for section in sections: result.append(course.create_course_section(**section)) logger.info("Successfully created %s sections", len(sections)) return result
def load_enrollments(course: Course, enrollments: List[Dict]) -> List[Enrollment]: """ Load a list of enrollments for a course via the Canvas API. Parameters ---------- course: Course A Course SDK object enrollments: List[Dict] A list of JSON-like enrollment objects in a form suitable for submission to the Canvas enrollment creation endpoint. Returns ------- List[Enrollment] A list of Canvas SDK Enrollment objects representing the created enrollments """ logger.info("Creating %s enrollments via Canvas API", len(enrollments)) result: List[Enrollment] = [] for enrollment in enrollments: result.append( course.enroll_user( enrollment["user"], enrollment["enrollment_type"], **enrollment["enrollment"] ) ) logger.info("Successfully created %s enrollments", len(enrollments)) return result
def __init__(self, course: Course, gui_list: List[Union[str, None, Assignment]], csv_path: str): # Step 8.1 self.course = course self.csv_path = csv_path self.gui_list = gui_list self.third_party_students_full_name_pool = [] self.third_party_students_last_name_pool = [] self.canvas_students_full_name_pool = [] self.canvas_students_last_name_pool = [] # Step 8.2 self.grade_book = self.create_empty_grade_book() # Step 8.3 self.third_party_students = self.create_third_party_student_list() # Step 8.4 self.canvas_students = set( course.get_users(enrollment_type=["student"])) # Step 8.5 self.create_canvas_name_pool() # Step 8.6 self.match_students()
def get_assignment_groups(self, canvas_course: Course): self.line_separator() print("These are the available ASSIGNMENT GROUPS from " + canvas_course.name + ".") last_assignment_index = self.print_list_with_index( canvas_course.get_assignment_groups()) ask_groups = "Please enter the index in front of the assignment GROUP(s) that you want to transfer grade from.\n" \ "If you want to select multiple groups, please separate them with commas: " user_input_groups = self.get_user_input_int( start_point=1, end_point=last_assignment_index, prompt=ask_groups, comma_allowed=True) assignments = [] for group in user_input_groups: group_id = canvas_course.get_assignment_groups().__getitem__( group - 1).id self.get_each_assignments(group_id, canvas_course, assignments) return assignments
def get_each_assignments(self, group_id: int, canvas_course: Course, assignments: List[Assignment]): self.line_separator() group_name = canvas_course.get_assignment_group(group_id).name print("These are the available assignment(s) in the " + group_name + " you just chose.") last_assignment_index = self.print_list_with_index( canvas_course.get_assignments_for_group(group_id)) ask_assignments = "Please enter the index number in front of the assignment(s) which you want to transfer \n" \ "grades from. If multiple, separate them with commas: " assignment_list = self.get_user_input_int( start_point=1, end_point=last_assignment_index, prompt=ask_assignments, comma_allowed=True) for index in assignment_list: assignments.append( canvas_course.get_assignments_for_group(group_id)[index - 1]) return
def get_submissions_for_assignments(course: Course, assignments): assignment_ids = [assignment.id for assignment in assignments] assignment_submissions = course.get_multiple_submissions( assignment_ids=assignment_ids) submissions_by_assignment = defaultdict(list) for submission in assignment_submissions: submissions_by_assignment[submission.assignment_id].append(submission) return submissions_by_assignment
def get_course(self, course_id, **kwargs): """ Retrieve a course by its ID. :calls: `GET /courses/:id \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.show>`_ :param course_id: The ID of the course to retrieve. :type course_id: int :rtype: :class:`canvasapi.course.Course` """ response = self.__requester.request('GET', 'courses/%s' % (course_id), **combine_kwargs(**kwargs)) return Course(self.__requester, response.json())
def get_modules(course: Course) -> List[Union[Module, ModuleItem]]: """ Returns a list of all modules for the given course. """ all_modules = [] for module in course.get_modules(): all_modules.append(module) for item in module.get_module_items(): all_modules.append(item) return all_modules
def create_course(self, **kwargs): """ Create a course. :calls: `POST /api/v1/accounts/:account_id/courses \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.create>`_ :rtype: :class:`canvasapi.course.Course` """ from canvasapi.course import Course response = self._requester.request('POST', 'accounts/%s/courses' % (self.id), account_id=self.id, **combine_kwargs(**kwargs)) return Course(self._requester, response.json())
def get_parent(self): """ Return the object that spawned this discussion topic. :rtype: :class:`canvasapi.group.Group` or :class:`canvasapi.course.Course` """ from canvasapi.group import Group from canvasapi.course import Course response = self._requester.request( 'GET', '{}s/{}'.format(self._parent_type, self._parent_id)) if self._parent_type == 'group': return Group(self._requester, response.json()) elif self._parent_type == 'course': return Course(self._requester, response.json())
def get_parent(self): """ Return the object that spawned this tool. :rtype: :class:`canvasapi.account.Account` or :class:`canvasapi.account.Course` """ from canvasapi.account import Account from canvasapi.course import Course response = self._requester.request( 'GET', '%ss/%s' % (self.parent_type, self.parent_id)) if self.parent_type == 'account': return Account(self._requester, response.json()) elif self.parent_type == 'course': return Course(self._requester, response.json())
def get_parent(self): """ Return the object that spawned this tool. :rtype: :class:`canvasapi.account.Account` or :class:`canvasapi.account.Course` """ from canvasapi.account import Account from canvasapi.course import Course response = self._requester.request( "GET", "{}s/{}".format(self.parent_type, self.parent_id)) if self.parent_type == "account": return Account(self._requester, response.json()) elif self.parent_type == "course": return Course(self._requester, response.json())
def get_staff_ids(course: Course) -> List[int]: """ Parameters ---------- course : `Course` The course to get staff IDs for Returns ------- `List[int]` A list of the IDs of all professors and TAs in the given course. """ staff = course.get_users(enrollment_type=["teacher", "ta"]) staff_ids = list(map(lambda user: user.id, staff)) return staff_ids
def get_parent(self, **kwargs): """ Return the object that spawned this discussion topic. :rtype: :class:`canvasapi.group.Group` or :class:`canvasapi.course.Course` """ from canvasapi.group import Group from canvasapi.course import Course response = self._requester.request( "GET", "{}s/{}".format(self._parent_type, self._parent_id), _kwargs=combine_kwargs(**kwargs), ) if self._parent_type == "group": return Group(self._requester, response.json()) elif self._parent_type == "course": return Course(self._requester, response.json())
def download_modules(course: Course): """ Download all modules for a Canvas course, storing each module's URL (or name/title if the url does not exist) in {COURSES_DIRECTORY}/{course.id}/modules.txt. Assumption: {COURSES_DIRECTORY}/{course.id}/modules.txt exists. """ modules_file = f"{COURSES_DIRECTORY}/{course.id}/modules.txt" with open(modules_file, "w") as f: for module in course.get_modules(): if hasattr(module, "name"): f.write(module.name + "\n") for item in module.get_module_items(): if hasattr(item, "title"): if hasattr(item, "html_url"): f.write(item.html_url + "\n") else: f.write(item.title + "\n")
def get_parent(self): """ Return the object that spawned this page. :calls: `GET /api/v1/groups/:group_id \ <https://canvas.instructure.com/doc/api/groups.html#method.groups.show>`_ or :calls: `GET /api/v1/courses/:course_id \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.show>`_ :rtype: :class:`canvasapi.group.Group` or :class:`canvasapi.course.Course` """ from canvasapi.group import Group from canvasapi.course import Course response = self._requester.request( 'GET', '{}s/{}'.format(self.parent_type, self.parent_id)) if self.parent_type == 'group': return Group(self._requester, response.json()) elif self.parent_type == 'course': return Course(self._requester, response.json())
def get_all_modules(course: Course, incl_unpublished: bool) -> list[Module | ModuleItem]: """ Returns a list of all modules for the given course. Includes unpublished modules if `incl_unpublished` is `True` and we have access to unpublished modules for the course. """ all_modules = [] for module in course.get_modules(): # If module does not have the "published" attribute, then the host of the bot does # not have access to unpublished modules. Reference: https://canvas.instructure.com/doc/api/modules.html if incl_unpublished or not hasattr( module, "published") or module.published: all_modules.append(module) for item in module.get_module_items(): # See comment about the "published" attribute above. if incl_unpublished or not hasattr( item, "published") or item.published: all_modules.append(item) return all_modules
def get_parent(self, **kwargs): """ Return the object that spawned this page. :calls: `GET /api/v1/groups/:group_id \ <https://canvas.instructure.com/doc/api/groups.html#method.groups.show>`_ or :calls: `GET /api/v1/courses/:id \ <https://canvas.instructure.com/doc/api/courses.html#method.courses.show>`_ :rtype: :class:`canvasapi.group.Group` or :class:`canvasapi.course.Course` """ from canvasapi.group import Group from canvasapi.course import Course response = self._requester.request( "GET", "{}s/{}".format(self.parent_type, self.parent_id), _kwargs=combine_kwargs(**kwargs), ) if self.parent_type == "group": return Group(self._requester, response.json()) elif self.parent_type == "course": return Course(self._requester, response.json())