File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.57: download - view: text, annotated - select for diffs
Mon Apr 10 07:37:16 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- switching to use the more official : as domain username seperator (rather tahn @, since MSU will want @ in valid usernames)

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

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