File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.63: download - view: text, annotated - select for diffs
Tue May 30 12:46:09 2006 UTC (18 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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