Moodle API Forums - C0D3D3V/Moodle-DL GitHub Wiki

To download databases the following functions are of interest:

Area Name Introduced in Description
mod_forum mod_forum_get_forums_by_courses 2.5 (2013051400) Returns a list of forum instances in a provided set of courses, if no courses are provided then all the forum instances the user has access to will be returned.
mod_forum mod_forum_get_forum_discussions_paginated 2.8 (2014111000) Returns a list of forum discussions optionally sorted and paginated.
mod_forum mod_forum_get_forum_discussions 3.7 (2019052000) Returns a list of forum discussions optionally sorted and paginated.
mod_forum mod_forum_get_forum_discussion_posts 2.7 (2014051200) Returns a list of forum posts for a discussion.
mod_forum mod_forum_get_discussion_posts 3.7 (2019052000) Returns a list of forum posts for a discussion.

Forums are part of the course content.

An example module which returns core_course_get_contents:

{
    "id":944164,
    "url":"https:\/\/moodle.uni.de\/m\/mod\/forum\/view.php?id=944164",
    "name":"Ank\u00fcndigungen",
    "instance":44539,
    "visible":1,
    "uservisible":true,
    "visibleoncoursepage":1,
    "modicon":"https:\/\/moodle.uni.de\/m\/theme\/image.php\/boost_campus\/forum\/1589567880\/icon",
    "modname":"forum",
    "modplural":"Foren\/Blogs",
    "indent":0,
    "onclick":"",
    "afterlink":null,
    "customdata":"\"\"",
    "completion":0
}

The module indicates where the forum is located in the course, nothing more can be derived from it.

Load a list of all forums

mod_forum_get_forums_by_courses

Parameter
Funktion
Returns

With the help of mod_forum_get_forums_by_courses all forum-id's of all courses can be loaded. It exists since Version 2.5. The function takes the following arguments:

  • courseids is an array of the course-ids from which the forums are to be loaded.

mod_forum_get_forums_by_courses returns a list of forums:


{
    "id":52079,
    "course":27288,
    "type":"news",
    "name":"Ank\u00fcndigungen",
    "intro":"Nachrichten und Ank\u00fcndigungen",
    "introformat":1,
    "introfiles":[

    ],
    "duedate":0,
    "cutoffdate":0,
    "assessed":0,
    "assesstimestart":0,
    "assesstimefinish":0,
    "scale":0,
    "maxbytes":0,
    "maxattachments":1,
    "forcesubscribe":1,
    "trackingtype":1,
    "rsstype":0,
    "rssarticles":0,
    "timemodified":1586339792,
    "warnafter":0,
    "blockafter":0,
    "blockperiod":0,
    "completiondiscussions":0,
    "completionreplies":0,
    "completionposts":0,
    "cmid":1086705,
    "numdiscussions":2,
    "cancreatediscussions":false,
    "lockdiscussionafter":0,
    "istracked":true,
    "unreadpostscount":0
}

Important attributes:

  • course is the id of the course the forum belongs to.
  • id is the id of the forum
  • cmid is the module id that was returned by core_course_get_contents
  • name is the name of the forum

Load a list of all discussions of a forum

mod_forum_get_forum_discussions_paginated

Parameter
Funktion
Returns

mod_forum_get_forum_discussions (since 3.7)

Parameter
Funktion
Returns

To get a list of discussions in the forum there are two functions. mod_forum_get_forum_discussions_paginated is used from version 2.8 to 3.7 and is the deprecated version of mod_forum_get_forum_discussions. Both functions work identically. The function takes the following arguments:

  • forumid is the id of the forum from which the discussions should be loaded.
  • perpage specifies how many entries should be returned at once, this is optional.
  • page is the page number of the paged result.

For the new function only:

  • sortorder Is the way the list should be sorted, as integer. SORTORDER_LASTPOST_DESC = 1 gives the best result, because you can filter the already downloaded discussions.

For the old function only:

  • sortby is an entry from id, timemodified, timestart or timeend
  • sortdirection is an entry from ASC or DESC

Download discussion posts

mod_forum_get_forum_discussion_posts

Parameter
Funktion
Returns

mod_forum_get_discussion_posts (since 3.7)

Parameter
Funktion
Returns
Result Structure

To finally download all entries of a discussion, the function mod_forum_get_forum_discussion_posts is required. This function exists since version 2.7 but will be deprecated in 4.1. The function takes the following arguments:

  • discussionid is the id of the discussion to be loaded.
  • sortby is an element of id, created or modified
  • sortdirection is an element of ASC or DESC

I think sorting by created ASC should return the best result.