/** * Get unread messages count function. * * @since 3.2 * @throws invalid_parameter_exception * @throws moodle_exception * @param int $useridto the user id who received the message * @return external_description */ public static function get_unread_conversations_count($useridto) { global $USER, $CFG; // Check if messaging is enabled. if (empty($CFG->messaging)) { throw new moodle_exception('disabled', 'message'); } $params = self::validate_parameters(self::get_unread_conversations_count_parameters(), array('useridto' => $useridto)); $context = context_system::instance(); self::validate_context($context); $useridto = $params['useridto']; if (!empty($useridto)) { if (core_user::is_real_user($useridto)) { $userto = core_user::get_user($useridto, '*', MUST_EXIST); } else { throw new moodle_exception('invaliduser'); } } else { $useridto = $USER->id; } // Check if the current user is the receiver or just a privileged user. if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { throw new moodle_exception('accessdenied', 'admin'); } return \core_message\api::count_unread_conversations($userto); }