public static function unassign_roles($unassignments) {
         global $DB;

        // Do basic automatic PARAM checks on incoming data, using params description
        // If any problems are found then exceptions are thrown with helpful error messages
        $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments));

        $transaction = $DB->start_delegated_transaction();

        foreach ($params['unassignments'] as $unassignment) {
            // Ensure the current user is allowed to run this function in the unassignment context
            $context = self::get_context_from_params($unassignment);
            self::validate_context($context);
            require_capability('moodle/role:assign', $context);

            // throw an exception if user is not able to unassign the role in this context
            $roles = get_assignable_roles($context, ROLENAME_SHORT);
            if (!array_key_exists($unassignment['roleid'], $roles)) {
                throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
            }

            role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
        }

        $transaction->allow_commit();
    }
public static function unassign_roles($unassignments) {
         global $DB;

        // Do basic automatic PARAM checks on incoming data, using params description
        // If any problems are found then exceptions are thrown with helpful error messages
        $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments));

        $transaction = $DB->start_delegated_transaction();

        foreach ($params['unassignments'] as $unassignment) {
            // Ensure the current user is allowed to run this function in the unassignment context
            $context = self::get_context_from_params($unassignment);
            self::validate_context($context);
            require_capability('moodle/role:assign', $context);

            // throw an exception if user is not able to unassign the role in this context
            $roles = get_assignable_roles($context, ROLENAME_SHORT);
            if (!array_key_exists($unassignment['roleid'], $roles)) {
                throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
            }

            role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
        }

        $transaction->allow_commit();
    }
public static function unassign_roles ( $ unassignments ) { global $ DB ; // Do basic automatic PARAM checks on incoming data, using params description // If any problems are found then exceptions are thrown with helpful error messages $ params = self :: validate_parameters ( self :: unassign_roles_parameters ( ) , array ( 'unassignments' => $ unassignments ) ) ; $ transaction = $ DB -> start_delegated_transaction ( ) ; foreach ( $ params [ 'unassignments' ] as $ unassignment ) { // Ensure the current user is allowed to run this function in the unassignment context $ context = self :: get_context_from_params ( $ unassignment ) ; self :: validate_context ( $ context ) ; require_capability ( 'moodle/role:assign' , $ context ) ; // throw an exception if user is not able to unassign the role in this context $ roles = get_assignable_roles ( $ context , ROLENAME_SHORT ) ; if ( ! array_key_exists ( $ unassignment [ 'roleid' ] , $ roles ) ) { throw new invalid_parameter_exception ( 'Can not unassign roleid=' . $ unassignment [ 'roleid' ] . ' in contextid=' . $ unassignment [ 'contextid' ] ) ; } role_unassign ( $ unassignment [ 'roleid' ] , $ unassignment [ 'userid' ] , $ context -> id ) ; } $ transaction -> allow_commit ( ) ; }
Unassign roles from users

@param array $unassignments An array of unassignment
Unassign roles from users