Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Miscellaneous callbacks

From MoodleDocs
Revision as of 20:21, 20 August 2018 by Paul Greidanus (talk | contribs) (Adding store_profiling_data)

This page contains documentation for general callbacks available in Moodle plugins.

control_view_profile

This callback allows an installation to control access to a user's profile page. It modifies the response of the user_can_view_profile function; this function is what causes Moodle to show a 'not allowed' error message if you try to view somebody's profile when you aren't allowed to.

You can use this callback if your institution needs to implement policies about who can view profiles in addition to those supported by standard Moodle. Normally it would be implemented in a local plugin, but you can implement in any type of plugin.

Your callback can return one of three values (all constants defined in the core_user class):

VIEWPROFILE_DO_NOT_PREVENT
Use standard Moodle behaviour - apply Moodle's normal rules about whether the current user is allowed to view that profile or not. Your callback should return this if you don't need to change behaviour for the profile in question.
VIEWPROFILE_PREVENT
Prevent the current user from viewing that user's profile, even if Moodle's normal rules would allow it.
VIEWPROFILE_FORCE_ALLOW
Allow the current user to view that user's profile, even if Moodle's normal rules would prevent it.

Example code

This silly example, placed in local/myplugin/lib.php, prevents everyone from accessing the profile of a user 'abc123'.

function local_myplugin_control_view_profile($user, $course = null, context_user $usercontext = null) {

   if ($user->username='abc123') {
       return core_user::VIEWPROFILE_PREVENT;
   }
   return core_user::VIEWPROFILE_DO_NOT_PREVENT;

}

Multiple plugins

The callback can be implemented by multiple plugins. Provided they behave sensibly and return DO_NOT_PREVENT when they don't need to modify behaviour, it should work as expected. The specific rules are:

  • If any function returns VIEWPROFILE_PREVENT then the user will be prevented from viewing the profile.
  • Otherwise, if no function returns VIEWPROFILE_PREVENT but any function returns VIEWPROFILE_FORCE_ALLOW , then the user will be allowed to view the profile.
  • Otherwise all plugins returned VIEWPROFILE_DO_NOT_PREVENT, and Moodle standard behaviour will be used.

store_profiling_data

This callback allows a plugin to get the profiling information from xhprof/tideways-xhprof and do it's own processing or storage with it.

Example code

This is a fragment of a possible plugin that writes files to /tmp/output.

function local_profilestorefile_store_profiling_data($rec) { $file = fopen("/tmp/output/" . $rec->runid, "w") or die("Unable to write to file, does the folder exist?"); fwrite($file, json_encode($rec)); fclose($file); }