Elgg  Version 3.0
Groups.php
Go to the documentation of this file.
1 <?php
2 
4 
10 class Groups extends Seed {
11 
12  private $visibility = [
16  ];
17 
18  private $content_access_modes = [
21  ];
22 
23  private $membership = [
26  ];
27 
31  public function seed() {
32 
33  $count_groups = function () {
34  return elgg_get_entities([
35  'types' => 'group',
36  'metadata_names' => '__faker',
37  'count' => true,
38  ]);
39  };
40 
41  $this->advance($count_groups());
42 
43  $count_members = function ($group) {
44  return elgg_get_entities([
45  'types' => 'user',
46  'relationship' => 'member',
47  'relationship_guid' => $group->getGUID(),
48  'inverse_relationship' => true,
49  'metadata_names' => '__faker',
50  'count' => true,
51  ]);
52  };
53 
54  $exclude = [];
55 
56  while ($count_groups() < $this->limit) {
57  $group = $this->getRandomGroup($exclude);
58  if (!$group) {
59  $group = $this->createGroup([
60  'access_id' => $this->getRandomVisibility(),
61  ], [
62  'content_access_mode' => $this->getRandomContentAccessMode(),
63  'membership' => $this->getRandomMembership(),
64  ], [
65  'profile_fields' => (array) elgg_get_config('group'),
66  'group_tool_options' => elgg()->group_tools->all(),
67  ]);
68  if (!$group) {
69  continue;
70  }
71  }
72 
73  $this->createIcon($group);
74 
75  $exclude[] = $group->guid;
76 
77  if ($count_members($group) > 1) {
78  // exclude owner from count
79  continue;
80  }
81 
82  $members_limit = $this->faker()->numberBetween(1, 5);
83 
84  $members_exclude = [];
85 
86  while ($count_members($group) - 1 < $members_limit) {
87  $member = $this->getRandomUser($members_exclude);
88  if (!$member) {
89  $member = $this->createUser();
90  if (!$member) {
91  continue;
92  }
93  }
94 
95  $members_exclude[] = $member->guid;
96 
97  if ($group->join($member)) {
98  $this->log("User {$member->getDisplayName()} [guid: {$member->guid}] joined group {$group->getDisplayName()} [guid: {$group->guid}]");
99  }
100 
101  if (!$group->isPublicMembership()) {
102  $invitee = $this->getRandomUser($members_exclude);
103  if (!$invitee) {
104  $invitee = $this->createUser();
105  }
106  if ($invitee) {
107  $members_exclude[] = $invitee->guid;
108  if (!check_entity_relationship($invitee->guid, 'member', $group->guid)) {
109  add_entity_relationship($group->guid, 'invited', $invitee->guid);
110  $this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] was invited to {$group->getDisplayName()} [guid: {$group->guid}]");
111  }
112  }
113 
114  $requestor = $this->getRandomUser($members_exclude);
115  if (!$requestor) {
116  $requestor = $this->createUser();
117  }
118  if ($requestor) {
119  $members_exclude[] = $requestor->guid;
120  if (!check_entity_relationship($group->guid, 'invited', $requestor->guid)
121  && !check_entity_relationship($requestor->guid, 'member', $group->guid)
122  ) {
123  add_entity_relationship($requestor->guid, 'membership_request', $group->guid);
124  $this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] requested to join {$group->getDisplayName()} [guid: {$group->guid}]");
125  }
126  }
127  }
128  }
129 
130  $this->advance();
131  }
132 
133  }
134 
138  public function unseed() {
139 
140  $groups = elgg_get_entities([
141  'types' => 'group',
142  'metadata_names' => '__faker',
143  'limit' => 0,
144  'batch' => true,
145  ]);
146 
147  /* @var $groups \ElggBatch */
148 
149  $groups->setIncrementOffset(false);
150 
151  foreach ($groups as $group) {
152  if ($group->delete()) {
153  $this->log("Deleted group $group->guid");
154  } else {
155  $this->log("Failed to delete group $group->guid");
156  }
157 
158  $this->advance();
159  }
160  }
161 
166  public function getRandomVisibility() {
167  $key = array_rand($this->visibility, 1);
168 
169  return $this->visibility[$key];
170  }
171 
176  public function getRandomContentAccessMode() {
177  $key = array_rand($this->content_access_modes, 1);
178 
179  return $this->content_access_modes[$key];
180  }
181 
186  public function getRandomMembership() {
187  $key = array_rand($this->membership, 1);
188 
189  return $this->membership[$key];
190  }
191 }
log($msg, $level=LogLevel::NOTICE)
Log a message.
Definition: Seeding.php:762
getRandomUser(array $exclude=[])
Returns random fake user.
Definition: Seeding.php:442
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
createIcon(ElggEntity $entity)
Create an icon for an entity.
Definition: Seeding.php:666
createGroup(array $attributes=[], array $metadata=[])
Create a new fake group.
faker($locale= 'en_US')
Returns an instance of faker.
Definition: Seeding.php:43
$limit
Definition: userpicker.php:52
const ACCESS_PRIVATE
Definition: constants.php:12
getRandomVisibility()
Returns random visibility value.
Definition: Groups.php:166
const ACCESS_LOGGED_IN
Definition: constants.php:13
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:14
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
createUser(array $attributes=[], array $metadata=[])
Create a new fake user.
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:15
getRandomGroup(array $exclude=[])
Returns random fake group.
Definition: Seeding.php:468
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
getRandomContentAccessMode()
Returns random content access mode value.
Definition: Groups.php:176
getRandomMembership()
Returns random membership mode.
Definition: Groups.php:186
Abstract seed.
Definition: Seed.php:13
const ACCESS_PUBLIC
Definition: constants.php:14
advance($step=1)
Advance progressbar.
Definition: Progressing.php:34
var elgg
Definition: elgglib.js:4
elgg_get_config($name, $default=null)
Get an Elgg configuration value.