Skip to content

Commit 0af2404

Browse files
authored
Merge pull request #257 from iMattPro/issue/250
Fix handling of newly registered users
2 parents fbcc655 + 5d19782 commit 0af2404

File tree

2 files changed

+49
-63
lines changed

2 files changed

+49
-63
lines changed

includes/qi_populate.php

Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function run()
170170
$this->email_domain = strtolower($this->email_domain);
171171

172172
// Populate the users array with some initial data.
173-
// I'm sure there where a reason to split this up to two functions.
173+
// I'm sure there was a reason to split this up to two functions.
174174
// Need to have a closer look at that later. The second one might need to move.
175175
$this->pop_user_arr();
176176
}
@@ -223,26 +223,10 @@ private function create_management()
223223
return;
224224
}
225225

226-
$admin_group = $mod_group = 0;
227-
228226
// Get group id for admins and moderators.
229-
$sql = 'SELECT group_id, group_name
230-
FROM ' . GROUPS_TABLE . "
231-
WHERE group_name = 'ADMINISTRATORS'
232-
OR group_name = 'GLOBAL_MODERATORS'";
233-
$result = $db->sql_query($sql);
234-
while ($row = $db->sql_fetchrow($result))
235-
{
236-
if ($row['group_name'] == 'ADMINISTRATORS')
237-
{
238-
$admin_group = (int) $row['group_id'];
239-
}
240-
else if ($row['group_name'] == 'GLOBAL_MODERATORS')
241-
{
242-
$mod_group = (int) $row['group_id'];
243-
}
244-
}
245-
$db->sql_freeresult($result);
227+
$user_groups = $this->get_groups();
228+
$admin_group = (int) array_search('ADMINISTRATORS', $user_groups, true);
229+
$mod_group = (int) array_search('GLOBAL_MODERATORS', $user_groups, true);;
246230

247231
if (file_exists("{$phpbb_root_path}language/" . $settings->get_config('default_lang') . "/common.$phpEx"))
248232
{
@@ -304,14 +288,25 @@ private function fill_forums()
304288
// Use the default user if no new users are being populated
305289
if (!$this->num_users && empty($this->user_arr))
306290
{
307-
$this->user_arr = $this->get_user(1);
291+
$this->user_arr = $users = $this->get_user(1);
292+
}
293+
// If there are going to be newly registered users, we need to not use them when filling forums
294+
else if ($this->num_new_group && $this->num_new_group < $this->num_users)
295+
{
296+
$users = array_slice($this->user_arr, 0, -$this->num_new_group, true);
297+
}
298+
else
299+
{
300+
$users = $this->user_arr;
308301
}
309302

310303
// Get the min and max for mt_rand.
311-
end($this->user_arr);
312-
$mt_max = (int) key($this->user_arr);
313-
reset($this->user_arr);
314-
$mt_min = (int) key($this->user_arr);
304+
end($users);
305+
$mt_max = (int) key($users);
306+
reset($users);
307+
$mt_min = (int) key($users);
308+
309+
unset($users);
315310

316311
// Flags for BBCodes.
317312
$flags = 7;
@@ -657,25 +652,10 @@ private function save_users()
657652
$password = phpbb_hash('123456');
658653
}
659654

660-
$registered_group = $newly_registered_group = 0;
661655
// Get the group id for registered users and newly registered.
662-
$sql = 'SELECT group_id, group_name FROM ' . GROUPS_TABLE . '
663-
WHERE group_name = \'REGISTERED\'
664-
OR group_name = \'NEWLY_REGISTERED\'';
665-
$result = $db->sql_query($sql);
666-
667-
while ($row = $db->sql_fetchrow($result))
668-
{
669-
if ($row['group_name'] == 'REGISTERED')
670-
{
671-
$registered_group = (int) $row['group_id'];
672-
}
673-
else
674-
{
675-
$newly_registered_group = (int) $row['group_id'];
676-
}
677-
}
678-
$db->sql_freeresult($result);
656+
$user_groups = $this->get_groups();
657+
$registered_group = (int) array_search('REGISTERED', $user_groups, true);
658+
$newly_registered_group = (int) array_search('NEWLY_REGISTERED', $user_groups, true);
679659

680660
$s_chunks = ($this->num_users > $this->user_chunks) ? true : false;
681661
$end = $this->num_users + 1;
@@ -756,36 +736,21 @@ private function save_users()
756736
unset($sql_ary);
757737

758738
// Put them in groups.
759-
$chunk_cnt = $newly_registered = $skip = 0;
739+
$chunk_cnt = 0;
760740

761-
// Don't add the first users to the newly registered group if a moderator and/or an admin is needed.
762-
$skip = ($this->create_mod) ? $skip + 1 : $skip;
763-
$skip = ($this->create_admin) ? $skip + 1 : $skip;
741+
// This is to skip adding mods and admins to the newly registered user group
742+
$managers = array_intersect($user_groups, ['ADMINISTRATORS', 'GLOBAL_MODERATORS']);
764743

765744
// First the registered group.
766745
foreach ($this->user_arr as $user)
767746
{
768747
$sql_ary[] = array(
769748
'user_id' => (int) $user['user_id'],
770-
'group_id' => (int) $registered_group,
749+
'group_id' => $this->num_new_group && $user['user_posts'] < 1 && !array_key_exists($user['group_id'], $managers) ? $newly_registered_group : $registered_group,
771750
'group_leader' => 0, // No group leaders.
772751
'user_pending' => 0, // User is not pending.
773752
);
774753

775-
if ($newly_registered < $this->num_new_group && $skip < 1)
776-
{
777-
$sql_ary[] = array(
778-
'user_id' => (int) $user['user_id'],
779-
'group_id' => (int) $newly_registered_group,
780-
'group_leader' => 0, // No group leaders.
781-
'user_pending' => 0, // User is not pending.
782-
);
783-
784-
$newly_registered++;
785-
}
786-
787-
$skip--;
788-
789754
if ($s_chunks && $chunk_cnt >= $this->user_chunks)
790755
{
791756
// throw the array to the users table
@@ -959,4 +924,25 @@ private function update_start_date()
959924

960925
$db->sql_transaction('commit');
961926
}
927+
928+
/**
929+
* Get an array of the user groups
930+
* @return array group_id => group_name
931+
*/
932+
private function get_groups()
933+
{
934+
global $db;
935+
936+
$groups = [];
937+
938+
$sql = 'SELECT group_id, group_name FROM ' . GROUPS_TABLE;
939+
$result = $db->sql_query($sql);
940+
while ($row = $db->sql_fetchrow($result))
941+
{
942+
$groups[$row['group_id']] = $row['group_name'];
943+
}
944+
$db->sql_freeresult($result);
945+
946+
return $groups;
947+
}
962948
}

language/en/qi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
'NUM_FORUMS' => 'Number of forums',
206206
'NUM_FORUMS_EXPLAIN' => 'The number of forums to create, they will be spread evenly over the created categories.',
207207
'NUM_NEW_GROUP' => 'Newly registered users',
208-
'NUM_NEW_GROUP_EXPLAIN' => 'The number of users to place in the newly registered group. If this number is larger than the number of users, all new users will be placed in the newly registered group.',
208+
'NUM_NEW_GROUP_EXPLAIN' => 'The number of users to place in the newly registered group. If this number is larger than the number of users, it will be ignored.',
209209
'NUM_REPLIES' => 'Number of replies',
210210
'NUM_REPLIES_EXPLAIN' => 'The number of replies. Each topic will receive a random number of replies between these min and max values.',
211211
'NUM_TOPICS' => 'Number of topics',

0 commit comments

Comments
 (0)