File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.48: download - view: text, annotated - select for diffs
Sun Feb 26 19:40:14 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, HEAD
- unique periods need tobe check across the whole course

    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 2006/02/26 19:40:14 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:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
  319: 					     $env{'form.udom'});
  320: 
  321:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
  322: 
  323:     my $hidden_input;
  324:     foreach my $parm ('uname','udom','slotname','entry','symb') {
  325: 	$hidden_input .=
  326: 	    '<input type="hidden" name="'.$parm.'" value="'
  327: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
  328:     }
  329:     $r->print(<<"END_CONFIRM");
  330: <p> Remove $name from slot $env{'form.slotname'} for $title</p>
  331: <form action="/adm/slotrequest" method="POST">
  332:     <input type="hidden" name="command" value="release" />
  333:     $hidden_input
  334:     <input type="submit" name="Yes" value="yes" />
  335: </form>
  336: <form action="/adm/slotrequest" method="POST">
  337:     <input type="hidden" name="command" value="showslots" />
  338:     <input type="submit" name="No" value="no" />
  339: </form>
  340: END_CONFIRM
  341: 
  342: }
  343: 
  344: sub release_slot {
  345:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
  346: 
  347:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  348:     my ($cnum,$cdom)=&get_course();
  349: 
  350:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
  351:     if ($mgr eq 'F' 
  352: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
  353: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
  354:     }
  355: 
  356:     if ($mgr eq 'F' 
  357: 	&& defined($env{'form.symb'})) {
  358: 	$symb = $env{'form.symb'};
  359:     }
  360:     my %slot=&Apache::lonnet::get_slot($slot_name);
  361:     my $description=&get_description($env{'form.slotname'},\%slot);
  362: 
  363:     if ($mgr ne 'F') {
  364: 	if ($slot{'starttime'} < time) {
  365: 	    $r->print("<p>Not allowed to release Reservation: $description, as it has already ended.  </p>");
  366: 	    &return_link($r);
  367: 	    return 0;
  368: 	}
  369:     }
  370:     # get parameter string, check for existance, rebuild string with the slot
  371:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  372: 					       $symb,$udom,$uname));
  373: 
  374:     my @new_slots;
  375:     foreach my $exist_slot (@slots) {
  376: 	if ($exist_slot eq $slot_name) { next; }
  377: 	push(@new_slots,$exist_slot);
  378:     }
  379:     my $new_param = join(':',@new_slots);
  380: 
  381:     # get slot reservations, check if user has one, if so remove reservation
  382:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  383: 				       "^$slot_name\0");
  384:     foreach my $entry (keys(%consumed)) {
  385: 	if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) {
  386: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  387: 				 $cdom,$cnum);
  388: 	}
  389:     }
  390: 
  391:     # store new parameter string
  392:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  393: 						      '0_availablestudent',
  394: 						      1, $new_param, 'string',
  395: 						      $uname,$udom);
  396:     $r->print("<p>Released Reservation: $description</p>");
  397:     if ($mgr eq 'F') {
  398: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  399: 		  &mt('Return to slot list').'</a></p>');
  400:     }
  401:     if (!$inhibit_return_link) { &return_link($r);  }
  402:     return 1;
  403: }
  404: 
  405: sub delete_slot {
  406:     my ($r)=@_;
  407: 
  408:     my $slot_name = $env{'form.slotname'};
  409:     my %slot=&Apache::lonnet::get_slot($slot_name);
  410: 
  411:     my ($cnum,$cdom)=&get_course();
  412:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  413: 				       "^$slot_name\0");
  414:     my ($tmp) = %consumed;
  415:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  416: 
  417:     if (%slot && !%consumed) {
  418: 	$slot{'type'} = 'deleted';
  419: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  420: 					$cdom, $cnum);
  421: 	if ($ret eq 'ok') {
  422: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
  423: 	} else {
  424: 	    $r->print("<p> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</p>");
  425: 	}
  426:     } else {
  427: 	if (%consumed) {
  428: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
  429: 	} else {
  430: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
  431: 	}
  432:     }
  433:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  434: 	      &mt('Return to slot list').'</a></p>');
  435:     &return_link($r);
  436: }
  437: 
  438: sub return_link {
  439:     my ($r) = @_;
  440:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  441: 	      &mt('Return to last resource').'</a></p>');
  442: }
  443: 
  444: sub get_slot {
  445:     my ($r,$symb)=@_;
  446: 
  447:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  448:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
  449: 
  450:     if ($slot_name =~ /^error: (.*)/) {
  451: 	$r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
  452: 	&return_link($r);
  453: 	return;
  454:     }
  455:     if ($slot_name) {
  456: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  457: 	my $description1=&get_description($slot_name,\%slot);
  458: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  459: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  460: 	$r->print("<p>Already have a reservation: $description1</p>");
  461: 	if ($slot_name ne $env{'form.slotname'}) {
  462: 	    $r->print(<<STUFF);
  463: <form method="POST" action="/adm/slotrequest">
  464:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  465:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  466:    <input type="hidden" name="releaseslot" value="$slot_name" />
  467:    <input type="hidden" name="command" value="change" />
  468: STUFF
  469:             $r->print("<p>You can either ");
  470: 	    $r->print(<<STUFF);
  471:    <input type="submit" name="change" value="Change" />
  472: STUFF
  473: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  474: 		      $description2.
  475: 		      '</b> <br />or </p>');
  476: 	    &return_link($r);
  477: 	    $r->print(<<STUFF);
  478: </form>
  479: STUFF
  480:         } else {
  481: 	    &return_link($r);
  482: 	}
  483: 	return;
  484:     }
  485: 
  486:     my $reserved=&make_reservation($env{'form.slotname'},
  487: 				   \%slot,$symb);
  488:     my $description=&get_description($env{'form.slotname'},\%slot);
  489:     if (defined($reserved)) {
  490: 	if ($slot_name =~ /^error: (.*)/) {
  491: 	    $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
  492: 	} elsif ($reserved > -1) {
  493: 	    $r->print("<p>Success: $description</p>");
  494: 	} elsif ($reserved < 0) {
  495: 	    $r->print("<p>Already reserved: $description</p>");
  496: 	}
  497: 	&return_link($r);
  498: 	return;
  499:     }
  500: 
  501:     my %lt=('request'=>"Availibility list",
  502: 	    'try'    =>'Try again');
  503:     %lt=&Apache::lonlocal::texthash(%lt);
  504: 
  505:     $r->print(<<STUFF);
  506: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  507: <p>
  508: <form method="POST" action="/adm/slotrequest">
  509:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  510:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  511:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  512:    <input type="hidden" name="command" value="get" />
  513: </form>
  514: ?
  515: </p>
  516: <p>
  517: or
  518: <form method="POST" action="/adm/slotrequest">
  519:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  520:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  521: </form>
  522: </p>
  523: or
  524: STUFF
  525: 
  526:     &return_link($r);
  527:     return;
  528: }
  529: 
  530: sub allowed_slot {
  531:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
  532:     #already started
  533:     if ($slot->{'starttime'} < time) {
  534: 	# all open slot to be schedulable
  535: 	#return 0;
  536:     }
  537:     &Apache::lonxml::debug("$slot_name starttime good");
  538:     #already ended
  539:     if ($slot->{'endtime'} < time) {
  540: 	return 0;
  541:     }
  542:     &Apache::lonxml::debug("$slot_name endtime good");
  543:     # not allowed to pick this one
  544:     if (defined($slot->{'type'})
  545: 	&& $slot->{'type'} ne 'schedulable_student') {
  546: 	return 0;
  547:     }
  548:     &Apache::lonxml::debug("$slot_name type good");
  549:     # not allowed for this resource
  550:     if (defined($slot->{'symb'})
  551: 	&& $slot->{'symb'} ne $symb) {
  552: 	return 0;
  553:     }
  554:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
  555: 				       $consumed_uniqueperiods);
  556:     if ($conflict) {
  557: 	if ($slots->{$conflict}{'starttime'} < time) {
  558: 	    return 0;
  559: 	}
  560:     }
  561:     &Apache::lonxml::debug("$slot_name symb good");
  562:     return 1;
  563: }
  564: 
  565: sub get_description {
  566:     my ($slot_name,$slot)=@_;
  567:     my $description=$slot->{'description'};
  568:     if (!defined($description)) {
  569: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  570: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  571: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  572:     }
  573:     return $description;
  574: }
  575: 
  576: sub show_choices {
  577:     my ($r,$symb)=@_;
  578: 
  579:     my ($cnum,$cdom)=&get_course();
  580:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  581:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
  582:     my $available;
  583:     $r->print('<table border="1">');
  584:     &Apache::lonxml::debug("Checking Slots");
  585:     my @got_slots=&check_for_reservation($symb,'allslots');
  586:     foreach my $slot (sort 
  587: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  588: 		      (keys(%slots)))  {
  589: 
  590: 	&Apache::lonxml::debug("Checking Slot $slot");
  591: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
  592: 				$consumed_uniqueperiods));
  593: 
  594: 	$available++;
  595: 
  596: 	my $description=&get_description($slot,$slots{$slot});
  597: 
  598: 	my $form=&mt('Unavailable');
  599: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
  600: 	    &space_available($slot,$slots{$slot},$symb)) {
  601: 	    my $text=&mt('Select');
  602: 	    my $command='get';
  603: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
  604: 		$text=&mt('Free Reservation');
  605: 		$command='release';
  606: 	    } else {
  607: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
  608: 						   \%slots,
  609: 						   $consumed_uniqueperiods);
  610: 		if ($conflict) {
  611: 		    $text=&mt('Change Reservation');
  612: 		    $command='get';
  613: 		}
  614: 	    }
  615: 	    my $escsymb=&Apache::lonnet::escape($symb);
  616: 	    $form=<<STUFF;
  617:    <form method="POST" action="/adm/slotrequest">
  618:      <input type="submit" name="Select" value="$text" />
  619:      <input type="hidden" name="symb" value="$escsymb" />
  620:      <input type="hidden" name="slotname" value="$slot" />
  621:      <input type="hidden" name="command" value="$command" />
  622:    </form>
  623: STUFF
  624: 	}
  625: 	$r->print(<<STUFF);
  626: <tr>
  627:  <td>$form</td>
  628:  <td>$description</td>
  629: </tr>
  630: STUFF
  631:     }
  632: 
  633:     if (!$available) {
  634: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  635: 		  &mt('Return to last resource').'</a></td></tr>');
  636:     }
  637:     $r->print('</table>');
  638: }
  639: 
  640: sub to_show {
  641:     my ($slot,$when,$deleted) = @_;
  642:     my $time=time;
  643:     my $week=60*60*24*7;
  644:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  645: 	return 0;
  646:     }
  647:     if ($when eq 'any') {
  648: 	return 1;
  649:     } elsif ($when eq 'now') {
  650: 	if ($time > $slot->{'starttime'} &&
  651: 	    $time < $slot->{'endtime'}) {
  652: 	    return 1;
  653: 	}
  654: 	return 0;
  655:     } elsif ($when eq 'nextweek') {
  656: 	if ( ($time        < $slot->{'starttime'} &&
  657: 	      ($time+$week) > $slot->{'starttime'})
  658: 	     ||
  659: 	     ($time        < $slot->{'endtime'} &&
  660: 	      ($time+$week) > $slot->{'endtime'}) ) {
  661: 	    return 1;
  662: 	}
  663: 	return 0;
  664:     } elsif ($when eq 'lastweek') {
  665: 	if ( ($time        > $slot->{'starttime'} &&
  666: 	      ($time-$week) < $slot->{'starttime'})
  667: 	     ||
  668: 	     ($time        > $slot->{'endtime'} &&
  669: 	      ($time-$week) < $slot->{'endtime'}) ) {
  670: 	    return 1;
  671: 	}
  672: 	return 0;
  673:     } elsif ($when eq 'willopen') {
  674: 	if ($time < $slot->{'starttime'}) {
  675: 	    return 1;
  676: 	}
  677: 	return 0;
  678:     } elsif ($when eq 'wereopen') {
  679: 	if ($time > $slot->{'endtime'}) {
  680: 	    return 1;
  681: 	}
  682: 	return 0;
  683:     }
  684:     
  685:     return 1;
  686: }
  687: 
  688: sub remove_link {
  689:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
  690: 
  691:     $slotname  = &Apache::lonnet::escape($slotname);
  692:     $entry     = &Apache::lonnet::escape($entry);
  693:     $uname     = &Apache::lonnet::escape($uname);
  694:     $udom      = &Apache::lonnet::escape($udom);
  695:     $symb      = &Apache::lonnet::escape($symb);
  696: 
  697:     my $remove= &mt('Remove');
  698: 
  699:     return <<"END_LINK";
  700:  <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
  701:    >($remove)</a>
  702: END_LINK
  703: 
  704: }
  705: 
  706: sub show_table {
  707:     my ($r,$mgr)=@_;
  708: 
  709:     my ($cnum,$cdom)=&get_course();
  710:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  711:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  712: 	undef(%slots);
  713:     } 
  714:     my $available;
  715:     if ($mgr eq 'F') {
  716: 	$r->print('<div>');
  717: 	$r->print('<form method="POST" action="/adm/slotrequest">
  718: <input type="hidden" name="command" value="uploadstart" />
  719: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  720: </form>');
  721: 	$r->print('<form method="POST" action="/adm/helper/newslot.helper">
  722: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  723: </form>');
  724: 	$r->print('</div>');
  725:     }
  726:     
  727:     my %Saveable_Parameters = ('show'    => 'array',
  728: 			       'when'    => 'scalar',
  729: 			       'order'   => 'scalar',
  730: 			       'deleted' => 'scalar',
  731: 			       );
  732:     &Apache::loncommon::store_course_settings('slotrequest',
  733: 					      \%Saveable_Parameters);
  734:     &Apache::loncommon::restore_course_settings('slotrequest',
  735: 						\%Saveable_Parameters);
  736:     &Apache::grades::init_perm();
  737:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
  738:     &Apache::grades::reset_perm();
  739: 
  740:     my %show_fields=&Apache::lonlocal::texthash(
  741: 	     'name'         => 'Slot Name',
  742: 	     'description'  => 'Description',
  743: 	     'type'         => 'Type',
  744: 	     'starttime'    => 'Start time',
  745: 	     'endtime'      => 'End Time',
  746:              'startreserve' => 'Time students can start reserving',
  747: 	     'secret'       => 'Secret Word',
  748: 	     'maxspace'     => 'Maximum # of students',
  749: 	     'ip'           => 'IP or DNS restrictions',
  750: 	     'symb'         => 'Resource slot is restricted to.',
  751: 	     'uniqueperiod' => 'Period of time slot is unique',
  752: 	     'scheduled'    => 'Scheduled Students',
  753: 	     'proctor'      => 'List of proctors');
  754:     my @show_order=('name','description','type','starttime','endtime',
  755: 	       'startreserve','secret','maxspace','ip','symb',
  756: 	       'uniqueperiod','scheduled','proctor');
  757:     my @show = 
  758: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  759: 	                            : keys(%show_fields);
  760:     my %show =  map { $_ => 1 } (@show);
  761: 
  762:     my %when_fields=&Apache::lonlocal::texthash(
  763: 	     'now'      => 'Open now',
  764: 	     'nextweek' => 'Open within the next week',
  765: 	     'lastweek' => 'Were open last week',
  766: 	     'willopen' => 'Will open later',
  767: 	     'wereopen' => 'Were open',
  768: 	     'any'      => 'Anytime',
  769: 						);
  770:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
  771:     $when_fields{'select_form_order'} = \@when_order;
  772:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
  773:                                             : 'now';
  774: 
  775:     my %stu_display_fields=
  776: 	&Apache::lonlocal::texthash('username' => 'User name',
  777: 				    'fullname' => 'Full name',
  778: 				    );
  779:     my @stu_display_order=('fullname','username');
  780:     my @stu_display = 
  781: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
  782: 	                                  : keys(%stu_display_fields);
  783:     my %stu_display =  map { $_ => 1 } (@stu_display);
  784: 
  785:     my $hide_radio = 
  786: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
  787:     my $show_radio = 
  788: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
  789: 	
  790:     $r->print('<form method="POST" action="/adm/slotrequest">
  791: <input type="hidden" name="command" value="showslots" />');
  792:     $r->print('<div>');
  793:     $r->print('<table class="inline">
  794:       <tr><th>'.&mt('Show').'</th>
  795:           <th>'.&mt('Student Display').'</th>
  796:           <th>'.&mt('Open').'</th>
  797:           <th>'.&mt('Options').'</th>
  798:       </tr>
  799:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
  800: 	      '</td>
  801:            <td>
  802:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
  803: 						    6,\%stu_display_fields,
  804: 						    \@stu_display_order).'
  805:            </td>
  806:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
  807:           '</td>
  808:            <td>
  809:             <table>
  810:               <tr>
  811:                 <td rowspan="2">Deleted slots:</td>
  812:                 <td><label>'.$show_radio.'Show</label></td>
  813:               </tr>
  814:               <tr>
  815:                 <td><label>'.$hide_radio.'Hide</label></td>
  816:               </tr>
  817:             </table>
  818: 	  </td>
  819:        </tr>
  820:     </table>');
  821:     $r->print('</div>');
  822:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
  823:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
  824:     $r->print('<table class="thinborder">
  825: <tr>
  826:   <th></th>');
  827:     foreach my $which (@show_order) {
  828: 	if ($which ne 'proctor' && exists($show{$which})) {
  829: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
  830: 	}
  831:     }
  832: 
  833:     my %name_cache;
  834:     my $slotsort = sub {
  835: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
  836: 	    if (lc($slots{$a}->{$env{'form.order'}})
  837: 		ne lc($slots{$b}->{$env{'form.order'}})) {
  838: 		return (lc($slots{$a}->{$env{'form.order'}}) 
  839: 			cmp lc($slots{$b}->{$env{'form.order'}}));
  840: 	    }
  841: 	} elsif ($env{'form.order'} eq 'name') {
  842: 	    if (lc($a) cmp lc($b)) {
  843: 		return lc($a) cmp lc($b);
  844: 	    }
  845: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
  846: 	    
  847: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
  848: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
  849: 		return ($slots{$a}->{'uniqueperiod'}[0]
  850: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
  851: 	    }
  852: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
  853: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
  854: 		return ($slots{$a}->{'uniqueperiod'}[1]
  855: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
  856: 	    }
  857: 	}
  858: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
  859:     };
  860:     foreach my $slot (sort $slotsort (keys(%slots)))  {
  861: 	if (!&to_show($slots{$slot},$when,$env{'form.deleted'})) { next; }
  862: 	if (defined($slots{$slot}->{'type'})
  863: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
  864: 	    #next;
  865: 	}
  866: 	my $description=&get_description($slot,$slots{$slot});
  867: 	my $ids;
  868: 	if (exists($show{'scheduled'})) {
  869: 	    my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  870: 					       "^$slot\0");
  871: 	    my ($tmp)=%consumed;
  872: 	    if ($tmp !~ /^error: /) {
  873: 		foreach my $entry (sort(keys(%consumed))) {
  874: 		    my (undef,$id)=split("\0",$entry);
  875: 		    my ($uname,$udom) = split('@',$consumed{$entry}{'name'});
  876: 		    $ids.= '<nobr>';
  877: 		    foreach my $item (@stu_display_order) {
  878: 			if ($stu_display{$item}) {
  879: 			    if ($item eq 'fullname') {
  880: 				$ids.=$fullname->{"$uname:$udom"}.' ';
  881: 			    } elsif ($item eq 'username') {
  882: 				$ids.="<tt>$uname\@$udom</tt> ";
  883: 			    }
  884: 			}
  885: 		    }
  886: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
  887: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
  888: 		}
  889: 	    }
  890: 	}
  891: 
  892: 	my $start=($slots{$slot}->{'starttime'}?
  893: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
  894: 	my $end=($slots{$slot}->{'endtime'}?
  895: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
  896: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
  897: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
  898: 	
  899: 	my $unique;
  900: 	if (ref($slots{$slot}{'uniqueperiod'})) {
  901: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
  902: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
  903: 	}
  904: 
  905: 	my $title;
  906: 	if (exists($slots{$slot}{'symb'})) {
  907: 	    my (undef,undef,$res)=
  908: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
  909: 	    $res =   &Apache::lonnet::clutter($res);
  910: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
  911: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
  912: 	}
  913: 
  914: 	my @proctors;
  915: 	my $rowspan=1;
  916: 	my $colspan=1;
  917: 	if (exists($show{'proctor'})) {
  918: 	    $rowspan=2;
  919: 	    @proctors= map {
  920: 		my ($uname,$udom)=split(/@/,$_);
  921: 		my $fullname=$name_cache{$_};
  922: 		if (!defined($fullname)) {
  923: 		    &Apache::lonnet::logthis("Gettign $uname $udom");
  924: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
  925: 		    $fullname =~s/\s/&nbsp;/g;
  926: 		    $name_cache{$_} = $fullname;
  927: 		}
  928: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
  929: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
  930: 	}
  931: 	my $proctors=join(', ',@proctors);
  932: 
  933: 	my $edit=(<<"EDITLINK");
  934: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
  935: EDITLINK
  936: 
  937: 	my $delete=(<<"DELETELINK");
  938: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
  939: DELETELINK
  940:         if ($ids ne '') { undef($delete); }
  941: 
  942:         $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
  943: 	if (exists($show{'name'})) {
  944: 	    $colspan++;$r->print("<td>$slot</td>");
  945: 	}
  946: 	if (exists($show{'description'})) {
  947: 	    $colspan++;$r->print("<td>$description</td>\n");
  948: 	}
  949: 	if (exists($show{'type'})) {
  950: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
  951: 	}
  952: 	if (exists($show{'starttime'})) {
  953: 	    $colspan++;$r->print("<td>$start</td>\n");
  954: 	}
  955: 	if (exists($show{'endtime'})) {
  956: 	    $colspan++;$r->print("<td>$end</td>\n");
  957: 	}
  958: 	if (exists($show{'startreserve'})) {
  959: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
  960: 	}
  961: 	if (exists($show{'secret'})) {
  962: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
  963: 	}
  964: 	if (exists($show{'maxspace'})) {
  965: 	    $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
  966: 	}
  967: 	if (exists($show{'ip'})) {
  968: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
  969: 	}
  970: 	if (exists($show{'symb'})) {
  971: 	    $colspan++;$r->print("<td>$title</td>\n");
  972: 	}
  973: 	if (exists($show{'uniqueperiod'})) {
  974: 	    $colspan++;$r->print("<td>$unique</td>\n");
  975: 	}
  976: 	if (exists($show{'scheduled'})) {
  977: 	    $colspan++;$r->print("<td>$ids</td>\n</tr>\n");
  978: 	}
  979: 	if (exists($show{'proctor'})) {
  980: 	    $r->print(<<STUFF);
  981: <tr>
  982:  <td colspan="$colspan">$proctors</td>
  983: </tr>
  984: STUFF
  985:         }
  986:     }
  987:     $r->print('</table>');
  988: }
  989: 
  990: sub upload_start {
  991:     my ($r)=@_;    
  992:     $r->print(&Apache::grades::checkforfile_js());
  993:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
  994:     $result.='&nbsp;<b>'.
  995: 	&mt('Specify a file containing the slot definitions.').
  996: 	'</b></td></tr>'."\n";
  997:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
  998:     my $upfile_select=&Apache::loncommon::upfile_select_html();
  999:     my $ignore=&mt('Ignore First Line');
 1000:     $result.=<<ENDUPFORM;
 1001: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1002: <input type="hidden" name="command" value="csvuploadmap" />
 1003: $upfile_select
 1004: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
 1005: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 1006: </form>
 1007: ENDUPFORM
 1008:     $result.='</td></tr></table>'."\n";
 1009:     $result.='</td></tr></table>'."\n";
 1010:     $r->print($result);
 1011: }
 1012: 
 1013: sub csvuploadmap_header {
 1014:     my ($r,$datatoken,$distotal)= @_;
 1015:     my $javascript;
 1016:     if ($env{'form.upfile_associate'} eq 'reverse') {
 1017: 	$javascript=&csvupload_javascript_reverse_associate();
 1018:     } else {
 1019: 	$javascript=&csvupload_javascript_forward_associate();
 1020:     }
 1021: 
 1022:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 1023:     my $ignore=&mt('Ignore First Line');
 1024:     $r->print(<<ENDPICK);
 1025: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1026: <h3>Identify fields</h3>
 1027: Total number of records found in file: $distotal <hr />
 1028: Enter as many fields as you can. The system will inform you and bring you back
 1029: to this page if the data selected is insufficient to create the slots.<hr />
 1030: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 1031: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 1032: <input type="hidden" name="associate"  value="" />
 1033: <input type="hidden" name="datatoken"  value="$datatoken" />
 1034: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 1035: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 1036: <input type="hidden" name="upfile_associate" 
 1037:                                        value="$env{'form.upfile_associate'}" />
 1038: <input type="hidden" name="command"    value="csvuploadassign" />
 1039: <hr />
 1040: <script type="text/javascript" language="Javascript">
 1041: $javascript
 1042: </script>
 1043: ENDPICK
 1044:     return '';
 1045: 
 1046: }
 1047: 
 1048: sub csvuploadmap_footer {
 1049:     my ($request,$i,$keyfields) =@_;
 1050:     $request->print(<<ENDPICK);
 1051: </table>
 1052: <input type="hidden" name="nfields" value="$i" />
 1053: <input type="hidden" name="keyfields" value="$keyfields" />
 1054: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
 1055: </form>
 1056: ENDPICK
 1057: }
 1058: 
 1059: sub csvupload_javascript_reverse_associate {
 1060:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1061:     return(<<ENDPICK);
 1062:   function verify(vf) {
 1063:     var foundstart=0;
 1064:     var foundend=0;
 1065:     var foundname=0;
 1066:     var foundtype=0;
 1067:     for (i=0;i<=vf.nfields.value;i++) {
 1068:       tw=eval('vf.f'+i+'.selectedIndex');
 1069:       if (i==0 && tw!=0) { foundname=1; }
 1070:       if (i==1 && tw!=0) { foundtype=1; }
 1071:       if (i==2 && tw!=0) { foundstat=1; }
 1072:       if (i==3 && tw!=0) { foundend=1; }
 1073:     }
 1074:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1075: 	alert('$error1');
 1076: 	return;
 1077:     }
 1078:     vf.submit();
 1079:   }
 1080:   function flip(vf,tf) {
 1081:   }
 1082: ENDPICK
 1083: }
 1084: 
 1085: sub csvupload_javascript_forward_associate {
 1086:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1087:   return(<<ENDPICK);
 1088:   function verify(vf) {
 1089:     var foundstart=0;
 1090:     var foundend=0;
 1091:     var foundname=0;
 1092:     var foundtype=0;
 1093:     for (i=0;i<=vf.nfields.value;i++) {
 1094:       tw=eval('vf.f'+i+'.selectedIndex');
 1095:       if (tw==1) { foundname=1; }
 1096:       if (tw==2) { foundtype=1; }
 1097:       if (tw==3) { foundstat=1; }
 1098:       if (tw==4) { foundend=1; }
 1099:     }
 1100:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1101: 	alert('$error1');
 1102: 	return;
 1103:     }
 1104:     vf.submit();
 1105:   }
 1106:   function flip(vf,tf) {
 1107:   }
 1108: ENDPICK
 1109: }
 1110: 
 1111: sub csv_upload_map {
 1112:     my ($r)= @_;
 1113: 
 1114:     my $datatoken;
 1115:     if (!$env{'form.datatoken'}) {
 1116: 	$datatoken=&Apache::loncommon::upfile_store($r);
 1117:     } else {
 1118: 	$datatoken=$env{'form.datatoken'};
 1119: 	&Apache::loncommon::load_tmp_file($r);
 1120:     }
 1121:     my @records=&Apache::loncommon::upfile_record_sep();
 1122:     if ($env{'form.noFirstLine'}) { shift(@records); }
 1123:     &csvuploadmap_header($r,$datatoken,$#records+1);
 1124:     my ($i,$keyfields);
 1125:     if (@records) {
 1126: 	my @fields=&csvupload_fields();
 1127: 
 1128: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 1129: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 1130: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 1131: 							  \@fields);
 1132: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 1133: 	    chop($keyfields);
 1134: 	} else {
 1135: 	    unshift(@fields,['none','']);
 1136: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1137: 							    \@fields);
 1138: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1139: 	    $keyfields=join(',',sort(keys(%sone)));
 1140: 	}
 1141:     }
 1142:     &csvuploadmap_footer($r,$i,$keyfields);
 1143: 
 1144:     return '';
 1145: }
 1146: 
 1147: sub csvupload_fields {
 1148:     return (['name','Slot name'],
 1149: 	    ['type','Type of slot'],
 1150: 	    ['starttime','Start Time of slot'],
 1151: 	    ['endtime','End Time of slot'],
 1152: 	    ['startreserve','Reservation Start Time'],
 1153: 	    ['ip','IP or DNS restriction'],
 1154: 	    ['proctor','List of proctor ids'],
 1155: 	    ['description','Slot Description'],
 1156: 	    ['maxspace','Maximum number of reservations'],
 1157: 	    ['symb','Resource Restriction'],
 1158: 	    ['uniqueperiod','Date range of slot exclusion'],
 1159: 	    ['secret','Secret word proctor uses to validate']);
 1160: }
 1161: 
 1162: sub csv_upload_assign {
 1163:     my ($r,$mgr)= @_;
 1164:     &Apache::loncommon::load_tmp_file($r);
 1165:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1166:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1167:     my %fields=&Apache::grades::get_fields();
 1168:     $r->print('<h3>Creating Slots</h3>');
 1169:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1170:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1171:     my $countdone=0;
 1172:     my @errors;
 1173:     foreach my $slot (@slotdata) {
 1174: 	my %slot;
 1175: 	my %entries=&Apache::loncommon::record_sep($slot);
 1176: 	my $domain;
 1177: 	my $name=$entries{$fields{'name'}};
 1178: 	if ($name=~/^\s*$/) {
 1179: 	    push(@errors,"Did not create slot with no name");
 1180: 	    next;
 1181: 	}
 1182: 	if ($name=~/\s/) { 
 1183: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1184: 	    next;
 1185: 	}
 1186: 	if ($name=~/\W/) { 
 1187: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1188: 	    next;
 1189: 	}
 1190: 	if ($entries{$fields{'type'}}) {
 1191: 	    $slot{'type'}=$entries{$fields{'type'}};
 1192: 	} else {
 1193: 	    $slot{'type'}='preassigned';
 1194: 	}
 1195: 	if ($slot{'type'} ne 'preassigned' &&
 1196: 	    $slot{'type'} ne 'schedulable_student') {
 1197: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1198: 	    next;
 1199: 	}
 1200: 	if ($entries{$fields{'starttime'}}) {
 1201: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1202: 	}
 1203: 	if ($entries{$fields{'endtime'}}) {
 1204: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1205: 	}
 1206: 	if ($entries{$fields{'startreserve'}}) {
 1207: 	    $slot{'startreserve'}=
 1208: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1209: 	}
 1210: 	foreach my $key ('ip','proctor','description','maxspace',
 1211: 			 'secret','symb') {
 1212: 	    if ($entries{$fields{$key}}) {
 1213: 		$slot{$key}=$entries{$fields{$key}};
 1214: 	    }
 1215: 	}
 1216: 	if ($entries{$fields{'uniqueperiod'}}) {
 1217: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1218: 	    my @times=(&UnixDate($start,"%s"),
 1219: 		       &UnixDate($end,"%s"));
 1220: 	    $slot{'uniqueperiod'}=\@times;
 1221: 	}
 1222: 
 1223: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1224: 	$r->print('.');
 1225: 	$r->rflush();
 1226: 	$countdone++;
 1227:     }
 1228:     $r->print("<p>Created $countdone slots\n</p>");
 1229:     foreach my $error (@errors) {
 1230: 	$r->print("<p>$error\n</p>");
 1231:     }
 1232:     &show_table($r,$mgr);
 1233:     return '';
 1234: }
 1235: 
 1236: sub handler {
 1237:     my $r=shift;
 1238: 
 1239:     &Apache::loncommon::content_type($r,'text/html');
 1240:     &Apache::loncommon::no_cache($r);
 1241:     if ($r->header_only()) {
 1242: 	$r->send_http_header();
 1243: 	return OK;
 1244:     }
 1245: 
 1246:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1247:     
 1248:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1249:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1250:     my $title='Requesting Another Worktime';
 1251:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1252: 	$title = 'Managing Slots';
 1253:     }
 1254:     &start_page($r,$title);
 1255: 
 1256:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1257: 	&show_table($r,$mgr);
 1258:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1259: 	&remove_registration($r);
 1260:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1261: 	&release_slot($r,undef,undef,undef,$mgr);
 1262:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1263: 	&delete_slot($r);
 1264:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1265: 	&upload_start($r);
 1266:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1267: 	&csv_upload_map($r);
 1268:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1269: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1270: 	    &csv_upload_assign($r,$mgr);
 1271: 	} else {
 1272: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1273: 		$env{'form.upfile_associate'} = 'reverse';
 1274: 	    } else {
 1275: 		$env{'form.upfile_associate'} = 'forward';
 1276: 	    }
 1277: 	    &csv_upload_map($r);
 1278: 	}
 1279:     } else {
 1280: 	my $symb=&Apache::lonnet::unescape($env{'form.symb'});
 1281: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1282: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1283: 	if ($useslots ne 'resource') {
 1284: 	    &fail($r,'not_valid');
 1285: 	    return OK;
 1286: 	}
 1287: 	$env{'request.symb'}=$symb;
 1288: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1289: 	                               : 'problem';
 1290: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1291: 	if ($status eq 'CAN_ANSWER' ||
 1292: 	    $status eq 'NEEDS_CHECKIN' ||
 1293: 	    $status eq 'WAITING_FOR_GRADE') {
 1294: 	    &fail($r,'not_allowed');
 1295: 	    return OK;
 1296: 	}
 1297: 	if ($env{'form.requestattempt'}) {
 1298: 	    &show_choices($r,$symb);
 1299: 	} elsif ($env{'form.command'} eq 'release') {
 1300: 	    &release_slot($r,$symb);
 1301: 	} elsif ($env{'form.command'} eq 'get') {
 1302: 	    &get_slot($r,$symb);
 1303: 	} elsif ($env{'form.command'} eq 'change') {
 1304: 	    if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1305: 		&get_slot($r,$symb);
 1306: 	    }
 1307: 	} else {
 1308: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1309: 	}
 1310:     }
 1311:     &end_page($r);
 1312:     return OK;
 1313: }
 1314: 
 1315: 1;
 1316: __END__

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