File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.39: download - view: text, annotated - select for diffs
Fri Feb 3 17:07:20 2006 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- was allowing users to release reservations for expired slots

    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.39 2006/02/03 17:07:20 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 Date::Manip;
   38: 
   39: sub fail {
   40:     my ($r,$code)=@_;
   41:     if ($code eq 'not_valid') {
   42: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
   43: 
   44:     } elsif ($code eq 'not_allowed') {
   45: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
   46:     } else {
   47: 	$r->print('<p>'.&mt('Failed.').'</p>');
   48:     }
   49:     
   50:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
   51: 	      &mt('Return to last resource').'</a></p>');
   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:     
   93:     my ($tmp)=%consumed;
   94:     if ($tmp=~/^error: 2 / ) {
   95: 	return 0;
   96:     }
   97:     return keys(%consumed);
   98: }
   99: 
  100: sub space_available {
  101:     my ($slot_name,$slot)=@_;
  102:     my $max=$slot->{'maxspace'};
  103: 
  104:     if (!defined($max)) { return 1; }
  105: 
  106:     my $consumed=scalar(&get_reservation_ids($slot_name));
  107:     if ($consumed < $max) {
  108: 	return 1
  109:     }
  110:     return 0;
  111: }
  112: 
  113: sub check_for_reservation {
  114:     my ($symb)=@_;
  115:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  116: 				       $env{'user.domain'}, $env{'user.name'});
  117: 
  118:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  119: 				    $env{'user.domain'}, $env{'user.name'});
  120:     my @slots = (split(/:/,$student), split(/:/, $course));
  121: 
  122:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  123: 
  124:     my ($cnum,$cdom)=&get_course();
  125:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  126: 
  127:     foreach my $slot_name (@slots) {
  128: 	next if (!defined($slots{$slot_name}) ||
  129: 		 !ref($slots{$slot_name}));
  130: 	&Apache::lonxml::debug(time." $slot_name ".
  131: 			       $slots{$slot_name}->{'starttime'}." -- ".
  132: 			       $slots{$slot_name}->{'startreserve'});
  133: 	if ($slots{$slot_name}->{'endtime'} > time &&
  134: 	    $slots{$slot_name}->{'startreserve'} < time) {
  135: 	    # between start of reservation times and end of slot
  136: 	    return($slot_name, $slots{$slot_name});
  137: 	}
  138:     }
  139:     return (undef,undef);
  140: }
  141: 
  142: sub check_for_conflict {
  143:     my ($symb,$new_slot_name)=@_;
  144:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  145: 				       $env{'user.domain'}, $env{'user.name'});
  146:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  147: 				      $env{'user.domain'}, $env{'user.name'});
  148:     my @slots = (split(/:/,$student), split(/:/, $course));
  149:     my ($cnum,$cdom)=&get_course();
  150:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  151:     foreach my $slot_name (@slots) {
  152: 	next if (!defined($slots{$slot_name}) ||
  153: 		 !ref($slots{$slot_name}));
  154: 
  155:         next if (!defined($slots{$slot_name}->{'uniqueperiod'}) ||
  156: 		 !ref($slots{$slot_name}->{'uniqueperiod'}));
  157: 	my ($start,$end)=@{$slots{$slot_name}->{'uniqueperiod'}};
  158: 	if ($start<time && time < $end) {
  159: 	    return $slot_name;
  160: 	}
  161:     }
  162:     return undef;
  163: 
  164: }
  165: 
  166: sub make_reservation {
  167:     my ($slot_name,$slot,$symb)=@_;
  168: 
  169:     my ($cnum,$cdom)=&get_course();
  170: 
  171:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  172: 				   $env{'user.domain'},$env{'user.name'});
  173:     &Apache::lonxml::debug("value is  $value<br />");
  174:     foreach my $other_slot (split(/:/, $value)) {
  175: 	if ($other_slot eq $slot_name) {
  176: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  177: 					       $cnum, "^$slot_name\0");   
  178: 
  179: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  180: 	    foreach my $key (keys(%consumed)) {
  181: 		if ($consumed{$key}->{'name'} eq $me) {
  182: 		    my $num=(split('\0',$key))[1];
  183: 		    return -$num;
  184: 		}
  185: 	    }
  186: 	}
  187:     }
  188: 
  189:     my $max=$slot->{'maxspace'};
  190:     if (!defined($max)) { $max=99999; }
  191: 
  192:     my (@ids)=&get_reservation_ids($slot_name);
  193: 
  194:     my $last=0;
  195:     foreach my $id (@ids) {
  196: 	my $num=(split('\0',$id))[1];
  197: 	if ($num > $last) { $last=$num; }
  198:     }
  199:     
  200:     my $wanted=$last+1;
  201:     &Apache::lonxml::debug("wanted $wanted<br />");
  202:     if (scalar(@ids) >= $max) {
  203: 	# full up
  204: 	return undef;
  205:     }
  206:     
  207:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  208: 		     'timestamp' => time,
  209: 		     'symb'      => $symb);
  210: 
  211:     my $success=&Apache::lonnet::newput('slot_reservations',
  212: 					{"$slot_name\0$wanted" =>
  213: 					     \%reservation},
  214: 					$cdom, $cnum);
  215: 
  216:     if ($success eq 'ok') {
  217: 	my $new_value=$slot_name;
  218: 	if ($value) {
  219: 	    $new_value=$value.':'.$new_value;
  220: 	}
  221: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  222: 						      '0_availablestudent',
  223: 						       1, $new_value, 'string',
  224: 						       $env{'user.name'},
  225: 					               $env{'user.domain'});
  226: 	&Apache::lonxml::debug("hrrm $result");
  227: 	return $wanted;
  228:     }
  229: 
  230:     # someone else got it
  231:     return undef;
  232: }
  233: 
  234: sub remove_registration {
  235:     my ($r) = @_;
  236:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
  237: 					     $env{'form.udom'});
  238: 
  239:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
  240: 
  241:     my $hidden_input;
  242:     foreach my $parm ('uname','udom','slotname','entry','symb') {
  243: 	$hidden_input .=
  244: 	    '<input type="hidden" name="'.$parm.'" value="'
  245: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
  246:     }
  247:     $r->print(<<"END_CONFIRM");
  248: <p> Remove $name from slot $env{'form.slotname'} for $title</p>
  249: <form action="/adm/slotrequest" method="POST">
  250:     <input type="hidden" name="command" value="release" />
  251:     $hidden_input
  252:     <input type="submit" name="Yes" value="yes" />
  253: </form>
  254: <form action="/adm/slotrequest" method="POST">
  255:     <input type="hidden" name="command" value="showslots" />
  256:     <input type="submit" name="No" value="no" />
  257: </form>
  258: END_CONFIRM
  259: 
  260: }
  261: 
  262: sub release_slot {
  263:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
  264: 
  265:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  266:     my ($cnum,$cdom)=&get_course();
  267: 
  268:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
  269:     if ($mgr eq 'F' 
  270: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
  271: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
  272:     }
  273: 
  274:     if ($mgr eq 'F' 
  275: 	&& defined($env{'form.symb'})) {
  276: 	$symb = $env{'form.symb'};
  277:     }
  278:     my %slot=&Apache::lonnet::get_slot($slot_name);
  279:     my $description=&get_description($env{'form.slotname'},\%slot);
  280: 
  281:     if ($mgr ne 'F') {
  282: 	if ($slot{$slot_name}{'starttime'} < time) {
  283: 	    $r->print("<p>Not allowed to release Reservation: $description, as it has already ended.</p>");
  284: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  285: 		      &mt('Return to last resource').'</a></p>');
  286: 	    return 0;
  287: 	}
  288:     }
  289:     # get parameter string, check for existance, rebuild string with the slot
  290:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  291: 					       $symb,$udom,$uname));
  292: 
  293:     my @new_slots;
  294:     foreach my $exist_slot (@slots) {
  295: 	if ($exist_slot eq $slot_name) { next; }
  296: 	push(@new_slots,$exist_slot);
  297:     }
  298:     my $new_param = join(':',@new_slots);
  299: 
  300:     # get slot reservations, check if user has one, if so remove reservation
  301:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  302: 				       "^$slot_name\0");
  303:     foreach my $entry (keys(%consumed)) {
  304: 	if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) {
  305: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  306: 				 $cdom,$cnum);
  307: 	}
  308:     }
  309: 
  310:     # store new parameter string
  311:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  312: 						      '0_availablestudent',
  313: 						      1, $new_param, 'string',
  314: 						      $uname,$udom);
  315:     my $description=&get_description($env{'form.slotname'},\%slot);
  316:     $r->print("<p>Released Reservation: $description</p>");
  317:     if ($mgr eq 'F') {
  318: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  319: 		  &mt('Return to slot list').'</a></p>');
  320:     }
  321:     if (!$inhibit_return_link) {
  322: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
  323: 		  &mt('Return to last resource').'</a></p>');
  324:     }
  325:     return 1;
  326: }
  327: 
  328: sub delete_slot {
  329:     my ($r)=@_;
  330: 
  331:     my $slot_name = $env{'form.slotname'};
  332:     my %slot=&Apache::lonnet::get_slot($slot_name);
  333: 
  334:     my ($cnum,$cdom)=&get_course();
  335:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  336: 				       "^$slot_name\0");
  337:     my ($tmp) = %consumed;
  338:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  339: 
  340:     if (%slot && !%consumed) {
  341: 	$slot{'type'} = 'deleted';
  342: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  343: 					$cdom, $cnum);
  344: 	if ($ret eq 'ok') {
  345: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
  346: 	} else {
  347: 	    $r->print("<p> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</p>");
  348: 	}
  349:     } else {
  350: 	if (%consumed) {
  351: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
  352: 	} else {
  353: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
  354: 	}
  355:     }
  356:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  357: 	      &mt('Return to slot list').'</a></p>');
  358:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  359: 	      &mt('Return to last resource').'</a></p>');
  360: }
  361: 
  362: sub get_slot {
  363:     my ($r,$symb)=@_;
  364: 
  365:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
  366:     if ($slot_name) {
  367: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  368: 	my $description1=&get_description($slot_name,\%slot);
  369: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  370: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  371: 	$r->print("<p>Already have a reservation: $description1</p>");
  372: 	if ($slot_name ne $env{'form.slotname'}) {
  373: 	    $r->print(<<STUFF);
  374: <form method="POST" action="/adm/slotrequest">
  375:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  376:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  377:    <input type="hidden" name="releaseslot" value="$slot_name" />
  378:    <input type="hidden" name="command" value="change" />
  379: STUFF
  380:             $r->print("<p>You can either ");
  381: 	    $r->print(<<STUFF);
  382:    <input type="submit" name="change" value="Change" />
  383: STUFF
  384: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  385: 		      $description2.
  386: 		      '</b> <br />or <a href="/adm/flip?postdata=return:">'.
  387: 		      &mt('Return to last resource').'</a></p>');
  388: 	    $r->print(<<STUFF);
  389: </form>
  390: STUFF
  391:         } else {
  392: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  393: 		      &mt('Return to last resource').'</a></p>');
  394: 	}
  395: 	return;
  396:     }
  397:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  398:     my $reserved=&make_reservation($env{'form.slotname'},
  399: 				   \%slot,$symb);
  400:     my $description=&get_description($env{'form.slotname'},\%slot);
  401:     if (defined($reserved)) {
  402: 	if ($reserved > -1) {
  403: 	    $r->print("<p>Success: $description</p>");
  404: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  405: 		      &mt('Return to last resource').'</a></p>');
  406: 	    return;
  407: 	} elsif ($reserved < 0) {
  408: 	    $r->print("<p>Already reserved: $description</p>");
  409: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  410: 		      &mt('Return to last resource').'</a></p>');
  411: 	    return;
  412: 	}
  413:     }
  414: 
  415:     my %lt=('request'=>"Availibility list",
  416: 	    'try'    =>'Try again');
  417:     %lt=&Apache::lonlocal::texthash(%lt);
  418: 
  419:     $r->print(<<STUFF);
  420: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  421: <p>
  422: <form method="POST" action="/adm/slotrequest">
  423:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  424:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  425:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  426:    <input type="hidden" name="command" value="get" />
  427: </form>
  428: ?
  429: </p>
  430: <p>
  431: or
  432: <form method="POST" action="/adm/slotrequest">
  433:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  434:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  435: </form>
  436: </p>
  437: or
  438: STUFF
  439:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  440: 	      &mt('Return to last resource').'</a></p>');
  441:     return;
  442: }
  443: 
  444: sub allowed_slot {
  445:     my ($slot_name,$slot,$symb)=@_;
  446:     #already started
  447:     if ($slot->{'starttime'} < time) {
  448: 	# all open slot to be schedulable
  449: 	#return 0;
  450:     }
  451:     &Apache::lonxml::debug("$slot_name starttime good");
  452:     #already ended
  453:     if ($slot->{'endtime'} < time) {
  454: 	return 0;
  455:     }
  456:     &Apache::lonxml::debug("$slot_name endtime good");
  457:     # not allowed to pick this one
  458:     if (defined($slot->{'type'})
  459: 	&& $slot->{'type'} ne 'schedulable_student') {
  460: 	return 0;
  461:     }
  462:     &Apache::lonxml::debug("$slot_name type good");
  463:     # not allowed for this resource
  464:     if (defined($slot->{'symb'})
  465: 	&& $slot->{'symb'} ne $symb) {
  466: 	return 0;
  467:     }
  468:     &Apache::lonxml::debug("$slot_name symb good");
  469:     return 1;
  470: }
  471: 
  472: sub get_description {
  473:     my ($slot_name,$slot)=@_;
  474:     my $description=$slot->{'description'};
  475:     if (!defined($description)) {
  476: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  477: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  478: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  479:     }
  480:     return $description;
  481: }
  482: 
  483: sub show_choices {
  484:     my ($r,$symb)=@_;
  485: 
  486:     my ($cnum,$cdom)=&get_course();
  487:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  488:     my $available;
  489:     $r->print('<table border="1">');
  490:     &Apache::lonxml::debug("Checking Slots");
  491:     my ($got_slot)=&check_for_reservation($symb);
  492:     foreach my $slot (sort 
  493: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  494: 		      (keys(%slots)))  {
  495: 
  496: 	&Apache::lonxml::debug("Checking Slot $slot");
  497: 	next if (!&allowed_slot($slot,$slots{$slot}));
  498: 
  499: 	$available++;
  500: 
  501: 	my $description=&get_description($slot,$slots{$slot});
  502: 
  503: 	my $form=&mt('Unavailable');
  504: 	if (($slot eq $got_slot) ||
  505: 	    &space_available($slot,$slots{$slot},$symb)) {
  506: 	    my $text=&mt('Select');
  507: 	    my $command='get';
  508: 	    if ($slot eq $got_slot) {
  509: 		$text=&mt('Free Reservation');
  510: 		$command='release';
  511: 	    }
  512: 	    my $escsymb=&Apache::lonnet::escape($symb);
  513: 	    $form=<<STUFF;
  514:    <form method="POST" action="/adm/slotrequest">
  515:      <input type="submit" name="Select" value="$text" />
  516:      <input type="hidden" name="symb" value="$escsymb" />
  517:      <input type="hidden" name="slotname" value="$slot" />
  518:      <input type="hidden" name="command" value="$command" />
  519:    </form>
  520: STUFF
  521: 	}
  522: 	$r->print(<<STUFF);
  523: <tr>
  524:  <td>$form</td>
  525:  <td>$description</td>
  526: </tr>
  527: STUFF
  528:     }
  529: 
  530:     if (!$available) {
  531: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  532: 		  &mt('Return to last resource').'</a></td></tr>');
  533:     }
  534:     $r->print('</table>');
  535: }
  536: 
  537: sub to_show {
  538:     my ($slot,$when,$deleted) = @_;
  539:     my $time=time;
  540:     my $week=60*60*24*7;
  541:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  542: 	return 0;
  543:     }
  544:     if ($when eq 'any') {
  545: 	return 1;
  546:     } elsif ($when eq 'now') {
  547: 	if ($time > $slot->{'starttime'} &&
  548: 	    $time < $slot->{'endtime'}) {
  549: 	    return 1;
  550: 	}
  551: 	return 0;
  552:     } elsif ($when eq 'nextweek') {
  553: 	if ( ($time        < $slot->{'starttime'} &&
  554: 	      ($time+$week) > $slot->{'starttime'})
  555: 	     ||
  556: 	     ($time        < $slot->{'endtime'} &&
  557: 	      ($time+$week) > $slot->{'endtime'}) ) {
  558: 	    return 1;
  559: 	}
  560: 	return 0;
  561:     } elsif ($when eq 'lastweek') {
  562: 	if ( ($time        > $slot->{'starttime'} &&
  563: 	      ($time-$week) < $slot->{'starttime'})
  564: 	     ||
  565: 	     ($time        > $slot->{'endtime'} &&
  566: 	      ($time-$week) < $slot->{'endtime'}) ) {
  567: 	    return 1;
  568: 	}
  569: 	return 0;
  570:     } elsif ($when eq 'willopen') {
  571: 	if ($time < $slot->{'starttime'}) {
  572: 	    return 1;
  573: 	}
  574: 	return 0;
  575:     } elsif ($when eq 'wereopen') {
  576: 	if ($time > $slot->{'endtime'}) {
  577: 	    return 1;
  578: 	}
  579: 	return 0;
  580:     }
  581:     
  582:     return 1;
  583: }
  584: 
  585: sub remove_link {
  586:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
  587: 
  588:     $slotname  = &Apache::lonnet::escape($slotname);
  589:     $entry     = &Apache::lonnet::escape($entry);
  590:     $uname     = &Apache::lonnet::escape($uname);
  591:     $udom      = &Apache::lonnet::escape($udom);
  592:     $symb      = &Apache::lonnet::escape($symb);
  593: 
  594:     my $remove= &mt('Remove');
  595: 
  596:     return <<"END_LINK";
  597:  <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
  598:    >($remove)</a>
  599: END_LINK
  600: 
  601: }
  602: 
  603: sub show_table {
  604:     my ($r,$mgr)=@_;
  605: 
  606:     my ($cnum,$cdom)=&get_course();
  607:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  608:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  609: 	undef(%slots);
  610:     } 
  611:     my $available;
  612:     if ($mgr eq 'F') {
  613: 	$r->print('<div>');
  614: 	$r->print('<form method="POST" action="/adm/slotrequest">
  615: <input type="hidden" name="command" value="uploadstart" />
  616: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  617: </form>');
  618: 	$r->print('<form method="POST" action="/adm/helper/newslot.helper">
  619: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  620: </form>');
  621: 	$r->print('</div>');
  622:     }
  623:     
  624:     my %Saveable_Parameters = ('show'    => 'array',
  625: 			       'when'    => 'scalar',
  626: 			       'order'   => 'scalar',
  627: 			       'deleted' => 'scalar',
  628: 			       );
  629:     &Apache::loncommon::store_course_settings('slotrequest',\%Saveable_Parameters);
  630:     &Apache::loncommon::restore_course_settings('slotrequest',\%Saveable_Parameters);
  631: 
  632:     my %show_fields=&Apache::lonlocal::texthash(
  633: 	     'name'         => 'Slot Name',
  634: 	     'description'  => 'Description',
  635: 	     'type'         => 'Type',
  636: 	     'starttime'    => 'Start time',
  637: 	     'endtime'      => 'End Time',
  638:              'startreserve' => 'Time students can start reserving',
  639: 	     'secret'       => 'Secret Word',
  640: 	     'maxspace'     => 'Maximum # of students',
  641: 	     'ip'           => 'IP or DNS restrictions',
  642: 	     'symb'         => 'Resource slot is restricted to.',
  643: 	     'uniqueperiod' => 'Period of time slot is unique',
  644: 	     'proctor'      => 'List of proctors');
  645:     my @show_order=('name','description','type','starttime','endtime',
  646: 	       'startreserve','secret','maxspace','ip','symb',
  647: 	       'uniqueperiod','proctor');
  648:     my @show = 
  649: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  650: 	                            : keys(%show_fields);
  651:     my %show =  map { $_ => 1 } (@show);
  652: 
  653:     my %when_fields=&Apache::lonlocal::texthash(
  654: 	     'now'      => 'Open now',
  655: 	     'nextweek' => 'Open within the next week',
  656: 	     'lastweek' => 'Were open last week',
  657: 	     'willopen' => 'Will open later',
  658: 	     'wereopen' => 'Were open',
  659: 	     'any'      => 'Anytime',
  660: 						);
  661:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
  662:     $when_fields{'select_form_order'} = \@when_order;
  663:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
  664:                                             : 'now';
  665: 
  666:     my $hide_radio = 
  667: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
  668:     my $show_radio = 
  669: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
  670: 	
  671:     $r->print('<form method="POST" action="/adm/slotrequest">
  672: <input type="hidden" name="command" value="showslots" />');
  673:     $r->print('<div>');
  674:     $r->print('<table class="inline">
  675:       <tr><th>'.&mt('Show').'</th>
  676:           <th>'.&mt('Open').'</th>
  677:           <th>'.&mt('Options').'</th>
  678:       </tr>
  679:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
  680: 	      '</td>
  681:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
  682:           '</td>
  683:            <td>
  684:             <table>
  685:               <tr>
  686:                 <td rowspan="2">Deleted slots:</td>
  687:                 <td><label>'.$show_radio.'Show</label></td>
  688:               </tr>
  689:               <tr>
  690:                 <td><label>'.$hide_radio.'Hide</label></td>
  691:               </tr>
  692:             </table>
  693: 	  </td>
  694:        </tr>
  695:     </table>');
  696:     $r->print('</div>');
  697:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
  698:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
  699:     $r->print('<table class="thinborder">
  700: <tr>
  701:   <th></th>');
  702:     foreach my $which (@show_order) {
  703: 	if ($which ne 'proctor' && exists($show{$which})) {
  704: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
  705: 	}
  706:     }
  707:     $r->print('<th>Scheduled Students</th></tr>');
  708: 
  709:     my %name_cache;
  710:     my $slotsort = sub {
  711: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
  712: 	    if (lc($slots{$a}->{$env{'form.order'}})
  713: 		ne lc($slots{$b}->{$env{'form.order'}})) {
  714: 		return (lc($slots{$a}->{$env{'form.order'}}) 
  715: 			cmp lc($slots{$b}->{$env{'form.order'}}));
  716: 	    }
  717: 	} elsif ($env{'form.order'} eq 'name') {
  718: 	    if (lc($a) cmp lc($b)) {
  719: 		return lc($a) cmp lc($b);
  720: 	    }
  721: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
  722: 	    
  723: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
  724: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
  725: 		return ($slots{$a}->{'uniqueperiod'}[0]
  726: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
  727: 	    }
  728: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
  729: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
  730: 		return ($slots{$a}->{'uniqueperiod'}[1]
  731: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
  732: 	    }
  733: 	}
  734: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
  735:     };
  736:     foreach my $slot (sort $slotsort (keys(%slots)))  {
  737: 	if (!&to_show($slots{$slot},$when,$env{'form.deleted'})) { next; }
  738: 	if (defined($slots{$slot}->{'type'})
  739: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
  740: 	    #next;
  741: 	}
  742: 	my $description=&get_description($slot,$slots{$slot});
  743: 	my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  744: 					   "^$slot\0");
  745: 	my $ids;
  746: 
  747: 	my ($tmp)=%consumed;
  748: 	if ($tmp !~ /^error: /) {
  749: 	    foreach my $entry (sort(keys(%consumed))) {
  750: 		my (undef,$id)=split("\0",$entry);
  751: 		my ($uname,$udom) = split('@',$consumed{$entry}{'name'});
  752: 		my $name = &Apache::loncommon::plainname($uname,$udom);
  753: 		$ids.= '<nobr>'.$name.&remove_link($slot,$entry,$uname,$udom,
  754: 						   $consumed{$entry}{'symb'})
  755: 		    .'</nobr><br />';
  756: 	    }
  757: 	}
  758: 
  759: 	my $start=($slots{$slot}->{'starttime'}?
  760: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
  761: 	my $end=($slots{$slot}->{'endtime'}?
  762: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
  763: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
  764: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
  765: 	
  766: 	my $unique;
  767: 	if (ref($slots{$slot}{'uniqueperiod'})) {
  768: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
  769: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
  770: 	}
  771: 
  772: 	my $title;
  773: 	if (exists($slots{$slot}{'symb'})) {
  774: 	    my (undef,undef,$res)=
  775: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
  776: 	    $res =   &Apache::lonnet::clutter($res);
  777: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
  778: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
  779: 	}
  780: 
  781: 	my @proctors;
  782: 	my $rowspan=1;
  783: 	my $colspan=1;
  784: 	if (exists($show{'proctor'})) {
  785: 	    $rowspan=2;
  786: 	    @proctors= map {
  787: 		my ($uname,$udom)=split(/@/,$_);
  788: 		my $fullname=$name_cache{$_};
  789: 		if (!defined($fullname)) {
  790: 		    &Apache::lonnet::logthis("Gettign $uname $udom");
  791: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
  792: 		    $fullname =~s/\s/&nbsp;/g;
  793: 		    $name_cache{$_} = $fullname;
  794: 		}
  795: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
  796: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
  797: 	}
  798: 	my $proctors=join(', ',@proctors);
  799: 
  800: 	my $edit=(<<"EDITLINK");
  801: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
  802: EDITLINK
  803: 
  804: 	my $delete=(<<"DELETELINK");
  805: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
  806: DELETELINK
  807:         if ($ids ne '') { undef($delete); }
  808: 
  809:         $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
  810: 	if (exists($show{'name'})) {
  811: 	    $colspan++;$r->print("<td>$slot</td>");
  812: 	}
  813: 	if (exists($show{'description'})) {
  814: 	    $colspan++;$r->print("<td>$description</td>\n");
  815: 	}
  816: 	if (exists($show{'type'})) {
  817: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
  818: 	}
  819: 	if (exists($show{'starttime'})) {
  820: 	    $colspan++;$r->print("<td>$start</td>\n");
  821: 	}
  822: 	if (exists($show{'endtime'})) {
  823: 	    $colspan++;$r->print("<td>$end</td>\n");
  824: 	}
  825: 	if (exists($show{'startreserve'})) {
  826: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
  827: 	}
  828: 	if (exists($show{'secret'})) {
  829: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
  830: 	}
  831: 	if (exists($show{'maxspace'})) {
  832: 	    $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
  833: 	}
  834: 	if (exists($show{'ip'})) {
  835: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
  836: 	}
  837: 	if (exists($show{'symb'})) {
  838: 	    $colspan++;$r->print("<td>$title</td>\n");
  839: 	}
  840: 	if (exists($show{'uniqueperiod'})) {
  841: 	    $colspan++;$r->print("<td>$unique</td>\n");
  842: 	}
  843: 	$colspan++;$r->print("<td>$ids</td>\n</tr>\n");
  844: 	if (exists($show{'proctor'})) {
  845: 	    $r->print(<<STUFF);
  846: <tr>
  847:  <td colspan="$colspan">$proctors</td>
  848: </tr>
  849: STUFF
  850:         }
  851:     }
  852:     $r->print('</table>');
  853: }
  854: 
  855: sub upload_start {
  856:     my ($r)=@_;    
  857:     $r->print(&Apache::grades::checkforfile_js());
  858:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
  859:     $result.='&nbsp;<b>'.
  860: 	&mt('Specify a file containing the slot definitions.').
  861: 	'</b></td></tr>'."\n";
  862:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
  863:     my $upfile_select=&Apache::loncommon::upfile_select_html();
  864:     my $ignore=&mt('Ignore First Line');
  865:     $result.=<<ENDUPFORM;
  866: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
  867: <input type="hidden" name="command" value="csvuploadmap" />
  868: $upfile_select
  869: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
  870: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
  871: </form>
  872: ENDUPFORM
  873:     $result.='</td></tr></table>'."\n";
  874:     $result.='</td></tr></table>'."\n";
  875:     $r->print($result);
  876: }
  877: 
  878: sub csvuploadmap_header {
  879:     my ($r,$datatoken,$distotal)= @_;
  880:     my $javascript;
  881:     if ($env{'form.upfile_associate'} eq 'reverse') {
  882: 	$javascript=&csvupload_javascript_reverse_associate();
  883:     } else {
  884: 	$javascript=&csvupload_javascript_forward_associate();
  885:     }
  886: 
  887:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
  888:     my $ignore=&mt('Ignore First Line');
  889:     $r->print(<<ENDPICK);
  890: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
  891: <h3>Identify fields</h3>
  892: Total number of records found in file: $distotal <hr />
  893: Enter as many fields as you can. The system will inform you and bring you back
  894: to this page if the data selected is insufficient to create the slots.<hr />
  895: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
  896: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
  897: <input type="hidden" name="associate"  value="" />
  898: <input type="hidden" name="datatoken"  value="$datatoken" />
  899: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
  900: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
  901: <input type="hidden" name="upfile_associate" 
  902:                                        value="$env{'form.upfile_associate'}" />
  903: <input type="hidden" name="command"    value="csvuploadassign" />
  904: <hr />
  905: <script type="text/javascript" language="Javascript">
  906: $javascript
  907: </script>
  908: ENDPICK
  909:     return '';
  910: 
  911: }
  912: 
  913: sub csvuploadmap_footer {
  914:     my ($request,$i,$keyfields) =@_;
  915:     $request->print(<<ENDPICK);
  916: </table>
  917: <input type="hidden" name="nfields" value="$i" />
  918: <input type="hidden" name="keyfields" value="$keyfields" />
  919: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
  920: </form>
  921: ENDPICK
  922: }
  923: 
  924: sub csvupload_javascript_reverse_associate {
  925:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
  926:     return(<<ENDPICK);
  927:   function verify(vf) {
  928:     var foundstart=0;
  929:     var foundend=0;
  930:     var foundname=0;
  931:     var foundtype=0;
  932:     for (i=0;i<=vf.nfields.value;i++) {
  933:       tw=eval('vf.f'+i+'.selectedIndex');
  934:       if (i==0 && tw!=0) { foundname=1; }
  935:       if (i==1 && tw!=0) { foundtype=1; }
  936:       if (i==2 && tw!=0) { foundstat=1; }
  937:       if (i==3 && tw!=0) { foundend=1; }
  938:     }
  939:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
  940: 	alert('$error1');
  941: 	return;
  942:     }
  943:     vf.submit();
  944:   }
  945:   function flip(vf,tf) {
  946:   }
  947: ENDPICK
  948: }
  949: 
  950: sub csvupload_javascript_forward_associate {
  951:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
  952:   return(<<ENDPICK);
  953:   function verify(vf) {
  954:     var foundstart=0;
  955:     var foundend=0;
  956:     var foundname=0;
  957:     var foundtype=0;
  958:     for (i=0;i<=vf.nfields.value;i++) {
  959:       tw=eval('vf.f'+i+'.selectedIndex');
  960:       if (tw==1) { foundname=1; }
  961:       if (tw==2) { foundtype=1; }
  962:       if (tw==3) { foundstat=1; }
  963:       if (tw==4) { foundend=1; }
  964:     }
  965:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
  966: 	alert('$error1');
  967: 	return;
  968:     }
  969:     vf.submit();
  970:   }
  971:   function flip(vf,tf) {
  972:   }
  973: ENDPICK
  974: }
  975: 
  976: sub csv_upload_map {
  977:     my ($r)= @_;
  978: 
  979:     my $datatoken;
  980:     if (!$env{'form.datatoken'}) {
  981: 	$datatoken=&Apache::loncommon::upfile_store($r);
  982:     } else {
  983: 	$datatoken=$env{'form.datatoken'};
  984: 	&Apache::loncommon::load_tmp_file($r);
  985:     }
  986:     my @records=&Apache::loncommon::upfile_record_sep();
  987:     if ($env{'form.noFirstLine'}) { shift(@records); }
  988:     &csvuploadmap_header($r,$datatoken,$#records+1);
  989:     my ($i,$keyfields);
  990:     if (@records) {
  991: 	my @fields=&csvupload_fields();
  992: 
  993: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
  994: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  995: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
  996: 							  \@fields);
  997: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
  998: 	    chop($keyfields);
  999: 	} else {
 1000: 	    unshift(@fields,['none','']);
 1001: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1002: 							    \@fields);
 1003: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1004: 	    $keyfields=join(',',sort(keys(%sone)));
 1005: 	}
 1006:     }
 1007:     &csvuploadmap_footer($r,$i,$keyfields);
 1008: 
 1009:     return '';
 1010: }
 1011: 
 1012: sub csvupload_fields {
 1013:     return (['name','Slot name'],
 1014: 	    ['type','Type of slot'],
 1015: 	    ['starttime','Start Time of slot'],
 1016: 	    ['endtime','End Time of slot'],
 1017: 	    ['startreserve','Reservation Start Time'],
 1018: 	    ['ip','IP or DNS restriction'],
 1019: 	    ['proctor','List of proctor ids'],
 1020: 	    ['description','Slot Description'],
 1021: 	    ['maxspace','Maximum number of reservations'],
 1022: 	    ['symb','Resource Restriction'],
 1023: 	    ['uniqueperiod','Date range of slot exclusion'],
 1024: 	    ['secret','Secret word proctor uses to validate']);
 1025: }
 1026: 
 1027: sub csv_upload_assign {
 1028:     my ($r,$mgr)= @_;
 1029:     &Apache::loncommon::load_tmp_file($r);
 1030:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1031:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1032:     my %fields=&Apache::grades::get_fields();
 1033:     $r->print('<h3>Creating Slots</h3>');
 1034:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1035:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1036:     my $countdone=0;
 1037:     my @errors;
 1038:     foreach my $slot (@slotdata) {
 1039: 	my %slot;
 1040: 	my %entries=&Apache::loncommon::record_sep($slot);
 1041: 	my $domain;
 1042: 	my $name=$entries{$fields{'name'}};
 1043: 	if ($name=~/^\s*$/) {
 1044: 	    push(@errors,"Did not create slot with no name");
 1045: 	    next;
 1046: 	}
 1047: 	if ($name=~/\s/) { 
 1048: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1049: 	    next;
 1050: 	}
 1051: 	if ($name=~/\W/) { 
 1052: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1053: 	    next;
 1054: 	}
 1055: 	if ($entries{$fields{'type'}}) {
 1056: 	    $slot{'type'}=$entries{$fields{'type'}};
 1057: 	} else {
 1058: 	    $slot{'type'}='preassigned';
 1059: 	}
 1060: 	if ($slot{'type'} ne 'preassigned' &&
 1061: 	    $slot{'type'} ne 'schedulable_student') {
 1062: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1063: 	    next;
 1064: 	}
 1065: 	if ($entries{$fields{'starttime'}}) {
 1066: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1067: 	}
 1068: 	if ($entries{$fields{'endtime'}}) {
 1069: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1070: 	}
 1071: 	if ($entries{$fields{'startreserve'}}) {
 1072: 	    $slot{'startreserve'}=
 1073: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1074: 	}
 1075: 	foreach my $key ('ip','proctor','description','maxspace',
 1076: 			 'secret','symb') {
 1077: 	    if ($entries{$fields{$key}}) {
 1078: 		$slot{$key}=$entries{$fields{$key}};
 1079: 	    }
 1080: 	}
 1081: 	if ($entries{$fields{'uniqueperiod'}}) {
 1082: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1083: 	    my @times=(&UnixDate($start,"%s"),
 1084: 		       &UnixDate($end,"%s"));
 1085: 	    $slot{'uniqueperiod'}=\@times;
 1086: 	}
 1087: 
 1088: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1089: 	$r->print('.');
 1090: 	$r->rflush();
 1091: 	$countdone++;
 1092:     }
 1093:     $r->print("<p>Created $countdone slots\n</p>");
 1094:     foreach my $error (@errors) {
 1095: 	$r->print("<p>$error\n</p>");
 1096:     }
 1097:     &show_table($r,$mgr);
 1098:     return '';
 1099: }
 1100: 
 1101: sub handler {
 1102:     my $r=shift;
 1103: 
 1104:     &Apache::loncommon::content_type($r,'text/html');
 1105:     &Apache::loncommon::no_cache($r);
 1106:     if ($r->header_only()) {
 1107: 	$r->send_http_header();
 1108: 	return OK;
 1109:     }
 1110: 
 1111:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1112:     
 1113:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1114:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1115:     my $title='Requesting Another Worktime';
 1116:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1117: 	$title = 'Managing Slots';
 1118:     }
 1119:     &start_page($r,$title);
 1120: 
 1121:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1122: 	&show_table($r,$mgr);
 1123:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1124: 	&remove_registration($r);
 1125:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1126: 	&release_slot($r,undef,undef,undef,$mgr);
 1127:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1128: 	&delete_slot($r);
 1129:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1130: 	&upload_start($r);
 1131:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1132: 	&csv_upload_map($r);
 1133:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1134: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1135: 	    &csv_upload_assign($r,$mgr);
 1136: 	} else {
 1137: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1138: 		$env{'form.upfile_associate'} = 'reverse';
 1139: 	    } else {
 1140: 		$env{'form.upfile_associate'} = 'forward';
 1141: 	    }
 1142: 	    &csv_upload_map($r);
 1143: 	}
 1144:     } else {
 1145: 	my $symb=&Apache::lonnet::unescape($env{'form.symb'});
 1146: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1147: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1148: 	if ($useslots ne 'resource') {
 1149: 	    &fail($r,'not_valid');
 1150: 	    return OK;
 1151: 	}
 1152: 	$env{'request.symb'}=$symb;
 1153: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1154: 	                               : 'problem';
 1155: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1156: 	if ($status eq 'CAN_ANSWER' ||
 1157: 	    $status eq 'NEEDS_CHECKIN' ||
 1158: 	    $status eq 'WAITING_FOR_GRADE') {
 1159: 	    &fail($r,'not_allowed');
 1160: 	    return OK;
 1161: 	}
 1162: 	if ($env{'form.requestattempt'}) {
 1163: 	    &show_choices($r,$symb);
 1164: 	} elsif ($env{'form.command'} eq 'release') {
 1165: 	    &release_slot($r,$symb);
 1166: 	} elsif ($env{'form.command'} eq 'get') {
 1167: 	    &get_slot($r,$symb);
 1168: 	} elsif ($env{'form.command'} eq 'change') {
 1169: 	    if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1170: 		&get_slot($r,$symb);
 1171: 	    }
 1172: 	} else {
 1173: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1174: 	}
 1175:     }
 1176:     &end_page($r);
 1177:     return OK;
 1178: }
 1179: 
 1180: 1;
 1181: __END__

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