I believe this message comes from the following code (the first if
block).
It looks like the call to can_create_contact()
fails for some reason. Maybe your user or role has no capability of some sort?
File: message/externallib.php
if (!\core_message\api::can_create_contact($params['userid'], $params['requesteduserid'])) {
$result['warnings'][] = [
'item' => 'user',
'itemid' => $params['requesteduserid'],
'warningcode' => 'cannotcreatecontactrequest',
'message' => 'You are unable to create a contact request for this user'
];
} else {
if ($requests = \core_message\api::get_contact_requests_between_users($params['userid'], $params['requesteduserid'])) {
// There should only ever be one but just in case there are multiple then we can return the first.
$result['request'] = array_shift($requests);
} else {
$result['request'] = \core_message\api::create_contact_request($params['userid'], $params['requesteduserid']);
}
}
Edit:
And indeed it very much may be a problem with configuration. Could you please check if you have messaging enabled? And if both users are sharing the same course?
File: message/classes/api.php
/**
* Checks if a user can create a contact request.
*
* @param int $userid The id of the user who is creating the contact request
* @param int $requesteduserid The id of the user being requested
* @return bool
*/
public static function can_create_contact(int $userid, int $requesteduserid) : bool {
global $CFG;
// If we can't message at all, then we can't create a contact.
if (empty($CFG->messaging)) {
return false;
}
// If we can message anyone on the site then we can create a contact.
if ($CFG->messagingallusers) {
return true;
}
// We need to check if they are in the same course.
return enrol_sharing_course($userid, $requesteduserid);
}