This is just a quick piece of coding, so excuse the magic numbers:
$name = 'Different Teacher';
$shortname = 'DifferentTeacher';
$description = 'Different Teacher';
$DifferentTeacherRoleID = create_role($name, $shortname, $description, $legacy='');
// duplicate all the base definitions of the teacher role
$sql = "SELECT * FROM {$CFG->prefix}role_capabilities WHERE roleid = 3 AND contextid = $sitewidecontextid->id";
$rsTeacherCaps = $Moodledb->query($sql);
while ($cap = $rsTeacherCaps->fetchRow())
{
assign_capability($cap['capability'], $cap['permission'], $DifferentTeacherRoleID, $sitewidecontextid);
}
// customise the DifferentTeacher role here
assign_capability('moodle/user:update', CAP_ALLOW, $DifferentTeacherRoleID, $sitewidecontextid);
assign_capability('moodle/user:viewdetails', CAP_ALLOW, $DifferentTeacherRoleID, $sitewidecontextid);
unassign_capability('moodle/role:manage',$DifferentTeacherRoleID);
// ensure that the Different Teacher role can assign or override the role for Student
// 5 = Student role id
$sql = "INSERT INTO {$CFG->prefix}role_allow_assign (roleid, allowassign) VALUES ($DifferentTeacherRoleID, 5)";
$rsTmp = $Moodledb->exec($sql);
$sql = "INSERT INTO {$CFG->prefix}role_allow_override (roleid, allowoverride) VALUES ($DifferentTeacherRoleID, 5)";
$rsTmp = $Moodledb->exec($sql);
print 'Created role: '.$name.'<br/>';
flush();
$shortname = 'DifferentTeacher';
$description = 'Different Teacher';
$DifferentTeacherRoleID = create_role($name, $shortname, $description, $legacy='');
// duplicate all the base definitions of the teacher role
$sql = "SELECT * FROM {$CFG->prefix}role_capabilities WHERE roleid = 3 AND contextid = $sitewidecontextid->id";
$rsTeacherCaps = $Moodledb->query($sql);
while ($cap = $rsTeacherCaps->fetchRow())
{
assign_capability($cap['capability'], $cap['permission'], $DifferentTeacherRoleID, $sitewidecontextid);
}
// customise the DifferentTeacher role here
assign_capability('moodle/user:update', CAP_ALLOW, $DifferentTeacherRoleID, $sitewidecontextid);
assign_capability('moodle/user:viewdetails', CAP_ALLOW, $DifferentTeacherRoleID, $sitewidecontextid);
unassign_capability('moodle/role:manage',$DifferentTeacherRoleID);
// ensure that the Different Teacher role can assign or override the role for Student
// 5 = Student role id
$sql = "INSERT INTO {$CFG->prefix}role_allow_assign (roleid, allowassign) VALUES ($DifferentTeacherRoleID, 5)";
$rsTmp = $Moodledb->exec($sql);
$sql = "INSERT INTO {$CFG->prefix}role_allow_override (roleid, allowoverride) VALUES ($DifferentTeacherRoleID, 5)";
$rsTmp = $Moodledb->exec($sql);
print 'Created role: '.$name.'<br/>';
flush();