--- loncom/interface/slotrequest.pm 2006/03/15 19:41:26 1.52 +++ loncom/interface/slotrequest.pm 2009/08/05 13:40:10 1.100 @@ -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.52 2006/03/15 19:41:26 albertel Exp $ +# $Id: slotrequest.pm,v 1.100 2009/08/05 13:40:10 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # @@ -36,12 +36,15 @@ use Apache::lonlocal; use Apache::lonnet; use Apache::lonnavmaps(); use Date::Manip; +use lib '/home/httpd/lib/perl/'; +use LONCAPA; 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 { @@ -53,8 +56,12 @@ sub fail { } sub start_page { - my ($r,$title)=@_; - $r->print(&Apache::loncommon::start_page($title)); + my ($r,$title,$brcrum)=@_; + my $args; + if (ref($brcrum) eq 'ARRAY') { + $args = {bread_crumbs => $brcrum}; + } + $r->print(&Apache::loncommon::start_page($title,undef,$args)); } sub end_page { @@ -74,7 +81,7 @@ sub end_page { =cut sub get_course { - (undef,my $courseid)=&Apache::lonxml::whichuser(); + (undef,my $courseid)=&Apache::lonnet::whichuser(); my $cdom=$env{'course.'.$courseid.'.domain'}; my $cnum=$env{'course.'.$courseid.'.num'}; return ($cnum,$cdom); @@ -87,7 +94,7 @@ sub get_reservation_ids { my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum, "^$slot_name\0"); - if (&network_error(%consumed)) { + if (&Apache::lonnet::error(%consumed)) { return 'error: Unable to determine current status'; } my ($tmp)=%consumed; @@ -124,19 +131,14 @@ sub check_for_reservation { my ($cnum,$cdom)=&get_course(); my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum); - if (&network_error($student) || &network_error($course) || - &network_error(%slots)) { + if (&Apache::lonnet::error($student) + || &Apache::lonnet::error($course) + || &Apache::lonnet::error(%slots)) { return 'error: Unable to determine current status'; } my @got; - foreach my $slot_name (sort { - if (ref($slots{$a}) && ref($slots{$b})) { - return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'} - } - if (ref($slots{$a})) { return -1;} - if (ref($slots{$b})) { return 1;} - return 0; - } @slots) { + my @sorted_slots = &Apache::loncommon::sorted_slots(\@slots,\%slots); + foreach my $slot_name (@sorted_slots) { next if (!defined($slots{$slot_name}) || !ref($slots{$slot_name})); &Apache::lonxml::debug(time." $slot_name ". @@ -161,6 +163,9 @@ sub check_for_reservation { sub get_consumed_uniqueperiods { my ($slots) = @_; my $navmap=Apache::lonnavmaps::navmap->new; + if (!defined($navmap)) { + return 'error: Unable to determine current status'; + } my @problems = $navmap->retrieveResources(undef, sub { $_[0]->is_problem() },1,0); my %used_slots; @@ -172,7 +177,8 @@ sub get_consumed_uniqueperiods { my $course = &Apache::lonnet::EXT("resource.0.available", $symb, $env{'user.domain'}, $env{'user.name'}); - if (&network_error($student) || &network_error($course)) { + if (&Apache::lonnet::error($student) + || &Apache::lonnet::error($course)) { return 'error: Unable to determine current status'; } foreach my $slot (split(/:/,$student), split(/:/, $course)) { @@ -183,7 +189,7 @@ sub get_consumed_uniqueperiods { if (!ref($slots)) { my ($cnum,$cdom)=&get_course(); my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum); - if (&network_error(%slots)) { + if (&Apache::lonnet::error(%slots)) { return 'error: Unable to determine current status'; } $slots = \%slots; @@ -209,9 +215,13 @@ sub check_for_conflict { if (!ref($consumed_uniqueperiods)) { $consumed_uniqueperiods = &get_consumed_uniqueperiods($slots); - if (&network_error(%$consumed_uniqueperiods)) { - return 'error: Unable to determine current status'; - } + if (ref($consumed_uniqueperiods) eq 'HASH') { + if (&Apache::lonnet::error(%$consumed_uniqueperiods)) { + return 'error: Unable to determine current status'; + } + } else { + return 'error: Unable to determine current status'; + } } my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}}; @@ -224,37 +234,40 @@ sub check_for_conflict { } } return undef; - -} - -sub network_error { - my ($result) = @_; - if ($result =~ /^(con_lost|no_such_host|error: [^2])/) { - return 1; - } - return 0; } sub make_reservation { - my ($slot_name,$slot,$symb)=@_; - - my ($cnum,$cdom)=&get_course(); + my ($slot_name,$slot,$symb,$cnum,$cdom)=@_; my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb, $env{'user.domain'},$env{'user.name'}); &Apache::lonxml::debug("value is $value".&mt('A network error has occurred.').'
'); + return; + } + if (!%consumed) { + $r->print(''.&mt('Slot [_1] has no reservations.', + ''.$slot_name.'').'
'); + return; + } + + my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed))); + my $names = join(' ',@names); + + my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name); + &remove_registration_confirmation($r,$msg,['entry','slotname','context']); +} + +sub remove_registration_user { + my ($r) = @_; + + my $slot_name = $env{'form.slotname'}; + my $name = &Apache::loncommon::plainname($env{'form.uname'}, $env{'form.udom'}); my $title = &Apache::lonnet::gettitle($env{'form.symb'}); + my $msg = &mt('Remove [_1] from slot [_2] for [_3]', + $name,$slot_name,$title); + + &remove_registration_confirmation($r,$msg,['uname','udom','slotname', + 'entry','symb','context']); +} + +sub remove_registration_confirmation { + my ($r,$msg,$inputs) =@_; + my $hidden_input; - foreach my $parm ('uname','udom','slotname','entry','symb') { + foreach my $parm (@{$inputs}) { $hidden_input .= '&\'').'" />'."\n"; } + my %lt = &Apache::lonlocal::texthash( + 'yes' => 'Yes', + 'no' => 'No', + ); $r->print(<<"END_CONFIRM"); -Remove $name from slot $env{'form.slotname'} for $title
- - END_CONFIRM } +sub release_all_slot { + my ($r,$mgr)=@_; + + my $slot_name = $env{'form.slotname'}; + + my ($cnum,$cdom)=&get_course(); + + my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum, + "^$slot_name\0"); + + $r->print(''.&mt('Releasing reservations').'
'); + + foreach my $entry (sort { $consumed{$a}{'name'} cmp + $consumed{$b}{'name'} } (keys(%consumed))) { + my ($uname,$udom) = split(':',$consumed{$entry}{'name'}); + my ($result,$msg) = + &release_reservation($slot_name,$uname,$udom, + $consumed{$entry}{'symb'},$mgr); + if (!$result) { + $r->print(''.&mt($msg).'
'); + } else { + $r->print("$msg
"); + } + $r->rflush(); + } + $r->print(''. + &mt('Return to slot list').'
'); + &return_link($r); +} + sub release_slot { my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_; if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; } - my ($cnum,$cdom)=&get_course(); my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'}); if ($mgr eq 'F' @@ -353,21 +462,55 @@ sub release_slot { if ($mgr eq 'F' && defined($env{'form.symb'})) { - $symb = $env{'form.symb'}; + $symb = &unescape($env{'form.symb'}); + } + + my ($result,$msg) = + &release_reservation($slot_name,$uname,$udom,$symb,$mgr); + if (!$result) { + $r->print(''.&mt($msg).'
'); + } else { + $r->print("$msg
"); + } + + if ($mgr eq 'F') { + $r->print(''. + &mt('Return to slot list').'
'); } + + if (!$inhibit_return_link) { &return_link($r); } + return $result; +} + +sub release_reservation { + my ($slot_name,$uname,$udom,$symb,$mgr) = @_; my %slot=&Apache::lonnet::get_slot($slot_name); - my $description=&get_description($env{'form.slotname'},\%slot); + my $description=&get_description($slot_name,\%slot); if ($mgr ne 'F') { if ($slot{'starttime'} < time) { - $r->print("Not allowed to release Reservation: $description, as it has already ended.
"); - &return_link($r); - return 0; + return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description)); } } + + # if the reservation symb is for a map get a resource in that map + # to check slot parameters on + my $navmap=Apache::lonnavmaps::navmap->new; + if (!defined($navmap)) { + return (0,'error: Unable to determine current status'); + } + my $passed_resource = $navmap->getBySymb($symb); + if ($passed_resource->is_map()) { + my ($a_resource) = + $navmap->retrieveResources($passed_resource, + sub {$_[0]->is_problem()},0,1); + $symb = $a_resource->symb(); + } + # get parameter string, check for existance, rebuild string with the slot - my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent", - $symb,$udom,$uname)); + my $student = &Apache::lonnet::EXT("resource.0.availablestudent", + $symb,$udom,$uname); + my @slots = split(/:/,$student); my @new_slots; foreach my $exist_slot (@slots) { @@ -376,28 +519,52 @@ sub release_slot { } my $new_param = join(':',@new_slots); + my ($cnum,$cdom)=&get_course(); + # get slot reservations, check if user has one, if so remove 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 %storehash = ( + symb => $symb, + slot => $slot_name, + action => 'release', + context => $env{'form.context'}, + ); + &Apache::lonnet::instructor_log('slotreservationslog',\%storehash, + 1,$uname,$udom,$cnum,$cdom); + &Apache::lonnet::instructor_log($cdom.'_'.$cnum.'_slotlog',\%storehash, + 1,$uname,$udom,$uname,$udom); } } + my $use_slots = &Apache::lonnet::EXT("resource.0.useslots", + $symb,$udom,$uname); + &Apache::lonxml::debug("use_slots is $use_slotsReleased Reservation: $description
"); + $parm_level, $new_param, + 'string', $uname, $udom); + my $msg; if ($mgr eq 'F') { - $r->print(''. - &mt('Return to slot list').'
'); + $msg = &mt('Released Reservation for user: [_1]',"$uname:$udom"); + } else { + $msg = &mt('Released Reservation: [_1]',$description); } - if (!$inhibit_return_link) { &return_link($r); } - return 1; + return (1,$msg); } sub delete_slot { @@ -417,15 +584,15 @@ sub delete_slot { my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot}, $cdom, $cnum); if ($ret eq 'ok') { - $r->print("Slot $slot_name marked as deleted.
"); + $r->print(''.&mt('Slot [_1] marked as deleted.',''.$slot_name.'').'
'); } else { - $r->print("An error ($ret) occurse when attempting to delete Slot $slot_name.
"); + $r->print(''.&mt('An error occurred when attempting to delete slot: [_1]',''.$slot_name.'')." ($ret)
"); } } else { if (%consumed) { - $r->print("Slot $slot_name has active reservations.
"); + $r->print(''.&mt('Slot [_1] has active reservations.',''.$slot_name.'').'
'); } else { - $r->print("Slot $slot_name does not exist.
"); + $r->print(''.&mt('Slot [_1] does not exist.',''.$slot_name.'').'
'); } } $r->print(''.
@@ -435,42 +602,50 @@ sub delete_slot {
sub return_link {
my ($r) = @_;
- $r->print(' '.
- &mt('Return to last resource').' '.
+ &mt('Return to reservations'));
+ } else {
+ $r->print(' '.
+ &mt('Return to last resource').' An error occured while attempting to make a reservation. ($1) '
+ .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
+ .' Already have a reservation: $description1 '.&mt('Already have a reservation: [_1].',$description1).'
-
-or -
- -or STUFF - &return_link($r); - return; + if (!$inhibit_return_link) { + $r->print(&mt('or').''); + &return_link($r); + } else { + $r->print(''); + } + return 0; } sub allowed_slot { @@ -530,8 +720,7 @@ sub allowed_slot { #already started if ($slot->{'starttime'} < time) { - # all open slot to be schedulable - #return 0; + return 0; } &Apache::lonxml::debug("$slot_name starttime good"); @@ -548,6 +737,12 @@ sub allowed_slot { } &Apache::lonxml::debug("$slot_name type good"); + # 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'})) { @@ -561,6 +756,16 @@ sub allowed_slot { split(',',$slot->{'allowedsections'}))) { $userallowed=1; } + if (defined($env{'request.course.groups'})) { + my @groups = split(/:/,$env{'request.course.groups'}); + my @allowed_sec = split(',',$slot->{'allowedsections'}); + foreach my $group (@groups) { + if (grep {$_ eq $group} (@allowed_sec)) { + $userallowed=1; + last; + } + } + } } &Apache::lonxml::debug("$slot_name sections is $userallowed"); @@ -587,7 +792,9 @@ sub allowed_slot { my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots, $consumed_uniqueperiods); - if ($conflict) { + if ($conflict =~ /^error: /) { + return 0; + } elsif ($conflict ne '') { if ($slots->{$conflict}{'starttime'} < time) { return 0; } @@ -608,15 +815,33 @@ sub get_description { } sub show_choices { - my ($r,$symb)=@_; + my ($r,$symb,$formname)=@_; my ($cnum,$cdom)=&get_course(); my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum); my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots); - my $available; - $r->print('$form | -$description | -$description | \n"; + if ($env{'form.command'} eq 'manageresv') { + $output .= ''; + } else { + $output .= &Apache::loncommon::end_data_table_row(); + } } - - if (!$available) { - $r->print('
No available times. '. - &mt('Return to last resource').' |