File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.80: download - view: text, annotated - select for diffs
Fri Sep 21 22:37:23 2007 UTC (16 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, HEAD
- map and map_map reservations store the map as their symb of the
  reservation occurred for, need to fetch a symb for a resource in the map
  to remove the parameter

    1: # The LearningOnline Network with CAPA
    2: # Handler for requesting to have slots added to a students record
    3: #
    4: # $Id: slotrequest.pm,v 1.80 2007/09/21 22:37:23 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::slotrequest;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http :methods);
   34: use Apache::loncommon();
   35: use Apache::lonlocal;
   36: use Apache::lonnet;
   37: use Apache::lonnavmaps();
   38: use Date::Manip;
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA;
   41: 
   42: sub fail {
   43:     my ($r,$code)=@_;
   44:     if ($code eq 'not_valid') {
   45: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
   46:     } elsif ($code eq 'not_available') {
   47: 	$r->print('<p>'.&mt('No slots are available.').'</p>');
   48:     } elsif ($code eq 'not_allowed') {
   49: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
   50:     } else {
   51: 	$r->print('<p>'.&mt('Failed.').'</p>');
   52:     }
   53:     
   54:     &return_link($r);
   55:     &end_page($r);
   56: }
   57: 
   58: sub start_page {
   59:     my ($r,$title)=@_;
   60:     $r->print(&Apache::loncommon::start_page($title));
   61: }
   62: 
   63: sub end_page {
   64:     my ($r)=@_;
   65:     $r->print(&Apache::loncommon::end_page());
   66: }
   67: 
   68: =pod
   69: 
   70:  slot_reservations db
   71:    - keys are 
   72:     - slotname\0id -> value is an hashref of
   73:                          name -> user@domain of holder
   74:                          timestamp -> timestamp of reservation
   75:                          symb -> symb of resource that it is reserved for
   76: 
   77: =cut
   78: 
   79: sub get_course {
   80:     (undef,my $courseid)=&Apache::lonnet::whichuser();
   81:     my $cdom=$env{'course.'.$courseid.'.domain'};
   82:     my $cnum=$env{'course.'.$courseid.'.num'};
   83:     return ($cnum,$cdom);
   84: }
   85: 
   86: sub get_reservation_ids {
   87:     my ($slot_name)=@_;
   88:     
   89:     my ($cnum,$cdom)=&get_course();
   90: 
   91:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
   92: 				       "^$slot_name\0");
   93:     if (&Apache::lonnet::error(%consumed)) { 
   94: 	return 'error: Unable to determine current status';
   95:     }
   96:     my ($tmp)=%consumed;
   97:     if ($tmp=~/^error: 2 / ) {
   98: 	return 0;
   99:     }
  100:     return keys(%consumed);
  101: }
  102: 
  103: sub space_available {
  104:     my ($slot_name,$slot)=@_;
  105:     my $max=$slot->{'maxspace'};
  106: 
  107:     if (!defined($max)) { return 1; }
  108: 
  109:     my $consumed=scalar(&get_reservation_ids($slot_name));
  110:     if ($consumed < $max) {
  111: 	return 1
  112:     }
  113:     return 0;
  114: }
  115: 
  116: sub check_for_reservation {
  117:     my ($symb,$mode)=@_;
  118:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  119: 				       $env{'user.domain'}, $env{'user.name'});
  120: 
  121:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  122: 				    $env{'user.domain'}, $env{'user.name'});
  123:     my @slots = (split(/:/,$student), split(/:/, $course));
  124: 
  125:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  126: 
  127:     my ($cnum,$cdom)=&get_course();
  128:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  129: 
  130:     if (&Apache::lonnet::error($student) 
  131: 	|| &Apache::lonnet::error($course)
  132: 	|| &Apache::lonnet::error(%slots)) {
  133: 	return 'error: Unable to determine current status';
  134:     }    
  135:     my @got;
  136:     foreach my $slot_name (sort {
  137: 	if (ref($slots{$a}) && ref($slots{$b})) {
  138: 	    return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
  139: 	}
  140: 	if (ref($slots{$a})) { return -1;}
  141: 	if (ref($slots{$b})) { return 1;}
  142: 	return 0;
  143:     } @slots) {
  144: 	next if (!defined($slots{$slot_name}) ||
  145: 		 !ref($slots{$slot_name}));
  146: 	&Apache::lonxml::debug(time." $slot_name ".
  147: 			       $slots{$slot_name}->{'starttime'}." -- ".
  148: 			       $slots{$slot_name}->{'startreserve'});
  149: 	if ($slots{$slot_name}->{'endtime'} > time &&
  150: 	    $slots{$slot_name}->{'startreserve'} < time) {
  151: 	    # between start of reservation times and end of slot
  152: 	    if ($mode eq 'allslots') {
  153: 		push(@got,$slot_name);
  154: 	    } else {
  155: 		return($slot_name, $slots{$slot_name});
  156: 	    }
  157: 	}
  158:     }
  159:     if ($mode eq 'allslots' && @got) {
  160: 	return @got;
  161:     }
  162:     return (undef,undef);
  163: }
  164: 
  165: sub get_consumed_uniqueperiods {
  166:     my ($slots) = @_;
  167:     my $navmap=Apache::lonnavmaps::navmap->new;
  168:     my @problems = $navmap->retrieveResources(undef,
  169: 					      sub { $_[0]->is_problem() },1,0);
  170:     my %used_slots;
  171:     foreach my $problem (@problems) {
  172: 	my $symb = $problem->symb();
  173: 	my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
  174: 					   $symb, $env{'user.domain'},
  175: 					   $env{'user.name'});
  176: 	my $course =  &Apache::lonnet::EXT("resource.0.available",
  177: 					   $symb, $env{'user.domain'},
  178: 					   $env{'user.name'});
  179: 	if (&Apache::lonnet::error($student) 
  180: 	    || &Apache::lonnet::error($course)) {
  181: 	    return 'error: Unable to determine current status';
  182: 	}
  183: 	foreach my $slot (split(/:/,$student), split(/:/, $course)) {
  184: 	    $used_slots{$slot}=1;
  185: 	}
  186:     }
  187: 
  188:     if (!ref($slots)) {
  189: 	my ($cnum,$cdom)=&get_course();
  190: 	my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
  191: 	if (&Apache::lonnet::error(%slots)) {
  192: 	    return 'error: Unable to determine current status';
  193: 	}
  194: 	$slots = \%slots;
  195:     }
  196: 
  197:     my %consumed_uniqueperiods;
  198:     foreach my $slot_name (keys(%used_slots)) {
  199: 	next if (!defined($slots->{$slot_name}) ||
  200: 		 !ref($slots->{$slot_name}));
  201: 	
  202:         next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
  203: 		 !ref($slots->{$slot_name}{'uniqueperiod'}));
  204: 	$consumed_uniqueperiods{$slot_name} = 
  205: 	    $slots->{$slot_name}{'uniqueperiod'};
  206:     }
  207:     return \%consumed_uniqueperiods;
  208: }
  209: 
  210: sub check_for_conflict {
  211:     my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
  212: 
  213:     if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
  214: 
  215:     if (!ref($consumed_uniqueperiods)) {
  216: 	$consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
  217: 	if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
  218: 	    return 'error: Unable to determine current status';
  219: 	}
  220:     }
  221:     
  222:     my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
  223:     foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
  224: 	my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
  225: 	if (!
  226: 	    ($start < $new_uniq_start &&  $end < $new_uniq_start) ||
  227: 	    ($start > $new_uniq_end   &&  $end > $new_uniq_end  )) {
  228: 	    return $slot_name;
  229: 	}
  230:     }
  231:     return undef;
  232: 
  233: }
  234: 
  235: sub make_reservation {
  236:     my ($slot_name,$slot,$symb)=@_;
  237: 
  238:     my ($cnum,$cdom)=&get_course();
  239: 
  240:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  241: 				   $env{'user.domain'},$env{'user.name'});
  242:     &Apache::lonxml::debug("value is  $value<br />");
  243: 
  244:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",$symb,
  245: 					 $env{'user.domain'},$env{'user.name'});
  246:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
  247: 
  248:     if (&Apache::lonnet::error($value) 
  249: 	|| &Apache::lonnet::error($use_slots)) { 
  250: 	return 'error: Unable to determine current status';
  251:     }
  252: 
  253:     my $parm_symb  = $symb;
  254:     my $parm_level = 1;
  255:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
  256: 	my ($map) = &Apache::lonnet::decode_symb($symb);
  257: 	$parm_symb = &Apache::lonnet::symbread($map);
  258: 	$parm_level = 2;
  259:     }
  260: 
  261:     foreach my $other_slot (split(/:/, $value)) {
  262: 	if ($other_slot eq $slot_name) {
  263: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  264: 					       $cnum, "^$slot_name\0");   
  265: 	    if (&Apache::lonnet::error($value)) { 
  266: 		return 'error: Unable to determine current status';
  267: 	    }
  268: 	    my $me=$env{'user.name'}.':'.$env{'user.domain'};
  269: 	    foreach my $key (keys(%consumed)) {
  270: 		if ($consumed{$key}->{'name'} eq $me) {
  271: 		    my $num=(split('\0',$key))[1];
  272: 		    return -$num;
  273: 		}
  274: 	    }
  275: 	}
  276:     }
  277: 
  278:     my $max=$slot->{'maxspace'};
  279:     if (!defined($max)) { $max=99999; }
  280: 
  281:     my (@ids)=&get_reservation_ids($slot_name);
  282:     if (&Apache::lonnet::error(@ids)) { 
  283: 	return 'error: Unable to determine current status';
  284:     }
  285:     my $last=0;
  286:     foreach my $id (@ids) {
  287: 	my $num=(split('\0',$id))[1];
  288: 	if ($num > $last) { $last=$num; }
  289:     }
  290:     
  291:     my $wanted=$last+1;
  292:     &Apache::lonxml::debug("wanted $wanted<br />");
  293:     if (scalar(@ids) >= $max) {
  294: 	# full up
  295: 	return undef;
  296:     }
  297:     
  298:     my %reservation=('name'      => $env{'user.name'}.':'.$env{'user.domain'},
  299: 		     'timestamp' => time,
  300: 		     'symb'      => $parm_symb);
  301: 
  302:     my $success=&Apache::lonnet::newput('slot_reservations',
  303: 					{"$slot_name\0$wanted" =>
  304: 					     \%reservation},
  305: 					$cdom, $cnum);
  306: 
  307:     if ($success eq 'ok') {
  308: 	my $new_value=$slot_name;
  309: 	if ($value) {
  310: 	    $new_value=$value.':'.$new_value;
  311: 	}
  312: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  313: 						      '0_availablestudent',
  314: 						       $parm_level, $new_value,
  315: 						       'string',
  316: 						       $env{'user.name'},
  317: 					               $env{'user.domain'});
  318: 	&Apache::lonxml::debug("hrrm $result");
  319: 	return $wanted;
  320:     }
  321: 
  322:     # someone else got it
  323:     return undef;
  324: }
  325: 
  326: sub remove_registration {
  327:     my ($r) = @_;
  328:     if ($env{'form.entry'} ne 'remove all') {
  329: 	return &remove_registration_user($r);
  330:     }
  331:     my $slot_name = $env{'form.slotname'};
  332:     my %slot=&Apache::lonnet::get_slot($slot_name);
  333: 
  334:     my ($cnum,$cdom)=&get_course();
  335:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  336: 				       "^$slot_name\0");
  337:     if (&Apache::lonnet::error(%consumed)) {
  338: 	$r->print("<p><span class=\"LC_error\">".&mt('A network error has occured.').'</span></p>');
  339: 	return;
  340:     }
  341:     if (!%consumed) {
  342: 	$r->print("<p>".&mt('Slot <tt>[_1]</tt> has no reservations.',
  343: 			    $slot_name)."</p>");
  344: 	return;
  345:     }
  346: 
  347:     my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed)));
  348:     my $names = join(' ',@names);
  349: 
  350:     my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name);
  351:     &remove_registration_confirmation($r,$msg,['entry','slotname']);
  352: }
  353: 
  354: sub remove_registration_user {
  355:     my ($r) = @_;
  356:     
  357:     my $slot_name = $env{'form.slotname'};
  358: 
  359:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
  360: 					     $env{'form.udom'});
  361: 
  362:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
  363: 
  364:     my $msg = &mt('Remove [_1] from slot [_2] for [_3]',
  365: 		  $name,$slot_name,$title);
  366:     
  367:     &remove_registration_confirmation($r,$msg,['uname','udom','slotname',
  368: 					       'entry','symb']);
  369: }
  370: 
  371: sub remove_registration_confirmation {
  372:     my ($r,$msg,$inputs) =@_;
  373: 
  374:     my $hidden_input;
  375:     foreach my $parm (@{$inputs}) {
  376: 	$hidden_input .=
  377: 	    '<input type="hidden" name="'.$parm.'" value="'
  378: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
  379:     }
  380:     my %lt = &Apache::lonlocal::texthash('yes' => 'Yes',
  381: 					 'no'  => 'No',);
  382:     $r->print(<<"END_CONFIRM");
  383: <p> $msg </p>
  384: <form action="/adm/slotrequest" method="post">
  385:     <input type="hidden" name="command" value="release" />
  386:     <input type="hidden" name="button" value="yes" />
  387:     $hidden_input
  388:     <input type="submit" value="$lt{'yes'}" />
  389: </form>
  390: <form action="/adm/slotrequest" method="post">
  391:     <input type="hidden" name="command" value="showslots" />
  392:     <input type="submit" value="$lt{'no'}" />
  393: </form>
  394: END_CONFIRM
  395: 
  396: }
  397: 
  398: sub release_all_slot {
  399:     my ($r,$mgr)=@_;
  400:     
  401:     my $slot_name = $env{'form.slotname'};
  402: 
  403:     my ($cnum,$cdom)=&get_course();
  404: 
  405:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  406: 				       "^$slot_name\0");
  407:     
  408:     $r->print('<p>'.&mt('Releasing reservations').'</p>');
  409: 
  410:     foreach my $entry (sort { $consumed{$a}{'name'} cmp 
  411: 				  $consumed{$b}{'name'} } (keys(%consumed))) {
  412: 	my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
  413: 	my ($result,$msg) =
  414: 	    &release_reservation($slot_name,$uname,$udom,
  415: 				 $consumed{$entry}{'symb'},$mgr);
  416: 	$r->print("<p>$msg</p>");
  417: 	$r->rflush();
  418:     }
  419:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  420: 	      &mt('Return to slot list').'</a></p>');
  421:     &return_link($r);
  422: }
  423: 
  424: sub release_slot {
  425:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
  426: 
  427:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  428: 
  429:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
  430:     if ($mgr eq 'F' 
  431: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
  432: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
  433:     }
  434: 
  435:     if ($mgr eq 'F' 
  436: 	&& defined($env{'form.symb'})) {
  437: 	$symb = &unescape($env{'form.symb'});
  438:     }
  439: 
  440:     my ($result,$msg) =
  441: 	&release_reservation($slot_name,$uname,$udom,$symb,$mgr);
  442:     $r->print("<p>$msg</p>");
  443:     
  444:     if ($mgr eq 'F') {
  445: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  446: 		  &mt('Return to slot list').'</a></p>');
  447:     }
  448: 
  449:     if (!$inhibit_return_link) { &return_link($r);  }
  450:     return $result;
  451: }
  452: 
  453: sub release_reservation {
  454:     my ($slot_name,$uname,$udom,$symb,$mgr) = @_;
  455:     my %slot=&Apache::lonnet::get_slot($slot_name);
  456:     my $description=&get_description($slot_name,\%slot);
  457: 
  458:     if ($mgr ne 'F') {
  459: 	if ($slot{'starttime'} < time) {
  460: 	    return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description));
  461: 	}
  462:     }
  463: 
  464:     # if the reservation symb is for a map get a resource in that map
  465:     # to check slot parameters on
  466:     my $navmap=Apache::lonnavmaps::navmap->new;
  467:     my $passed_resource = $navmap->getBySymb($symb);
  468:     if ($passed_resource->is_map()) {
  469: 	my ($a_resource) = 
  470: 	    $navmap->retrieveResources($passed_resource, 
  471: 				       sub {$_[0]->is_problem()},0,1);
  472: 	$symb = $a_resource->symb();
  473:     }
  474: 
  475:     # get parameter string, check for existance, rebuild string with the slot
  476:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  477: 					       $symb,$udom,$uname));
  478: 
  479:     my @new_slots;
  480:     foreach my $exist_slot (@slots) {
  481: 	if ($exist_slot eq $slot_name) { next; }
  482: 	push(@new_slots,$exist_slot);
  483:     }
  484:     my $new_param = join(':',@new_slots);
  485: 
  486:     my ($cnum,$cdom)=&get_course();
  487: 
  488:     # get slot reservations, check if user has one, if so remove reservation
  489:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  490: 				       "^$slot_name\0");
  491:     foreach my $entry (keys(%consumed)) {
  492: 	if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
  493: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  494: 				 $cdom,$cnum);
  495: 	}
  496:     }
  497: 
  498:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",
  499: 					 $symb,$udom,$uname);
  500:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
  501: 
  502:     if (&Apache::lonnet::error($use_slots)) { 
  503: 	return (0,'error: Unable to determine current status');
  504:     }
  505: 
  506:     my $parm_level = 1;
  507:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
  508: 	$parm_level = 2;
  509:     }
  510:     # store new parameter string
  511:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  512: 						      '0_availablestudent',
  513: 						      $parm_level, $new_param,
  514: 						      'string', $uname, $udom);
  515: 
  516:     my $msg;
  517:     if ($mgr eq 'F') {
  518: 	$msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
  519:     } else {
  520: 	$msg = &mt('Released Reservation: [_1]',$description);
  521:     }
  522:     return (1,$msg);
  523: }
  524: 
  525: sub delete_slot {
  526:     my ($r)=@_;
  527: 
  528:     my $slot_name = $env{'form.slotname'};
  529:     my %slot=&Apache::lonnet::get_slot($slot_name);
  530: 
  531:     my ($cnum,$cdom)=&get_course();
  532:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  533: 				       "^$slot_name\0");
  534:     my ($tmp) = %consumed;
  535:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  536: 
  537:     if (%slot && !%consumed) {
  538: 	$slot{'type'} = 'deleted';
  539: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  540: 					$cdom, $cnum);
  541: 	if ($ret eq 'ok') {
  542: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
  543: 	} else {
  544: 	    $r->print("<p><span class=\"LC_error\"> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</span></p>");
  545: 	}
  546:     } else {
  547: 	if (%consumed) {
  548: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
  549: 	} else {
  550: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
  551: 	}
  552:     }
  553:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  554: 	      &mt('Return to slot list').'</a></p>');
  555:     &return_link($r);
  556: }
  557: 
  558: sub return_link {
  559:     my ($r) = @_;
  560:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  561: 	      &mt('Return to last resource').'</a></p>');
  562: }
  563: 
  564: sub get_slot {
  565:     my ($r,$symb,$conflictable_slot,$inhibit_return_link)=@_;
  566: 
  567:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  568:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
  569: 
  570:     if ($slot_name =~ /^error: (.*)/) {
  571: 	$r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
  572: 	&return_link($r);
  573: 	return 0;
  574:     }
  575:     if ($slot_name && $slot_name ne $conflictable_slot) {
  576: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  577: 	my $description1=&get_description($slot_name,\%slot);
  578: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  579: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  580: 	$r->print("<p>Already have a reservation: $description1</p>");
  581: 	if ($slot_name ne $env{'form.slotname'}) {
  582: 	    $r->print(<<STUFF);
  583: <form method="post" action="/adm/slotrequest">
  584:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  585:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  586:    <input type="hidden" name="releaseslot" value="$slot_name" />
  587:    <input type="hidden" name="command" value="change" />
  588: STUFF
  589:             $r->print("<p>You can either ");
  590: 	    $r->print(<<STUFF);
  591:    <input type="submit" name="change" value="Change" />
  592: STUFF
  593: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  594: 		      $description2.
  595: 		      '</b> <br />or </p>');
  596: 	    &return_link($r);
  597: 	    $r->print(<<STUFF);
  598: </form>
  599: STUFF
  600:         } else {
  601: 	    &return_link($r);
  602: 	}
  603: 	return 0;
  604:     }
  605: 
  606:     my $reserved=&make_reservation($env{'form.slotname'},
  607: 				   \%slot,$symb);
  608:     my $description=&get_description($env{'form.slotname'},\%slot);
  609:     if (defined($reserved)) {
  610: 	my $retvalue = 0;
  611: 	if ($slot_name =~ /^error: (.*)/) {
  612: 	    $r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
  613: 	} elsif ($reserved > -1) {
  614: 	    $r->print("<p>Success: $description</p>");
  615: 	    $retvalue = 1;
  616: 	} elsif ($reserved < 0) {
  617: 	    $r->print("<p>Already reserved: $description</p>");
  618: 	}
  619: 	if (!$inhibit_return_link) { &return_link($r); }
  620: 	return 1;
  621:     }
  622: 
  623:     my %lt=('request'=>"Availibility list",
  624: 	    'try'    =>'Try again');
  625:     %lt=&Apache::lonlocal::texthash(%lt);
  626: 
  627:     my $extra_input;
  628:     if ($conflictable_slot) {
  629: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
  630:     }
  631: 
  632:     $r->print(<<STUFF);
  633: <p> <span class="LC_warning">Failed</span> to reserve a spot for $description. </p>
  634: <p>
  635: <form method="post" action="/adm/slotrequest">
  636:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  637:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  638:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  639:    <input type="hidden" name="command" value="$env{'form.command'}" />
  640:    $extra_input
  641: </form>
  642: ?
  643: </p>
  644: <p>
  645: or
  646: <form method="post" action="/adm/slotrequest">
  647:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  648:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  649: </form>
  650: </p>
  651: or
  652: STUFF
  653: 
  654:     if (!$inhibit_return_link) { &return_link($r); }
  655:     return 0;
  656: }
  657: 
  658: sub allowed_slot {
  659:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
  660: 
  661:     #already started
  662:     if ($slot->{'starttime'} < time) {
  663: 	return 0;
  664:     }
  665:     &Apache::lonxml::debug("$slot_name starttime good");
  666: 
  667:     #already ended
  668:     if ($slot->{'endtime'} < time) {
  669: 	return 0;
  670:     }
  671:     &Apache::lonxml::debug("$slot_name endtime good");
  672: 
  673:     # not allowed to pick this one
  674:     if (defined($slot->{'type'})
  675: 	&& $slot->{'type'} ne 'schedulable_student') {
  676: 	return 0;
  677:     }
  678:     &Apache::lonxml::debug("$slot_name type good");
  679: 
  680:     # reserve time not yet started
  681:     if ($slot->{'startreserve'} > time) {
  682: 	return 0;
  683:     }
  684:     &Apache::lonxml::debug("$slot_name reserve good");
  685: 
  686:     my $userallowed=0;
  687:     # its for a different set of users
  688:     if (defined($slot->{'allowedsections'})) {
  689: 	if (!defined($env{'request.role.sec'})
  690: 	    && grep(/^No section assigned$/,
  691: 		    split(',',$slot->{'allowedsections'}))) {
  692: 	    $userallowed=1;
  693: 	}
  694: 	if (defined($env{'request.role.sec'})
  695: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
  696: 		    split(',',$slot->{'allowedsections'}))) {
  697: 	    $userallowed=1;
  698: 	}
  699: 	if (defined($env{'request.course.groups'})) {
  700: 	    my @groups = split(/:/,$env{'request.course.groups'});
  701: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
  702: 	    foreach my $group (@groups) {
  703: 		if (grep {$_ eq $group} (@allowed_sec)) {
  704: 		    $userallowed=1;
  705: 		    last;
  706: 		}
  707: 	    }
  708: 	}
  709:     }
  710:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
  711: 
  712:     # its for a different set of users
  713:     if (defined($slot->{'allowedusers'})
  714: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
  715: 		split(',',$slot->{'allowedusers'}))) {
  716: 	$userallowed=1;
  717:     }
  718: 
  719:     if (!defined($slot->{'allowedusers'})
  720: 	&& !defined($slot->{'allowedsections'})) {
  721: 	$userallowed=1;
  722:     }
  723: 
  724:     &Apache::lonxml::debug("$slot_name user is $userallowed");
  725:     return 0 if (!$userallowed);
  726: 
  727:     # not allowed for this resource
  728:     if (defined($slot->{'symb'})
  729: 	&& $slot->{'symb'} ne $symb) {
  730: 	return 0;
  731:     }
  732: 
  733:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
  734: 				       $consumed_uniqueperiods);
  735:     if ($conflict) {
  736: 	if ($slots->{$conflict}{'starttime'} < time) {
  737: 	    return 0;
  738: 	}
  739:     }
  740:     &Apache::lonxml::debug("$slot_name symb good");
  741:     return 1;
  742: }
  743: 
  744: sub get_description {
  745:     my ($slot_name,$slot)=@_;
  746:     my $description=$slot->{'description'};
  747:     if (!defined($description)) {
  748: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  749: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  750: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  751:     }
  752:     return $description;
  753: }
  754: 
  755: sub show_choices {
  756:     my ($r,$symb)=@_;
  757: 
  758:     my ($cnum,$cdom)=&get_course();
  759:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  760:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
  761:     my $available;
  762:     $r->print('<table border="1">');
  763:     &Apache::lonxml::debug("Checking Slots");
  764:     my @got_slots=&check_for_reservation($symb,'allslots');
  765:     foreach my $slot (sort 
  766: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  767: 		      (keys(%slots)))  {
  768: 
  769: 	&Apache::lonxml::debug("Checking Slot $slot");
  770: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
  771: 				$consumed_uniqueperiods));
  772: 
  773: 	$available++;
  774: 
  775: 	my $description=&get_description($slot,$slots{$slot});
  776: 
  777: 	my $form=&mt('Unavailable');
  778: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
  779: 	    &space_available($slot,$slots{$slot},$symb)) {
  780: 	    my $text=&mt('Select');
  781: 	    my $command='get';
  782: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
  783: 		$text=&mt('Drop Reservation');
  784: 		$command='release';
  785: 	    } else {
  786: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
  787: 						   \%slots,
  788: 						   $consumed_uniqueperiods);
  789: 		if ($conflict) {
  790: 		    $text=&mt('Change Reservation');
  791: 		    $command='get';
  792: 		}
  793: 	    }
  794: 	    my $escsymb=&escape($symb);
  795: 	    $form=<<STUFF;
  796:    <form method="post" action="/adm/slotrequest">
  797:      <input type="submit" name="Select" value="$text" />
  798:      <input type="hidden" name="symb" value="$escsymb" />
  799:      <input type="hidden" name="slotname" value="$slot" />
  800:      <input type="hidden" name="command" value="$command" />
  801:    </form>
  802: STUFF
  803: 	}
  804: 	$r->print(<<STUFF);
  805: <tr>
  806:  <td>$form</td>
  807:  <td>$description</td>
  808: </tr>
  809: STUFF
  810:     }
  811: 
  812:     if (!$available) {
  813: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  814: 		  &mt('Return to last resource').'</a></td></tr>');
  815:     }
  816:     $r->print('</table>');
  817: }
  818: 
  819: sub to_show {
  820:     my ($slotname,$slot,$when,$deleted,$name) = @_;
  821:     my $time=time;
  822:     my $week=60*60*24*7;
  823: 
  824:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  825: 	return 0;
  826:     }
  827: 
  828:     if ($name && $name->{'value'} =~ /\w/) {
  829: 	if ($name->{'type'} eq 'substring') {
  830: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
  831: 		return 0;
  832: 	    }
  833: 	}
  834: 	if ($name->{'type'} eq 'exact') {
  835: 	    if ($slotname eq $name->{'value'}) {
  836: 		return 0;
  837: 	    }
  838: 	}
  839:     }
  840: 
  841:     if ($when eq 'any') {
  842: 	return 1;
  843:     } elsif ($when eq 'now') {
  844: 	if ($time > $slot->{'starttime'} &&
  845: 	    $time < $slot->{'endtime'}) {
  846: 	    return 1;
  847: 	}
  848: 	return 0;
  849:     } elsif ($when eq 'nextweek') {
  850: 	if ( ($time        < $slot->{'starttime'} &&
  851: 	      ($time+$week) > $slot->{'starttime'})
  852: 	     ||
  853: 	     ($time        < $slot->{'endtime'} &&
  854: 	      ($time+$week) > $slot->{'endtime'}) ) {
  855: 	    return 1;
  856: 	}
  857: 	return 0;
  858:     } elsif ($when eq 'lastweek') {
  859: 	if ( ($time        > $slot->{'starttime'} &&
  860: 	      ($time-$week) < $slot->{'starttime'})
  861: 	     ||
  862: 	     ($time        > $slot->{'endtime'} &&
  863: 	      ($time-$week) < $slot->{'endtime'}) ) {
  864: 	    return 1;
  865: 	}
  866: 	return 0;
  867:     } elsif ($when eq 'willopen') {
  868: 	if ($time < $slot->{'starttime'}) {
  869: 	    return 1;
  870: 	}
  871: 	return 0;
  872:     } elsif ($when eq 'wereopen') {
  873: 	if ($time > $slot->{'endtime'}) {
  874: 	    return 1;
  875: 	}
  876: 	return 0;
  877:     }
  878:     
  879:     return 1;
  880: }
  881: 
  882: sub remove_link {
  883:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
  884: 
  885:     my $remove = &mt('Remove');
  886: 
  887:     if ($entry eq 'remove all') {
  888: 	$remove = &mt('Remove All');
  889: 	undef($uname);
  890: 	undef($udom);
  891:     }
  892: 
  893:     $slotname  = &escape($slotname);
  894:     $entry     = &escape($entry);
  895:     $uname     = &escape($uname);
  896:     $udom      = &escape($udom);
  897:     $symb      = &escape($symb);
  898: 
  899:     return <<"END_LINK";
  900:  <a href="/adm/slotrequest?command=remove_registration&amp;slotname=$slotname&amp;entry=$entry&amp;uname=$uname&amp;udom=$udom&amp;symb=$symb"
  901:    >($remove)</a>
  902: END_LINK
  903: 
  904: }
  905: 
  906: sub show_table {
  907:     my ($r,$mgr)=@_;
  908: 
  909:     my ($cnum,$cdom)=&get_course();
  910:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  911:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  912: 	undef(%slots);
  913:     } 
  914:     my $available;
  915:     if ($mgr eq 'F') {
  916:     # FIXME: This line should be deleted once Slots uses breadcrumbs
  917:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
  918: 
  919: 	$r->print('<div>');
  920: 	$r->print('<form method="post" action="/adm/slotrequest">
  921: <input type="hidden" name="command" value="uploadstart" />
  922: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  923: </form>');
  924: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
  925: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
  926: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  927: </form>');
  928: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
  929: 	$r->print('</div>');
  930:     }
  931:     
  932:     my %Saveable_Parameters = ('show'              => 'array',
  933: 			       'when'              => 'scalar',
  934: 			       'order'             => 'scalar',
  935: 			       'deleted'           => 'scalar',
  936: 			       'name_filter_type'  => 'scalar',
  937: 			       'name_filter_value' => 'scalar',
  938: 			       );
  939:     &Apache::loncommon::store_course_settings('slotrequest',
  940: 					      \%Saveable_Parameters);
  941:     &Apache::loncommon::restore_course_settings('slotrequest',
  942: 						\%Saveable_Parameters);
  943:     &Apache::grades::init_perm();
  944:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
  945:     &Apache::grades::reset_perm();
  946: 
  947:     # what to display filtering
  948:     my %show_fields=&Apache::lonlocal::texthash(
  949: 	     'name'            => 'Slot Name',
  950: 	     'description'     => 'Description',
  951: 	     'type'            => 'Type',
  952: 	     'starttime'       => 'Start time',
  953: 	     'endtime'         => 'End Time',
  954:              'startreserve'    => 'Time students can start reserving',
  955: 	     'secret'          => 'Secret Word',
  956: 	     'space'           => '# of students/max',
  957: 	     'ip'              => 'IP or DNS restrictions',
  958: 	     'symb'            => 'Resource slot is restricted to.',
  959: 	     'allowedsections' => 'Sections slot is restricted to.',
  960: 	     'allowedusers'    => 'Users slot is restricted to.',
  961: 	     'uniqueperiod'    => 'Period of time slot is unique',
  962: 	     'scheduled'       => 'Scheduled Students',
  963: 	     'proctor'         => 'List of proctors');
  964:     my @show_order=('name','description','type','starttime','endtime',
  965: 		    'startreserve','secret','space','ip','symb',
  966: 		    'allowedsections','allowedusers','uniqueperiod',
  967: 		    'scheduled','proctor');
  968:     my @show = 
  969: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  970: 	                            : keys(%show_fields);
  971:     my %show =  map { $_ => 1 } (@show);
  972: 
  973:     #when filtering setup
  974:     my %when_fields=&Apache::lonlocal::texthash(
  975: 	     'now'      => 'Open now',
  976: 	     'nextweek' => 'Open within the next week',
  977: 	     'lastweek' => 'Were open last week',
  978: 	     'willopen' => 'Will open later',
  979: 	     'wereopen' => 'Were open',
  980: 	     'any'      => 'Anytime',
  981: 						);
  982:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
  983:     $when_fields{'select_form_order'} = \@when_order;
  984:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
  985:                                             : 'now';
  986: 
  987:     #display of students setup
  988:     my %stu_display_fields=
  989: 	&Apache::lonlocal::texthash('username' => 'User name',
  990: 				    'fullname' => 'Full name',
  991: 				    );
  992:     my @stu_display_order=('fullname','username');
  993:     my @stu_display = 
  994: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
  995: 	                                  : keys(%stu_display_fields);
  996:     my %stu_display =  map { $_ => 1 } (@stu_display);
  997: 
  998:     #name filtering setup
  999:     my %name_filter_type_fields=
 1000: 	&Apache::lonlocal::texthash('substring' => 'Substring',
 1001: 				    'exact'     => 'Exact',
 1002: 				    #'reg'       => 'Regular Expression',
 1003: 				    );
 1004:     my @name_filter_type_order=('substring','exact');
 1005: 
 1006:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
 1007:     my $name_filter_type = 
 1008: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
 1009:                                                 : 'substring';
 1010:     my $name_filter = {'type'  => $name_filter_type,
 1011: 		       'value' => $env{'form.name_filter_value'},};
 1012: 
 1013:     
 1014:     #deleted slot filtering
 1015:     #default to hide if no value
 1016:     $env{'form.deleted'} ||= 'hide';
 1017:     my $hide_radio = 
 1018: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
 1019:     my $show_radio = 
 1020: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
 1021: 	
 1022:     $r->print('<form method="post" action="/adm/slotrequest">
 1023: <input type="hidden" name="command" value="showslots" />');
 1024:     $r->print('<div>');
 1025:     $r->print('<table class="inline">
 1026:       <tr><th>'.&mt('Show').'</th>
 1027:           <th>'.&mt('Student Display').'</th>
 1028:           <th>'.&mt('Open').'</th>
 1029:           <th>'.&mt('Slot Name Filter').'</th>
 1030:           <th>'.&mt('Options').'</th>
 1031:       </tr>
 1032:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
 1033: 	      '</td>
 1034:            <td>
 1035:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
 1036: 						    6,\%stu_display_fields,
 1037: 						    \@stu_display_order).'
 1038:            </td>
 1039:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
 1040:           '</td>
 1041:            <td>'.&Apache::loncommon::select_form($name_filter_type,
 1042: 						 'name_filter_type',
 1043: 						 %name_filter_type_fields).
 1044: 	      '<br />'.
 1045: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
 1046: 					      $env{'form.name_filter_value'},
 1047: 					      15).
 1048:           '</td>
 1049:            <td>
 1050:             <table>
 1051:               <tr>
 1052:                 <td rowspan="2">Deleted slots:</td>
 1053:                 <td><label>'.$show_radio.'Show</label></td>
 1054:               </tr>
 1055:               <tr>
 1056:                 <td><label>'.$hide_radio.'Hide</label></td>
 1057:               </tr>
 1058:             </table>
 1059: 	  </td>
 1060:        </tr>
 1061:     </table>');
 1062:     $r->print('</div>');
 1063:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
 1064:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
 1065:     $r->print(&Apache::loncommon::start_data_table().
 1066: 	      &Apache::loncommon::start_data_table_header_row().'
 1067: 	       <th></th>');
 1068:     foreach my $which (@show_order) {
 1069: 	if ($which ne 'proctor' && exists($show{$which})) {
 1070: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
 1071: 	}
 1072:     }
 1073:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1074: 
 1075:     my %name_cache;
 1076:     my $slotsort = sub {
 1077: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
 1078: 	    if (lc($slots{$a}->{$env{'form.order'}})
 1079: 		ne lc($slots{$b}->{$env{'form.order'}})) {
 1080: 		return (lc($slots{$a}->{$env{'form.order'}}) 
 1081: 			cmp lc($slots{$b}->{$env{'form.order'}}));
 1082: 	    }
 1083: 	} elsif ($env{'form.order'} eq 'space') {
 1084: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
 1085: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
 1086: 	    }
 1087: 	} elsif ($env{'form.order'} eq 'name') {
 1088: 	    if (lc($a) cmp lc($b)) {
 1089: 		return lc($a) cmp lc($b);
 1090: 	    }
 1091: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
 1092: 	    
 1093: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
 1094: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
 1095: 		return ($slots{$a}->{'uniqueperiod'}[0]
 1096: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
 1097: 	    }
 1098: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
 1099: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
 1100: 		return ($slots{$a}->{'uniqueperiod'}[1]
 1101: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
 1102: 	    }
 1103: 	}
 1104: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
 1105:     };
 1106: 
 1107:     my %consumed;
 1108:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1109: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
 1110: 	my ($tmp)=%consumed;
 1111: 	if ($tmp =~ /^error: /) { undef(%consumed); }
 1112:     }
 1113: 
 1114:     foreach my $slot (sort $slotsort (keys(%slots)))  {
 1115: 	if (!&to_show($slot,$slots{$slot},$when,
 1116: 		      $env{'form.deleted'},$name_filter)) { next; }
 1117: 	if (defined($slots{$slot}->{'type'})
 1118: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
 1119: 	    #next;
 1120: 	}
 1121: 	my $description=&get_description($slot,$slots{$slot});
 1122: 	my ($id_count,$ids);
 1123: 	    
 1124: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1125: 	    my $re_str = "$slot\0";
 1126: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
 1127: 	    $id_count = scalar(@this_slot);
 1128: 	    if (exists($show{'scheduled'})) {
 1129: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
 1130: 					      $consumed{$b}{name} }
 1131: 				   (@this_slot)) {
 1132: 		    my (undef,$id)=split("\0",$entry);
 1133: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
 1134: 		    $ids.= '<nobr>';
 1135: 		    foreach my $item (@stu_display_order) {
 1136: 			if ($stu_display{$item}) {
 1137: 			    if ($item eq 'fullname') {
 1138: 				$ids.=$fullname->{"$uname:$udom"}.' ';
 1139: 			    } elsif ($item eq 'username') {
 1140: 				$ids.="<tt>$uname:$udom</tt> ";
 1141: 			    }
 1142: 			}
 1143: 		    }
 1144: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
 1145: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
 1146: 		}
 1147: 	    }
 1148: 	}
 1149: 
 1150: 	my $start=($slots{$slot}->{'starttime'}?
 1151: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
 1152: 	my $end=($slots{$slot}->{'endtime'}?
 1153: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
 1154: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
 1155: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
 1156: 	
 1157: 	my $unique;
 1158: 	if (ref($slots{$slot}{'uniqueperiod'})) {
 1159: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
 1160: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
 1161: 	}
 1162: 
 1163: 	my $title;
 1164: 	if (exists($slots{$slot}{'symb'})) {
 1165: 	    my (undef,undef,$res)=
 1166: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
 1167: 	    $res =   &Apache::lonnet::clutter($res);
 1168: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
 1169: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
 1170: 	}
 1171: 
 1172: 	my $allowedsections;
 1173: 	if (exists($show{'allowedsections'})) {
 1174: 	    $allowedsections = 
 1175: 		join(', ',sort(split(/\s*,\s*/,
 1176: 				     $slots{$slot}->{'allowedsections'})));
 1177: 	}
 1178: 
 1179: 	my @allowedusers;
 1180: 	if (exists($show{'allowedusers'})) {
 1181: 	    @allowedusers= map {
 1182: 		my ($uname,$udom)=split(/:/,$_);
 1183: 		my $fullname=$name_cache{$_};
 1184: 		if (!defined($fullname)) {
 1185: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1186: 		    $fullname =~s/\s/&nbsp;/g;
 1187: 		    $name_cache{$_} = $fullname;
 1188: 		}
 1189: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1190: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
 1191: 	}
 1192: 	my $allowedusers=join(', ',@allowedusers);
 1193: 	
 1194: 	my @proctors;
 1195: 	my $rowspan=1;
 1196: 	my $colspan=1;
 1197: 	if (exists($show{'proctor'})) {
 1198: 	    $rowspan=2;
 1199: 	    @proctors= map {
 1200: 		my ($uname,$udom)=split(/:/,$_);
 1201: 		my $fullname=$name_cache{$_};
 1202: 		if (!defined($fullname)) {
 1203: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1204: 		    $fullname =~s/\s/&nbsp;/g;
 1205: 		    $name_cache{$_} = $fullname;
 1206: 		}
 1207: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1208: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
 1209: 	}
 1210: 	my $proctors=join(', ',@proctors);
 1211: 
 1212: 	my $edit=(<<"EDITLINK");
 1213: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
 1214: EDITLINK
 1215: 
 1216: 	my $delete=(<<"DELETELINK");
 1217: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
 1218: DELETELINK
 1219: 
 1220:         my $remove_all=&remove_link($slot,'remove all').'<br />';
 1221: 
 1222:         if ($ids ne '') { undef($delete); }
 1223: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
 1224: 	    || $ids eq '') { 
 1225: 	    undef($remove_all);
 1226: 	}
 1227: 
 1228: 	my $row_start=&Apache::loncommon::start_data_table_row();
 1229: 	my $row_end=&Apache::loncommon::end_data_table_row();
 1230:         $r->print($row_start.
 1231: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
 1232: 	if (exists($show{'name'})) {
 1233: 	    $colspan++;$r->print("<td>$slot</td>");
 1234: 	}
 1235: 	if (exists($show{'description'})) {
 1236: 	    $colspan++;$r->print("<td>$description</td>\n");
 1237: 	}
 1238: 	if (exists($show{'type'})) {
 1239: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
 1240: 	}
 1241: 	if (exists($show{'starttime'})) {
 1242: 	    $colspan++;$r->print("<td>$start</td>\n");
 1243: 	}
 1244: 	if (exists($show{'endtime'})) {
 1245: 	    $colspan++;$r->print("<td>$end</td>\n");
 1246: 	}
 1247: 	if (exists($show{'startreserve'})) {
 1248: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
 1249: 	}
 1250: 	if (exists($show{'secret'})) {
 1251: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
 1252: 	}
 1253: 	if (exists($show{'space'})) {
 1254: 	    my $display = $id_count;
 1255: 	    if ($slots{$slot}{'maxspace'}>0) {
 1256: 		$display.='/'.$slots{$slot}{'maxspace'};
 1257: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
 1258: 		    $display = '<strong>'.$display.' (full) </strong>';
 1259: 		}
 1260: 	    }
 1261: 	    $colspan++;$r->print("<td>$display</td>\n");
 1262: 	}
 1263: 	if (exists($show{'ip'})) {
 1264: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
 1265: 	}
 1266: 	if (exists($show{'symb'})) {
 1267: 	    $colspan++;$r->print("<td>$title</td>\n");
 1268: 	}
 1269: 	if (exists($show{'allowedsections'})) {
 1270: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
 1271: 	}
 1272: 	if (exists($show{'allowedusers'})) {
 1273: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
 1274: 	}
 1275: 	if (exists($show{'uniqueperiod'})) {
 1276: 	    $colspan++;$r->print("<td>$unique</td>\n");
 1277: 	}
 1278: 	if (exists($show{'scheduled'})) {
 1279: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
 1280: 	}
 1281: 	$r->print("$row_end\n");
 1282: 	if (exists($show{'proctor'})) {
 1283: 	    $r->print(<<STUFF);
 1284: $row_start
 1285:  <td colspan="$colspan">$proctors</td>
 1286: $row_end
 1287: STUFF
 1288:         }
 1289:     }
 1290:     $r->print('</table></form>');
 1291: }
 1292: 
 1293: sub upload_start {
 1294:     my ($r)=@_;    
 1295:     $r->print(&Apache::grades::checkforfile_js());
 1296:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
 1297:     $result.='&nbsp;<b>'.
 1298: 	&mt('Specify a file containing the slot definitions.').
 1299: 	'</b></td></tr>'."\n";
 1300:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 1301:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1302:     my $ignore=&mt('Ignore First Line');
 1303:     $result.=<<ENDUPFORM;
 1304: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1305: <input type="hidden" name="command" value="csvuploadmap" />
 1306: $upfile_select
 1307: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
 1308: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 1309: </form>
 1310: ENDUPFORM
 1311:     $result.='</td></tr></table>'."\n";
 1312:     $result.='</td></tr></table>'."\n";
 1313:     $r->print($result);
 1314: }
 1315: 
 1316: sub csvuploadmap_header {
 1317:     my ($r,$datatoken,$distotal)= @_;
 1318:     my $javascript;
 1319:     if ($env{'form.upfile_associate'} eq 'reverse') {
 1320: 	$javascript=&csvupload_javascript_reverse_associate();
 1321:     } else {
 1322: 	$javascript=&csvupload_javascript_forward_associate();
 1323:     }
 1324: 
 1325:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 1326:     my $ignore=&mt('Ignore First Line');
 1327: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
 1328: 
 1329:     $r->print(<<ENDPICK);
 1330: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1331: <h3>Identify fields $help_field</h3>	
 1332: Total number of records found in file: $distotal <hr />
 1333: Enter as many fields as you can. The system will inform you and bring you back
 1334: to this page if the data selected is insufficient to create the slots.<hr />
 1335: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 1336: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 1337: <input type="hidden" name="associate"  value="" />
 1338: <input type="hidden" name="datatoken"  value="$datatoken" />
 1339: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 1340: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 1341: <input type="hidden" name="upfile_associate" 
 1342:                                        value="$env{'form.upfile_associate'}" />
 1343: <input type="hidden" name="command"    value="csvuploadassign" />
 1344: <hr />
 1345: <script type="text/javascript" language="Javascript">
 1346: $javascript
 1347: </script>
 1348: ENDPICK
 1349:     return '';
 1350: 
 1351: }
 1352: 
 1353: sub csvuploadmap_footer {
 1354:     my ($request,$i,$keyfields) =@_;
 1355:     $request->print(<<ENDPICK);
 1356: </table>
 1357: <input type="hidden" name="nfields" value="$i" />
 1358: <input type="hidden" name="keyfields" value="$keyfields" />
 1359: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
 1360: </form>
 1361: ENDPICK
 1362: }
 1363: 
 1364: sub csvupload_javascript_reverse_associate {
 1365:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1366:     return(<<ENDPICK);
 1367:   function verify(vf) {
 1368:     var foundstart=0;
 1369:     var foundend=0;
 1370:     var foundname=0;
 1371:     var foundtype=0;
 1372:     for (i=0;i<=vf.nfields.value;i++) {
 1373:       tw=eval('vf.f'+i+'.selectedIndex');
 1374:       if (i==0 && tw!=0) { foundname=1; }
 1375:       if (i==1 && tw!=0) { foundtype=1; }
 1376:       if (i==2 && tw!=0) { foundstat=1; }
 1377:       if (i==3 && tw!=0) { foundend=1; }
 1378:     }
 1379:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1380: 	alert('$error1');
 1381: 	return;
 1382:     }
 1383:     vf.submit();
 1384:   }
 1385:   function flip(vf,tf) {
 1386:   }
 1387: ENDPICK
 1388: }
 1389: 
 1390: sub csvupload_javascript_forward_associate {
 1391:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1392:   return(<<ENDPICK);
 1393:   function verify(vf) {
 1394:     var foundstart=0;
 1395:     var foundend=0;
 1396:     var foundname=0;
 1397:     var foundtype=0;
 1398:     for (i=0;i<=vf.nfields.value;i++) {
 1399:       tw=eval('vf.f'+i+'.selectedIndex');
 1400:       if (tw==1) { foundname=1; }
 1401:       if (tw==2) { foundtype=1; }
 1402:       if (tw==3) { foundstat=1; }
 1403:       if (tw==4) { foundend=1; }
 1404:     }
 1405:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1406: 	alert('$error1');
 1407: 	return;
 1408:     }
 1409:     vf.submit();
 1410:   }
 1411:   function flip(vf,tf) {
 1412:   }
 1413: ENDPICK
 1414: }
 1415: 
 1416: sub csv_upload_map {
 1417:     my ($r)= @_;
 1418: 
 1419:     my $datatoken;
 1420:     if (!$env{'form.datatoken'}) {
 1421: 	$datatoken=&Apache::loncommon::upfile_store($r);
 1422:     } else {
 1423: 	$datatoken=$env{'form.datatoken'};
 1424: 	&Apache::loncommon::load_tmp_file($r);
 1425:     }
 1426:     my @records=&Apache::loncommon::upfile_record_sep();
 1427:     if ($env{'form.noFirstLine'}) { shift(@records); }
 1428:     &csvuploadmap_header($r,$datatoken,$#records+1);
 1429:     my ($i,$keyfields);
 1430:     if (@records) {
 1431: 	my @fields=&csvupload_fields();
 1432: 
 1433: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 1434: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 1435: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 1436: 							  \@fields);
 1437: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 1438: 	    chop($keyfields);
 1439: 	} else {
 1440: 	    unshift(@fields,['none','']);
 1441: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1442: 							    \@fields);
 1443: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1444: 	    $keyfields=join(',',sort(keys(%sone)));
 1445: 	}
 1446:     }
 1447:     &csvuploadmap_footer($r,$i,$keyfields);
 1448: 
 1449:     return '';
 1450: }
 1451: 
 1452: sub csvupload_fields {
 1453:     return (['name','Slot name'],
 1454: 	    ['type','Type of slot'],
 1455: 	    ['starttime','Start Time of slot'],
 1456: 	    ['endtime','End Time of slot'],
 1457: 	    ['startreserve','Reservation Start Time'],
 1458: 	    ['ip','IP or DNS restriction'],
 1459: 	    ['proctor','List of proctor ids'],
 1460: 	    ['description','Slot Description'],
 1461: 	    ['maxspace','Maximum number of reservations'],
 1462: 	    ['symb','Resource Restriction'],
 1463: 	    ['uniqueperiod','Date range of slot exclusion'],
 1464: 	    ['secret','Secret word proctor uses to validate'],
 1465: 	    ['allowedsections','Sections slot is restricted to'],
 1466: 	    ['allowedusers','Users slot is restricted to'],
 1467: 	    );
 1468: }
 1469: 
 1470: sub csv_upload_assign {
 1471:     my ($r,$mgr)= @_;
 1472:     &Apache::loncommon::load_tmp_file($r);
 1473:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1474:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1475:     my %fields=&Apache::grades::get_fields();
 1476:     $r->print('<h3>Creating Slots</h3>');
 1477:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1478:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1479:     my $countdone=0;
 1480:     my @errors;
 1481:     foreach my $slot (@slotdata) {
 1482: 	my %slot;
 1483: 	my %entries=&Apache::loncommon::record_sep($slot);
 1484: 	my $domain;
 1485: 	my $name=$entries{$fields{'name'}};
 1486: 	if ($name=~/^\s*$/) {
 1487: 	    push(@errors,"Did not create slot with no name");
 1488: 	    next;
 1489: 	}
 1490: 	if ($name=~/\s/) { 
 1491: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1492: 	    next;
 1493: 	}
 1494: 	if ($name=~/\W/) { 
 1495: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1496: 	    next;
 1497: 	}
 1498: 	if ($entries{$fields{'type'}}) {
 1499: 	    $slot{'type'}=$entries{$fields{'type'}};
 1500: 	} else {
 1501: 	    $slot{'type'}='preassigned';
 1502: 	}
 1503: 	if ($slot{'type'} ne 'preassigned' &&
 1504: 	    $slot{'type'} ne 'schedulable_student') {
 1505: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1506: 	    next;
 1507: 	}
 1508: 	if ($entries{$fields{'starttime'}}) {
 1509: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1510: 	}
 1511: 	if ($entries{$fields{'endtime'}}) {
 1512: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1513: 	}
 1514: 
 1515: 	# start/endtime must be defined and greater than zero
 1516: 	if (!$slot{'starttime'}) {
 1517: 	    push(@errors,"$name not created -- Invalid start time");
 1518: 	    next;
 1519: 	}
 1520: 	if (!$slot{'endtime'}) {
 1521: 	    push(@errors,"$name not created -- Invalid end time");
 1522: 	    next;
 1523: 	}
 1524: 	if ($slot{'starttime'} > $slot{'endtime'}) {
 1525: 	    push(@errors,"$name not created -- Slot starts after it ends");
 1526: 	    next;
 1527: 	}
 1528: 
 1529: 	if ($entries{$fields{'startreserve'}}) {
 1530: 	    $slot{'startreserve'}=
 1531: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1532: 	}
 1533: 	if (defined($slot{'startreserve'})
 1534: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
 1535: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
 1536: 	    next;
 1537: 	}
 1538: 
 1539: 	foreach my $key ('ip','proctor','description','maxspace',
 1540: 			 'secret','symb') {
 1541: 	    if ($entries{$fields{$key}}) {
 1542: 		$slot{$key}=$entries{$fields{$key}};
 1543: 	    }
 1544: 	}
 1545: 
 1546: 	if ($entries{$fields{'uniqueperiod'}}) {
 1547: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1548: 	    my @times=(&UnixDate($start,"%s"),
 1549: 		       &UnixDate($end,"%s"));
 1550: 	    $slot{'uniqueperiod'}=\@times;
 1551: 	}
 1552: 	if (defined($slot{'uniqueperiod'})
 1553: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
 1554: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
 1555: 	    next;
 1556: 	}
 1557: 
 1558: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1559: 	$r->print('.');
 1560: 	$r->rflush();
 1561: 	$countdone++;
 1562:     }
 1563:     $r->print("<p>Created $countdone slots\n</p>");
 1564:     foreach my $error (@errors) {
 1565: 	$r->print("<p><span class=\"LC_warning\">$error</span></p>\n");
 1566:     }
 1567:     &show_table($r,$mgr);
 1568:     return '';
 1569: }
 1570: 
 1571: sub handler {
 1572:     my $r=shift;
 1573: 
 1574:     &Apache::loncommon::content_type($r,'text/html');
 1575:     &Apache::loncommon::no_cache($r);
 1576:     if ($r->header_only()) {
 1577: 	$r->send_http_header();
 1578: 	return OK;
 1579:     }
 1580: 
 1581:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1582:     
 1583:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1584:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1585:     my $title='Requesting Another Worktime';
 1586:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1587: 	$title = 'Managing Slots';
 1588:     }
 1589:     &start_page($r,$title);
 1590: 
 1591:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1592: 	&show_table($r,$mgr);
 1593:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1594: 	&remove_registration($r);
 1595:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1596: 	if ($env{'form.entry'} eq 'remove all') {
 1597: 	    &release_all_slot($r,$mgr);
 1598: 	} else {
 1599: 	    &release_slot($r,undef,undef,undef,$mgr);
 1600: 	}
 1601:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1602: 	&delete_slot($r);
 1603:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1604: 	&upload_start($r);
 1605:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1606: 	&csv_upload_map($r);
 1607:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1608: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1609: 	    &csv_upload_assign($r,$mgr);
 1610: 	} else {
 1611: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1612: 		$env{'form.upfile_associate'} = 'reverse';
 1613: 	    } else {
 1614: 		$env{'form.upfile_associate'} = 'forward';
 1615: 	    }
 1616: 	    &csv_upload_map($r);
 1617: 	}
 1618:     } else {
 1619: 	my $symb=&unescape($env{'form.symb'});
 1620: 	if (!defined($symb)) {
 1621: 	    &fail($r,'not_valid');
 1622: 	    return OK;
 1623: 	}
 1624: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1625: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1626: 	if ($useslots ne 'resource' 
 1627: 	    && $useslots ne 'map' 
 1628: 	    && $useslots ne 'map_map') {
 1629: 	    &fail($r,'not_available');
 1630: 	    return OK;
 1631: 	}
 1632: 	$env{'request.symb'}=$symb;
 1633: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1634: 	                               : 'problem';
 1635: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1636: 	if ($status eq 'CAN_ANSWER' ||
 1637: 	    $status eq 'NEEDS_CHECKIN' ||
 1638: 	    $status eq 'WAITING_FOR_GRADE') {
 1639: 	    &fail($r,'not_allowed');
 1640: 	    return OK;
 1641: 	}
 1642: 	if ($env{'form.requestattempt'}) {
 1643: 	    &show_choices($r,$symb);
 1644: 	} elsif ($env{'form.command'} eq 'release') {
 1645: 	    &release_slot($r,$symb);
 1646: 	} elsif ($env{'form.command'} eq 'get') {
 1647: 	    &get_slot($r,$symb);
 1648: 	} elsif ($env{'form.command'} eq 'change') {
 1649: 	    if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1650: 		&release_slot($r,$symb,$env{'form.releaseslot'});
 1651: 	    }
 1652: 	} else {
 1653: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1654: 	}
 1655:     }
 1656:     &end_page($r);
 1657:     return OK;
 1658: }
 1659: 
 1660: 1;
 1661: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>