Elgg  Version 2.3
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 () {
19  'types' => 'user',
20  'metadata_names' => '__faker',
21  'count' => true,
22  ]);
23  };
24 
25  $count_friends = function ($user) {
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  while ($count_users() < $this->limit) {
39  $user = $this->getRandomUser($exclude);
40  if (!$user) {
41  $user = $this->createUser();
42  if (!$user) {
43  continue;
44  }
45  }
46 
47  $exclude[] = $user->guid;
48 
49  // Friend the user other members
50  // Create a friend access collection and add some random friends to it
51 
52  if ($count_friends($user)) {
53  continue;
54  }
55 
56  $collection_id = create_access_collection('Best Fake Friends Collection', $user->guid);
57  if ($collection_id) {
58  $this->log("Created new friend collection for user $user->name [collection_id: $collection_id]");
59  }
60 
61  $friends_limit = $this->faker->numberBetween(5, 10);
62 
63  $friends_exclude = [$user->guid];
64  while ($count_friends($user) < $friends_limit) {
65 
66  $friend = $this->getRandomUser($friends_exclude);
67  if (!$friend) {
68  $this->createUser();
69  if (!$friend) {
70  continue;
71  }
72  }
73 
74  $friends_exclude[] = $friend->guid;
75 
76  if ($user->addFriend($friend->guid, true)) {
77  $this->log("User $user->name [guid: $user->guid] friended user $friend->name [guid: $friend->guid]");
78  if ($this->faker->boolean() && $collection_id) {
80  }
81  }
82  }
83 
84  }
85 
86  }
87 
91  public function unseed() {
92 
94  'types' => 'user',
95  'metadata_names' => '__faker',
96  'limit' => 0,
97  'batch' => true,
98  ]);
99 
100  /* @var $users \ElggBatch */
101 
102  $users->setIncrementOffset(false);
103 
104  foreach ($users as $user) {
105  if ($user->delete()) {
106  $this->log("Deleted user $user->guid");
107  } else {
108  $this->log("Failed to delete user $user->guid");
109  }
110  }
111  }
112 
113 }
elgg_get_entities_from_metadata(array $options=array())
interfaces
Definition: metadata.php:276
getRandomUser(array $exclude=[])
Returns random fake user.
Definition: Seed.php:351
$friend
Definition: add.php:12
log($msg, $level= 'NOTICE')
Log a message.
Definition: Seed.php:685
add_user_to_access_collection($user_guid, $collection_id)
Adds a user to an access collection.
Definition: access.php:373
$users
Definition: newest.php:8
$user
Definition: ban.php:13
$collection_id
Definition: delete.php:9
Abstract seed.
Definition: Seed.php:20
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
createUser(array $attributes=[], array $metadata=[])
Create a new faker user.
Definition: Seed.php:67
create_access_collection($name, $owner_guid=0, $site_guid=0)
Creates a new access collection.
Definition: access.php:308