core_role_assign_roles to asssign mentor role

core_role_assign_roles to asssign mentor role

by Doug Stevens -
Number of replies: 2

Hi all, I've written a PHP cron script to synchronize students and parents against our SIS.  I've managed to get all of it working except assigning parents to students (done on the site via "Assign roles relative to this user".  If I do this manually via our moodle site, it works perfectly, however when I run the code below there are no role assignments created nor are there any errors flagged to the console, the apache error logs, nor the PHP cli logs.  

For the code below:

roleid = 9 is assigned to a system level role with a user context called "Parent".  This shows up in the "Assign roles relative to this user" in the student's preferences.

Via a var_dump I can confirm that both $moodlelookup and $parentlookup below both provide the correct moodle userids which are stored in $role correctly.

In the userlogs for the API token's user the method call shows as "unknown method".

Can anyone spot my mistake?

Thanks!

Doug

----Code below----

$roles=array();

foreach($parentstudent as $thiskid)

    {

        $role['roleid']=9;

        $role['userid']=(int)$parentidlookup[$thiskid['parent']]; //Recover Parent's moodle userid                                         

                                                                  //Both of these Confirmed by using in Moodle profile                     

        $role['instanceid']=(int)$moodlelookup[$thiskid['student']]; //Recover Student's moodle userid                                     

        $role['contextid']=(int)30;

        $role['contextlevel']="user";

        array_push($roles,$role);

    }


unset($resp);

try{

    $params=array();

    $params['assignments']=$roles;

    $resp=$clientm->core_role_assign_roles($params);

}catch(Exception $e){

    echo $e."\n";

    }

var_dump($resp);

Average of ratings: -
In reply to Doug Stevens

Re: core_role_assign_roles to asssign mentor role

by Doug Stevens -
Just a bit more info:

A var_dump of $params above reveals what I believe are the correct arguments.  I can confirm that the userid and instanceid are the correct parent and student users in Moodle:

 

array(1) {

  ["assignments"]=>

  array(530) {

.

.

.

    [209]=>

    array(5) {

      ["roleid"]=>

      int(9)

      ["userid"]=>

      int(37597)

      ["instanceid"]=>

      int(34248)

      ["contextid"]=>

      int(30)

      ["contextlevel"]=>

      string(4) "user"

    }

.

.

.

  }

}


In reply to Doug Stevens

Re: core_role_assign_roles to asssign mentor role

by Doug Stevens -
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"
  }