Skip to content

Commit 5d19782

Browse files
committed
All this to get rid of that skip logic
Signed-off-by: Matt Friedman <[email protected]>
1 parent 184b866 commit 5d19782

File tree

1 file changed

+31
-44
lines changed

1 file changed

+31
-44
lines changed

includes/qi_populate.php

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{
@@ -668,25 +652,10 @@ private function save_users()
668652
$password = phpbb_hash('123456');
669653
}
670654

671-
$registered_group = $newly_registered_group = 0;
672655
// Get the group id for registered users and newly registered.
673-
$sql = 'SELECT group_id, group_name FROM ' . GROUPS_TABLE . '
674-
WHERE group_name = \'REGISTERED\'
675-
OR group_name = \'NEWLY_REGISTERED\'';
676-
$result = $db->sql_query($sql);
677-
678-
while ($row = $db->sql_fetchrow($result))
679-
{
680-
if ($row['group_name'] == 'REGISTERED')
681-
{
682-
$registered_group = (int) $row['group_id'];
683-
}
684-
else
685-
{
686-
$newly_registered_group = (int) $row['group_id'];
687-
}
688-
}
689-
$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);
690659

691660
$s_chunks = ($this->num_users > $this->user_chunks) ? true : false;
692661
$end = $this->num_users + 1;
@@ -767,24 +736,21 @@ private function save_users()
767736
unset($sql_ary);
768737

769738
// Put them in groups.
770-
$chunk_cnt = $skip = 0;
739+
$chunk_cnt = 0;
771740

772-
// Don't add the first users to the newly registered group if a moderator and/or an admin is needed.
773-
$skip = ($this->create_mod) ? $skip + 1 : $skip;
774-
$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']);
775743

776744
// First the registered group.
777745
foreach ($this->user_arr as $user)
778746
{
779747
$sql_ary[] = array(
780748
'user_id' => (int) $user['user_id'],
781-
'group_id' => $this->num_new_group && $user['user_posts'] < 1 && $skip < 1 ? $newly_registered_group : $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,
782750
'group_leader' => 0, // No group leaders.
783751
'user_pending' => 0, // User is not pending.
784752
);
785753

786-
$skip--;
787-
788754
if ($s_chunks && $chunk_cnt >= $this->user_chunks)
789755
{
790756
// throw the array to the users table
@@ -958,4 +924,25 @@ private function update_start_date()
958924

959925
$db->sql_transaction('commit');
960926
}
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+
}
961948
}

0 commit comments

Comments
 (0)