/** * Get a user's messages read and unread. * * @param int $userid * @return message[] */ public static function get_user_messages($userid) { global $DB; $select = 'm.id, m.useridfrom, m.useridto, m.subject, m.fullmessage, m.fullmessageformat, m.fullmessagehtml, ' . 'm.smallmessage, m.timecreated, m.notification, m.contexturl, m.contexturlname, ' . \user_picture::fields('u', null, 'useridfrom', 'fromuser'); $records = $DB->get_records_sql("\n (\n SELECT {$select}, 1 unread\n FROM {message} m\n INNER JOIN {user} u ON u.id = m.useridfrom\n WHERE m.useridto = ?\n AND contexturl IS NULL\n ) UNION ALL (\n SELECT {$select}, 0 unread\n FROM {message_read} m\n INNER JOIN {user} u ON u.id = m.useridfrom\n WHERE m.useridto = ?\n AND contexturl IS NULL\n )\n ORDER BY timecreated DESC\n ", array($userid, $userid), 0, 5); $messages = array(); foreach ($records as $record) { $message = new message($record); $message->set_fromuser(\user_picture::unalias($record, null, 'useridfrom', 'fromuser')); $messages[] = $message; } return $messages; }
/** * Get a user's messages read and unread. * * @param int $userid * @param int $since optional timestamp, only return newer messages * @return message[] */ public static function get_user_messages($userid, $since = null) { global $DB; if ($since === null) { $since = time() - 12 * WEEKSECS; } $select = 'm.id, m.useridfrom, m.useridto, m.subject, m.fullmessage, m.fullmessageformat, m.fullmessagehtml, ' . 'm.smallmessage, m.timecreated, m.notification, m.contexturl, m.contexturlname, ' . \user_picture::fields('u', null, 'useridfrom', 'fromuser'); $sql = "\n (\n SELECT {$select}, 1 unread\n FROM {message} m\n INNER JOIN {user} u ON u.id = m.useridfrom AND u.deleted = 0\n WHERE m.useridto = :userid1\n AND contexturl IS NULL\n AND m.timecreated > :fromdate1\n AND m.timeusertodeleted = 0\n ) UNION ALL (\n SELECT {$select}, 0 unread\n FROM {message_read} m\n INNER JOIN {user} u ON u.id = m.useridfrom AND u.deleted = 0\n WHERE m.useridto = :userid2\n AND contexturl IS NULL\n AND m.timecreated > :fromdate2\n AND m.timeusertodeleted = 0\n )\n ORDER BY timecreated DESC"; $params = array('userid1' => $userid, 'userid2' => $userid, 'fromdate1' => $since, 'fromdate2' => $since); $records = $DB->get_records_sql($sql, $params, 0, 5); $messages = array(); foreach ($records as $record) { $message = new message($record); $message->set_fromuser(\user_picture::unalias($record, null, 'useridfrom', 'fromuser')); $messages[] = $message; } return $messages; }