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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1928 - (download) (as text) (annotate)
Sun Mar 28 03:25:47 2004 UTC (9 years, 1 month ago) by gage
File size: 5774 byte(s)
Cleanup -- moving toward using the Apache:Request object and URLpath.

It remains to use URLpath to construct new paths in these files.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetsAssignedToUser.pm,v 1.8 2004/03/04 21:05:58 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 package WeBWorK::ContentGenerator::Instructor::SetsAssignedToUser;
   18 use base qw(WeBWorK::ContentGenerator::Instructor);
   19 
   20 =head1 NAME
   21 
   22 WeBWorK::ContentGenerator::Instructor::SetsAssignedToUsers - List and edit which
   23 sets are assigned to a given user.
   24 
   25 =cut
   26 
   27 use strict;
   28 use warnings;
   29 use CGI qw();
   30 use WeBWorK::Utils qw(formatDateTime);
   31 
   32 sub initialize {
   33   my ($self)     = @_;
   34   my $r          = $self->r;
   35   my $urlpath    = $r->urlpath;
   36   my $db         = $r->db;
   37   my $authz      = $r->authz;
   38 
   39   my $userID     = $urlpath->arg("userID");
   40   my $user       = $r->param("user");
   41 
   42   # check authorization
   43   unless ($authz->hasPermissions($user, "assign_problem_sets")) {
   44     $self->{submitError} = "You are not authorized to assign problem sets";
   45     return;
   46   }
   47 
   48   # get the global user, if there is one
   49   my $globalUserID = "";
   50   $globalUserID = $db->{set}->{params}->{globalUserID}
   51     if ref $db->{set} eq "WeBWorK::DB::Schema::GlobalTableEmulator";
   52 
   53   if (defined $r->param("assignToAll")) {
   54     $self->assignAllSetsToUser($userID);
   55   } elsif (defined $r->param("unassignFromAll")) {
   56     if ($userID ne $globalUserID) {
   57       $self->unassignAllSetsFromUser($userID);
   58     }
   59   } elsif (defined $r->param('assignToSelected')) {
   60     # get list of all sets and a hash for checking selectedness
   61     my @setIDs = $db->listGlobalSets;
   62     my %selectedSets = map { $_ => 1 } $r->param("selected");
   63 
   64     # get current user
   65     my $User = $db->getUser($userID); # checked
   66     die "record not found for $userID.\n" unless $User;
   67 
   68     unless ($User->user_id eq $globalUserID) {
   69       # go through each possible set
   70       foreach my $setID (@setIDs) {
   71         # does the user want it to be assigned to the selected user
   72         if (exists $selectedSets{$setID}) {
   73           # user asked to have the set assigned to the selected user
   74           my $Set = $db->getGlobalSet($setID); #checked
   75           if ($Set) {
   76             $self->assignSetToUser($userID, $Set);
   77           } else {
   78             warn "global set $setID appeared in listGlobalSets() but does not exist.\n"
   79           }
   80         } else {
   81           # user asked to NOT have the set assigned to the selected user
   82           # this will unassign it if it is assigned
   83           $db->deleteUserSet($userID, $setID);
   84         }
   85       }
   86     }
   87   }
   88 }
   89 
   90 sub getUserName {
   91   my ($self, $pathUserName) = @_;
   92 
   93   if (ref $pathUserName eq "HASH") {
   94     $pathUserName = undef;
   95   }
   96 
   97   return $pathUserName;
   98 }
   99 
  100 
  101 
  102 sub body {
  103   my ($self)      = @_;
  104   my $r           = $self->r;
  105   my $urlpath     = $r->urlpath;
  106   my $db          = $r->db;
  107   my $ce          = $r->ce;
  108   my $authz       = $r->authz;
  109   my $courseName  = $urlpath->arg("courseID");
  110   my $webworkRoot = $ce->{webworkURLs}->{root};
  111   my $userID      = $urlpath->arg("userID");
  112 
  113   my $user        = $r->param('user');
  114 
  115   # check authorization
  116   return CGI::em("You are not authorized to access the Instructor tools.")
  117     unless $authz->hasPermissions($user, "access_instructor_tools");
  118 
  119   # get list of sets
  120   my @setIDs = $db->listGlobalSets;
  121   my @Sets = $db->getGlobalSets(@setIDs);
  122 
  123   # sort first by open date and then by name (this should be replaced with a
  124   # call to a standard sorting routine!)
  125   @Sets = sort {
  126     $a->open_date <=> $b->open_date
  127     || lc($a->set_id) cmp lc($b->set_id)
  128   } @Sets;
  129 
  130   print CGI::start_form({method=>"post", action=>$r->uri});
  131   print $self->hidden_authen_fields;
  132 
  133   # get the global user, if there is one
  134   my $globalUserID = "";
  135   $globalUserID = $db->{set}->{params}->{globalUserID}
  136     if ref $db->{set} eq "WeBWorK::DB::Schema::GlobalTableEmulator";
  137 
  138   if ($userID ne $globalUserID) {
  139     print CGI::p(
  140       CGI::submit({name=>"assignToAll", value=>"Assign all sets"}),
  141       CGI::submit({name=>"unassignFromAll", value=>"Unassign all sets"}),
  142     );
  143   }
  144 
  145   print CGI::start_table({});
  146 
  147   foreach my $Set (@Sets) {
  148     my $setID = $Set->set_id;
  149 
  150     # this is true if $Set is assigned to the selected user
  151     my $UserSet = $db->getUserSet($userID, $setID); # checked
  152     my $currentlyAssigned = defined $UserSet;
  153 
  154     my $prettyDate = formatDateTime($Set->due_date);
  155     if ($currentlyAssigned and $UserSet->due_date) {
  156       $prettyDate = formatDateTime($UserSet->due_date);
  157     }
  158 
  159     # URL to edit user-specific set data
  160     my $url = $ce->{webworkURLs}->{root}
  161       . "/$courseName/instructor/sets/$setID/?editForUser=$userID&"
  162       . $self->url_authen_args();
  163 
  164     print CGI::Tr({},
  165       CGI::td({}, [
  166         ($userID eq $globalUserID
  167           ? "" # no checkboxes for global user!
  168           : CGI::checkbox({
  169             type=>"checkbox",
  170             name=>"selected",
  171             checked=>$currentlyAssigned,
  172             value=>$setID,
  173             label=>"",
  174           })
  175         ),
  176         $setID,
  177         "($prettyDate)",
  178         " ",
  179         $currentlyAssigned
  180           ? CGI::a({href=>$url}, "Edit user-specific set data")
  181           : (),
  182       ])
  183     );
  184   }
  185   print CGI::end_table();
  186   print CGI::submit({name=>"assignToSelected", value=>"Save"});
  187   print CGI::end_form();
  188 
  189   return "";
  190 }
  191 
  192 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9