MDL-54973 message: Taking care of support and noreply users on search
[moodle.git] / message / classes / search / message_received.php
index eaf9d00..f57dc9f 100644 (file)
@@ -35,6 +35,22 @@ defined('MOODLE_INTERNAL') || die();
  */
 class message_received extends base_message {
 
+    /**
+     * Returns recordset containing message records.
+     *
+     * @param int $modifiedfrom timestamp
+     * @return \moodle_recordset
+     */
+    public function get_recordset_by_timestamp($modifiedfrom = 0) {
+        global $DB;
+
+        // We don't want to index messages received from noreply and support users.
+        $params = array('modifiedfrom' => $modifiedfrom, 'noreplyuser' => \core_user::NOREPLY_USER,
+            'supportuser' => \core_user::SUPPORT_USER);
+        return $DB->get_recordset_select('message_read', 'timecreated >= :modifiedfrom AND
+            useridto != :noreplyuser AND useridto != :supportuser', $params, 'timecreated ASC');
+    }
+
     /**
      * Returns the document associated with this message record.
      *
@@ -64,8 +80,8 @@ class message_received extends base_message {
             return \core_search\manager::ACCESS_DELETED;
         }
 
-        $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
-        $userto = $DB->get_record('user', array('id' => $message->useridto));
+        $userfrom = \core_user::get_user($message->useridfrom, 'id, deleted');
+        $userto = \core_user::get_user($message->useridto, 'id, deleted');
 
         if (!$userfrom || !$userto || $userfrom->deleted || $userto->deleted) {
             return \core_search\manager::ACCESS_DELETED;
@@ -82,4 +98,4 @@ class message_received extends base_message {
         return \core_search\manager::ACCESS_GRANTED;
     }
 
-}
\ No newline at end of file
+}