Skip to content

Commit

Permalink
Merge branch 'MDL-63549-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Nov 1, 2018
2 parents 7b70c6b + eb35e0b commit 1becffa
Show file tree
Hide file tree
Showing 8 changed files with 1,054 additions and 273 deletions.
11 changes: 10 additions & 1 deletion lib/db/services.php
Expand Up @@ -989,7 +989,8 @@
'classname' => 'core_message_external',
'methodname' => 'data_for_messagearea_conversations',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve the template data for the conversation list',
'description' => '** DEPRECATED ** Please do not call this function any more.
Retrieve the template data for the conversation list',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
Expand Down Expand Up @@ -1036,6 +1037,14 @@
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_conversations' => array(
'classname' => 'core_message_external',
'methodname' => 'get_conversations',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve a list of conversations for a user',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_messages' => array(
'classname' => 'core_message_external',
'methodname' => 'get_messages',
Expand Down
315 changes: 220 additions & 95 deletions message/classes/api.php

Large diffs are not rendered by default.

40 changes: 37 additions & 3 deletions message/classes/helper.php
Expand Up @@ -491,14 +491,13 @@ public static function get_member_info(int $referenceuserid, array $userids) : a

list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
$userfields = \user_picture::fields('u', array('lastaccess'));
$userssql = "SELECT $userfields, mc.id AS contactid, mub.id AS blockedid
$userssql = "SELECT $userfields, u.deleted, mc.id AS contactid, mub.id AS blockedid
FROM {user} u
LEFT JOIN {message_contacts} mc
ON (mc.userid = ? AND mc.contactid = u.id)
LEFT JOIN {message_users_blocked} mub
ON (mub.userid = ? AND mub.blockeduserid = u.id)
WHERE u.id $useridsql
AND u.deleted = 0";
WHERE u.id $useridsql";
$usersparams = array_merge([$referenceuserid, $referenceuserid], $usersparams);
$otherusers = $DB->get_records_sql($userssql, $usersparams);

Expand All @@ -524,8 +523,43 @@ public static function get_member_info(int $referenceuserid, array $userids) : a
$data->iscontact = ($member->contactid) ? true : false;
$data->isblocked = ($member->blockedid) ? true : false;

$data->isdeleted = ($member->deleted) ? true : false;

$members[$data->id] = $data;
}
return $members;
}

/**
* Backwards compatibility formatter, transforming the new output of get_conversations() into the old format.
*
* @param array $conversations the array of conversations, which must come from get_conversations().
* @return array the array of conversations, formatted in the legacy style.
*/
public static function get_conversations_legacy_formatter(array $conversations) : array {
// Transform new data format back into the old format, just for BC during the deprecation life cycle.
$tmp = [];
foreach ($conversations as $id => $conv) {
$data = new \stdClass();
// The logic for the 'other user' is as follows:
// If a conversation is of type 'individual', the other user is always the member who is not the current user.
// If the conversation is of type 'group', the other user is always the sender of the most recent message.
// The get_conversations method already follows this logic, so we just need the first member.
$otheruser = reset($conv->members);
$data->userid = $otheruser->id;
$data->useridfrom = $conv->messages[0]->useridfrom ?? null;
$data->fullname = $conv->members[$otheruser->id]->fullname;
$data->profileimageurl = $conv->members[$otheruser->id]->profileimageurl;
$data->profileimageurlsmall = $conv->members[$otheruser->id]->profileimageurlsmall;
$data->ismessaging = isset($conv->messages[0]->text) ? true : false;
$data->lastmessage = $conv->messages[0]->text ?? null;
$data->messageid = $conv->messages[0]->id ?? null;
$data->isonline = $conv->members[$otheruser->id]->isonline ?? null;
$data->isblocked = $conv->members[$otheruser->id]->isblocked ?? null;
$data->isread = $conv->isread;
$data->unreadcount = $conv->unreadcount;
$tmp[$data->userid] = $data;
}
return $tmp;
}
}
127 changes: 127 additions & 0 deletions message/externallib.php
Expand Up @@ -868,6 +868,34 @@ private static function get_messagearea_contact_structure() {
);
}

/**
* Return the structure of a conversation.
*
* @return external_single_structure
* @since Moodle 3.6
*/
private static function get_conversation_structure() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'The conversation id'),
'name' => new external_value(PARAM_NOTAGS, 'The conversation name, if set', VALUE_DEFAULT, null),
'subname' => new external_value(PARAM_NOTAGS, 'A subtitle for the conversation name, if set', VALUE_DEFAULT, null),
'type' => new external_value(PARAM_INT, 'The type of the conversation (1=individual,2=group)'),
'membercount' => new external_value(PARAM_INT, 'Total number of conversation members'),
'isfavourite' => new external_value(PARAM_BOOL, 'If the user marked conversation this conversation as a favourite'),
'isread' => new external_value(PARAM_BOOL, 'If the user has read all messages in the conversation'),
'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation',
VALUE_DEFAULT, null),
'members' => new external_multiple_structure(
self::get_conversation_member_structure()
),
'messages' => new external_multiple_structure(
self::get_conversation_message_structure()
),
)
);
}

/**
* Return the structure of a conversation member.
*
Expand Down Expand Up @@ -1165,9 +1193,87 @@ public static function data_for_messagearea_search_messages_returns() {
);
}

/**
* Get conversations parameters.
*
* @return external_function_parameters
* @since 3.6
*/
public static function get_conversations_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'),
'limitfrom' => new external_value(PARAM_INT, 'The offset to start at', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number of conversations to this', VALUE_DEFAULT, 0),
'type' => new external_value(PARAM_INT, 'Filter by type', VALUE_DEFAULT, null),
'favourites' => new external_value(PARAM_BOOL, 'Whether to restrict the results to contain NO favourite
conversations (false), ONLY favourite conversation (true), or ignore any restriction altogether (null)',
VALUE_DEFAULT, null),

)
);
}

/**
* Get the list of conversations for the user.
*
* @param int $userid The id of the user who is performing the search
* @param int $limitfrom
* @param int $limitnum
* @param int|null $type
* @param bool|null $favourites
* @return stdClass
* @throws \moodle_exception if the messaging feature is disabled on the site.
* @since 3.2
*/
public static function get_conversations($userid, $limitfrom = 0, $limitnum = 0, int $type = null, bool $favourites = null) {
global $CFG, $USER;

// All the standard BL checks.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}

$params = array(
'userid' => $userid,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum,
'type' => $type,
'favourites' => $favourites
);
self::validate_parameters(self::get_conversations_parameters(), $params);

$systemcontext = context_system::instance();
self::validate_context($systemcontext);

if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
throw new moodle_exception('You do not have permission to perform this action.');
}

$conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum, $type, $favourites);
return (object) ['conversations' => $conversations];
}

/**
* Get conversations returns.
*
* @return external_single_structure
* @since 3.6
*/
public static function get_conversations_returns() {
return new external_single_structure(
[
'conversations' => new external_multiple_structure(
self::get_conversation_structure()
)
]
);
}

/**
* The messagearea conversations parameters.
*
* @deprecated since 3.6
* @return external_function_parameters
* @since 3.2
*/
Expand All @@ -1184,6 +1290,13 @@ public static function data_for_messagearea_conversations_parameters() {
/**
* Get messagearea conversations.
*
* NOTE FOR FINAL DEPRECATION:
* When removing this method, please also consider removal of get_conversations_legacy_formatter()
* from the \core_message\helper class. This helper method was used solely to format the new get_conversations() return data
* into the old format used here, and in message/index.php. If we no longer need either of these, then that method can be
* removed.
*
* @deprecated since 3.6
* @param int $userid The id of the user who we are viewing conversations for
* @param int $limitfrom
* @param int $limitnum
Expand Down Expand Up @@ -1214,6 +1327,10 @@ public static function data_for_messagearea_conversations($userid, $limitfrom =
}

$conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum);

// Format the conversations in the legacy style, as the get_conversations method has since been changed.
$conversations = \core_message\helper::get_conversations_legacy_formatter($conversations);

$conversations = new \core_message\output\messagearea\contacts(null, $conversations);

$renderer = $PAGE->get_renderer('core_message');
Expand All @@ -1223,6 +1340,7 @@ public static function data_for_messagearea_conversations($userid, $limitfrom =
/**
* The messagearea conversations return structure.
*
* @deprecated since 3.6
* @return external_single_structure
* @since 3.2
*/
Expand All @@ -1236,6 +1354,15 @@ public static function data_for_messagearea_conversations_returns() {
);
}

/**
* Marking the method as deprecated.
*
* @return bool
*/
public static function data_for_messagearea_conversations_is_deprecated() {
return true;
}

/**
* The messagearea contacts return parameters.
*
Expand Down
3 changes: 3 additions & 0 deletions message/index.php
Expand Up @@ -106,6 +106,9 @@
$conversations = \core_message\api::get_contacts($user1->id, 0, 20);
} else {
$conversations = \core_message\api::get_conversations($user1->id, 0, 20);

// Format the conversations in the legacy style, as the get_conversations method has since been changed.
$conversations = \core_message\helper::get_conversations_legacy_formatter($conversations);
}
$messages = [];
if (!$user2realuser) {
Expand Down

0 comments on commit 1becffa

Please sign in to comment.