--- loncom/interface/slotrequest.pm 2006/03/30 05:13:38 1.48.2.3 +++ loncom/interface/slotrequest.pm 2006/05/12 05:12:27 1.61 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler for requesting to have slots added to a students record # -# $Id: slotrequest.pm,v 1.48.2.3 2006/03/30 05:13:38 albertel Exp $ +# $Id: slotrequest.pm,v 1.61 2006/05/12 05:12:27 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -41,7 +41,8 @@ sub fail { my ($r,$code)=@_; if ($code eq 'not_valid') { $r->print('

'.&mt('Unable to understand what resource you wanted to sign up for.').'

'); - + } elsif ($code eq 'not_available') { + $r->print('

'.&mt('No slots are available.').'

'); } elsif ($code eq 'not_allowed') { $r->print('

'.&mt('Not allowed to sign up or change reservations at this time.').'

'); } else { @@ -54,14 +55,12 @@ sub fail { sub start_page { my ($r,$title)=@_; - my $html=&Apache::lonxml::xmlbegin(); - $r->print($html.''.&mt($title).''); - $r->print(&Apache::loncommon::bodytag($title)); + $r->print(&Apache::loncommon::start_page($title)); } sub end_page { my ($r)=@_; - $r->print(&Apache::loncommon::endbodytag().''); + $r->print(&Apache::loncommon::end_page()); } =pod @@ -245,10 +244,22 @@ sub make_reservation { my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb, $env{'user.domain'},$env{'user.name'}); &Apache::lonxml::debug("value is $value
"); - if (&network_error($value)) { + + my $use_slots = &Apache::lonnet::EXT("resource.0.useslots"); + &Apache::lonxml::debug("use_slots is $use_slots
"); + + if (&network_error($value) || &network_error($use_slots)) { return 'error: Unable to determine current status'; } + my $parm_symb = $symb; + my $parm_level = 1; + if ($use_slots eq 'map') { + my ($map) = &Apache::lonnet::decode_symb($symb); + $parm_symb = &Apache::lonnet::symbread($map); + $parm_level = 2; + } + foreach my $other_slot (split(/:/, $value)) { if ($other_slot eq $slot_name) { my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom, @@ -256,7 +267,7 @@ sub make_reservation { if (&network_error($value)) { return 'error: Unable to determine current status'; } - my $me=$env{'user.name'}.'@'.$env{'user.domain'}; + my $me=$env{'user.name'}.':'.$env{'user.domain'}; foreach my $key (keys(%consumed)) { if ($consumed{$key}->{'name'} eq $me) { my $num=(split('\0',$key))[1]; @@ -286,9 +297,9 @@ sub make_reservation { return undef; } - my %reservation=('name' => $env{'user.name'}.'@'.$env{'user.domain'}, + my %reservation=('name' => $env{'user.name'}.':'.$env{'user.domain'}, 'timestamp' => time, - 'symb' => $symb); + 'symb' => $parm_symb); my $success=&Apache::lonnet::newput('slot_reservations', {"$slot_name\0$wanted" => @@ -302,7 +313,8 @@ sub make_reservation { } my $result=&Apache::lonparmset::storeparm_by_symb($symb, '0_availablestudent', - 1, $new_value, 'string', + $parm_level, $new_value, + 'string', $env{'user.name'}, $env{'user.domain'}); &Apache::lonxml::debug("hrrm $result"); @@ -399,7 +411,7 @@ sub release_all_slot { foreach my $entry (sort { $consumed{$a}{'name'} cmp $consumed{$b}{'name'} } (keys(%consumed))) { - my ($uname,$udom) = split('@',$consumed{$entry}{'name'}); + my ($uname,$udom) = split(':',$consumed{$entry}{'name'}); my ($result,$msg) = &release_reservation($slot_name,$uname,$udom, $consumed{$entry}{'symb'},$mgr); @@ -467,17 +479,28 @@ sub release_reservation { my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum, "^$slot_name\0"); foreach my $entry (keys(%consumed)) { - if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) { + if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) { &Apache::lonnet::del('slot_reservations',[$entry], $cdom,$cnum); } } + my $use_slots = &Apache::lonnet::EXT("resource.0.useslots"); + &Apache::lonxml::debug("use_slots is $use_slots
"); + + if (&network_error($use_slots)) { + return (0,'error: Unable to determine current status'); + } + + my $parm_level = 1; + if ($use_slots eq 'map') { + $parm_level = 2; + } # store new parameter string my $result=&Apache::lonparmset::storeparm_by_symb($symb, '0_availablestudent', - 1, $new_param, 'string', - $uname,$udom); + $parm_level, $new_param, + 'string', $uname, $udom); my $msg; if ($mgr eq 'F') { @@ -615,34 +638,70 @@ STUFF sub allowed_slot { my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_; + #already started if ($slot->{'starttime'} < time) { # all open slot to be schedulable #return 0; } &Apache::lonxml::debug("$slot_name starttime good"); + #already ended if ($slot->{'endtime'} < time) { return 0; } &Apache::lonxml::debug("$slot_name endtime good"); + # not allowed to pick this one if (defined($slot->{'type'}) && $slot->{'type'} ne 'schedulable_student') { return 0; } &Apache::lonxml::debug("$slot_name type good"); - # not allowed for this resource - if (defined($slot->{'symb'}) - && $slot->{'symb'} ne $symb) { - return 0; - } + # reserve time not yet started if ($slot->{'startreserve'} > time) { return 0; } &Apache::lonxml::debug("$slot_name reserve good"); + my $userallowed=0; + # its for a different set of users + if (defined($slot->{'allowedsections'})) { + if (!defined($env{'request.role.sec'}) + && grep(/^No section assigned$/, + split(',',$slot->{'allowedsections'}))) { + $userallowed=1; + } + if (defined($env{'request.role.sec'}) + && grep(/^\Q$env{'request.role.sec'}\E$/, + split(',',$slot->{'allowedsections'}))) { + $userallowed=1; + } + } + &Apache::lonxml::debug("$slot_name sections is $userallowed"); + + # its for a different set of users + if (defined($slot->{'allowedusers'}) + && grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/, + split(',',$slot->{'allowedusers'}))) { + $userallowed=1; + } + + if (!defined($slot->{'allowedusers'}) + && !defined($slot->{'allowedsections'})) { + $userallowed=1; + } + + &Apache::lonxml::debug("$slot_name user is $userallowed"); + return 0 if (!$userallowed); + + # not allowed for this resource + if (defined($slot->{'symb'}) + && $slot->{'symb'} ne $symb) { + return 0; + } + my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots, $consumed_uniqueperiods); if ($conflict) { @@ -854,22 +913,25 @@ sub show_table { # what to display filtering my %show_fields=&Apache::lonlocal::texthash( - 'name' => 'Slot Name', - 'description' => 'Description', - 'type' => 'Type', - 'starttime' => 'Start time', - 'endtime' => 'End Time', - 'startreserve' => 'Time students can start reserving', - 'secret' => 'Secret Word', - 'maxspace' => 'Maximum # of students', - 'ip' => 'IP or DNS restrictions', - 'symb' => 'Resource slot is restricted to.', - 'uniqueperiod' => 'Period of time slot is unique', - 'scheduled' => 'Scheduled Students', - 'proctor' => 'List of proctors'); + 'name' => 'Slot Name', + 'description' => 'Description', + 'type' => 'Type', + 'starttime' => 'Start time', + 'endtime' => 'End Time', + 'startreserve' => 'Time students can start reserving', + 'secret' => 'Secret Word', + 'maxspace' => 'Maximum # of students', + 'ip' => 'IP or DNS restrictions', + 'symb' => 'Resource slot is restricted to.', + 'allowedsections' => 'Sections slot is restricted to.', + 'allowedusers' => 'Users slot is restricted to.', + 'uniqueperiod' => 'Period of time slot is unique', + 'scheduled' => 'Scheduled Students', + 'proctor' => 'List of proctors'); my @show_order=('name','description','type','starttime','endtime', - 'startreserve','secret','maxspace','ip','symb', - 'uniqueperiod','scheduled','proctor'); + 'startreserve','secret','maxspace','ip','symb', + 'allowedsections','allowedusers','uniqueperiod', + 'scheduled','proctor'); my @show = (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show') : keys(%show_fields); @@ -975,7 +1037,7 @@ sub show_table { my %name_cache; my $slotsort = sub { - if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) { + if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb|allowedsections|allowedusers)$/) { if (lc($slots{$a}->{$env{'form.order'}}) ne lc($slots{$b}->{$env{'form.order'}})) { return (lc($slots{$a}->{$env{'form.order'}}) @@ -1018,14 +1080,14 @@ sub show_table { $consumed{$b}{name} } (keys(%consumed))) { my (undef,$id)=split("\0",$entry); - my ($uname,$udom) = split('@',$consumed{$entry}{'name'}); + my ($uname,$udom) = split(':',$consumed{$entry}{'name'}); $ids.= ''; foreach my $item (@stu_display_order) { if ($stu_display{$item}) { if ($item eq 'fullname') { $ids.=$fullname->{"$uname:$udom"}.' '; } elsif ($item eq 'username') { - $ids.="$uname\@$udom "; + $ids.="$uname:$udom "; } } } @@ -1057,6 +1119,28 @@ sub show_table { $title=''.$title.''; } + my $allowedsections; + if (exists($show{'allowedsections'})) { + $allowedsections = + join(', ',sort(split(/\s*,\s*/, + $slots{$slot}->{'allowedsections'}))); + } + + my @allowedusers; + if (exists($show{'allowedusers'})) { + @allowedusers= map { + my ($uname,$udom)=split(/:/,$_); + my $fullname=$name_cache{$_}; + if (!defined($fullname)) { + $fullname = &Apache::loncommon::plainname($uname,$udom); + $fullname =~s/\s/ /g; + $name_cache{$_} = $fullname; + } + &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom); + } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'}))); + } + my $allowedusers=join(', ',@allowedusers); + my @proctors; my $rowspan=1; my $colspan=1; @@ -1066,7 +1150,6 @@ sub show_table { my ($uname,$udom)=split(/@/,$_); my $fullname=$name_cache{$_}; if (!defined($fullname)) { - &Apache::lonnet::logthis("Gettign $uname $udom"); $fullname = &Apache::loncommon::plainname($uname,$udom); $fullname =~s/\s/ /g; $name_cache{$_} = $fullname; @@ -1123,8 +1206,11 @@ DELETELINK if (exists($show{'symb'})) { $colspan++;$r->print("$title\n"); } - if (exists($show{'uniqueperiod'})) { - $colspan++;$r->print("$unique\n"); + if (exists($show{'allowedsections'})) { + $colspan++;$r->print("$allowedsections\n"); + } + if (exists($show{'allowedusers'})) { + $colspan++;$r->print("$allowedusers\n"); } if (exists($show{'scheduled'})) { $colspan++;$r->print("$remove_all $ids\n\n"); @@ -1309,7 +1395,10 @@ sub csvupload_fields { ['maxspace','Maximum number of reservations'], ['symb','Resource Restriction'], ['uniqueperiod','Date range of slot exclusion'], - ['secret','Secret word proctor uses to validate']); + ['secret','Secret word proctor uses to validate'], + ['allowedsections','Sections slot is restricted to'], + ['allowedusers','Users slot is restricted to'], + ); } sub csv_upload_assign { @@ -1356,22 +1445,49 @@ sub csv_upload_assign { if ($entries{$fields{'endtime'}}) { $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s"); } + + # start/endtime must be defined and greater than zero + if (!$slot{'starttime'}) { + push(@errors,"$name not created -- Invalid start time"); + next; + } + if (!$slot{'endtime'}) { + push(@errors,"$name not created -- Invalid end time"); + next; + } + if ($slot{'starttime'} > $slot{'endtime'}) { + push(@errors,"$name not created -- Slot starts after it ends"); + next; + } + if ($entries{$fields{'startreserve'}}) { $slot{'startreserve'}= &UnixDate($entries{$fields{'startreserve'}},"%s"); } + if (defined($slot{'startreserve'}) + && $slot{'startreserve'} > $slot{'starttime'}) { + push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time."); + next; + } + foreach my $key ('ip','proctor','description','maxspace', 'secret','symb') { if ($entries{$fields{$key}}) { $slot{$key}=$entries{$fields{$key}}; } } + if ($entries{$fields{'uniqueperiod'}}) { my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}}); my @times=(&UnixDate($start,"%s"), &UnixDate($end,"%s")); $slot{'uniqueperiod'}=\@times; } + if (defined($slot{'uniqueperiod'}) + && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) { + push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time."); + next; + } &Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname); $r->print('.'); @@ -1435,10 +1551,14 @@ sub handler { } } else { my $symb=&Apache::lonnet::unescape($env{'form.symb'}); + if (!defined($symb)) { + &fail($r,'not_valid'); + return OK; + } my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb); my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb); - if ($useslots ne 'resource') { - &fail($r,'not_valid'); + if ($useslots ne 'resource' && $useslots ne 'map') { + &fail($r,'not_available'); return OK; } $env{'request.symb'}=$symb;