General developer forum

Find and update page module instance

Picture of Eduardo Rey
Find and update page module instance


I'd like to find a specific page module instance in a course and update its contents, appending a specific html string that will depend on the current content. The page has a series of h2 with sequential titles like "Title 1", "Title 2", etc. I want to find out the last number and append the next number on the sequence.

This is what I have in mind:
  1. Use get_array_of_activities from course/lib.php to find the instance (by name if there's no hidden editable "ID" field like a course shortname).
  2. Use page_get_coursemodule_info from mod/page/lib.php to retrieve the page content. I haven't been able to get it to work because get_array_of_activities' return doesn't include the instance property required by this function, so I'm considering setting this property by myself. Another option would be to run a query and get the content directly from the database, but I'd like to use appropriate Moodle functions and OO if possible.
  3. Parse the HTML content using some kind of native PHP/Moodle HTML reader to figure out the last number in a h2.
  4. Update page content with page_update_instance from mod/page/lib.php. Not sure what I need to provide in the $data argument, hoping I can use the same object from get_array_of_activities.

Can someone please tell if this is the best way to accomplish what I want or am I missing something? Thanks in advance!

Average of ratings: -