You are here

function devel_create_users in Devel 7

Same name and namespace in other branches
  1. 5 devel_generate.inc \devel_create_users()
  2. 6 devel_generate.inc \devel_create_users()

Generate some random users.

Parameters

$num: Number of users to generate.

$kill: Boolean that indicates if existing users should be removed first.

$age: The max age of each randomly-generated user, in seconds.

$roles: An array of role IDs that the users should receive.

$pass: A string to be used as common password for all generated users.

2 calls to devel_create_users()
devel_generate_users_form_submit in devel_generate/devel_generate.module
FormAPI submission to generate users.
drush_devel_generate_users in devel_generate/devel_generate.drush.inc
Command callback. Generate a number of users.

File

devel_generate/devel_generate.inc, line 17

Code

function devel_create_users($num, $kill, $age = 0, $roles = array(), $pass = NULL) {
  $url = parse_url($GLOBALS['base_url']);
  if ($kill) {
    $uids = db_select('users', 'u')
      ->fields('u', array(
      'uid',
    ))
      ->condition('uid', 1, '>')
      ->execute()
      ->fetchAllAssoc('uid');
    user_delete_multiple(array_keys($uids));
    drupal_set_message(format_plural(count($uids), '1 user deleted', '@count users deleted.'));
  }

  // Determine if we should create user pictures.
  $pic_config = FALSE;
  module_load_include('inc', 'system', 'image.gd');
  if (variable_get('user_pictures', 0) && function_exists('image_gd_check_settings') && image_gd_check_settings()) {
    $pic_config['path'] = variable_get('user_picture_path', 'pictures');
    list($pic_config['width'], $pic_config['height']) = explode('x', variable_get('user_picture_dimensions', '85x85'));
  }
  if ($num > 0) {
    $names = array();
    while (count($names) < $num) {
      $name = devel_generate_word(mt_rand(6, 12));
      $names[$name] = '';
    }
    if (empty($roles)) {
      $roles = array(
        DRUPAL_AUTHENTICATED_RID,
      );
    }
    foreach ($names as $name => $value) {
      $edit = array(
        'uid' => NULL,
        'name' => $name,
        'pass' => $pass,
        'mail' => $name . '@' . $url['host'] . '.invalid',
        'status' => 1,
        'created' => REQUEST_TIME - mt_rand(0, $age),
        'roles' => drupal_map_assoc($roles),
        'devel_generate' => TRUE,
      );

      // Populate all core fields on behalf of field.module
      module_load_include('inc', 'devel_generate', 'devel_generate.fields');
      $edit = (object) $edit;
      $edit->language = LANGUAGE_NONE;
      devel_generate_fields($edit, 'user', 'user');
      $edit = (array) $edit;
      $account = user_save(drupal_anonymous_user(), $edit);
      if ($pic_config) {

        // Since the image.module should scale the picture just pick an
        // arbitrary size that it's too big for our font.
        $im = imagecreatetruecolor(200, 200);

        // Randomize the foreground using the md5 of the user id, then invert it
        // for the background color so there's enough contrast to read the text.
        $parts = array_map('hexdec', str_split(md5($account->uid), 2));
        $fg = imagecolorallocate($im, $parts[1], $parts[3], $parts[5]);
        $bg = imagecolorallocate($im, 255 - $parts[0], 255 - $parts[1], 255 - $parts[2]);

        // Fill the background then print their user info.
        imagefill($im, 0, 0, $bg);
        imagestring($im, 5, 5, 5, "#" . $account->uid, $fg);
        imagestring($im, 5, 5, 25, $account->name, $fg);

        // Create an empty, managed file where we want the user's picture to
        // be so we can have GD overwrite it with the image.
        $picture_directory = variable_get('file_default_scheme', 'public') . '://' . variable_get('user_picture_path', 'pictures');
        file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
        $destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '.png');
        $file = file_save_data('', $destination);

        // GD doesn't like stream wrapped paths so convert the URI to a normal
        // file system path.
        if (isset($file) && ($wrapper = file_stream_wrapper_get_instance_by_uri($file->uri))) {
          imagepng($im, $wrapper
            ->realpath());
        }
        imagedestroy($im);

        // Clear the cached filesize, set the owner and MIME-type then re-save
        // the file.
        clearstatcache();
        $file->uid = $account->uid;
        $file->filemime = 'image/png';
        $file = file_save($file);

        // Save the user record with the new picture.
        $edit = (array) $account;
        $edit['picture'] = $file;
        $edit['pass'] = $pass;

        // Reassign password as it is replaced with the hashed version in $account
        user_save($account, $edit);
      }
    }
  }
  drupal_set_message(t('!num_users created.', array(
    '!num_users' => format_plural($num, '1 user', '@count users'),
  )));
}