I programmatically change the value of the availability field in the course_modules database record, so that the module associated with that course_modules record will have an access restriction that depends on the completion of another module in that course. That works just fine.
Programmatically changing that course_modules.availability field was suggested by comments in: https://moodle.org/mod/forum/discuss.php?d=305383
However, after I have updated the course_modules record, and redirect back to the course main page, that page does not correctly display the new access restrictions. The only thing that I have found so far that works is to click the "Purge all caches" hyperlink at the bottom of the page.
Is there something I can do programmatically after I have updated the course_modules record?
Calling purge_by_event() in the following way did not work. I did not find any other event besides 'changesincourse' that seemed relevant.
cache_helper::purge_by_event( 'changesincourse' );
When you edit the settings of a module manually, that is done by moodle/course/modedit.php. If you change the module's access restrictions and submit (e.g., by clicking the "Save and return to course" button), and redirect back to the course main page, that page *does* correctly display the new access restrictions. Hmmm...
So, something that modedit.php does would be good for me to do also. When the form has data, it calls update_moduleinfo(), which is defined in moodle/course/modlib.php. One of the things that function does is trigger an event.
I tried triggering such an event. But, it did not work. Alas!
Thank you, Renaat. That worked. Actually, setting the second parameter to true to do a clearonly is sufficient in my situation. That results in the correct access restrictions being displayed on the course main page for all modules.
rebuild_course_cache( $courseId, true );