Elgg  Version 2.3
Groups.php
Go to the documentation of this file.
1 <?php
2 
4 
10 class Groups extends Seed {
11 
15  private $users;
16 
17  private $visibility = [
21  ];
22 
23  private $content_access_modes = [
26  ];
27 
28  private $membership = [
31  ];
32 
36  public function seed() {
37 
38  $count_groups = function () {
40  'types' => 'group',
41  'metadata_names' => '__faker',
42  'count' => true,
43  ]);
44  };
45 
46  $count_members = function ($group) {
48  'types' => 'user',
49  'relationship' => 'member',
50  'relationship_guid' => $group->getGUID(),
51  'inverse_relationship' => true,
52  'metadata_names' => '__faker',
53  'count' => true,
54  ]);
55  };
56 
57  $exclude = [];
58 
59  while ($count_groups() < $this->limit) {
60  $group = $this->getRandomGroup($exclude);
61  if (!$group) {
62  $group = $this->createGroup([
63  'access_id' => $this->getRandomVisibility(),
64  ], [
65  'content_access_mode' => $this->getRandomContentAccessMode(),
66  'membership' => $this->getRandomMembership(),
67  ]);
68  if (!$group) {
69  continue;
70  }
71  }
72 
73  $exclude[] = $group->guid;
74 
75  if ($count_members($group) > 1) {
76  // exclude owner from count
77  continue;
78  }
79 
80  $members_limit = $this->faker->numberBetween(5, 10);
81 
82  $members_exclude = [];
83 
84  while ($count_members($group) - 1 < $members_limit) {
85 
86  $member = $this->getRandomUser($members_exclude);
87  if (!$member) {
88  $member = $this->createUser();
89  if (!$member) {
90  continue;
91  }
92  }
93 
94  $members_exclude[] = $member->guid;
95 
96  if ($group->join($member)) {
97  $this->log("User $member->name [guid: $member->guid] joined group $group->name [guid: $group->guid]");
98  }
99 
100  if (!$group->isPublicMembership()) {
101  $invitee = $this->getRandomUser($members_exclude);
102  if ($invitee) {
103  $members_exclude[] = $invitee->guid;
104  if (!check_entity_relationship($invitee->guid, 'member', $group->guid)) {
105  add_entity_relationship($group->guid, 'invited', $invitee->guid);
106  $this->log("User $invitee->name [guid: $invitee->guid] was invited to $group->name [guid: $group->guid]");
107  }
108  }
109 
110  $requestor = $this->getRandomUser($members_exclude);
111  if ($requestor) {
112  $members_exclude[] = $requestor->guid;
113  if (!check_entity_relationship($group->guid, 'invited', $requestor->guid)
114  && !check_entity_relationship($requestor->guid, 'member', $group->guid)
115  ) {
116  add_entity_relationship($requestor->guid, 'membership_request', $group->guid);
117  $this->log("User $invitee->name [guid: $invitee->guid] requested to join $group->name [guid: $group->guid]");
118  }
119  }
120  }
121  }
122 
123  }
124 
125  }
126 
130  public function unseed() {
131 
133  'types' => 'group',
134  'metadata_names' => '__faker',
135  'limit' => 0,
136  'batch' => true,
137  ]);
138 
139  /* @var $groups \ElggBatch */
140 
141  $groups->setIncrementOffset(false);
142 
143  foreach ($groups as $group) {
144  if ($group->delete()) {
145  $this->log("Deleted group $group->guid");
146  } else {
147  $this->log("Failed to delete group $group->guid");
148  }
149  }
150  }
151 
156  public function getRandomVisibility() {
157  $key = array_rand($this->visibility, 1);
158 
159  return $this->visibility[$key];
160  }
161 
166  public function getRandomContentAccessMode() {
167  $key = array_rand($this->content_access_modes, 1);
168 
169  return $this->content_access_modes[$key];
170  }
171 
176  public function getRandomMembership() {
177  $key = array_rand($this->membership, 1);
178 
179  return $this->membership[$key];
180  }
181 }
elgg_get_entities_from_metadata(array $options=array())
interfaces
Definition: metadata.php:276
getRandomGroup(array $exclude=[])
Returns random fake group.
Definition: Seed.php:378
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
getRandomUser(array $exclude=[])
Returns random fake user.
Definition: Seed.php:351
createGroup(array $attributes=[], array $metadata=[])
Create a new faker group.
Definition: Seed.php:132
log($msg, $level= 'NOTICE')
Log a message.
Definition: Seed.php:685
$key
Definition: summary.php:34
getRandomVisibility()
Returns random visibility value.
Definition: Groups.php:156
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
const ACCESS_PRIVATE
Definition: elgglib.php:2082
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
getRandomContentAccessMode()
Returns random content access mode value.
Definition: Groups.php:166
const ACCESS_PUBLIC
Definition: elgglib.php:2084
getRandomMembership()
Returns random membership mode.
Definition: Groups.php:176
Abstract seed.
Definition: Seed.php:20
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
const ACCESS_LOGGED_IN
Definition: elgglib.php:2083
createUser(array $attributes=[], array $metadata=[])
Create a new faker user.
Definition: Seed.php:67