Elgg  Version 4.3
Groups.php
Go to the documentation of this file.
1 <?php
2 
4 
10 class Groups extends Seed {
11 
15  public function seed() {
16  $this->advance($this->getCount());
17 
18  $count_members = function ($group) {
19  return elgg_count_entities([
20  'types' => 'user',
21  'relationship' => 'member',
22  'relationship_guid' => $group->getGUID(),
23  'inverse_relationship' => true,
24  'metadata_names' => '__faker',
25  ]);
26  };
27 
28  $exclude = [];
29 
30  $profile_fields_config = _elgg_services()->fields->get('group', 'group');
31  $profile_fields = [];
32  foreach ($profile_fields_config as $field) {
33  $profile_fields[$field['name']] = $field['#type'];
34  }
35 
36  while ($this->getCount() < $this->limit) {
37  if ($this->create) {
38  $group = $this->createGroup([
39  'access_id' => $this->getRandomGroupVisibility(),
40  ], [
41  'content_access_mode' => $this->getRandomGroupContentAccessMode(),
42  'membership' => $this->getRandomGroupMembership(),
43  ], [
44  'profile_fields' => $profile_fields,
45  'group_tool_options' => _elgg_services()->group_tools->all(),
46  ]);
47  } else {
48  $group = $this->getRandomGroup($exclude);
49  }
50 
51  if (!$group) {
52  continue;
53  }
54 
55  $this->createIcon($group);
56 
57  $exclude[] = $group->guid;
58 
59  if ($count_members($group) > 1) {
60  // exclude owner from count
61  continue;
62  }
63 
64  $members_limit = $this->faker()->numberBetween(1, 5);
65 
66  $members_exclude = [];
67 
68  while ($count_members($group) - 1 < $members_limit) {
69  $member = $this->getRandomUser($members_exclude);
70  if (!$member) {
71  continue;
72  }
73 
74  $members_exclude[] = $member->guid;
75 
76  if ($group->join($member)) {
77  $this->log("User {$member->getDisplayName()} [guid: {$member->guid}] joined group {$group->getDisplayName()} [guid: {$group->guid}]");
78  }
79 
80  if (!$group->isPublicMembership()) {
81  $invitee = $this->getRandomUser($members_exclude);
82  if ($invitee) {
83  $members_exclude[] = $invitee->guid;
84  if (!$group->isMember($invitee)) {
85  $group->addRelationship($invitee->guid, 'invited');
86  $this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] was invited to {$group->getDisplayName()} [guid: {$group->guid}]");
87  }
88  }
89 
90  $requestor = $this->getRandomUser($members_exclude);
91  if ($requestor) {
92  $members_exclude[] = $requestor->guid;
93  if (!$group->hasRelationship($requestor->guid, 'invited')
94  && !$group->isMember($requestor)
95  ) {
96  $requestor->addRelationship($group->guid, 'membership_request');
97  $this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] requested to join {$group->getDisplayName()} [guid: {$group->guid}]");
98  }
99  }
100  }
101  }
102 
103  $this->advance();
104  }
105 
106  }
107 
111  public function unseed() {
112 
113  /* @var $groups \ElggBatch */
114  $groups = elgg_get_entities([
115  'type' => 'group',
116  'metadata_name' => '__faker',
117  'limit' => false,
118  'batch' => true,
119  'batch_inc_offset' => false,
120  ]);
121 
122  /* @var $group \ElggGroup */
123  foreach ($groups as $group) {
124  if ($group->delete()) {
125  $this->log("Deleted group {$group->guid}");
126  } else {
127  $this->log("Failed to delete group {$group->guid}");
128  }
129 
130  $this->advance();
131  }
132  }
133 
137  public static function getType() : string {
138  return 'group';
139  }
140 
144  protected function getCountOptions() : array {
145  return [
146  'type' => 'group',
147  ];
148  }
149 }
getRandomGroupVisibility()
Returns random visibility value.
getRandomGroupContentAccessMode()
Returns random content access mode value.
createGroup(array $attributes=[], array $metadata=[])
Create a new fake group.
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
advance(int $step=1)
Advance progressbar.
Definition: Seed.php:96
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:556
getRandomUser(array $exclude=[], bool $allow_create=true)
Returns random fake user.
Definition: Seeding.php:518
if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:37
createIcon(\ElggEntity $entity)
Create an icon for an entity.
Definition: Seeding.php:780
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
Abstract seed.
Definition: Seed.php:14
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
faker($locale= 'en_US')
Returns an instance of faker.
Definition: Seeding.php:48
getCount()
Get the count of the seeded entities.
Definition: Seed.php:76
getRandomGroupMembership()
Returns random membership mode.
getRandomGroup(array $exclude=[], bool $allow_create=true)
Returns random fake group.
Definition: Seeding.php:565