--- loncom/interface/slotrequest.pm	2006/04/10 07:37:16	1.57
+++ loncom/interface/slotrequest.pm	2006/05/18 20:53:24	1.62
@@ -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.57 2006/04/10 07:37:16 albertel Exp $
+# $Id: slotrequest.pm,v 1.62 2006/05/18 20:53:24 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('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
-
+    } elsif ($code eq 'not_available') {
+	$r->print('<p>'.&mt('No slots are available.').'</p>');
     } elsif ($code eq 'not_allowed') {
 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
     } else {
@@ -243,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<br />");
-    if (&network_error($value)) { 
+
+    my $use_slots = &Apache::lonnet::EXT("resource.0.useslots");
+    &Apache::lonxml::debug("use_slots is  $use_slots<br />");
+
+    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,
@@ -286,7 +299,7 @@ sub make_reservation {
     
     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" =>
@@ -300,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");
@@ -471,11 +485,22 @@ sub release_reservation {
 	}
     }
 
+    my $use_slots = &Apache::lonnet::EXT("resource.0.useslots");
+    &Apache::lonxml::debug("use_slots is  $use_slots<br />");
+
+    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') {
@@ -1122,7 +1147,7 @@ sub show_table {
 	if (exists($show{'proctor'})) {
 	    $rowspan=2;
 	    @proctors= map {
-		my ($uname,$udom)=split(/@/,$_);
+		my ($uname,$udom)=split(/:/,$_);
 		my $fullname=$name_cache{$_};
 		if (!defined($fullname)) {
 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
@@ -1420,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('.');
@@ -1499,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;