0

I want to get all the HTML content of the page resources in a Moodle course through a Moodle Webservice. The documentation is missing examples, and discussion threads I have read through don't seem to address this seemingly basic action.

What is the correct webservice function to get page resource HTML content?

martin-martin
  • 3,274
  • 1
  • 33
  • 60

1 Answers1

1

The mod_page_get_pages_by_courses function returns information plus HTML content of a given course (if provided) or all courses the user has access to (if courseids is left blank).

Below is a generalized call to the webservice to retrieve all page information of a specific course, assuming you have a Moodle Webservice correctly set up:

https://<your-moodle-url>/webservice/rest/server.php?wstoken=<your-token>&moodlewsrestformat=json&wsfunction=mod_page_get_pages_by_courses&courseids[0]=<your-course-id>

Here's the abbreviated info from the documentation that helped me figure it out:

mod_page_get_pages_by_courses

Returns a list of pages in a provided list of courses, if no list is provided all pages that the user can view will be returned.

Arguments

courseids (Default to "Array ( ) ") - Array of course ids

Response

Note the content entry, which is a HTML string with the page content.

object {
pages list of ( 
object {
id int   //Module id
coursemodule int   //Course module id
course int   //Course id
name string   //Page name
intro string   //Summary
introformat int  Default to "1" //intro format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
introfiles   //Files in the introduction text
list of ( 
  //File.
object {
filename string  Optional //File name.
filepath string  Optional //File path.
filesize int  Optional //File size.
fileurl string  Optional //Downloadable file url.
timemodified int  Optional //Time modified.
mimetype string  Optional //File mime type.
isexternalfile int  Optional //Whether is an external file.
repositorytype string  Optional //The repository type for external files.
} 
)content string   //Page content
contentformat int  Default to "1" //content format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN)
contentfiles   //Files in the content
list of ( 
  //File.
object {
filename string  Optional //File name.
filepath string  Optional //File path.
filesize int  Optional //File size.
fileurl string  Optional //Downloadable file url.
timemodified int  Optional //Time modified.
mimetype string  Optional //File mime type.
isexternalfile int  Optional //Whether is an external file.
repositorytype string  Optional //The repository type for external files.
} 
)legacyfiles int   //Legacy files flag
legacyfileslast int   //Legacy files last control flag
display int   //How to display the page
displayoptions string   //Display options (width, height)
revision int   //Incremented when after each file changes, to avoid cache
timemodified int   //Last time the page was modified
section int   //Course section id
visible int   //Module visibility
groupmode int   //Group mode
groupingid int   //Grouping id
} 
)warnings  Optional //list of warnings
list of ( 
  //warning
object {
item string  Optional //item
itemid int  Optional //item id
warningcode string   //the warning code can be used by the client app to implement specific behaviour
message string   //untranslated english message to explain the warning
} 
)}

Alternative: core_course_get_contents

Found this helpful forum post by Juan Levya that presents an alternative way of getting the page contents as downloadable HTML files. Adapted info from the original post:

The function core_course_get_contents returns:

  • Sections
    • Modules in each section (activities or resources)
      • If the module is a resource it returns a list of the files used by this resource in the "contents" attribute

That attribute is an array of files, there you will have a fileurl attribute that is the download URL for the file. If you are using Web Services you should append the WS token.

Example of URL returned:

https://<your-moodle-url>/webservice/pluginfile.php/29/mod_page/content/index.html?forcedownload=1

You need to modify the URL to download the file by adding your WS token as a query parameter with the key token:

https://<your-moodle-url>/webservice/pluginfile.php/29/mod_page/content/index.html?forcedownload=1&token=<your-token>

This will initialize a download of the resource file.


Get Course Contents Using Webservice is a somewhat related question, however it specifically asks about lesson resources, which have their own dedicated function.

martin-martin
  • 3,274
  • 1
  • 33
  • 60