Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-45374 messages: get_messages ws unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 3, 2014
1 parent aff9da1 commit 6ff4464
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 38 deletions.
12 changes: 6 additions & 6 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,12 @@
),

'core_message_get_messages' => array(
'classname' => 'core_message_external',
'methodname' => 'get_messages',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve a message list (conversations, notifications or boths)',
'type' => 'read',
'capabilities'=> '',
'classname' => 'core_message_external',
'methodname' => 'get_messages',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve a list of messages send or received by a user (conversations, notifications or both)',
'type' => 'read',
'capabilities' => '',
),

// === notes related functions ===
Expand Down
73 changes: 41 additions & 32 deletions message/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,31 +578,34 @@ public static function search_contacts_returns() {
public static function get_messages_parameters() {
return new external_function_parameters(
array(
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user'),
'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
'useridfrom' => new external_value(PARAM_INT,
'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
VALUE_DEFAULT, 0),
'type' => new external_value(PARAM_ALPHA,
'type of message to return, expected values are: notifications, conversations, both'),
'read' => new external_value(PARAM_BOOL, 'true for retreiving read messages, false for unread'),
'useridfrom' => new external_value(PARAM_INT, 'user id who send the message, 0 for any user', VALUE_DEFAULT, 0),
'type of message to return, expected values are: notifications, conversations and both',
VALUE_DEFAULT, 'both'),
'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true),
'newestfirst' => new external_value(PARAM_BOOL,
'true order by newest first, false for oldest first', VALUE_DEFAULT, true),
'true for ordering by newest first, false for oldest first', VALUE_DEFAULT, true),
'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0) )
);
}

/**
* Get messages function implementation.
* @param int $useridto the user id who received the messages
* @param string $type type of message tu return, notifications, conversations or both
* @param int $useridto the user id who received the message
* @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
* @param string $type type of message tu return, expected values: notifications, conversations and both
* @param bool $read true for retreiving read messages, false for unread
* @param int $useridfrom user id from for filtering
* @param bool $newestfirst true order by newest first, false for oldest first
* @param bool $newestfirst true for ordering by newest first, false for oldest first
* @param int $limitfrom limit from
* @param int $limitnum limit num
* @return external_description
* @since 2.7
*/
public static function get_messages($useridto, $type, $read, $useridfrom = 0,
public static function get_messages($useridto, $useridfrom = 0, $type = 'both' , $read = true,
$newestfirst = true, $limitfrom = 0, $limitnum = 0) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . "/message/lib.php");
Expand All @@ -611,9 +614,9 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,

$params = array(
'useridto' => $useridto,
'useridfrom' => $useridfrom,
'type' => $type,
'read' => $read,
'useridfrom' => $useridfrom,
'newestfirst' => $newestfirst,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
Expand All @@ -625,9 +628,9 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
self::validate_context($context);

$useridto = $params['useridto'];
$useridfrom = $params['useridfrom'];
$type = $params['type'];
$read = $params['read'];
$useridfrom = $params['useridfrom'];
$newestfirst = $params['newestfirst'];
$limitfrom = $params['limitfrom'];
$limitnum = $params['limitnum'];
Expand All @@ -641,7 +644,6 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
// Check if private messaging between users is allowed.
if (empty($CFG->messaging)) {
// If we are retreiving only conversations, and messaging is disabled, throw an exception.
// DOUBT -- check user is admin?
if ($type == "conversations") {
throw new moodle_exception('disabled', 'message');
}
Expand All @@ -657,7 +659,11 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
}

if (!empty($useridto)) {
$userto = $DB->get_record('user', array('id' => $useridto), '*', MUST_EXIST);
if (core_user::is_real_user($useridto)) {
$userto = core_user::get_user($useridto, '*', MUST_EXIST);
} else {
throw new moodle_exception('invaliduser');
}
}

if (!empty($useridfrom)) {
Expand All @@ -671,25 +677,23 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
throw new moodle_exception('accessdenied', 'admin');
}

if ($useridto == $useridfrom) {
throw new moodle_exception('invaliduserid');
}

// Get messages.
$messagetable = $read ? '{message_read}' : '{message}';
$usersql = "";
$joinsql = "";
$params = array('deleted' => 0);

// Empty useridto means that we are going to retrieve messages send by the useridfrom to any user.
if (empty($useridto)) {
$userfields = get_all_user_name_fields(true, 'u', 'userto', 'userto');
$joinuserfield = "mr.useridto";
$usersql = "mr.useridfrom = :useridfrom";
$userfields = get_all_user_name_fields(true, 'u', '', 'userto');
$joinsql = "JOIN {user} u ON u.id = mr.useridto";
$usersql = "mr.useridfrom = :useridfrom AND u.deleted = :deleted";
$params['useridfrom'] = $useridfrom;
} else {
$userfields = get_all_user_name_fields(true, 'u', 'userfrom', 'userfrom');
$joinuserfield = "mr.useridfrom";
$usersql = "mr.useridto = :useridto";
$userfields = get_all_user_name_fields(true, 'u', '', 'userfrom');
// Left join because useridfrom may be -10 or -20 (no-reply and support users).
$joinsql = "LEFT JOIN {user} u ON u.id = mr.useridfrom";
$usersql = "mr.useridto = :useridto AND (u.deleted IS NULL OR u.deleted = :deleted)";
$params['useridto'] = $useridto;
if (!empty($useridfrom)) {
$usersql .= " AND mr.useridfrom = :useridfrom";
Expand All @@ -701,16 +705,16 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
$typesql = "";
if ($type != 'both') {
$typesql = "AND mr.notification = :notification";
$params['notification'] = ($type == 'notification') ? 1 : 0;
$params['notification'] = ($type == 'notifications') ? 1 : 0;
}

// Finally the sort direction.
$orderdirection = $newestfirst ? 'DESC' : 'ASC';

$sql = "SELECT mr.*, $userfields
FROM $messagetable mr
JOIN {user} u ON u.id = $joinuserfield
WHERE $usersql AND u.deleted = :deleted
$joinsql
WHERE $usersql
$typesql
ORDER BY mr.timecreated $orderdirection";

Expand All @@ -737,9 +741,15 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,

// We need to get the user from the query.
if (empty($userfromfullname)) {
$user = new stdclass();
$user = username_load_fields_from_object($user, $message, 'userfrom');
$message->userfromfullname = fullname($user, $canviewfullname);
// Check for non-reply and support users.
if (core_user::is_real_user($message->useridfrom)) {
$user = new stdclass();
$user = username_load_fields_from_object($user, $message, 'userfrom');
$message->userfromfullname = fullname($user, $canviewfullname);
} else {
$user = core_user::get_user($message->useridfrom);
$message->userfromfullname = fullname($user, $canviewfullname);
}
} else {
$message->userfromfullname = $userfromfullname;
}
Expand All @@ -757,7 +767,6 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
$message->timeread = 0;
}

// DOUBT, getting crazy with formats...
$message->text = message_format_message_text($message);

$messages[$mid] = (array) $message;
Expand All @@ -775,7 +784,7 @@ public static function get_messages($useridto, $type, $read, $useridfrom = 0,
/**
* Get messages return description.
*
* @return external_description
* @return external_single_structure
* @since 2.7
*/
public static function get_messages_returns() {
Expand Down
Loading

0 comments on commit 6ff4464

Please sign in to comment.