Ejemplo n.º 1
0
 def process_response(self, data: Any) -> Any:
     if type(data) == dict:
         if 'warnings' in data and data['warnings']:
             warning = from_dict(Warning, data['warnings'])
             self.logger.warning(str(warning))
         if 'exception' in data or 'errorcode' in data:
             raise from_dict(MoodleException, data)
     return data
Ejemplo n.º 2
0
    def get_user_badges(self,
                        userid: Optional[int] = 0,
                        courseid: Optional[int] = 0,
                        page: Optional[int] = 0,
                        perpage: Optional[int] = 0,
                        search: Optional[str] = None,
                        onlypublic: Optional[int] = None) -> BadgeResponse:
        """Get list of badges awarded to a user.

        Args:
            userid (Optional[int], optional): Badges only for this user id, empty for current user. Defaults to 0.
            courseid (Optional[int], optional): Filter badges by course id, empty all the courses. Defaults to 0.
            page (Optional[int], optional): The page of records to return.. Defaults to 0.
            perpage (Optional[int], optional): The number of records to return per page. Defaults to 0.
            search (Optional[str], optional): A simple string to search for. Defaults to None.
            onlypublic (Optional[int], optional): Whether to return only public badges. Defaults to None.

        Returns:
            BadgeResponse: Returns the list of badges awarded to a user
        """
        res = self.moodle.post(
            'core_badges_get_user_badges',
            userid=userid,
            courseid=courseid,
            page=page,
            perpage=perpage,
            search=search,
            onlypublic=onlypublic,
        )
        return from_dict(BadgeResponse, res)
Ejemplo n.º 3
0
    def get_forum_discussions(self,
                              forumid: int,
                              sortorder: int = -1,
                              page: int = -1,
                              perpage: int = 0,
                              groupid: int = 0) -> Discussions:
        """Returns a list of forum discussions optionally sorted and paginated.

        Args:
            forumid (int): forum instance id
            sortorder (int, optional): sort by this element: numreplies, , created or timemodified. Defaults to -1.
            page (int, optional): current page. Defaults to -1.
            perpage (int, optional): items per page. Defaults to 0.
            groupid (int, optional): group id. Defaults to 0.

        Returns:
            Discussions: List of Discussion
        """
        res = self.moodle.post(
            'mod_forum_get_forum_discussions',
            forumid=forumid,
            sortorder=sortorder,
            page=page,
            perpage=perpage,
            groupid=groupid,
        )
        return from_dict(Discussions, res)
Ejemplo n.º 4
0
    def add_discussion_post(self,
                            postid: int,
                            subject: str,
                            message: str,
                            options: Optional[List[Posts.Option]] = None,
                            messageformat: int = 1) -> NewPost:
        """Create new posts into an existing discussion.

        Args:
            postid (int): the post id we are going to reply to (can be the initial discussion post
            subject (str): new post subject
            message (str): new post message (html assumed if messageformat is not provided)
            options (List[Posts.Option], optional): Options. Defaults to None.
            messageformat (int, optional): message format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN). Defaults to 1.

        Returns:
            NewPost: [description]
        """
        res = self.moodle.post(
            'mod_forum_add_discussion_post',
            postid=postid,
            subject=subject,
            message=message,
            options=options or [],
            messageformat=messageformat,
        )
        return from_dict(NewPost, res)
Ejemplo n.º 5
0
    def search_courses(self,
                       criterianame: str,
                       criteriavalue: str,
                       page: int = 0,
                       perpage: int = 0,
                       requiredcapabilities: Optional[List[str]] = None,
                       limittoenrolled: int = 0,
                       onlywithcompletion: int = 0) -> SearchResult:
        """Search courses by (name, module, block, tag)

        Args:
            criterianame (str): criteria name (search, modulelist (only admins), blocklist (only admins), tagid)
            criteriavalue (str): criteria value
            page (int, optional): page number (0 based). Defaults to 0.
            perpage (int, optional): items per page. Defaults to 0.
            requiredcapabilities (Optional[List[str]], optional): Optional list of required capabilities (used to filter the list). Defaults to None.
            limittoenrolled (int, optional): limit to enrolled courses. Defaults to 0.
            onlywithcompletion (int, optional): limit to courses where completion is enabled. Defaults to 0.

        Returns:
            SearchResult: List of result courses
        """
        res = self.moodle.post(
            "core_course_search_courses",
            criterianame=criterianame,
            criteriavalue=criteriavalue,
            page=page,
            perpage=perpage,
            requiredcapabilities=requiredcapabilities,
            limittoenrolled=limittoenrolled,
            onlywithcompletion=onlywithcompletion,
        )
        return from_dict(SearchResult, res)
Ejemplo n.º 6
0
    def get_enrolled_courses_by_timeline_classification(
            self,
            classification: str,
            limit: int = 0,
            offset: int = 0,
            sort: Optional[str] = None) -> CoursesBTC:
        """List of enrolled courses for the given timeline classification (past, inprogress, or future).

        Args:
            classification (str): future, inprogress, or past
            limit (int, optional): Result set limit. Defaults to 0.
            offset (int, optional): Result set offset. Defaults to 0.
            sort (Optional[str], optional): Sort string. Defaults to None.

        Returns:
            CoursesBTC: List of enrolled courses
        """
        res = self.moodle.post(
            "core_course_get_enrolled_courses_by_timeline_classification",
            classification=classification,
            limit=limit,
            offset=offset,
            sort=sort,
        )
        return from_dict(CoursesBTC, res)
Ejemplo n.º 7
0
    def get_forum_discussions_paginated(self,
                                        forumid: int,
                                        sortby: str = 'timemodified',
                                        sortdirection: str = 'DESC',
                                        page: int = -1,
                                        perpage: int = 0) -> Discussions:
        """** DEPRECATED ** Please do not call this function any more. Returns a list of forum discussions optionally sorted and paginated.

        Args:
            forumid (int): forum instance id
            sortby (str, optional): sort by this element: id, timemodified, timestart or timeend. Defaults to 'timemodified'.
            sortdirection (str, optional): sort direction: ASC or DESC. Defaults to 'DESC'.
            page (int, optional): current page. Defaults to -1.
            perpage (int, optional): items per page. Defaults to 0.

        Returns:
            Discussions: list of Discussion
        """
        res = self.moodle.post(
            'mod_forum_get_forum_discussions_paginated',
            forumid=forumid,
            sortby=sortby,
            sortdirection=sortdirection,
            page=page,
            perpage=perpage,
        )
        return from_dict(Discussions, res)
Ejemplo n.º 8
0
 def set_user_flags(self, assignmentid: int,
                    userflags: List[UserFlag]) -> List[UserFlag.Result]:
     res = self.moodle.post('mod_assign_set_user_flags',
                            assignmentid=assignmentid,
                            userflags=userflags)
     return [from_dict(UserFlag.Result, data)
             for data in res] if res else []
Ejemplo n.º 9
0
    def add_discussion(
            self,
            forumid: int,
            subject: str,
            message: str,
            groupid: int = 0,
            options: Optional[List[Discussions.Option]] = None
    ) -> Discussions.New:
        """Add a new discussion into an existing forum.

        Args:
            forumid (int): Forum instance ID
            subject (str): New Discussion subject
            message (str): New Discussion message (only html format allowed)
            groupid (int, optional): The group, default to 0. Defaults to 0.
            options (List[Discussions.Option], optional): Options. Defaults to None.

        Returns:
            Discussions.New: [description]
        """
        res = self.moodle.post(
            'mod_forum_add_discussion',
            forumid=forumid,
            subject=subject,
            message=message,
            groupid=groupid,
            options=options or [],
        )
        return from_dict(Discussions.New, res)
Ejemplo n.º 10
0
    def get_public_config(self) -> MobilePublicConfig:
        """Get a list of the site public settings, those not requiring authentication.

        Returns:
            MobilePublicConfig: Returns a list of the site public settings, those not requiring authentication.
        """
        data = self.moodle.get('tool_mobile_get_public_config')
        return from_dict(MobilePublicConfig, data)
Ejemplo n.º 11
0
 def lock_submissions(self, assignmentid: int,
                      userids: List[int]) -> List[Warning]:
     res = self.moodle.post(
         'mod_assign_lock_submissions',
         assignmentid=assignmentid,
         userids=userids,
     )
     return [from_dict(Warning, data) for data in res] if res else []
Ejemplo n.º 12
0
 def get_calendar_events(
         self,
         events: Optional[Events.Details] = None,
         options: Optional[Events.Options] = None) -> Events:
     res = self.moodle.get('core_calendar_get_calendar_events',
                           events=events if events else {},
                           options=options if options else {})
     return from_dict(Events, res)
Ejemplo n.º 13
0
 def save_user_extensions(
         self, assignmentid: int, userids: List[int],
         dates: List[Union[datetime, int]]) -> List[Warning]:
     res = self.moodle.post('mod_assign_save_user_extensions',
                            assignmentid=assignmentid,
                            userids=userids,
                            dates=dates)
     return [from_dict(Warning, data) for data in res] if res else []
Ejemplo n.º 14
0
    def get_plugins_supporting_mobile(self) -> List[MobilePlugin]:
        """Get a list of Moodle plugins supporting the mobile app.

        Returns:
            List[MobilePlugin]: Returns a list of Moodle plugins supporting the mobile app.
        """
        res = self.moodle.get('tool_mobile_get_plugins_supporting_mobile')
        return [from_dict(MobilePlugin, data)
                for data in res['plugins']] if 'plugins' in res else []
Ejemplo n.º 15
0
 def get_categories(
         self,
         criteria: Optional[List[Category.Criteria]] = None
 ) -> List[Category]:
     res = self.moodle.post(
         "core_course_get_categories",
         criteria=criteria or [],
     )
     return [from_dict(Category, data) for data in res] if res else []
Ejemplo n.º 16
0
 def get_calendar_monthly_view(self,
                               year: int,
                               month: int,
                               courseid: int = 1,
                               categoryid: Optional[int] = None,
                               includenavigation: int = 1,
                               mini: Optional[int] = None) -> MonthlyView:
     res = self.moodle.get('core_calendar_get_calendar_monthly_view')
     return from_dict(MonthlyView, res)
Ejemplo n.º 17
0
 def can_add_discussion(
         self,
         forumid: int,
         groupid: Optional[int] = None) -> Discussions.CanAdd:
     res = self.moodle.post(
         'mod_forum_can_add_discussion',
         forumid=forumid,
         groupid=groupid,
     )
     return from_dict(Discussions.CanAdd, res)
Ejemplo n.º 18
0
    def view_url(self, urlid: int) -> View:
        """Trigger the course module viewed event and update the module completion status.

        Args:
            urlid (int): url instance id

        Returns:
            View: Response
        """
        res = self.moodle.post('mod_url_view_url', urlid=urlid)
        return from_dict(View, res)
Ejemplo n.º 19
0
    def get_config(self, section: Optional[str] = None) -> MobileConfig:
        """Returns a list of the site configurations, filtering by section.

        Args:
            section (str, optional): Settings section name. Defaults to None.

        Returns:
            MobileConfig: Returns a list of the site configurations, filtering by section.
        """
        res = self.moodle.get('tool_mobile_get_config', section=section or '')
        return from_dict(MobileConfig, res)
Ejemplo n.º 20
0
    def view_folder(self, folderid: int) -> View:
        """Simulate the view.php web interface folder: trigger events, completion, etc...

        Args:
            folderid (int): folder instance id

        Returns:
            View: Response
        """
        res = self.moodle.post('mod_folder_view_folder')
        return from_dict(View, res)
Ejemplo n.º 21
0
    def create_notes(self, notes: List[Note]) -> List[Note.Result]:
        """Create notes

        Args:
            notes (List[Note]): Notes to create

        Returns:
            List[Note.Result]: Create note result
        """
        res = self.moodle.post('core_notes_create_notes', notes=notes)
        return [from_dict(Note.Result, data) for data in res] if res else []
Ejemplo n.º 22
0
    def get_course_module(self, cmid: int) -> CourseModule:
        """Return information about a course module

        Args:
            cmid (int): The course module id

        Returns:
            CourseModule: Course Module wrapper
        """
        res = self.moodle.post("core_course_get_course_module", cmid=cmid)
        return from_dict(CourseModule, res)
Ejemplo n.º 23
0
    def delete_notes(self, notes: List[int]) -> List[Warning]:
        """Delete notes

        Args:
            notes (List[int]): Array of Note Ids to be deleted.

        Returns:
            List[Warning]: list of warnings
        """
        res = self.moodle.post('core_notes_delete_notes', notes=notes)
        return [from_dict(Warning, data) for data in res] if res else []
Ejemplo n.º 24
0
    def view_resource(self, resourceid: int) -> View:
        """Simulate the view.php web interface resource: trigger events, completion, etc...

        Args:
            resourceid (int): resource instance id

        Returns:
            View: View Resource response
        """
        res = self.moodle.post('mod_resource_view_resource',
                               resourceid=resourceid)
        return from_dict(View, res)
Ejemplo n.º 25
0
 def get_action_events_by_courses(self,
                                  courseids: List[int],
                                  timesortfrom: Optional[Union[datetime,
                                                               int]] = None,
                                  timesortto: Optional[Union[datetime,
                                                             int]] = None,
                                  limitnum: int = 10) -> ActionEventCourses:
     res = self.moodle.get('core_calendar_get_action_events_by_courses',
                           courseids=courseids,
                           timesortfrom=timesortfrom,
                           limitnum=limitnum)
     return from_dict(ActionEventCourses, res)
Ejemplo n.º 26
0
    def get_resources_by_courses(self, courseids: List[int]) -> Resources:
        """Returns a list of files in a provided list of courses, if no list is provided all files that the user can view will be returned.

        Args:
            courseids (List[int]): Array of course ids

        Returns:
            Resources: List of Resource
        """
        res = self.moodle.post('mod_resource_get_resources_by_courses',
                               courseids=courseids)
        return from_dict(Resources, res)
Ejemplo n.º 27
0
    def mark_course_self_completed(self, courseid: int) -> StatusCompletion:
        """Update the course completion status for the current user (if course self-completion is enabled).

        Args:
            courseid (int): Course ID

        Returns:
            SelfCompleted: Response
        """
        res = self.moodle.post('core_completion_mark_course_self_completed',
                               courseid=courseid)
        return from_dict(StatusCompletion, res)
Ejemplo n.º 28
0
    def view_entries(self,
                     filters: Optional[List[Filter]] = None) -> ViewEntry:
        """Trigger the blog_entries_viewed event.

        Args:
            filters (Optional[List[Filter]], optional): Parameters used in the filter of view_entries.. Defaults to None.

        Returns:
            ViewEntry: the blog_entries_viewed response.
        """
        res = self.moodle.post('core_blog_view_entries', filters=filters)
        return from_dict(ViewEntry, res)
Ejemplo n.º 29
0
    def submit_create_update_form(self,
                                  formdata: str) -> CourseEvents.EventForm:
        """Submit form data for event form

        Args:
            formdata (str): The data from the event form

        Returns:
            CourseEvents.EventForm: Event form
        """
        res = self.moodle.get('core_calendar_submit_create_update_form')
        return from_dict(CourseEvents.EventForm, res)
Ejemplo n.º 30
0
    def get_folders_by_courses(self, courseids: List[int]) -> Folders:
        """Returns a list of folders in a provided list of courses, if no list is provided all folders that the user can view will be returned.
        Please note that this WS is not returning the folder contents.

        Args:
            courseids (List[int]): Array of course ids

        Returns:
            Folders: List of Folder
        """
        res = self.moodle.post('mod_folder_get_folders_by_courses')
        return from_dict(Folders, res)