File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.81: download - view: text, annotated - select for diffs
Mon Jan 14 14:32:30 2008 UTC (16 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, HEAD
When using calls to &Apache::lonnet::EXT(), request a scalar explicitly, to avoid similar problems to those eencountered in bug 5578, following the change in lonnet.pm rev 1.927 which enabled EXT() to return the level a parameter is set at, if an array was requested.

    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.81 2008/01/14 14:32:30 raeburn 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 $student = &Apache::lonnet::EXT("resource.0.availablestudent",
  477:                                        $symb,$udom,$uname);
  478:     my @slots = split(/:/,$student);
  479: 
  480:     my @new_slots;
  481:     foreach my $exist_slot (@slots) {
  482: 	if ($exist_slot eq $slot_name) { next; }
  483: 	push(@new_slots,$exist_slot);
  484:     }
  485:     my $new_param = join(':',@new_slots);
  486: 
  487:     my ($cnum,$cdom)=&get_course();
  488: 
  489:     # get slot reservations, check if user has one, if so remove reservation
  490:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  491: 				       "^$slot_name\0");
  492:     foreach my $entry (keys(%consumed)) {
  493: 	if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
  494: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  495: 				 $cdom,$cnum);
  496: 	}
  497:     }
  498: 
  499:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",
  500: 					 $symb,$udom,$uname);
  501:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
  502: 
  503:     if (&Apache::lonnet::error($use_slots)) { 
  504: 	return (0,'error: Unable to determine current status');
  505:     }
  506: 
  507:     my $parm_level = 1;
  508:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
  509: 	$parm_level = 2;
  510:     }
  511:     # store new parameter string
  512:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  513: 						      '0_availablestudent',
  514: 						      $parm_level, $new_param,
  515: 						      'string', $uname, $udom);
  516: 
  517:     my $msg;
  518:     if ($mgr eq 'F') {
  519: 	$msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
  520:     } else {
  521: 	$msg = &mt('Released Reservation: [_1]',$description);
  522:     }
  523:     return (1,$msg);
  524: }
  525: 
  526: sub delete_slot {
  527:     my ($r)=@_;
  528: 
  529:     my $slot_name = $env{'form.slotname'};
  530:     my %slot=&Apache::lonnet::get_slot($slot_name);
  531: 
  532:     my ($cnum,$cdom)=&get_course();
  533:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  534: 				       "^$slot_name\0");
  535:     my ($tmp) = %consumed;
  536:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  537: 
  538:     if (%slot && !%consumed) {
  539: 	$slot{'type'} = 'deleted';
  540: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  541: 					$cdom, $cnum);
  542: 	if ($ret eq 'ok') {
  543: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
  544: 	} else {
  545: 	    $r->print("<p><span class=\"LC_error\"> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</span></p>");
  546: 	}
  547:     } else {
  548: 	if (%consumed) {
  549: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
  550: 	} else {
  551: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
  552: 	}
  553:     }
  554:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  555: 	      &mt('Return to slot list').'</a></p>');
  556:     &return_link($r);
  557: }
  558: 
  559: sub return_link {
  560:     my ($r) = @_;
  561:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  562: 	      &mt('Return to last resource').'</a></p>');
  563: }
  564: 
  565: sub get_slot {
  566:     my ($r,$symb,$conflictable_slot,$inhibit_return_link)=@_;
  567: 
  568:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  569:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
  570: 
  571:     if ($slot_name =~ /^error: (.*)/) {
  572: 	$r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
  573: 	&return_link($r);
  574: 	return 0;
  575:     }
  576:     if ($slot_name && $slot_name ne $conflictable_slot) {
  577: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  578: 	my $description1=&get_description($slot_name,\%slot);
  579: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  580: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  581: 	$r->print("<p>Already have a reservation: $description1</p>");
  582: 	if ($slot_name ne $env{'form.slotname'}) {
  583: 	    $r->print(<<STUFF);
  584: <form method="post" action="/adm/slotrequest">
  585:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  586:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  587:    <input type="hidden" name="releaseslot" value="$slot_name" />
  588:    <input type="hidden" name="command" value="change" />
  589: STUFF
  590:             $r->print("<p>You can either ");
  591: 	    $r->print(<<STUFF);
  592:    <input type="submit" name="change" value="Change" />
  593: STUFF
  594: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  595: 		      $description2.
  596: 		      '</b> <br />or </p>');
  597: 	    &return_link($r);
  598: 	    $r->print(<<STUFF);
  599: </form>
  600: STUFF
  601:         } else {
  602: 	    &return_link($r);
  603: 	}
  604: 	return 0;
  605:     }
  606: 
  607:     my $reserved=&make_reservation($env{'form.slotname'},
  608: 				   \%slot,$symb);
  609:     my $description=&get_description($env{'form.slotname'},\%slot);
  610:     if (defined($reserved)) {
  611: 	my $retvalue = 0;
  612: 	if ($slot_name =~ /^error: (.*)/) {
  613: 	    $r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
  614: 	} elsif ($reserved > -1) {
  615: 	    $r->print("<p>Success: $description</p>");
  616: 	    $retvalue = 1;
  617: 	} elsif ($reserved < 0) {
  618: 	    $r->print("<p>Already reserved: $description</p>");
  619: 	}
  620: 	if (!$inhibit_return_link) { &return_link($r); }
  621: 	return 1;
  622:     }
  623: 
  624:     my %lt=('request'=>"Availibility list",
  625: 	    'try'    =>'Try again');
  626:     %lt=&Apache::lonlocal::texthash(%lt);
  627: 
  628:     my $extra_input;
  629:     if ($conflictable_slot) {
  630: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
  631:     }
  632: 
  633:     $r->print(<<STUFF);
  634: <p> <span class="LC_warning">Failed</span> to reserve a spot for $description. </p>
  635: <p>
  636: <form method="post" action="/adm/slotrequest">
  637:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  638:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  639:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  640:    <input type="hidden" name="command" value="$env{'form.command'}" />
  641:    $extra_input
  642: </form>
  643: ?
  644: </p>
  645: <p>
  646: or
  647: <form method="post" action="/adm/slotrequest">
  648:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  649:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  650: </form>
  651: </p>
  652: or
  653: STUFF
  654: 
  655:     if (!$inhibit_return_link) { &return_link($r); }
  656:     return 0;
  657: }
  658: 
  659: sub allowed_slot {
  660:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
  661: 
  662:     #already started
  663:     if ($slot->{'starttime'} < time) {
  664: 	return 0;
  665:     }
  666:     &Apache::lonxml::debug("$slot_name starttime good");
  667: 
  668:     #already ended
  669:     if ($slot->{'endtime'} < time) {
  670: 	return 0;
  671:     }
  672:     &Apache::lonxml::debug("$slot_name endtime good");
  673: 
  674:     # not allowed to pick this one
  675:     if (defined($slot->{'type'})
  676: 	&& $slot->{'type'} ne 'schedulable_student') {
  677: 	return 0;
  678:     }
  679:     &Apache::lonxml::debug("$slot_name type good");
  680: 
  681:     # reserve time not yet started
  682:     if ($slot->{'startreserve'} > time) {
  683: 	return 0;
  684:     }
  685:     &Apache::lonxml::debug("$slot_name reserve good");
  686: 
  687:     my $userallowed=0;
  688:     # its for a different set of users
  689:     if (defined($slot->{'allowedsections'})) {
  690: 	if (!defined($env{'request.role.sec'})
  691: 	    && grep(/^No section assigned$/,
  692: 		    split(',',$slot->{'allowedsections'}))) {
  693: 	    $userallowed=1;
  694: 	}
  695: 	if (defined($env{'request.role.sec'})
  696: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
  697: 		    split(',',$slot->{'allowedsections'}))) {
  698: 	    $userallowed=1;
  699: 	}
  700: 	if (defined($env{'request.course.groups'})) {
  701: 	    my @groups = split(/:/,$env{'request.course.groups'});
  702: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
  703: 	    foreach my $group (@groups) {
  704: 		if (grep {$_ eq $group} (@allowed_sec)) {
  705: 		    $userallowed=1;
  706: 		    last;
  707: 		}
  708: 	    }
  709: 	}
  710:     }
  711:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
  712: 
  713:     # its for a different set of users
  714:     if (defined($slot->{'allowedusers'})
  715: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
  716: 		split(',',$slot->{'allowedusers'}))) {
  717: 	$userallowed=1;
  718:     }
  719: 
  720:     if (!defined($slot->{'allowedusers'})
  721: 	&& !defined($slot->{'allowedsections'})) {
  722: 	$userallowed=1;
  723:     }
  724: 
  725:     &Apache::lonxml::debug("$slot_name user is $userallowed");
  726:     return 0 if (!$userallowed);
  727: 
  728:     # not allowed for this resource
  729:     if (defined($slot->{'symb'})
  730: 	&& $slot->{'symb'} ne $symb) {
  731: 	return 0;
  732:     }
  733: 
  734:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
  735: 				       $consumed_uniqueperiods);
  736:     if ($conflict) {
  737: 	if ($slots->{$conflict}{'starttime'} < time) {
  738: 	    return 0;
  739: 	}
  740:     }
  741:     &Apache::lonxml::debug("$slot_name symb good");
  742:     return 1;
  743: }
  744: 
  745: sub get_description {
  746:     my ($slot_name,$slot)=@_;
  747:     my $description=$slot->{'description'};
  748:     if (!defined($description)) {
  749: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  750: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  751: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  752:     }
  753:     return $description;
  754: }
  755: 
  756: sub show_choices {
  757:     my ($r,$symb)=@_;
  758: 
  759:     my ($cnum,$cdom)=&get_course();
  760:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  761:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
  762:     my $available;
  763:     $r->print('<table border="1">');
  764:     &Apache::lonxml::debug("Checking Slots");
  765:     my @got_slots=&check_for_reservation($symb,'allslots');
  766:     foreach my $slot (sort 
  767: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  768: 		      (keys(%slots)))  {
  769: 
  770: 	&Apache::lonxml::debug("Checking Slot $slot");
  771: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
  772: 				$consumed_uniqueperiods));
  773: 
  774: 	$available++;
  775: 
  776: 	my $description=&get_description($slot,$slots{$slot});
  777: 
  778: 	my $form=&mt('Unavailable');
  779: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
  780: 	    &space_available($slot,$slots{$slot},$symb)) {
  781: 	    my $text=&mt('Select');
  782: 	    my $command='get';
  783: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
  784: 		$text=&mt('Drop Reservation');
  785: 		$command='release';
  786: 	    } else {
  787: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
  788: 						   \%slots,
  789: 						   $consumed_uniqueperiods);
  790: 		if ($conflict) {
  791: 		    $text=&mt('Change Reservation');
  792: 		    $command='get';
  793: 		}
  794: 	    }
  795: 	    my $escsymb=&escape($symb);
  796: 	    $form=<<STUFF;
  797:    <form method="post" action="/adm/slotrequest">
  798:      <input type="submit" name="Select" value="$text" />
  799:      <input type="hidden" name="symb" value="$escsymb" />
  800:      <input type="hidden" name="slotname" value="$slot" />
  801:      <input type="hidden" name="command" value="$command" />
  802:    </form>
  803: STUFF
  804: 	}
  805: 	$r->print(<<STUFF);
  806: <tr>
  807:  <td>$form</td>
  808:  <td>$description</td>
  809: </tr>
  810: STUFF
  811:     }
  812: 
  813:     if (!$available) {
  814: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  815: 		  &mt('Return to last resource').'</a></td></tr>');
  816:     }
  817:     $r->print('</table>');
  818: }
  819: 
  820: sub to_show {
  821:     my ($slotname,$slot,$when,$deleted,$name) = @_;
  822:     my $time=time;
  823:     my $week=60*60*24*7;
  824: 
  825:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  826: 	return 0;
  827:     }
  828: 
  829:     if ($name && $name->{'value'} =~ /\w/) {
  830: 	if ($name->{'type'} eq 'substring') {
  831: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
  832: 		return 0;
  833: 	    }
  834: 	}
  835: 	if ($name->{'type'} eq 'exact') {
  836: 	    if ($slotname eq $name->{'value'}) {
  837: 		return 0;
  838: 	    }
  839: 	}
  840:     }
  841: 
  842:     if ($when eq 'any') {
  843: 	return 1;
  844:     } elsif ($when eq 'now') {
  845: 	if ($time > $slot->{'starttime'} &&
  846: 	    $time < $slot->{'endtime'}) {
  847: 	    return 1;
  848: 	}
  849: 	return 0;
  850:     } elsif ($when eq 'nextweek') {
  851: 	if ( ($time        < $slot->{'starttime'} &&
  852: 	      ($time+$week) > $slot->{'starttime'})
  853: 	     ||
  854: 	     ($time        < $slot->{'endtime'} &&
  855: 	      ($time+$week) > $slot->{'endtime'}) ) {
  856: 	    return 1;
  857: 	}
  858: 	return 0;
  859:     } elsif ($when eq 'lastweek') {
  860: 	if ( ($time        > $slot->{'starttime'} &&
  861: 	      ($time-$week) < $slot->{'starttime'})
  862: 	     ||
  863: 	     ($time        > $slot->{'endtime'} &&
  864: 	      ($time-$week) < $slot->{'endtime'}) ) {
  865: 	    return 1;
  866: 	}
  867: 	return 0;
  868:     } elsif ($when eq 'willopen') {
  869: 	if ($time < $slot->{'starttime'}) {
  870: 	    return 1;
  871: 	}
  872: 	return 0;
  873:     } elsif ($when eq 'wereopen') {
  874: 	if ($time > $slot->{'endtime'}) {
  875: 	    return 1;
  876: 	}
  877: 	return 0;
  878:     }
  879:     
  880:     return 1;
  881: }
  882: 
  883: sub remove_link {
  884:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
  885: 
  886:     my $remove = &mt('Remove');
  887: 
  888:     if ($entry eq 'remove all') {
  889: 	$remove = &mt('Remove All');
  890: 	undef($uname);
  891: 	undef($udom);
  892:     }
  893: 
  894:     $slotname  = &escape($slotname);
  895:     $entry     = &escape($entry);
  896:     $uname     = &escape($uname);
  897:     $udom      = &escape($udom);
  898:     $symb      = &escape($symb);
  899: 
  900:     return <<"END_LINK";
  901:  <a href="/adm/slotrequest?command=remove_registration&amp;slotname=$slotname&amp;entry=$entry&amp;uname=$uname&amp;udom=$udom&amp;symb=$symb"
  902:    >($remove)</a>
  903: END_LINK
  904: 
  905: }
  906: 
  907: sub show_table {
  908:     my ($r,$mgr)=@_;
  909: 
  910:     my ($cnum,$cdom)=&get_course();
  911:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  912:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  913: 	undef(%slots);
  914:     } 
  915:     my $available;
  916:     if ($mgr eq 'F') {
  917:     # FIXME: This line should be deleted once Slots uses breadcrumbs
  918:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
  919: 
  920: 	$r->print('<div>');
  921: 	$r->print('<form method="post" action="/adm/slotrequest">
  922: <input type="hidden" name="command" value="uploadstart" />
  923: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  924: </form>');
  925: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
  926: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
  927: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  928: </form>');
  929: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
  930: 	$r->print('</div>');
  931:     }
  932:     
  933:     my %Saveable_Parameters = ('show'              => 'array',
  934: 			       'when'              => 'scalar',
  935: 			       'order'             => 'scalar',
  936: 			       'deleted'           => 'scalar',
  937: 			       'name_filter_type'  => 'scalar',
  938: 			       'name_filter_value' => 'scalar',
  939: 			       );
  940:     &Apache::loncommon::store_course_settings('slotrequest',
  941: 					      \%Saveable_Parameters);
  942:     &Apache::loncommon::restore_course_settings('slotrequest',
  943: 						\%Saveable_Parameters);
  944:     &Apache::grades::init_perm();
  945:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
  946:     &Apache::grades::reset_perm();
  947: 
  948:     # what to display filtering
  949:     my %show_fields=&Apache::lonlocal::texthash(
  950: 	     'name'            => 'Slot Name',
  951: 	     'description'     => 'Description',
  952: 	     'type'            => 'Type',
  953: 	     'starttime'       => 'Start time',
  954: 	     'endtime'         => 'End Time',
  955:              'startreserve'    => 'Time students can start reserving',
  956: 	     'secret'          => 'Secret Word',
  957: 	     'space'           => '# of students/max',
  958: 	     'ip'              => 'IP or DNS restrictions',
  959: 	     'symb'            => 'Resource slot is restricted to.',
  960: 	     'allowedsections' => 'Sections slot is restricted to.',
  961: 	     'allowedusers'    => 'Users slot is restricted to.',
  962: 	     'uniqueperiod'    => 'Period of time slot is unique',
  963: 	     'scheduled'       => 'Scheduled Students',
  964: 	     'proctor'         => 'List of proctors');
  965:     my @show_order=('name','description','type','starttime','endtime',
  966: 		    'startreserve','secret','space','ip','symb',
  967: 		    'allowedsections','allowedusers','uniqueperiod',
  968: 		    'scheduled','proctor');
  969:     my @show = 
  970: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  971: 	                            : keys(%show_fields);
  972:     my %show =  map { $_ => 1 } (@show);
  973: 
  974:     #when filtering setup
  975:     my %when_fields=&Apache::lonlocal::texthash(
  976: 	     'now'      => 'Open now',
  977: 	     'nextweek' => 'Open within the next week',
  978: 	     'lastweek' => 'Were open last week',
  979: 	     'willopen' => 'Will open later',
  980: 	     'wereopen' => 'Were open',
  981: 	     'any'      => 'Anytime',
  982: 						);
  983:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
  984:     $when_fields{'select_form_order'} = \@when_order;
  985:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
  986:                                             : 'now';
  987: 
  988:     #display of students setup
  989:     my %stu_display_fields=
  990: 	&Apache::lonlocal::texthash('username' => 'User name',
  991: 				    'fullname' => 'Full name',
  992: 				    );
  993:     my @stu_display_order=('fullname','username');
  994:     my @stu_display = 
  995: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
  996: 	                                  : keys(%stu_display_fields);
  997:     my %stu_display =  map { $_ => 1 } (@stu_display);
  998: 
  999:     #name filtering setup
 1000:     my %name_filter_type_fields=
 1001: 	&Apache::lonlocal::texthash('substring' => 'Substring',
 1002: 				    'exact'     => 'Exact',
 1003: 				    #'reg'       => 'Regular Expression',
 1004: 				    );
 1005:     my @name_filter_type_order=('substring','exact');
 1006: 
 1007:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
 1008:     my $name_filter_type = 
 1009: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
 1010:                                                 : 'substring';
 1011:     my $name_filter = {'type'  => $name_filter_type,
 1012: 		       'value' => $env{'form.name_filter_value'},};
 1013: 
 1014:     
 1015:     #deleted slot filtering
 1016:     #default to hide if no value
 1017:     $env{'form.deleted'} ||= 'hide';
 1018:     my $hide_radio = 
 1019: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
 1020:     my $show_radio = 
 1021: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
 1022: 	
 1023:     $r->print('<form method="post" action="/adm/slotrequest">
 1024: <input type="hidden" name="command" value="showslots" />');
 1025:     $r->print('<div>');
 1026:     $r->print('<table class="inline">
 1027:       <tr><th>'.&mt('Show').'</th>
 1028:           <th>'.&mt('Student Display').'</th>
 1029:           <th>'.&mt('Open').'</th>
 1030:           <th>'.&mt('Slot Name Filter').'</th>
 1031:           <th>'.&mt('Options').'</th>
 1032:       </tr>
 1033:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
 1034: 	      '</td>
 1035:            <td>
 1036:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
 1037: 						    6,\%stu_display_fields,
 1038: 						    \@stu_display_order).'
 1039:            </td>
 1040:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
 1041:           '</td>
 1042:            <td>'.&Apache::loncommon::select_form($name_filter_type,
 1043: 						 'name_filter_type',
 1044: 						 %name_filter_type_fields).
 1045: 	      '<br />'.
 1046: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
 1047: 					      $env{'form.name_filter_value'},
 1048: 					      15).
 1049:           '</td>
 1050:            <td>
 1051:             <table>
 1052:               <tr>
 1053:                 <td rowspan="2">Deleted slots:</td>
 1054:                 <td><label>'.$show_radio.'Show</label></td>
 1055:               </tr>
 1056:               <tr>
 1057:                 <td><label>'.$hide_radio.'Hide</label></td>
 1058:               </tr>
 1059:             </table>
 1060: 	  </td>
 1061:        </tr>
 1062:     </table>');
 1063:     $r->print('</div>');
 1064:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
 1065:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
 1066:     $r->print(&Apache::loncommon::start_data_table().
 1067: 	      &Apache::loncommon::start_data_table_header_row().'
 1068: 	       <th></th>');
 1069:     foreach my $which (@show_order) {
 1070: 	if ($which ne 'proctor' && exists($show{$which})) {
 1071: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
 1072: 	}
 1073:     }
 1074:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1075: 
 1076:     my %name_cache;
 1077:     my $slotsort = sub {
 1078: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
 1079: 	    if (lc($slots{$a}->{$env{'form.order'}})
 1080: 		ne lc($slots{$b}->{$env{'form.order'}})) {
 1081: 		return (lc($slots{$a}->{$env{'form.order'}}) 
 1082: 			cmp lc($slots{$b}->{$env{'form.order'}}));
 1083: 	    }
 1084: 	} elsif ($env{'form.order'} eq 'space') {
 1085: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
 1086: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
 1087: 	    }
 1088: 	} elsif ($env{'form.order'} eq 'name') {
 1089: 	    if (lc($a) cmp lc($b)) {
 1090: 		return lc($a) cmp lc($b);
 1091: 	    }
 1092: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
 1093: 	    
 1094: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
 1095: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
 1096: 		return ($slots{$a}->{'uniqueperiod'}[0]
 1097: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
 1098: 	    }
 1099: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
 1100: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
 1101: 		return ($slots{$a}->{'uniqueperiod'}[1]
 1102: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
 1103: 	    }
 1104: 	}
 1105: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
 1106:     };
 1107: 
 1108:     my %consumed;
 1109:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1110: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
 1111: 	my ($tmp)=%consumed;
 1112: 	if ($tmp =~ /^error: /) { undef(%consumed); }
 1113:     }
 1114: 
 1115:     foreach my $slot (sort $slotsort (keys(%slots)))  {
 1116: 	if (!&to_show($slot,$slots{$slot},$when,
 1117: 		      $env{'form.deleted'},$name_filter)) { next; }
 1118: 	if (defined($slots{$slot}->{'type'})
 1119: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
 1120: 	    #next;
 1121: 	}
 1122: 	my $description=&get_description($slot,$slots{$slot});
 1123: 	my ($id_count,$ids);
 1124: 	    
 1125: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1126: 	    my $re_str = "$slot\0";
 1127: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
 1128: 	    $id_count = scalar(@this_slot);
 1129: 	    if (exists($show{'scheduled'})) {
 1130: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
 1131: 					      $consumed{$b}{name} }
 1132: 				   (@this_slot)) {
 1133: 		    my (undef,$id)=split("\0",$entry);
 1134: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
 1135: 		    $ids.= '<nobr>';
 1136: 		    foreach my $item (@stu_display_order) {
 1137: 			if ($stu_display{$item}) {
 1138: 			    if ($item eq 'fullname') {
 1139: 				$ids.=$fullname->{"$uname:$udom"}.' ';
 1140: 			    } elsif ($item eq 'username') {
 1141: 				$ids.="<tt>$uname:$udom</tt> ";
 1142: 			    }
 1143: 			}
 1144: 		    }
 1145: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
 1146: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
 1147: 		}
 1148: 	    }
 1149: 	}
 1150: 
 1151: 	my $start=($slots{$slot}->{'starttime'}?
 1152: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
 1153: 	my $end=($slots{$slot}->{'endtime'}?
 1154: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
 1155: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
 1156: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
 1157: 	
 1158: 	my $unique;
 1159: 	if (ref($slots{$slot}{'uniqueperiod'})) {
 1160: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
 1161: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
 1162: 	}
 1163: 
 1164: 	my $title;
 1165: 	if (exists($slots{$slot}{'symb'})) {
 1166: 	    my (undef,undef,$res)=
 1167: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
 1168: 	    $res =   &Apache::lonnet::clutter($res);
 1169: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
 1170: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
 1171: 	}
 1172: 
 1173: 	my $allowedsections;
 1174: 	if (exists($show{'allowedsections'})) {
 1175: 	    $allowedsections = 
 1176: 		join(', ',sort(split(/\s*,\s*/,
 1177: 				     $slots{$slot}->{'allowedsections'})));
 1178: 	}
 1179: 
 1180: 	my @allowedusers;
 1181: 	if (exists($show{'allowedusers'})) {
 1182: 	    @allowedusers= map {
 1183: 		my ($uname,$udom)=split(/:/,$_);
 1184: 		my $fullname=$name_cache{$_};
 1185: 		if (!defined($fullname)) {
 1186: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1187: 		    $fullname =~s/\s/&nbsp;/g;
 1188: 		    $name_cache{$_} = $fullname;
 1189: 		}
 1190: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1191: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
 1192: 	}
 1193: 	my $allowedusers=join(', ',@allowedusers);
 1194: 	
 1195: 	my @proctors;
 1196: 	my $rowspan=1;
 1197: 	my $colspan=1;
 1198: 	if (exists($show{'proctor'})) {
 1199: 	    $rowspan=2;
 1200: 	    @proctors= map {
 1201: 		my ($uname,$udom)=split(/:/,$_);
 1202: 		my $fullname=$name_cache{$_};
 1203: 		if (!defined($fullname)) {
 1204: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1205: 		    $fullname =~s/\s/&nbsp;/g;
 1206: 		    $name_cache{$_} = $fullname;
 1207: 		}
 1208: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1209: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
 1210: 	}
 1211: 	my $proctors=join(', ',@proctors);
 1212: 
 1213: 	my $edit=(<<"EDITLINK");
 1214: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
 1215: EDITLINK
 1216: 
 1217: 	my $delete=(<<"DELETELINK");
 1218: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
 1219: DELETELINK
 1220: 
 1221:         my $remove_all=&remove_link($slot,'remove all').'<br />';
 1222: 
 1223:         if ($ids ne '') { undef($delete); }
 1224: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
 1225: 	    || $ids eq '') { 
 1226: 	    undef($remove_all);
 1227: 	}
 1228: 
 1229: 	my $row_start=&Apache::loncommon::start_data_table_row();
 1230: 	my $row_end=&Apache::loncommon::end_data_table_row();
 1231:         $r->print($row_start.
 1232: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
 1233: 	if (exists($show{'name'})) {
 1234: 	    $colspan++;$r->print("<td>$slot</td>");
 1235: 	}
 1236: 	if (exists($show{'description'})) {
 1237: 	    $colspan++;$r->print("<td>$description</td>\n");
 1238: 	}
 1239: 	if (exists($show{'type'})) {
 1240: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
 1241: 	}
 1242: 	if (exists($show{'starttime'})) {
 1243: 	    $colspan++;$r->print("<td>$start</td>\n");
 1244: 	}
 1245: 	if (exists($show{'endtime'})) {
 1246: 	    $colspan++;$r->print("<td>$end</td>\n");
 1247: 	}
 1248: 	if (exists($show{'startreserve'})) {
 1249: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
 1250: 	}
 1251: 	if (exists($show{'secret'})) {
 1252: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
 1253: 	}
 1254: 	if (exists($show{'space'})) {
 1255: 	    my $display = $id_count;
 1256: 	    if ($slots{$slot}{'maxspace'}>0) {
 1257: 		$display.='/'.$slots{$slot}{'maxspace'};
 1258: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
 1259: 		    $display = '<strong>'.$display.' (full) </strong>';
 1260: 		}
 1261: 	    }
 1262: 	    $colspan++;$r->print("<td>$display</td>\n");
 1263: 	}
 1264: 	if (exists($show{'ip'})) {
 1265: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
 1266: 	}
 1267: 	if (exists($show{'symb'})) {
 1268: 	    $colspan++;$r->print("<td>$title</td>\n");
 1269: 	}
 1270: 	if (exists($show{'allowedsections'})) {
 1271: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
 1272: 	}
 1273: 	if (exists($show{'allowedusers'})) {
 1274: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
 1275: 	}
 1276: 	if (exists($show{'uniqueperiod'})) {
 1277: 	    $colspan++;$r->print("<td>$unique</td>\n");
 1278: 	}
 1279: 	if (exists($show{'scheduled'})) {
 1280: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
 1281: 	}
 1282: 	$r->print("$row_end\n");
 1283: 	if (exists($show{'proctor'})) {
 1284: 	    $r->print(<<STUFF);
 1285: $row_start
 1286:  <td colspan="$colspan">$proctors</td>
 1287: $row_end
 1288: STUFF
 1289:         }
 1290:     }
 1291:     $r->print('</table></form>');
 1292: }
 1293: 
 1294: sub upload_start {
 1295:     my ($r)=@_;    
 1296:     $r->print(&Apache::grades::checkforfile_js());
 1297:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
 1298:     $result.='&nbsp;<b>'.
 1299: 	&mt('Specify a file containing the slot definitions.').
 1300: 	'</b></td></tr>'."\n";
 1301:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 1302:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1303:     my $ignore=&mt('Ignore First Line');
 1304:     $result.=<<ENDUPFORM;
 1305: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1306: <input type="hidden" name="command" value="csvuploadmap" />
 1307: $upfile_select
 1308: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
 1309: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 1310: </form>
 1311: ENDUPFORM
 1312:     $result.='</td></tr></table>'."\n";
 1313:     $result.='</td></tr></table>'."\n";
 1314:     $r->print($result);
 1315: }
 1316: 
 1317: sub csvuploadmap_header {
 1318:     my ($r,$datatoken,$distotal)= @_;
 1319:     my $javascript;
 1320:     if ($env{'form.upfile_associate'} eq 'reverse') {
 1321: 	$javascript=&csvupload_javascript_reverse_associate();
 1322:     } else {
 1323: 	$javascript=&csvupload_javascript_forward_associate();
 1324:     }
 1325: 
 1326:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 1327:     my $ignore=&mt('Ignore First Line');
 1328: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
 1329: 
 1330:     $r->print(<<ENDPICK);
 1331: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1332: <h3>Identify fields $help_field</h3>	
 1333: Total number of records found in file: $distotal <hr />
 1334: Enter as many fields as you can. The system will inform you and bring you back
 1335: to this page if the data selected is insufficient to create the slots.<hr />
 1336: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 1337: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 1338: <input type="hidden" name="associate"  value="" />
 1339: <input type="hidden" name="datatoken"  value="$datatoken" />
 1340: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 1341: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 1342: <input type="hidden" name="upfile_associate" 
 1343:                                        value="$env{'form.upfile_associate'}" />
 1344: <input type="hidden" name="command"    value="csvuploadassign" />
 1345: <hr />
 1346: <script type="text/javascript" language="Javascript">
 1347: $javascript
 1348: </script>
 1349: ENDPICK
 1350:     return '';
 1351: 
 1352: }
 1353: 
 1354: sub csvuploadmap_footer {
 1355:     my ($request,$i,$keyfields) =@_;
 1356:     $request->print(<<ENDPICK);
 1357: </table>
 1358: <input type="hidden" name="nfields" value="$i" />
 1359: <input type="hidden" name="keyfields" value="$keyfields" />
 1360: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
 1361: </form>
 1362: ENDPICK
 1363: }
 1364: 
 1365: sub csvupload_javascript_reverse_associate {
 1366:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1367:     return(<<ENDPICK);
 1368:   function verify(vf) {
 1369:     var foundstart=0;
 1370:     var foundend=0;
 1371:     var foundname=0;
 1372:     var foundtype=0;
 1373:     for (i=0;i<=vf.nfields.value;i++) {
 1374:       tw=eval('vf.f'+i+'.selectedIndex');
 1375:       if (i==0 && tw!=0) { foundname=1; }
 1376:       if (i==1 && tw!=0) { foundtype=1; }
 1377:       if (i==2 && tw!=0) { foundstat=1; }
 1378:       if (i==3 && tw!=0) { foundend=1; }
 1379:     }
 1380:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1381: 	alert('$error1');
 1382: 	return;
 1383:     }
 1384:     vf.submit();
 1385:   }
 1386:   function flip(vf,tf) {
 1387:   }
 1388: ENDPICK
 1389: }
 1390: 
 1391: sub csvupload_javascript_forward_associate {
 1392:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1393:   return(<<ENDPICK);
 1394:   function verify(vf) {
 1395:     var foundstart=0;
 1396:     var foundend=0;
 1397:     var foundname=0;
 1398:     var foundtype=0;
 1399:     for (i=0;i<=vf.nfields.value;i++) {
 1400:       tw=eval('vf.f'+i+'.selectedIndex');
 1401:       if (tw==1) { foundname=1; }
 1402:       if (tw==2) { foundtype=1; }
 1403:       if (tw==3) { foundstat=1; }
 1404:       if (tw==4) { foundend=1; }
 1405:     }
 1406:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1407: 	alert('$error1');
 1408: 	return;
 1409:     }
 1410:     vf.submit();
 1411:   }
 1412:   function flip(vf,tf) {
 1413:   }
 1414: ENDPICK
 1415: }
 1416: 
 1417: sub csv_upload_map {
 1418:     my ($r)= @_;
 1419: 
 1420:     my $datatoken;
 1421:     if (!$env{'form.datatoken'}) {
 1422: 	$datatoken=&Apache::loncommon::upfile_store($r);
 1423:     } else {
 1424: 	$datatoken=$env{'form.datatoken'};
 1425: 	&Apache::loncommon::load_tmp_file($r);
 1426:     }
 1427:     my @records=&Apache::loncommon::upfile_record_sep();
 1428:     if ($env{'form.noFirstLine'}) { shift(@records); }
 1429:     &csvuploadmap_header($r,$datatoken,$#records+1);
 1430:     my ($i,$keyfields);
 1431:     if (@records) {
 1432: 	my @fields=&csvupload_fields();
 1433: 
 1434: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 1435: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 1436: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 1437: 							  \@fields);
 1438: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 1439: 	    chop($keyfields);
 1440: 	} else {
 1441: 	    unshift(@fields,['none','']);
 1442: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1443: 							    \@fields);
 1444: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1445: 	    $keyfields=join(',',sort(keys(%sone)));
 1446: 	}
 1447:     }
 1448:     &csvuploadmap_footer($r,$i,$keyfields);
 1449: 
 1450:     return '';
 1451: }
 1452: 
 1453: sub csvupload_fields {
 1454:     return (['name','Slot name'],
 1455: 	    ['type','Type of slot'],
 1456: 	    ['starttime','Start Time of slot'],
 1457: 	    ['endtime','End Time of slot'],
 1458: 	    ['startreserve','Reservation Start Time'],
 1459: 	    ['ip','IP or DNS restriction'],
 1460: 	    ['proctor','List of proctor ids'],
 1461: 	    ['description','Slot Description'],
 1462: 	    ['maxspace','Maximum number of reservations'],
 1463: 	    ['symb','Resource Restriction'],
 1464: 	    ['uniqueperiod','Date range of slot exclusion'],
 1465: 	    ['secret','Secret word proctor uses to validate'],
 1466: 	    ['allowedsections','Sections slot is restricted to'],
 1467: 	    ['allowedusers','Users slot is restricted to'],
 1468: 	    );
 1469: }
 1470: 
 1471: sub csv_upload_assign {
 1472:     my ($r,$mgr)= @_;
 1473:     &Apache::loncommon::load_tmp_file($r);
 1474:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1475:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1476:     my %fields=&Apache::grades::get_fields();
 1477:     $r->print('<h3>Creating Slots</h3>');
 1478:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1479:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1480:     my $countdone=0;
 1481:     my @errors;
 1482:     foreach my $slot (@slotdata) {
 1483: 	my %slot;
 1484: 	my %entries=&Apache::loncommon::record_sep($slot);
 1485: 	my $domain;
 1486: 	my $name=$entries{$fields{'name'}};
 1487: 	if ($name=~/^\s*$/) {
 1488: 	    push(@errors,"Did not create slot with no name");
 1489: 	    next;
 1490: 	}
 1491: 	if ($name=~/\s/) { 
 1492: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1493: 	    next;
 1494: 	}
 1495: 	if ($name=~/\W/) { 
 1496: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1497: 	    next;
 1498: 	}
 1499: 	if ($entries{$fields{'type'}}) {
 1500: 	    $slot{'type'}=$entries{$fields{'type'}};
 1501: 	} else {
 1502: 	    $slot{'type'}='preassigned';
 1503: 	}
 1504: 	if ($slot{'type'} ne 'preassigned' &&
 1505: 	    $slot{'type'} ne 'schedulable_student') {
 1506: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1507: 	    next;
 1508: 	}
 1509: 	if ($entries{$fields{'starttime'}}) {
 1510: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1511: 	}
 1512: 	if ($entries{$fields{'endtime'}}) {
 1513: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1514: 	}
 1515: 
 1516: 	# start/endtime must be defined and greater than zero
 1517: 	if (!$slot{'starttime'}) {
 1518: 	    push(@errors,"$name not created -- Invalid start time");
 1519: 	    next;
 1520: 	}
 1521: 	if (!$slot{'endtime'}) {
 1522: 	    push(@errors,"$name not created -- Invalid end time");
 1523: 	    next;
 1524: 	}
 1525: 	if ($slot{'starttime'} > $slot{'endtime'}) {
 1526: 	    push(@errors,"$name not created -- Slot starts after it ends");
 1527: 	    next;
 1528: 	}
 1529: 
 1530: 	if ($entries{$fields{'startreserve'}}) {
 1531: 	    $slot{'startreserve'}=
 1532: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1533: 	}
 1534: 	if (defined($slot{'startreserve'})
 1535: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
 1536: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
 1537: 	    next;
 1538: 	}
 1539: 
 1540: 	foreach my $key ('ip','proctor','description','maxspace',
 1541: 			 'secret','symb') {
 1542: 	    if ($entries{$fields{$key}}) {
 1543: 		$slot{$key}=$entries{$fields{$key}};
 1544: 	    }
 1545: 	}
 1546: 
 1547: 	if ($entries{$fields{'uniqueperiod'}}) {
 1548: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1549: 	    my @times=(&UnixDate($start,"%s"),
 1550: 		       &UnixDate($end,"%s"));
 1551: 	    $slot{'uniqueperiod'}=\@times;
 1552: 	}
 1553: 	if (defined($slot{'uniqueperiod'})
 1554: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
 1555: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
 1556: 	    next;
 1557: 	}
 1558: 
 1559: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1560: 	$r->print('.');
 1561: 	$r->rflush();
 1562: 	$countdone++;
 1563:     }
 1564:     $r->print("<p>Created $countdone slots\n</p>");
 1565:     foreach my $error (@errors) {
 1566: 	$r->print("<p><span class=\"LC_warning\">$error</span></p>\n");
 1567:     }
 1568:     &show_table($r,$mgr);
 1569:     return '';
 1570: }
 1571: 
 1572: sub handler {
 1573:     my $r=shift;
 1574: 
 1575:     &Apache::loncommon::content_type($r,'text/html');
 1576:     &Apache::loncommon::no_cache($r);
 1577:     if ($r->header_only()) {
 1578: 	$r->send_http_header();
 1579: 	return OK;
 1580:     }
 1581: 
 1582:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1583:     
 1584:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1585:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1586:     my $title='Requesting Another Worktime';
 1587:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1588: 	$title = 'Managing Slots';
 1589:     }
 1590:     &start_page($r,$title);
 1591: 
 1592:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1593: 	&show_table($r,$mgr);
 1594:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1595: 	&remove_registration($r);
 1596:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1597: 	if ($env{'form.entry'} eq 'remove all') {
 1598: 	    &release_all_slot($r,$mgr);
 1599: 	} else {
 1600: 	    &release_slot($r,undef,undef,undef,$mgr);
 1601: 	}
 1602:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1603: 	&delete_slot($r);
 1604:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1605: 	&upload_start($r);
 1606:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1607: 	&csv_upload_map($r);
 1608:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1609: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1610: 	    &csv_upload_assign($r,$mgr);
 1611: 	} else {
 1612: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1613: 		$env{'form.upfile_associate'} = 'reverse';
 1614: 	    } else {
 1615: 		$env{'form.upfile_associate'} = 'forward';
 1616: 	    }
 1617: 	    &csv_upload_map($r);
 1618: 	}
 1619:     } else {
 1620: 	my $symb=&unescape($env{'form.symb'});
 1621: 	if (!defined($symb)) {
 1622: 	    &fail($r,'not_valid');
 1623: 	    return OK;
 1624: 	}
 1625: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1626: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1627: 	if ($useslots ne 'resource' 
 1628: 	    && $useslots ne 'map' 
 1629: 	    && $useslots ne 'map_map') {
 1630: 	    &fail($r,'not_available');
 1631: 	    return OK;
 1632: 	}
 1633: 	$env{'request.symb'}=$symb;
 1634: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1635: 	                               : 'problem';
 1636: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1637: 	if ($status eq 'CAN_ANSWER' ||
 1638: 	    $status eq 'NEEDS_CHECKIN' ||
 1639: 	    $status eq 'WAITING_FOR_GRADE') {
 1640: 	    &fail($r,'not_allowed');
 1641: 	    return OK;
 1642: 	}
 1643: 	if ($env{'form.requestattempt'}) {
 1644: 	    &show_choices($r,$symb);
 1645: 	} elsif ($env{'form.command'} eq 'release') {
 1646: 	    &release_slot($r,$symb);
 1647: 	} elsif ($env{'form.command'} eq 'get') {
 1648: 	    &get_slot($r,$symb);
 1649: 	} elsif ($env{'form.command'} eq 'change') {
 1650: 	    if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1651: 		&release_slot($r,$symb,$env{'form.releaseslot'});
 1652: 	    }
 1653: 	} else {
 1654: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1655: 	}
 1656:     }
 1657:     &end_page($r);
 1658:     return OK;
 1659: }
 1660: 
 1661: 1;
 1662: __END__

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