enrol_manual_enrol_users, link user to course in Moodle via webservice

Asked

Viewed 852 times

0

I need to create a link between a user and a course, I would like to do this using webservice.

Moodle has native enrol_manual_enrol_users function for this purpose, which has as mandatory parameters the link type roleid, user id userid and the course id courseid.

But if I try to use the following error is returned, and nothing is inserted in the table mdl_role_assignments

    "debuginfo":"Missing required key in single structure: enrolments"

Even with the three mandatory fields existing at the base of the Moodle.

    /// Connection
    $token = 'ed874e6d9f52539e180f49f4d926d50f';
    $domainname = 'http://localhost/moodle';
    $functionname = 'enrol_manual_enrol_users';
    $restformat = 'json';

    //////// enrol_manual_enrol_users ////////

    /// Paramètres
    $enrolment = new stdClass();
    $enrolment->roleid = 5; //estudante(student) -> 5; moderador(teacher) -> 4; professor(editingteacher) -> 3;
    $enrolment->userid = 2;
    $enrolment->courseid = 5; 
    $enrolments = array( $enrolment);
    $params = array('enrolments' => $enrolments);

    print_r($params);

    header('Content-Type: text/plain');

    $serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
    require_once('./curl.php');
    $curl = new curl;
    //if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
    $restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
    $resp = $curl->post($serverurl . $restformat, $params);
    print_r($resp);

Follow the list of parameters accepted by this function

    'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
    'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
    'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'),
    'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL),
    'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL),
    'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL)

I have done tests assigning values to these optional parameters, but nothing changes.

1 answer

1


As I could not make this function of the webservice work, I decided to create the link directly via bank.

For this I adapted the blog querys Moodlesql in the following functions (the implementation was done in a Joomla component)

    public function moodleEnrolUsers($roleid, $userid, $courseid){
        require_once JPATH_COMPONENT.'/helpers/moodle.php';

        //getDatabaseOption retorna o objeto com as configurações de conexão com a base de dados externa
        $db = JDatabase::getInstance( MoodleHelper::getDatabaseOption() );

        // pega o id do contexto a partir do id do curso
        $contexto = $this->getMoodleContext($courseid);

        $assignment = new stdClass();
        $assignment->roleid = $roleid; //indica o tipo de vinculo que haverá entre o aluno e o curso
        $assignment->contextid= $contexto; //o id do contexto referencia indiretamente o curso
        $assignment->userid= $userid;


        $result = $db->insertObject('#__role_assignments', $assignment);

        //INSERT INTO mdl_role_assignments (roleid,contextid,userid) VALUES (?,?,?)
        return $result;

    }

getMoodleContext() returns the contextid of a course from your courseid

    //muitas tabelas não referenciam o curso diretamente pelo id, o fazem através da tabela de contexto
    public function getMoodleContext($courseid){
        require_once JPATH_COMPONENT.'/helpers/moodle.php';

        $db = JDatabase::getInstance( MoodleHelper::getDatabaseOption() );

        $query = $db->getQuery(true);
        $query->select('id');
        $query->from('`#__context`');
        $query->where('contextlevel=50 AND instanceid='.$courseid);

        $db->setQuery($query);

        return $db->loadResult();
    }

To abstract the roleid also created the following functions

    public function vincularAluno($userid, $courseid){
        return $this->moodleEnrolUsers(5, $userid, $courseid);
    }

    public function vincularTutor($userid, $courseid){
        return $this->moodleEnrolUsers(4, $userid, $courseid);
    }

    public function vincularProfessor($userid, $courseid){
        return $this->moodleEnrolUsers(3, $userid, $courseid);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.