original_string: public static function search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) {
          global $DB;
  
          // Get the user fields we want.
          $ufields = \user_picture::fields('u', array('lastaccess'), 'userfrom_id', 'userfrom_');
          $ufields2 = \user_picture::fields('u2', array('lastaccess'), 'userto_id', 'userto_');
          // Add the uniqueid column to make each row unique and avoid SQL errors.
          $uniqueidsql = $DB->sql_concat('m.id', "'_'", 'm.useridfrom', "'_'", 'mcm.userid');
  
          $sql = "SELECT $uniqueidsql AS uniqueid, m.id, m.useridfrom, mcm.userid as useridto, m.subject, m.fullmessage,
                         m.fullmessagehtml, m.fullmessageformat, m.smallmessage, m.conversationid, m.timecreated, 0 as isread,
                         $ufields, mub.id as userfrom_blocked, $ufields2, mub2.id as userto_blocked
                    FROM {messages} m
              INNER JOIN {user} u
                      ON u.id = m.useridfrom
              INNER JOIN {message_conversations} mc
                      ON mc.id = m.conversationid
              INNER JOIN {message_conversation_members} mcm
                      ON mcm.conversationid = m.conversationid
              INNER JOIN {user} u2
                      ON u2.id = mcm.userid
               LEFT JOIN {message_users_blocked} mub
                      ON (mub.blockeduserid = u.id AND mub.userid = ?)
               LEFT JOIN {message_users_blocked} mub2
                      ON (mub2.blockeduserid = u2.id AND mub2.userid = ?)
               LEFT JOIN {message_user_actions} mua
                      ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
                   WHERE (m.useridfrom = ? OR mcm.userid = ?)
                     AND (m.useridfrom != mcm.userid OR mc.type = ?)
                     AND u.deleted = 0
                     AND u2.deleted = 0
                     AND mua.id is NULL
                     AND " . $DB->sql_like('smallmessage', '?', false) . "
                ORDER BY timecreated DESC";
  
          $params = array($userid, $userid, $userid, self::MESSAGE_ACTION_DELETED, $userid, $userid,
              self::MESSAGE_CONVERSATION_TYPE_SELF, '%' . $search . '%');
  
          // Convert the messages into searchable contacts with their last message being the message that was searched.
          $conversations = array();
          if ($messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) {
              foreach ($messages as $message) {
                  $prefix = 'userfrom_';
                  if ($userid == $message->useridfrom) {
                      $prefix = 'userto_';
                      // If it from the user, then mark it as read, even if it wasn't by the receiver.
                      $message->isread = true;
                  }
                  $blockedcol = $prefix . 'blocked';
                  $message->blocked = $message->$blockedcol ? 1 : 0;
  
                  $message->messageid = $message->id;
                  // To avoid duplicate messages, only add the message if it hasn't been added previously.
                  if (!array_key_exists($message->messageid, $conversations)) {
                      $conversations[$message->messageid] = helper::create_contact($message, $prefix);
                  }
              }
              // Remove the messageid keys (to preserve the expected type).
              $conversations = array_values($conversations);
          }
  
          return $conversations;
      }
code: public static function search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) {
          global $DB;
  
          // Get the user fields we want.
          $ufields = \user_picture::fields('u', array('lastaccess'), 'userfrom_id', 'userfrom_');
          $ufields2 = \user_picture::fields('u2', array('lastaccess'), 'userto_id', 'userto_');
          // Add the uniqueid column to make each row unique and avoid SQL errors.
          $uniqueidsql = $DB->sql_concat('m.id', "'_'", 'm.useridfrom', "'_'", 'mcm.userid');
  
          $sql = "SELECT $uniqueidsql AS uniqueid, m.id, m.useridfrom, mcm.userid as useridto, m.subject, m.fullmessage,
                         m.fullmessagehtml, m.fullmessageformat, m.smallmessage, m.conversationid, m.timecreated, 0 as isread,
                         $ufields, mub.id as userfrom_blocked, $ufields2, mub2.id as userto_blocked
                    FROM {messages} m
              INNER JOIN {user} u
                      ON u.id = m.useridfrom
              INNER JOIN {message_conversations} mc
                      ON mc.id = m.conversationid
              INNER JOIN {message_conversation_members} mcm
                      ON mcm.conversationid = m.conversationid
              INNER JOIN {user} u2
                      ON u2.id = mcm.userid
               LEFT JOIN {message_users_blocked} mub
                      ON (mub.blockeduserid = u.id AND mub.userid = ?)
               LEFT JOIN {message_users_blocked} mub2
                      ON (mub2.blockeduserid = u2.id AND mub2.userid = ?)
               LEFT JOIN {message_user_actions} mua
                      ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
                   WHERE (m.useridfrom = ? OR mcm.userid = ?)
                     AND (m.useridfrom != mcm.userid OR mc.type = ?)
                     AND u.deleted = 0
                     AND u2.deleted = 0
                     AND mua.id is NULL
                     AND " . $DB->sql_like('smallmessage', '?', false) . "
                ORDER BY timecreated DESC";
  
          $params = array($userid, $userid, $userid, self::MESSAGE_ACTION_DELETED, $userid, $userid,
              self::MESSAGE_CONVERSATION_TYPE_SELF, '%' . $search . '%');
  
          // Convert the messages into searchable contacts with their last message being the message that was searched.
          $conversations = array();
          if ($messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) {
              foreach ($messages as $message) {
                  $prefix = 'userfrom_';
                  if ($userid == $message->useridfrom) {
                      $prefix = 'userto_';
                      // If it from the user, then mark it as read, even if it wasn't by the receiver.
                      $message->isread = true;
                  }
                  $blockedcol = $prefix . 'blocked';
                  $message->blocked = $message->$blockedcol ? 1 : 0;
  
                  $message->messageid = $message->id;
                  // To avoid duplicate messages, only add the message if it hasn't been added previously.
                  if (!array_key_exists($message->messageid, $conversations)) {
                      $conversations[$message->messageid] = helper::create_contact($message, $prefix);
                  }
              }
              // Remove the messageid keys (to preserve the expected type).
              $conversations = array_values($conversations);
          }
  
          return $conversations;
      }
code_tokens: public static function search_messages ( $ userid , $ search , $ limitfrom = 0 , $ limitnum = 0 ) { global $ DB ; // Get the user fields we want. $ ufields = \ user_picture :: fields ( 'u' , array ( 'lastaccess' ) , 'userfrom_id' , 'userfrom_' ) ; $ ufields2 = \ user_picture :: fields ( 'u2' , array ( 'lastaccess' ) , 'userto_id' , 'userto_' ) ; // Add the uniqueid column to make each row unique and avoid SQL errors. $ uniqueidsql = $ DB -> sql_concat ( 'm.id' , "'_'" , 'm.useridfrom' , "'_'" , 'mcm.userid' ) ; $ sql = "SELECT $uniqueidsql AS uniqueid, m.id, m.useridfrom, mcm.userid as useridto, m.subject, m.fullmessage,
                         m.fullmessagehtml, m.fullmessageformat, m.smallmessage, m.conversationid, m.timecreated, 0 as isread,
                         $ufields, mub.id as userfrom_blocked, $ufields2, mub2.id as userto_blocked
                    FROM {messages} m
              INNER JOIN {user} u
                      ON u.id = m.useridfrom
              INNER JOIN {message_conversations} mc
                      ON mc.id = m.conversationid
              INNER JOIN {message_conversation_members} mcm
                      ON mcm.conversationid = m.conversationid
              INNER JOIN {user} u2
                      ON u2.id = mcm.userid
               LEFT JOIN {message_users_blocked} mub
                      ON (mub.blockeduserid = u.id AND mub.userid = ?)
               LEFT JOIN {message_users_blocked} mub2
                      ON (mub2.blockeduserid = u2.id AND mub2.userid = ?)
               LEFT JOIN {message_user_actions} mua
                      ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
                   WHERE (m.useridfrom = ? OR mcm.userid = ?)
                     AND (m.useridfrom != mcm.userid OR mc.type = ?)
                     AND u.deleted = 0
                     AND u2.deleted = 0
                     AND mua.id is NULL
                     AND " . $ DB -> sql_like ( 'smallmessage' , '?' , false ) . "
                ORDER BY timecreated DESC" ; $ params = array ( $ userid , $ userid , $ userid , self :: MESSAGE_ACTION_DELETED , $ userid , $ userid , self :: MESSAGE_CONVERSATION_TYPE_SELF , '%' . $ search . '%' ) ; // Convert the messages into searchable contacts with their last message being the message that was searched. $ conversations = array ( ) ; if ( $ messages = $ DB -> get_records_sql ( $ sql , $ params , $ limitfrom , $ limitnum ) ) { foreach ( $ messages as $ message ) { $ prefix = 'userfrom_' ; if ( $ userid == $ message -> useridfrom ) { $ prefix = 'userto_' ; // If it from the user, then mark it as read, even if it wasn't by the receiver. $ message -> isread = true ; } $ blockedcol = $ prefix . 'blocked' ; $ message -> blocked = $ message -> $ blockedcol ? 1 : 0 ; $ message -> messageid = $ message -> id ; // To avoid duplicate messages, only add the message if it hasn't been added previously. if ( ! array_key_exists ( $ message -> messageid , $ conversations ) ) { $ conversations [ $ message -> messageid ] = helper :: create_contact ( $ message , $ prefix ) ; } } // Remove the messageid keys (to preserve the expected type). $ conversations = array_values ( $ conversations ) ; } return $ conversations ; }
docstring: Handles searching for messages in the message area.
  
  @param int $userid The user id doing the searching
  @param string $search The string the user is searching
  @param int $limitfrom
  @param int $limitnum
  @return array
docstring_tokens: Handles searching for messages in the message area .