[LON-CAPA-cvs] cvs: loncom /interface slotrequest.pm

albertel lon-capa-cvs@mail.lon-capa.org
Sat, 04 Jun 2005 07:44:50 -0000


albertel		Sat Jun  4 03:44:50 2005 EDT

  Modified files:              
    /loncom/interface	slotrequest.pm 
  Log:
  - can actually aquire a reservation
  - respects starttime/endtime/type/symb restrctions on slots
  - sets the parameter associated with the slot
  - doesn't allow a user to reserve a slot several times
  
  
  
Index: loncom/interface/slotrequest.pm
diff -u loncom/interface/slotrequest.pm:1.2 loncom/interface/slotrequest.pm:1.3
--- loncom/interface/slotrequest.pm:1.2	Tue May 31 17:13:01 2005
+++ loncom/interface/slotrequest.pm	Sat Jun  4 03:44:48 2005
@@ -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 @@
     }
     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<br />");
+    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 @@
     }
     
     my $wanted=$last+1;
+    &Apache::lonxml::debug("wanted $wanted<br />");
     if ($wanted >= $max) {
 	# full up
 	return -1;
@@ -130,41 +154,142 @@
 		     '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("<p>Success: $description</p>");
+	$r->print('<p><a href="/adm/flip?postdata=return:">'.
+		  &mt('Return to last resource').'</a></p>');
+	return;
+    } elsif ($reserved < 0) {
+	$r->print("<p>Already reserved: $description</p>");
+	$r->print('<p><a href="/adm/flip?postdata=return:">'.
+		  &mt('Return to last resource').'</a></p>');
+	return;
+    }
+
+    my %lt=('request'=>"Request another attempt",
+	    'try'    =>'Try again');
+    %lt=&Apache::lonlocal::texthash(%lt);
+
+    $r->print(<<STUFF);
+<p> <font color="red">Failed</font> to reserve a spot for $description. </p>
+<p>
+<form method="POST" action="/adm/slotrequest">
+   <input type="submit" name="Try Again" value="$lt{'try'}" />
+   <input type="hidden" name="symb" value="$env{'form.symb'}" />
+   <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
+   <input type="hidden" name="command" value="get" />
+</form>
+?
+</p>
+<p>
+or
+<form method="POST" action="/adm/slotrequest">
+    <input type="hidden" name="symb" value="$env{'form.symb'}" />
+    <input type="submit" name="requestattempt" value="$lt{'request'}" />
+</form>
+</p>
+or
+STUFF
+    $r->print('<p><a href="/adm/flip?postdata=return:">'.
+	      &mt('Return to last resource').'</a></p>');
+    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('<table border="1">');
     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;
-   <form>
+   <form method="POST" action="/adm/slotrequest">
      <input type="submit" name="Select" value="Select" />
-     <!-- FIXME needs to send data -->
+     <input type="hidden" name="symb" value="$escsymb" />
+     <input type="hidden" name="slotname" value="$slot" />
+     <input type="hidden" name="command" value="get" />
    </form>
 STUFF
 	}
@@ -175,6 +300,11 @@
 </tr>
 STUFF
     }
+
+    if (!$available) {
+	$r->print('<tr><td>No avaliable times. <a href="/adm/flip?postdata=return:">'.
+		  &mt('Return to last resource').'</a></td></tr>');
+    }
     $r->print('</table>');
 }
 
@@ -191,8 +321,12 @@
     
     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__