[system] / trunk / webwork2 / lib / WeBWorK / ContentGenerator / Instructor.pm Repository:
ViewVC logotype

View of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1026 - (download) (as text) (annotate)
Thu Jun 5 19:26:31 2003 UTC (9 years, 11 months ago) by malsyned
File size: 4128 byte(s)
Added a function "initializeUserProblem" to WeBWorK::DB::Utils and
changed my code to use it.
-Dennis

    1 package WeBWorK::ContentGenerator::Instructor;
    2 use base qw(WeBWorK::ContentGenerator);
    3 
    4 =head1 NAME
    5 
    6 WeBWorK::ContentGenerator::Instructor - Abstract superclass for the Instructor pages
    7 
    8 =cut
    9 
   10 use strict;
   11 use warnings;
   12 use CGI qw();
   13 use WeBWorK::DB::Utils qw(global2user initializeUserProblem);
   14 
   15 sub hiddenEditForUserFields {
   16   my ($self, @editForUser) = @_;
   17   my $return = "";
   18   foreach my $editUser (@editForUser) {
   19     $return .= CGI::input({type=>"hidden", name=>"editForUser", value=>$editUser});
   20   }
   21 
   22   return $return;
   23 }
   24 
   25 sub userCountMessage {
   26   my ($self, $count, $numUsers) = @_;
   27 
   28   my $message;
   29   if ($count == 0) {
   30     $message = CGI::em("no users");
   31   } elsif ($count == $numUsers) {
   32     $message = "all users";
   33   } elsif ($count == 1) {
   34     $message = "1 user";
   35   } elsif ($count > $numUsers || $count < 0) {
   36     $message = CGI::em("an impossible number of users: $count out of $numUsers");
   37   } else {
   38     $message = "$count users";
   39   }
   40 
   41   return $message;
   42 }
   43 
   44 ### Utility functions for assigning sets to users.
   45 # These silently fail if the problem or set exists for the user.
   46 
   47 sub assignProblemToUser {
   48   my ($self, $user, $globalProblem) = @_;
   49   my $db = $self->{db};
   50   my $userProblem = $db->{problem_user}->{record}->new;
   51 
   52   # Set up the key
   53   $userProblem->user_id($user);
   54   $userProblem->set_id($globalProblem->set_id);
   55   $userProblem->problem_id($globalProblem->problem_id);
   56 
   57   initializeUserProblem($userProblem);
   58   eval {$db->addUserProblem($userProblem)};
   59 }
   60 
   61 sub assignSetToUser {
   62   my ($self, $user, $globalSet) = @_;
   63   my $db = $self->{db};
   64   my $userSet = $db->{set_user}->{record}->new;
   65   my $setID = $globalSet->set_id;
   66 
   67   $userSet->user_id($user);
   68   $userSet->set_id($setID);
   69   eval {$db->addUserSet($userSet)};
   70 
   71   foreach my $problemID ($db->listGlobalProblems) {
   72     my $problemRecord = $db->getGlobalProblem($setID, $problemID);
   73     $self->assignProblemToUser($user, $problemRecord);
   74   }
   75 }
   76 
   77 # When a new problem is added to a set, all students to whom the set
   78 # it belongs to is assigned should have it assigned to them.
   79 # Note that this does NOT assign to all users of a course, just all users
   80 # of a set.
   81 sub assignProblemToAllUsers {
   82   my ($self, $globalProblem) = @_;
   83   my $db = $self->{db};
   84   my $setID = $globalProblem->set_id;
   85   my @users = $db->listSetUsers;
   86 
   87   foreach my $user (@users) {
   88     $self->assignProblemToUser($user, $globalProblem);
   89   }
   90 }
   91 
   92 # READ THIS: Unlike the above function, "All" here refers to all of the
   93 # users of a course.
   94 # This function caches database data as a speed optimization.
   95 sub assignSetToAllUsers {
   96   my ($self, $setID) = @_;
   97   my $db = $self->{db};
   98   my @problems = ();
   99   my @users = $db->listUsers($setID);
  100   my @problemRecords = map {$db->getGlobalProblem($setID, $_)} $db->listGlobalProblems($setID);
  101 
  102   foreach my $user (@users) {
  103     # FIXME: Create a UserSet record for the user!!!!
  104     my $userSet = $db->{set_user}->{record}->new;
  105     $userSet->user_id($user);
  106     $userSet->set_id($setID);
  107     eval {$db->addUserSet($userSet)};
  108     foreach my $problemRecord (@problemRecords) {
  109       $self->assignProblemToUser($user, $problemRecord);
  110     }
  111   }
  112 }
  113 
  114 ## Template Escapes ##
  115 
  116 sub links {
  117   my $self    = shift;
  118 
  119   # keep the links from the parent
  120   my $pathString  = "";
  121 
  122 
  123   my $ce = $self->{ce};
  124   my $db = $self->{db};
  125   my $userName = $self->{r}->param("user");
  126   my $courseName = $ce->{courseName};
  127   my $root = $ce->{webworkURLs}->{root};
  128   my $permLevel = $db->getPermissionLevel($userName)->permission();
  129   my $key = $db->getKey($userName)->key();
  130   return "" unless defined $key;
  131 
  132   # new URLS
  133   my $classList = "$root/$courseName/instructor/users/?". $self->url_authen_args();
  134   my $addStudent  = "$root/$courseName/instructor/addStudent/?". $self->url_authen_args();
  135   my $problemSetList = "$root/$courseName/instructor/sets/?". $self->url_authen_args();
  136 
  137   if ($permLevel > 0 ) {
  138     $pathString .="<hr>";
  139     $pathString .=  CGI::a({-href=>$classList}, "Class&nbsp;editor") . CGI::br();
  140     $pathString .=  '&nbsp;&nbsp;'.CGI::a({-href=>$addStudent}, "Add&nbsp;Student") . CGI::br();
  141     $pathString .= CGI::a({-href=>$problemSetList}, "ProbSet&nbsp;list") . CGI::br();
  142   }
  143   return $self->SUPER::links() . $pathString;
  144 }
  145 
  146 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9