first commit
[step2_drupal.git] / devel / generate / generate-og-users.php
blobd6b624584f12d2b4cd81aede9392b2d19c1ac90e
1 <?php
3 // $Id: generate-og-users.php,v 1.3 2006/06/11 00:43:25 killes Exp $:
5 /**
6 * This script assigns existing users to existing groups.
7 * Requires Drupal 4.7 and OG module.
9 * Takes all groups, and assines a decreasing number of random users
10 * as members.
12 * If n is the total number of registered users, the first group gets
13 * all of them, the next gets half of them, etc. Admins are excluded.
15 * Sponsored by CivicSpace Labs
17 require_once './includes/bootstrap.inc';
18 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
20 function gou_get_groups() {
21 $result = db_query("SELECT nid, uid FROM {og_uid} WHERE is_admin >= 1");
22 $groups = array();
23 while ($group = db_fetch_array($result)) {
24 $groups[$group['nid']][] = $group['uid'];
27 return $groups;
30 function gou_count_users() {
31 return db_result(db_query('SELECT COUNT(*) FROM {users} WHERE uid > 0'));
34 function gou_assign_users($nid, $admins, $limit = NULL) {
35 $sql = 'INSERT INTO og_uid (nid, uid, is_active, mail_type) SELECT %d, uid, 1, 1 FROM users u WHERE u.uid NOT IN ('. str_pad('', count($admins) * 3 - 1, '%d,') .') ORDER BY RAND()';
36 if ($limit) {
37 $sql .= " LIMIT $limit";
39 db_query($sql, $nid, implode(',', $admins));
43 $users = gou_count_users();
44 $groups = gou_get_groups();
46 foreach ($groups as $nid => $group) {
47 $node = node_load($nid);
48 drupal_set_message(t('Assigned %n users to group %t.', array('%n' => $users, '%t' => theme('placeholder', $node->title))));
49 gou_assign_users($nid, $group, $users);
50 $users = floor($users / 2 + count($group));