File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.87: download - view: text, annotated - select for diffs
Mon Feb 2 02:56:12 2009 UTC (15 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Missing &mt().
- Move <tt> </tt> tags outside translatable string in &mt() call.
- xhtml.

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

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