Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

Creating roles programmatically

From MoodleDocs

You may find that the default roles do not answer the needs of your plugin. In that case, you want to create a role programmatically.

Setup

We will use a plugin called myplugin.

It is best to create capabilities for your plugin first as they will be used to create the role. You can also use default Moodle capabilities. For more information, please refer to Roles#Programming_Interface The first step is to create an install.php file in the db folder of your plugin.

/mod/myplugin/db/install.php