Features & Development

Assigning sets and problems through wwsh

Assigning sets and problems through wwsh

by Rishi Dhar -
Number of replies: 1
Hi,

I was trying to use wwsh and develop a script to add user and register the user for a particular course and set under the course.

Following is what my script is doing :

$db->addUser($db->newUser(
user_id=>"$uid", first_name=>"$first", last_name=>"$last",email_address=>"$eid", student_id=>"$sid", status=>"C",section=>"", recitation=>""));
$db->putPassword($db->newPassword(user_id=>"$uid",password=>crypt("myPassword", "liquid09")));
$db->putPermissionLevel($db->newPermissionLevel(user_id=>"$uid",permission=>"0"));
$db->addUserSet($db->newUserSet(user_id=>"$uid", set_id=>"$setid"));

but the last line apparently is assigning a set to the user but this doesn't seem to assign problems to the users and what I get in the logs is "there are no problems in the set" although there are problems and once I login to webwork and assign the courses manually through instructor tools it works fine.

Can you help me with the code that I'm missing for assigning the problems in a set to the users?

Regards,
Rishi
In reply to Rishi Dhar

Re: Assigning sets and problems through wwsh

by Gavin LaRose -
Hi Rishi,

Sorry for not responding to this sooner; I didn't notice it until now. Here's a partial answer that has been working for me: instead of using $db->addUserSet(), I use WeBWorK::ContentGenerator::Instructor->assignSetsToUsers(). I should caution that this has worked up to WeBWorK 2.7, but I haven't tested it on 2.10 yet. My code looks something like the following:

  # $db is the database object
  # $ce is the course environment object
  my $fr = new FakeRequest($db, $ce);
  my $instrCG = WeBWorK::ContentGenerator::Instructor->new($fr);
  $instrCG->assignSetsToUsers( $setNamesRef, [@userIDs] );

  # FakeRequest is a shell that has the 
  # methods that the content generator expects
  # of a Request object:
  package FakeRequest;
  sub new {
    my ( $class, $db, $ce ) = @_;
    my $authz = new FakeAuthz;
    return( bless( { ce=>$ce, db=>$db, authz=>$authz }, $class );
  }
  sub ce { my $self = shift(); return $self->{ce}; }
  sub db { my $self = shift(); return $self->{db}; }
  sub authz { my $self = shift(); return $self->{authz}; }
  sub param {
    my $self = shift();
    $self->{$_[0]} = $_[1] if ( @_ > 1 );
    return $self->{$_[0]};
  }
  1;
  package FakeAuthz;
  sub new {
    my $class = shift();
    return( bless( { }, $class ) );
  }
  sub hasPermissions {
    return 1;
  }
  1;

Here $setNamesRef is an array reference giving the list of set names to assign to the array of users @userIDs.

In any case, in case it's useful.

Cheers,
Gavin