Elgg  Version 3.0
Users.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Seeds;
4 
10 class Users extends Seed {
11 
15  public function seed() {
16 
17  $count_users = function () {
18  return elgg_get_entities([
19  'types' => 'user',
20  'metadata_names' => '__faker',
21  'count' => true,
22  ]);
23  };
24 
25  $count_friends = function ($user) {
26  return elgg_get_entities([
27  'types' => 'user',
28  'relationship' => 'friend',
29  'relationship_guid' => $user->guid,
30  'inverse_relationship' => false,
31  'metadata_names' => '__faker',
32  'count' => true,
33  ]);
34  };
35 
36  $exclude = [];
37 
38  $this->advance($count_users());
39 
40  while ($count_users() < $this->limit) {
41  $user = $this->getRandomUser($exclude);
42  if (!$user) {
43  $user = $this->createUser([], [], [
44  'profile_fields' => (array) elgg_get_config('profile_fields'),
45  ]);
46  if (!$user) {
47  continue;
48  }
49  }
50 
51  $this->createIcon($user);
52 
53  $exclude[] = $user->guid;
54 
55  // Friend the user other members
56  // Create a friend access collection and add some random friends to it
57 
58  if ($count_friends($user)) {
59  continue;
60  }
61 
62  $collection_id = create_access_collection('Best Fake Friends Collection', $user->guid, 'friends_collection');
63  if ($collection_id > 0) {
64  $this->log("Created new friend collection for user {$user->getDisplayName()} [collection_id: {$collection_id}]");
65  }
66 
67  $friends_limit = $this->faker()->numberBetween(5, 10);
68 
69  $friends_exclude = [$user->guid];
70  while ($count_friends($user) < $friends_limit) {
71  $friend = $this->getRandomUser($friends_exclude);
72  if (!$friend) {
73  $this->createUser();
74  if (!$friend) {
75  continue;
76  }
77  }
78 
79  $friends_exclude[] = $friend->guid;
80 
81  if ($user->addFriend($friend->guid, true)) {
82  $this->log("User {$user->getDisplayName()} [guid: {$user->guid}] friended user {$friend->getDisplayName()} [guid: {$friend->guid}]");
83  if ($this->faker()->boolean() && $collection_id > 0) {
84  add_user_to_access_collection($friend->guid, $collection_id);
85  }
86  }
87  }
88 
89  $this->advance();
90  }
91 
92  }
93 
97  public function unseed() {
98 
99  $users = elgg_get_entities([
100  'types' => 'user',
101  'metadata_names' => '__faker',
102  'limit' => 0,
103  'batch' => true,
104  ]);
105 
106  /* @var $users \ElggBatch */
107 
108  $users->setIncrementOffset(false);
109 
110  foreach ($users as $user) {
111  if ($user->delete()) {
112  $this->log("Deleted user $user->guid");
113  } else {
114  $this->log("Failed to delete user $user->guid");
115  }
116 
117  $this->advance();
118  }
119  }
120 
121 }
log($msg, $level=LogLevel::NOTICE)
Log a message.
Definition: Seeding.php:762
getRandomUser(array $exclude=[])
Returns random fake user.
Definition: Seeding.php:442
createIcon(ElggEntity $entity)
Create an icon for an entity.
Definition: Seeding.php:666
faker($locale= 'en_US')
Returns an instance of faker.
Definition: Seeding.php:43
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
add_user_to_access_collection($user_guid, $collection_id)
Adds a user to an access collection.
Definition: access.php:324
$user
Definition: ban.php:7
create_access_collection($name, $owner_guid=0, $subtype=null)
Creates a new access collection.
Definition: access.php:259
createUser(array $attributes=[], array $metadata=[])
Create a new fake user.
Abstract seed.
Definition: Seed.php:13
advance($step=1)
Advance progressbar.
Definition: Progressing.php:34
elgg_get_config($name, $default=null)
Get an Elgg configuration value.