MDL-45374 messages: get_messages ws definition
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 5 May 2014 12:34:17 +0000 (14:34 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 3 Oct 2014 14:37:12 +0000 (16:37 +0200)
lib/db/services.php
message/externallib.php

index 6b7130b..941a2a7 100644 (file)
@@ -761,6 +761,15 @@ $functions = array(
         'capabilities'=> '',
     ),
 
+    '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'=> '',
+    ),
+
     // === notes related functions ===
 
     'moodle_notes_create_notes' => array(
@@ -958,7 +967,8 @@ $services = array(
             'mod_forum_get_forums_by_courses',
             'mod_forum_get_forum_discussions',
             'mod_forum_get_forum_discussion_posts',
-            'core_files_get_files'),
+            'core_files_get_files',
+            'core_message_get_messages'),
         'enabled' => 0,
         'restrictedusers' => 0,
         'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE,
index 1ea4932..6406ba8 100644 (file)
@@ -568,6 +568,246 @@ class core_message_external extends external_api {
             'List of contacts'
         );
     }
+
+    /**
+     * Get messages parameters description.
+     *
+     * @return external_function_parameters
+     * @since 2.7
+     */
+    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'),
+                '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),
+                'newestfirst' => new external_value(PARAM_BOOL,
+                            'true order 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  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  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,
+                                        $newestfirst = true, $limitfrom = 0, $limitnum = 0) {
+        global $CFG, $DB, $USER;
+        require_once($CFG->dirroot . "/message/lib.php");
+
+        $warnings = array();
+
+        $params = array(
+            'useridto' => $useridto,
+            'type' => $type,
+            'read' => $read,
+            'useridfrom' => $useridfrom,
+            'newestfirst' => $newestfirst,
+            'limitfrom' => $limitfrom,
+            'limitnum' => $limitnum
+        );
+
+        $params = self::validate_parameters(self::get_messages_parameters(), $params);
+
+        $context = context_system::instance();
+        self::validate_context($context);
+
+        $useridto = $params['useridto'];
+        $type = $params['type'];
+        $read = $params['read'];
+        $useridfrom = $params['useridfrom'];
+        $newestfirst = $params['newestfirst'];
+        $limitfrom = $params['limitfrom'];
+        $limitnum = $params['limitnum'];
+
+        $allowedvalues = array('notifications', 'conversations', 'both');
+        if (!in_array($type, $allowedvalues)) {
+            throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' .
+                'allowed values are: ' . implode(',', $allowedvalues));
+        }
+
+        // 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');
+            }
+            if ($type == "both") {
+                $warning = array();
+                $warning['item'] = 'message';
+                $warning['itemid'] = $USER->id;
+                $warning['warningcode'] = '1';
+                $warning['message'] = 'Private messages (conversations) are not enabled in this site.
+                    Only notifications will be returned';
+                $warnings[] = $warning;
+            }
+        }
+
+        if (!empty($useridto)) {
+            $userto = $DB->get_record('user', array('id' => $useridto), '*', MUST_EXIST);
+        }
+
+        if (!empty($useridfrom)) {
+            // We use get_user here because the from user can be the noreply or support user.
+            $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
+        }
+
+        // Check if the current user is the sender/receiver or just a privileged user.
+        if ($useridto != $USER->id and $useridfrom != $USER->id and
+             !has_capability('moodle/site:readallmessages', $context)) {
+            throw new moodle_exception('accessdenied', 'admin');
+        }
+
+        if ($useridto == $useridfrom) {
+            throw new moodle_exception('invaliduserid');
+        }
+
+        // Get messages.
+        $messagetable = $read ? '{message_read}' : '{message}';
+        $usersql = "";
+        $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";
+            $params['useridfrom'] = $useridfrom;
+        } else {
+            $userfields = get_all_user_name_fields(true, 'u', 'userfrom', 'userfrom');
+            $joinuserfield = "mr.useridfrom";
+            $usersql = "mr.useridto = :useridto";
+            $params['useridto'] = $useridto;
+            if (!empty($useridfrom)) {
+                $usersql .= " AND mr.useridfrom = :useridfrom";
+                $params['useridfrom'] = $useridfrom;
+            }
+        }
+
+        // Now, if retrieve notifications, conversations or both.
+        $typesql = "";
+        if ($type != 'both') {
+            $typesql = "AND mr.notification = :notification";
+            $params['notification'] = ($type == 'notification') ? 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
+                        $typesql
+                 ORDER BY mr.timecreated $orderdirection";
+
+        if ($messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) {
+            $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
+
+            // In some cases, we don't need to get the to/from user objects from the sql query.
+            $userfromfullname = '';
+            $usertofullname = '';
+
+            // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
+            if (!empty($useridto)) {
+                $usertofullname = fullname($userto, $canviewfullname);
+                // The user from may or may not be filled.
+                if (!empty($useridfrom)) {
+                    $userfromfullname = fullname($userfrom, $canviewfullname);
+                }
+            } else {
+                // If the useridto field is empty, the useridfrom must be filled.
+                $userfromfullname = fullname($userfrom, $canviewfullname);
+            }
+
+            foreach ($messages as $mid => $message) {
+
+                // 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);
+                } else {
+                    $message->userfromfullname = $userfromfullname;
+                }
+
+                // We need to get the user from the query.
+                if (empty($usertofullname)) {
+                    $user = new stdclass();
+                    $user = username_load_fields_from_object($user, $message, 'userto');
+                    $message->usertofullname = fullname($user, $canviewfullname);
+                } else {
+                    $message->usertofullname = $usertofullname;
+                }
+
+                if (!isset($message->timeread)) {
+                    $message->timeread = 0;
+                }
+
+                // DOUBT, getting crazy with formats...
+                $message->text = message_format_message_text($message);
+
+                $messages[$mid] = (array) $message;
+            }
+        }
+
+        $results = array(
+            'messages' => $messages,
+            'warnings' => $warnings
+        );
+
+        return $results;
+    }
+
+    /**
+     * Get messages return description.
+     *
+     * @return external_description
+     * @since 2.7
+     */
+    public static function get_messages_returns() {
+        return new external_single_structure(
+            array(
+                'messages' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'id' => new external_value(PARAM_INT, 'mMssage id'),
+                            'useridfrom' => new external_value(PARAM_INT, 'User from id'),
+                            'useridto' => new external_value(PARAM_INT, 'User to id'),
+                            'subject' => new external_value(PARAM_TEXT, 'The message subject'),
+                            'text' => new external_value(PARAM_RAW, 'The message text formated'),
+                            'fullmessage' => new external_value(PARAM_RAW, 'The message'),
+                            'fullmessageformat' => new external_value(PARAM_INT, 'The message message format'),
+                            'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'),
+                            'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'),
+                            'notification' => new external_value(PARAM_INT, 'Is a notification?'),
+                            'contexturl' => new external_value(PARAM_RAW, 'Context URL'),
+                            'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'),
+                            'timecreated' => new external_value(PARAM_INT, 'Time created'),
+                            'timeread' => new external_value(PARAM_INT, 'Time read'),
+                            'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'),
+                            'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name')
+                        ), 'message'
+                    )
+                ),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
+
 }
 
 /**