You are here

function privatemsg_devel_generate_new_thread in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.devel_generate.inc \privatemsg_devel_generate_new_thread()

Single Message Generation

2 calls to privatemsg_devel_generate_new_thread()
privatemsg_devel_generate_batch_new_thread in ./privatemsg.devel_generate.inc
privatemsg_devel_generate_threads in ./privatemsg.devel_generate.inc
Mass Message Generation

File

./privatemsg.devel_generate.inc, line 155

Code

function privatemsg_devel_generate_new_thread($values) {
  module_load_include('inc', 'devel_generate');

  // Make sure that uids are keyed by the actual user id.
  $uids = drupal_map_assoc(devel_get_users());

  // Do not allow anonymous (key) to send/receive private messages
  unset($uids[key($uids)]);
  $author = user_load($uids[array_rand($uids)]);
  $subject = devel_create_greeking(rand(1, $values['subject_length']), TRUE);
  $body = devel_create_content();
  $timestamp = rand(0, $values['time_range']);
  $options = array(
    'author' => $author,
    'timestamp' => REQUEST_TIME - $timestamp,
  );

  // Remove author when adding new recipients.
  unset($uids[array_search($author->uid, $uids)]);

  // Get a random amount of user ids.
  $num_recipients = rand($values['min_recipients'], $values['max_recipients']);
  $recipient_uids = array_rand($uids, $num_recipients);

  // Convert to array if just a single user has been loaded.
  if ($num_recipients > 1) {
    $recipients = user_load_multiple($recipient_uids);
  }
  else {
    $recipients = user_load($recipient_uids);
    $recipients = array(
      $recipients,
    );
  }

  // Generate message.
  $validated = privatemsg_new_thread($recipients, $subject, $body, $options);

  // Get thread information for generating replies.
  $thread_id = $validated['message']->thread_id;
  $num_replies = rand(0, $values['max_thread_length']);
  $reply_timestamp = $timestamp;

  // Add author back in to possible senders.
  $recipients[$author->uid] = $author;

  // Generate thread replies.
  for ($j = 0; $j <= $num_replies; $j++) {
    $reply_body = devel_create_content();
    $reply_author = $recipients[array_rand($recipients)];
    $reply_timestamp = rand(0, $reply_timestamp);
    $reply_options = array(
      'author' => $reply_author,
      'timestamp' => REQUEST_TIME - $reply_timestamp,
    );
    privatemsg_reply($thread_id, $reply_body, $reply_options);
  }
}