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