+ /**
+ * 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()
+ )
+ ]
+ );
+ }
+