If you are in a public function, then $this should be defined. (Unless something is doing some evil low-level hackers to call this method in a non-standard way.)
Anyway, this error does not happen in the standard Moodle release. (This functionality is covered by automates tests which pass.) So, that suggest the problem is caused by whatever customisation you have done in your version of Moodle. I hope you can work it out.
Anyway, this error does not happen in the standard Moodle release. (This functionality is covered by automates tests which pass.) So, that suggest the problem is caused by whatever customisation you have done in your version of Moodle. I hope you can work it out.