--- loncom/interface/slotrequest.pm 2005/05/31 21:13:01 1.2 +++ loncom/interface/slotrequest.pm 2005/06/04 07:44:48 1.3 @@ -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.2 2005/05/31 21:13:01 albertel Exp $ +# $Id: slotrequest.pm,v 1.3 2005/06/04 07:44:48 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -104,12 +104,35 @@ sub space_available { } return 0; } - + +# FIXME - depends on the parameter for the resource to be correct +# tho prevent multiple reservations + sub make_reservation { my ($slot_name,$slot,$symb)=@_; - my $max=$slot->{'maxspace'}; - if (!defined($max)) { return 1; } + my ($cnum,$cdom)=&get_course(); + + my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb, + $env{'user.domain'},$env{'user.name'}); + &Apache::lonxml::debug("value is $value
"); + foreach my $other_slot (split(/:/, $value)) { + if ($other_slot eq $slot_name) { + my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom, + $cnum, "^$slot_name\0"); + + 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]; + return -$num; + } + } + } + } + + my $max=$slot->{'maxspace'}; + if (!defined($max)) { $max=99999; } my (@ids)=&get_reservation_ids($slot_name); @@ -121,6 +144,7 @@ sub make_reservation { } my $wanted=$last+1; + &Apache::lonxml::debug("wanted $wanted
"); if ($wanted >= $max) { # full up return -1; @@ -130,41 +154,142 @@ sub make_reservation { 'timestamp' => time, 'symb' => $symb); - my ($cnum,$cdom)=&get_course(); my $success=&Apache::lonnet::newput('slot_reservations', {"$slot_name\0$wanted" => \%reservation}, - $cdom,$cnum); + $cdom, $cnum); + if ($success eq 'ok') { + #FIXME need to set the parm + my $new_value=$slot_name; + if ($value) { + $new_value=$value.':'.$new_value; + } + my $result=&Apache::lonparmset::storeparm_by_symb($symb, + '0_availablestudent', + 1, $new_value, 'string', + $env{'user.name'}, + $env{'user.domain'}); + &Apache::lonxml::debug("hrrm $result"); return $wanted; } + # someone else got it - return -1; + return undef; +} + +sub get_slot { + my ($r,$symb)=@_; + + my %slot=&Apache::lonnet::get_slot($env{'form.slotname'}); + my $reserved=&make_reservation($env{'form.slotname'}, + \%slot,$symb); + my $description=&get_description($env{'form.slotname'},\%slot); + if ($reserved > -1) { + $r->print("

Success: $description

"); + $r->print('

'. + &mt('Return to last resource').'

'); + return; + } elsif ($reserved < 0) { + $r->print("

Already reserved: $description

"); + $r->print('

'. + &mt('Return to last resource').'

'); + return; + } + + my %lt=('request'=>"Request another attempt", + 'try' =>'Try again'); + %lt=&Apache::lonlocal::texthash(%lt); + + $r->print(< Failed to reserve a spot for $description.

+

+

+ + + + +
+? +

+

+or +

+ + +
+

+or +STUFF + $r->print('

'. + &mt('Return to last resource').'

'); + return; +} + +sub allowed_slot { + my ($slot_name,$slot,$symb)=@_; + #already started + if ($slot->{'starttime'} < time) { + return 0; + } + + #already ended + if ($slot->{'endtime'} < time) { + return 0; + } + + # not allowed to pick this one + if (defined($slot->{'type'}) + && $slot->{'type'} ne 'schedulable_student') { + return 0; + } + + # not allowed for this resource + if (defined($slot->{'symb'}) + && $slot->{'symb'} ne $symb) { + return 0; + } + + return 1; } +sub get_description { + my ($slot_name,$slot)=@_; + my $description=$slot->{'description'}; + if (!defined($description)) { + $description=&mt('[_1] From [_2] to [_3]',$slot, + &Apache::lonlocal::locallocaltime($slot->{'starttime'}), + &Apache::lonlocal::locallocaltime($slot->{'endtime'})); + } + return $description; +} sub show_choices { my ($r,$symb)=@_; my ($cnum,$cdom)=&get_course(); my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum); + my $available; $r->print(''); foreach my $slot (sort { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} } (keys(%slots))) { - my $description=$slots{$slot}->{'description'}; - if (!defined($description)) { - $description=&mt('[_1] From [_2] to [_3]',$slot, - &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}), - &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'})); - } + + next if (!&allowed_slot($slot,$slots{$slot})); + + $available++; + + my $description=&get_description($slot,$slots{$slot}); my $form=&mt('Unavailable'); - if (&space_available($slot,$slots{$slot})) { + if (&space_available($slot,$slots{$slot},$symb)) { + my $escsymb=&Apache::lonnet::escape($symb); $form=< +
- + + + STUFF } @@ -175,6 +300,11 @@ STUFF STUFF } + + if (!$available) { + $r->print('
'); + } $r->print('
No avaliable times. '. + &mt('Return to last resource').'
'); } @@ -191,8 +321,12 @@ sub handler { if ($env{'form.requestattempt'}) { &show_choices($r,$symb); + } elsif ($env{'form.command'} eq 'get') { + &get_slot($r,$symb); } - &end_page($r); return OK; } + +1; +__END__