File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.62: download - view: text, annotated - select for diffs
Thu May 18 20:53:24 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- missed a @ -> :

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

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