File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.54: download - view: text, annotated - select for diffs
Thu Mar 30 04:34:32 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- filter which slots to show based on name

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

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