function devel_generate_add_comments

Same name in other branches
  1. 4.x devel_generate/devel_generate.module \devel_generate_add_comments()
1 call to devel_generate_add_comments()
devel_generate_node_insert in devel_generate/devel_generate.module
Inserts nodes properly based on generation options.

File

devel_generate/devel_generate.inc, line 151

Code

function devel_generate_add_comments($node, $users, $max_comments, $title_length = 8) {
    $num_comments = mt_rand(1, $max_comments);
    for ($i = 1; $i <= $num_comments; $i++) {
        $comment = new stdClass();
        $comment->nid = $node->nid;
        $comment->cid = NULL;
        $comment->name = 'devel generate';
        $comment->mail = 'devel_generate@example.com';
        $comment->timestamp = mt_rand($node->created, REQUEST_TIME);
        switch ($i % 3) {
            case 1:
                $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid = 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(
                    ':nid' => $comment->nid,
                ))
                    ->fetchField();
                break;
            case 2:
                $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid > 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(
                    ':nid' => $comment->nid,
                ))
                    ->fetchField();
                break;
            default:
                $comment->pid = 0;
        }
        // The subject column has a max character length of 64
        // See bug: http://drupal.org/node/1024340
        $comment->subject = substr(devel_create_greeking(mt_rand(2, $title_length), TRUE), 0, 63);
        $comment->uid = $users[array_rand($users)];
        $comment->language = LANGUAGE_NONE;
        // Populate all core fields on behalf of field.module
        module_load_include('inc', 'devel_generate', 'devel_generate.fields');
        devel_generate_fields($comment, 'comment', 'comment_node_' . $node->type);
        comment_save($comment);
    }
}