[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 1397 - (download) (as text) (annotate)
Wed Jul 16 12:58:29 2003 UTC (9 years, 10 months ago) by gage
File size: 5470 byte(s)
Abstracted "read directory" functions in SendMail and moved them
to Instructor.  The instructor calls WeBWorK::Utils::readDirectory
and then filters the output according to a pattern match. Sorts as well.

Also move read_scoring_file to instructor since it will need to be used
in other scripts as well.
--Mike

    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   warn $@ if $@ and not $@ =~ m/user problem exists/;
   60 }
   61 
   62 sub assignSetToUser {
   63   my ($self, $user, $globalSet) = @_;
   64   my $db = $self->{db};
   65   my $userSet = $db->{set_user}->{record}->new;
   66   my $setID = $globalSet->set_id;
   67 
   68   $userSet->user_id($user);
   69   $userSet->set_id($setID);
   70   eval {$db->addUserSet($userSet)};
   71   warn $@ if $@ and not $@ =~ m/user set exists/;
   72 
   73   foreach my $problemID ($db->listGlobalProblems($setID)) {
   74     my $problemRecord = $db->getGlobalProblem($setID, $problemID);
   75     $self->assignProblemToUser($user, $problemRecord);
   76   }
   77 }
   78 
   79 # When a new problem is added to a set, all students to whom the set
   80 # it belongs to is assigned should have it assigned to them.
   81 # Note that this does NOT assign to all users of a course, just all users
   82 # of a set.
   83 sub assignProblemToAllSetUsers {
   84   my ($self, $globalProblem) = @_;
   85   my $db = $self->{db};
   86   my $setID = $globalProblem->set_id;
   87   my @users = $db->listSetUsers($setID);
   88 
   89   foreach my $user (@users) {
   90     $self->assignProblemToUser($user, $globalProblem);
   91   }
   92 }
   93 
   94 # READ THIS: Unlike the above function, "All" here refers to all of the
   95 # users of a course.
   96 # This function caches database data as a speed optimization.
   97 sub assignSetToAllUsers {
   98   my ($self, $setID) = @_;
   99   my $db = $self->{db};
  100   my @problems = ();
  101   my @users = $db->listUsers;
  102   my @problemRecords = map {$db->getGlobalProblem($setID, $_)} $db->listGlobalProblems($setID);
  103 
  104   foreach my $user (@users) {
  105     # FIXME: Create a UserSet record for the user!!!!
  106     my $userSet = $db->{set_user}->{record}->new;
  107     $userSet->user_id($user);
  108     $userSet->set_id($setID);
  109     eval {$db->addUserSet($userSet)};
  110     foreach my $problemRecord (@problemRecords) {
  111       $self->assignProblemToUser($user, $problemRecord);
  112     }
  113   }
  114 }
  115 
  116 sub read_dir {  # read a directory
  117   my $self      = shift;
  118   my $directory = shift;
  119   my $pattern   = shift;
  120   my @files = grep /$pattern/, WeBWorK::Utils::readDirectory($directory);
  121   return sort @files;
  122 }
  123 
  124 sub read_scoring_file    { # used in SendMail and ....?
  125   my $self            = shift;
  126   my $fileName        = shift;
  127   my $delimiter       = shift;
  128   $delimiter          = ',' unless defined($delimiter);
  129   my $scoringDirectory= $self->{ce}->{courseDirs}->{scoring};
  130   my $filePath        = "$scoringDirectory/$fileName";
  131         #       Takes a delimited file as a parameter and returns an
  132         #       associative array with the first field as the key.
  133         #       Blank lines are skipped. White space is removed
  134     my(@dbArray,$key,$dbString);
  135     my %assocArray = ();
  136     local(*FILE);
  137     if ($fileName eq 'None') {
  138       # do nothing
  139     } elsif ( open(FILE, "$filePath")  )   {
  140     my $index=0;
  141     while (<FILE>){
  142       unless ($_ =~ /\S/)  {next;}               ## skip blank lines
  143       chomp;
  144       @{$dbArray[$index]} =$self->getRecord($_,$delimiter);
  145       $key    =$dbArray[$index][0];
  146       $assocArray{$key}=$dbArray[$index];
  147       $index++;
  148     }
  149     close(FILE);
  150      } else {
  151       warn "Couldn't read file $filePath";
  152      }
  153      return \%assocArray;
  154 }
  155 ## Template Escapes ##
  156 
  157 sub links {
  158   my $self    = shift;
  159 #   FIXME these links are being placed in ContentGenerator.pm
  160 #
  161 #   my $pathString  = "";
  162 #
  163 #
  164 #   my $ce = $self->{ce};
  165 #   my $db = $self->{db};
  166 #   my $userName = $self->{r}->param("user");
  167 #   my $courseName = $ce->{courseName};
  168 #   my $root = $ce->{webworkURLs}->{root};
  169 #   my $permLevel = $db->getPermissionLevel($userName)->permission();
  170 #   my $key = $db->getKey($userName)->key();
  171 #   return "" unless defined $key;
  172 #
  173 #   # new URLS
  174 #   my $classList = "$root/$courseName/instructor/users/?". $self->url_authen_args();
  175 #   my $addStudent  = "$root/$courseName/instructor/addStudent/?". $self->url_authen_args();
  176 #   my $problemSetList = "$root/$courseName/instructor/sets/?". $self->url_authen_args();
  177 #
  178 #   if ($permLevel > 0 ) {
  179 #     $pathString .="<hr>";
  180 #     $pathString .=  CGI::a({-href=>$classList}, "Class&nbsp;editor") . CGI::br();
  181 #     $pathString .= CGI::a({-href=>$problemSetList}, "Set editor") . CGI::br();
  182 #   }
  183   return $self->SUPER::links(); # . $pathString;
  184 }
  185 
  186 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9