you shouldn't insert directly to the mdl_role_assignments and should really be using the standard API functions to do this. Ideally you should be calling "role_assign" from accesslib.php or the webservice core_role_assign_roles to do this.
If the pre-conditions involve moodle events, eg "course completion" etc - you might even be able to use the tool_trigger plugin to listen to course_completion events, and then based on the data you receive you could make a webservice call to the core_role_assign_roles function. It might take a bit to understand how the tool_trigger plugin works and if this will suit your requirements..
alternatively you could just create a plugin that implements the Moodle task api, and triggers this for you (like Tim says)