File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.48.2.2: download - view: text, annotated - select for diffs
Thu Mar 30 04:37:22 2006 UTC (18 years, 3 months ago) by albertel
Branches: version_2_1_X
- backport 1.54 1.55

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

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