Commit 806e7971 authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Added support for generating random activities. Added support for

generating a random number of groups/activities for each user.
parent 10fd4063
......@@ -139,4 +139,51 @@ sub insert_random_groups {
}
sub insert_random_groups_all_users {
my ($self, $count) = @_;
my $existing_users = $self->{dbh}->selectall_hashref('SELECT id, username FROM ' . $prefix . 'usr', 'username');
foreach my $user ( keys %{$existing_users} ) {
$self->insert_random_groups($user, $count);
}
}
sub insert_random_activity {
my ($self, $user, $count) = @_;
my $prefix = $self->{config}{dbprefix};
my $user_id = $self->{dbh}->selectall_arrayref('SELECT id FROM ' . $prefix . 'usr WHERE username = ?', undef, $user)->[0][0];
unless ( defined $user_id ) {
croak qq{User '$user' doesn't exist\n};
}
print qq{Adding activity for '$user' ($user_id)\n};
$self->{dbh}->begin_work();
my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
foreach ( 1 .. $count ) { ### [... ] (%)
my $message = join(' ', $wl->get_words(int(rand(3)) + 2));
$self->{dbh}->do(
'INSERT INTO ' . $prefix . 'notification_internal_activity (type, usr, ctime, message, url, read) VALUES (?, ?, current_timestamp, ?, ?, ?)',
undef,
'maharamessage', $user_id, $message, 'http://mahara.org/', int(rand(2)));
}
$self->{dbh}->commit();
}
sub insert_random_activity_all_users {
my ($self, $count) = @_;
my $existing_users = $self->{dbh}->selectall_hashref('SELECT id, username FROM ' . $prefix . 'usr', 'username');
foreach my $user ( keys %{$existing_users} ) {
$self->insert_random_activity($user, $count);
}
}
1;
......@@ -14,8 +14,9 @@ use Mahara::RandomData;
my $args = Getopt::Declare->new(q(
[strict]
-t <type> Select type of data (user or group) [required]
-u <user> User to create data for (required for type 'group')
-t <type> Select type of data (user, group, activity) [required]
-u <user> User to create data for (required for type 'group' and 'activity')
-ua Create data for all users (can be used instead of -u)
-c <configfile> What config.php to use (defaults to ../htdocs/config.php)
-n <count> How many to generate (default 1)
{ reject $count !~ /^\d+$/; }
......@@ -38,9 +39,25 @@ if ( $args->{-t} eq 'user' ) {
}
if ( $args->{-t} eq 'group' ) {
unless ( defined $args->{-u} ) {
croak 'Need to specify a user with -u';
unless ( defined $args->{-u} or defined $args->{-ua} ) {
croak 'Need to specify a user with -u or -ua';
}
if ( defined $args->{-u} ) {
$randomdata->insert_random_groups($args->{-u}, $args->{-n});
}
else {
$randomdata->insert_random_groups_all_users($args->{-n});
}
$randomdata->insert_random_groups($args->{-u}, $args->{-n});
}
if ( $args->{-t} eq 'activity' ) {
unless ( defined $args->{-u} or defined $args->{-ua} ) {
croak 'Need to specify a user with -u or -ua';
}
if ( defined $args->{-u} ) {
$randomdata->insert_random_activity($args->{-u}, $args->{-n});
}
else {
$randomdata->insert_random_activity_all_users($args->{-n});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment