You are here

function devel_generate_add_comments in Devel 6

Same name and namespace in other branches
  1. 8.3 devel_generate/devel_generate.module \devel_generate_add_comments()
  2. 8 devel_generate/devel_generate.module \devel_generate_add_comments()
  3. 8.2 devel_generate/devel_generate.module \devel_generate_add_comments()
  4. 7 devel_generate/devel_generate.inc \devel_generate_add_comments()
  5. 4.x devel_generate/devel_generate.module \devel_generate_add_comments()
1 call to devel_generate_add_comments()
devel_generate_nodeapi in ./devel_generate.inc

File

./devel_generate.inc, line 73

Code

function devel_generate_add_comments($node, $users, $max_comments, $title_length = 8) {

  // Insert new data:
  $num_comments = mt_rand(1, $max_comments);
  for ($i = 1; $i <= $num_comments; $i++) {
    $comment->nid = $node->nid;
    $comment->cid = NULL;
    $comment->format = FILTER_FORMAT_DEFAULT;
    $comment->name = 'devel generate';
    $comment->mail = 'devel_generate@example.com';
    $comment->timestamp = mt_rand($node->created, time());
    switch ($i % 3) {
      case 1:
        $comment->pid = db_result(db_query_range("SELECT cid FROM {comments} WHERE pid = 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
        break;
      case 2:
        $comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid > 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
        break;
      default:
        $comment->pid = 0;
    }
    $comment->subject = devel_create_greeking(mt_rand(1, $title_length), TRUE);
    $comment->comment = devel_create_content();
    $comment->uid = $users[array_rand($users)];

    // this is slow but gets the threading right.
    comment_save((array) $comment);
  }
}