So some interesting results, I seem to be able to make the method work for a system role but not for a user level role. This seems like a new issue since we moved to Moodle 3.7. Any ideas?
//This works foreach($parentidlookup as $thisone) { // echo "Adding ".$thisone."\n"; $role['roleid']=10; //Defines what the parent can access generally. Based on student role $role['userid']=$thisone; $role['contextlevel']="system"; $role['contextid']=1; array_push($roles,$role); } unset($resp); try{ $resp=$clientm->core_role_assign_roles($roles); }catch(Exception $e){ echo $e."\n"; }
The above fills $roles with below and works:
array(527) { [0]=> array(4) { ["roleid"]=> int(10) ["userid"]=> int(24844) ["contextlevel"]=> string(6) "system" ["contextid"]=> int(1) }
//Doesn't flag error but doesn't work either. Confirmed that the $role parameters are correct by browsing database unset($roles); unset($role); unset($resp); $roles=array(); foreach($parentstudent as $thiskid) { $role['roleid']=9; //A user level role assigning a parent to student. $role['userid']=$parentidlookup[$thiskid['parent']]; //Recover Parent's moodle userid $instanceid=$moodlelookup[$thiskid['student']]; //Recover Student's moodle userid $thisid=$context_lookup[array_search($instanceid,array_column($context_lookup,1))][0]; echo "Found ".$instanceid." with contextid=".$thisid."\n"; $role['contextid']=(int)$thisid; $role['instanceid']=(int)$instanceid; $role['contextlevel']='user'; array_push($roles,$role); } unset($resp); try{ $resp=$clientm->core_role_assign_roles($roles); }catch(Exception $e){ echo $e."\n"; } var_dump($resp);
The above fills $roles with below and doesn't work. Here userid is parent, instanceid is student and contextid is the context that matches instanceid.
array(534) { [0]=> array(5) { ["roleid"]=> int(9) ["userid"]=> int(37393) ["contextid"]=> int(36887) ["instanceid"]=> int(19846) ["contextlevel"]=> string(4) "user" }