Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Raúl González Álvarez -
Number of replies: 8

Hi guys; good morning from Madrid.

 

I am having problems with the function as web service, called "moodle_role_assign" in my moodle 2.0.2+; the problem is the following:

When the user is already enrolled in the course and I can see it through the participants block or through "the Block settings/course Administration/enrolled users" I haven't got any problem with the function "moodle_role_assign" because, I am able to see clearly how It has just worked out and the role has just been assigned to the user which has been choosen; here you are the source:

<?php

 

require_once("config.php");
require_once "Zend/XmlRpc/Client.php";

$assignments = array(
                array('roleid'=>'3','userid'=>'3721','contextid'=>'88')
            );

  
  $asignaciones = array($assignments);
  $params = array('assignments' =>$assignments);
                

  $token='283e113ff4b047d0aa4f11832695c444';
  $serverurl = $CFG->wwwroot.'/webservice/xmlrpc/server.php?wstoken='.$token;
  
  $xmlrpcclient = new Zend_XmlRpc_Client($serverurl);

  $function='moodle_role_assign';

  try{
   $createduser = $xmlrpcclient->call($function, $params);
   
   
  }catch(Exception $e) {
      
   var_dump($e);
  }
  var_dump($createduser);


?>  
 

The problem comes when the person, who is wanted to be enrolled, isn't even user in the course yet. In this case the call to the function "moodle_role_assign" keeps not returning any error as the above case, but after being called I can not see the user neither in the participants block  nir enrolled users and eventually the users is not enrolled in the course so It seems that it isn't working well but I don´t know why, moreover I can not watch any debug trace because the function, as i said,  doesn´t throw any fault or exception.

Does Someboy know anything about this matter? Maybe I forgot doing something more in the source.

 

Hugs and thanks a lot for all.

Regards.

 

 

Average of ratings: -
In reply to Raúl González Álvarez

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Netram Dhakar -

HI Raul,

I have faced this issue. It is a fault  in code in enrol/externallib.php

And have fixed this this issue. Steps are as follows: 

1. Open open file enrol/externallib.php in a text editor.

2. Search a  function "public static function role_assign($assignments) {"

3. Then go to line  "role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);"

4. Add the following code after just above line.

global $USER, $CFG;
if(!$alreadyenroled=$DB->get_records_sql("SELECT * FROM ".$CFG->prefix."user_enrolments WHERE userid=".$assignment['userid']." AND enrolid=(SELECT id FROM ".$CFG->prefix."enrol WHERE courseid=$context->instanceid AND enrol='manual')"))
{
$ue = new stdClass();
$ue->enrolid =$DB->get_field('enrol', 'id', array('courseid'=>$context->instanceid, 'enrol'=>'manual'));
$ue->status = 0;
$ue->userid = $assignment['userid'];
$ue->timestart = time();
$ue->timeend = 0;
$ue->modifierid = $USER->id;
$ue->timecreated = time();
$ue->timemodified = $ue->timecreated;
$ue->id = $DB->insert_record('user_enrolments', $ue);
}

Note: If still any issue then let me know. I am also attaching my externallib.php file for reference.

Regards

Netram Dhakar

Average of ratings: Useful (1)
In reply to Raúl González Álvarez

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Netram Dhakar -

HI Raul,

I have faced this issue. It is a fault  in code in enrol/externallib.php

And have fixed this this issue. Steps are as follows: 

1. Open open file enrol/externallib.php in a text editor.

2. Search a  function "public static function role_assign($assignments) {"

3. Then go to line  "role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);"

4. Add the following code after just above line.

global $USER, $CFG;
if(!$alreadyenroled=$DB->get_records_sql("SELECT * FROM ".$CFG->prefix."user_enrolments WHERE userid=".$assignment['userid']." AND enrolid=(SELECT id FROM ".$CFG->prefix."enrol WHERE courseid=$context->instanceid AND enrol='manual')"))
{
$ue = new stdClass();
$ue->enrolid =$DB->get_field('enrol', 'id', array('courseid'=>$context->instanceid, 'enrol'=>'manual'));
$ue->status = 0;
$ue->userid = $assignment['userid'];
$ue->timestart = time();
$ue->timeend = 0;
$ue->modifierid = $USER->id;
$ue->timecreated = time();
$ue->timemodified = $ue->timecreated;
$ue->id = $DB->insert_record('user_enrolments', $ue);
}

Note: If still any issue then let me know. I am also attaching my externallib.php file for reference.

Regards

Netram Dhakar

Average of ratings: Useful (1)
In reply to Netram Dhakar

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Raúl González Álvarez -

Hi Netram;

thank you very much for your reply, it worked perfectly.

My best regards; It was really usefull for me.

 

 

In reply to Raúl González Álvarez

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Pia Liersch -

I have similar Problem with the function "core_role_assign"

My Code works and the User is in the role, but become a error_msg.

 

anyone a ideas, what this mean?

 

    $function = "core_role_assign_roles";
    $assignment = array( 'roleid' => $role_id, 'userid' => $user_id, 'contextid' => $context_id );
    $assignments = array( $assignment );
    $params = array('assignments' => $assignments);
    $response = $this->call_moodle($function, $params);

output from $response

string

 '<?xml version="1.0" encoding="UTF-8" ?> 
<EXCEPTION class="coding_exception">
<MESSAGE>Fehler in der Kodierung gefunden, den nur ein Programmierer korrigieren kann: PHP catchable fatal error</MESSAGE>
<DEBUGINFO>Argument 1 passed to external_api::clean_returnvalue() must be an instance of external_description, null given, called in /srv/www/moodle/testinst4/webservice/rest/locallib.php on line 88 and defined</DEBUGINFO>
</EXCEPTION> '
(length=436)
In reply to Pia Liersch

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Netram Dhakar -

Hi Patrick,

Please check your code carefuly. According to my exp./knowledge any one argument going empty.

Regards

Netram Dhakar

In reply to Netram Dhakar

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Pia Liersch -

sry, for double Post. My Bug is allready on the Bug Tracker

 

http://moodle.org/mod/forum/discuss.php?d=193402

https://tracker.moodle.org/browse/MDL-31082

In reply to Pia Liersch

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Fábio Souto -

Hello,

That bug is already fixed, meaning that you have an outdated installation.

If you update it, it will fix that problem.

 

 

Fábio

In reply to Netram Dhakar

Re: Problems with web service function "moodle_role_assign" if the user, before calling the function, isn't enrolled in the course yet.

by Jérôme Mouneyrac -

Hi,
the following comment mention code from 2.2 but is applicable to 2.0:

I understand that it is confusing and not obvious that core_role_assign_roles does not enrol a user. However it is a Moodle feature.

In Moodle you can assign a role to a user who is not enrolled. For example: Hidden_roles (you probably better ask in the role forum for more information about the subject)

You most likely want to use core_enrol_get_enrolled_users (to know who is already enrolled) and enrol_manual_enrol_users (to enrol the students not already enrolled) too.

Cheers