File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.66: download - view: text, annotated - select for diffs
Tue Jun 20 03:32:40 2006 UTC (17 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- new useslots status of 'map_map'

    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.66 2006/06/20 03:32:40 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: 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' || $use_slots eq 'map_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' || $use_slots eq 'map_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&amp;slotname=$slotname&amp;entry=$entry&amp;uname=$uname&amp;udom=$udom&amp;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:     
  983:     #deleted slot filtering
  984:     #default to hide if no value
  985:     $env{'form.deleted'} ||= 'hide';
  986:     my $hide_radio = 
  987: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
  988:     my $show_radio = 
  989: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
  990: 	
  991:     $r->print('<form method="post" action="/adm/slotrequest">
  992: <input type="hidden" name="command" value="showslots" />');
  993:     $r->print('<div>');
  994:     $r->print('<table class="inline">
  995:       <tr><th>'.&mt('Show').'</th>
  996:           <th>'.&mt('Student Display').'</th>
  997:           <th>'.&mt('Open').'</th>
  998:           <th>'.&mt('Slot Name Filter').'</th>
  999:           <th>'.&mt('Options').'</th>
 1000:       </tr>
 1001:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
 1002: 	      '</td>
 1003:            <td>
 1004:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
 1005: 						    6,\%stu_display_fields,
 1006: 						    \@stu_display_order).'
 1007:            </td>
 1008:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
 1009:           '</td>
 1010:            <td>'.&Apache::loncommon::select_form($name_filter_type,
 1011: 						 'name_filter_type',
 1012: 						 %name_filter_type_fields).
 1013: 	      '<br />'.
 1014: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
 1015: 					      $env{'form.name_filter_value'},
 1016: 					      15).
 1017:           '</td>
 1018:            <td>
 1019:             <table>
 1020:               <tr>
 1021:                 <td rowspan="2">Deleted slots:</td>
 1022:                 <td><label>'.$show_radio.'Show</label></td>
 1023:               </tr>
 1024:               <tr>
 1025:                 <td><label>'.$hide_radio.'Hide</label></td>
 1026:               </tr>
 1027:             </table>
 1028: 	  </td>
 1029:        </tr>
 1030:     </table>');
 1031:     $r->print('</div>');
 1032:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
 1033:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
 1034:     $r->print(&Apache::loncommon::start_data_table().
 1035: 	      &Apache::loncommon::start_data_table_header_row().'
 1036: 	       <th></th>');
 1037:     foreach my $which (@show_order) {
 1038: 	if ($which ne 'proctor' && exists($show{$which})) {
 1039: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
 1040: 	}
 1041:     }
 1042:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1043: 
 1044:     my %name_cache;
 1045:     my $slotsort = sub {
 1046: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb|allowedsections|allowedusers)$/) {
 1047: 	    if (lc($slots{$a}->{$env{'form.order'}})
 1048: 		ne lc($slots{$b}->{$env{'form.order'}})) {
 1049: 		return (lc($slots{$a}->{$env{'form.order'}}) 
 1050: 			cmp lc($slots{$b}->{$env{'form.order'}}));
 1051: 	    }
 1052: 	} elsif ($env{'form.order'} eq 'name') {
 1053: 	    if (lc($a) cmp lc($b)) {
 1054: 		return lc($a) cmp lc($b);
 1055: 	    }
 1056: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
 1057: 	    
 1058: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
 1059: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
 1060: 		return ($slots{$a}->{'uniqueperiod'}[0]
 1061: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
 1062: 	    }
 1063: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
 1064: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
 1065: 		return ($slots{$a}->{'uniqueperiod'}[1]
 1066: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
 1067: 	    }
 1068: 	}
 1069: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
 1070:     };
 1071:     foreach my $slot (sort $slotsort (keys(%slots)))  {
 1072: 	if (!&to_show($slot,$slots{$slot},$when,
 1073: 		      $env{'form.deleted'},$name_filter)) { next; }
 1074: 	if (defined($slots{$slot}->{'type'})
 1075: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
 1076: 	    #next;
 1077: 	}
 1078: 	my $description=&get_description($slot,$slots{$slot});
 1079: 	my $ids;
 1080: 	if (exists($show{'scheduled'})) {
 1081: 	    my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
 1082: 					       "^$slot\0");
 1083: 	    my ($tmp)=%consumed;
 1084: 	    if ($tmp !~ /^error: /) {
 1085: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
 1086: 					      $consumed{$b}{name} }
 1087: 				   (keys(%consumed))) {
 1088: 		    my (undef,$id)=split("\0",$entry);
 1089: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
 1090: 		    $ids.= '<nobr>';
 1091: 		    foreach my $item (@stu_display_order) {
 1092: 			if ($stu_display{$item}) {
 1093: 			    if ($item eq 'fullname') {
 1094: 				$ids.=$fullname->{"$uname:$udom"}.' ';
 1095: 			    } elsif ($item eq 'username') {
 1096: 				$ids.="<tt>$uname:$udom</tt> ";
 1097: 			    }
 1098: 			}
 1099: 		    }
 1100: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
 1101: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
 1102: 		}
 1103: 	    }
 1104: 	}
 1105: 
 1106: 	my $start=($slots{$slot}->{'starttime'}?
 1107: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
 1108: 	my $end=($slots{$slot}->{'endtime'}?
 1109: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
 1110: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
 1111: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
 1112: 	
 1113: 	my $unique;
 1114: 	if (ref($slots{$slot}{'uniqueperiod'})) {
 1115: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
 1116: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
 1117: 	}
 1118: 
 1119: 	my $title;
 1120: 	if (exists($slots{$slot}{'symb'})) {
 1121: 	    my (undef,undef,$res)=
 1122: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
 1123: 	    $res =   &Apache::lonnet::clutter($res);
 1124: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
 1125: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
 1126: 	}
 1127: 
 1128: 	my $allowedsections;
 1129: 	if (exists($show{'allowedsections'})) {
 1130: 	    $allowedsections = 
 1131: 		join(', ',sort(split(/\s*,\s*/,
 1132: 				     $slots{$slot}->{'allowedsections'})));
 1133: 	}
 1134: 
 1135: 	my @allowedusers;
 1136: 	if (exists($show{'allowedusers'})) {
 1137: 	    @allowedusers= map {
 1138: 		my ($uname,$udom)=split(/:/,$_);
 1139: 		my $fullname=$name_cache{$_};
 1140: 		if (!defined($fullname)) {
 1141: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1142: 		    $fullname =~s/\s/&nbsp;/g;
 1143: 		    $name_cache{$_} = $fullname;
 1144: 		}
 1145: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1146: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
 1147: 	}
 1148: 	my $allowedusers=join(', ',@allowedusers);
 1149: 	
 1150: 	my @proctors;
 1151: 	my $rowspan=1;
 1152: 	my $colspan=1;
 1153: 	if (exists($show{'proctor'})) {
 1154: 	    $rowspan=2;
 1155: 	    @proctors= map {
 1156: 		my ($uname,$udom)=split(/:/,$_);
 1157: 		my $fullname=$name_cache{$_};
 1158: 		if (!defined($fullname)) {
 1159: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1160: 		    $fullname =~s/\s/&nbsp;/g;
 1161: 		    $name_cache{$_} = $fullname;
 1162: 		}
 1163: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1164: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
 1165: 	}
 1166: 	my $proctors=join(', ',@proctors);
 1167: 
 1168: 	my $edit=(<<"EDITLINK");
 1169: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
 1170: EDITLINK
 1171: 
 1172: 	my $delete=(<<"DELETELINK");
 1173: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
 1174: DELETELINK
 1175: 
 1176:         my $remove_all=&remove_link($slot,'remove all').'<br />';
 1177: 
 1178:         if ($ids ne '') { undef($delete); }
 1179: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
 1180: 	    || $ids eq '') { 
 1181: 	    undef($remove_all);
 1182: 	}
 1183: 
 1184: 	my $row_start=&Apache::loncommon::start_data_table_row();
 1185: 	my $row_end=&Apache::loncommon::end_data_table_row();
 1186:         $r->print($row_start.
 1187: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
 1188: 	if (exists($show{'name'})) {
 1189: 	    $colspan++;$r->print("<td>$slot</td>");
 1190: 	}
 1191: 	if (exists($show{'description'})) {
 1192: 	    $colspan++;$r->print("<td>$description</td>\n");
 1193: 	}
 1194: 	if (exists($show{'type'})) {
 1195: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
 1196: 	}
 1197: 	if (exists($show{'starttime'})) {
 1198: 	    $colspan++;$r->print("<td>$start</td>\n");
 1199: 	}
 1200: 	if (exists($show{'endtime'})) {
 1201: 	    $colspan++;$r->print("<td>$end</td>\n");
 1202: 	}
 1203: 	if (exists($show{'startreserve'})) {
 1204: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
 1205: 	}
 1206: 	if (exists($show{'secret'})) {
 1207: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
 1208: 	}
 1209: 	if (exists($show{'maxspace'})) {
 1210: 	    $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
 1211: 	}
 1212: 	if (exists($show{'ip'})) {
 1213: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
 1214: 	}
 1215: 	if (exists($show{'symb'})) {
 1216: 	    $colspan++;$r->print("<td>$title</td>\n");
 1217: 	}
 1218: 	if (exists($show{'allowedsections'})) {
 1219: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
 1220: 	}
 1221: 	if (exists($show{'allowedusers'})) {
 1222: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
 1223: 	}
 1224: 	if (exists($show{'uniqueperiod'})) {
 1225: 	    $colspan++;$r->print("<td>$unique</td>\n");
 1226: 	}
 1227: 	if (exists($show{'scheduled'})) {
 1228: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
 1229: 	}
 1230: 	$r->print("$row_end\n");
 1231: 	if (exists($show{'proctor'})) {
 1232: 	    $r->print(<<STUFF);
 1233: $row_start
 1234:  <td colspan="$colspan">$proctors</td>
 1235: $row_end
 1236: STUFF
 1237:         }
 1238:     }
 1239:     $r->print('</table></form>');
 1240: }
 1241: 
 1242: sub upload_start {
 1243:     my ($r)=@_;    
 1244:     $r->print(&Apache::grades::checkforfile_js());
 1245:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
 1246:     $result.='&nbsp;<b>'.
 1247: 	&mt('Specify a file containing the slot definitions.').
 1248: 	'</b></td></tr>'."\n";
 1249:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 1250:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1251:     my $ignore=&mt('Ignore First Line');
 1252:     $result.=<<ENDUPFORM;
 1253: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1254: <input type="hidden" name="command" value="csvuploadmap" />
 1255: $upfile_select
 1256: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
 1257: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 1258: </form>
 1259: ENDUPFORM
 1260:     $result.='</td></tr></table>'."\n";
 1261:     $result.='</td></tr></table>'."\n";
 1262:     $r->print($result);
 1263: }
 1264: 
 1265: sub csvuploadmap_header {
 1266:     my ($r,$datatoken,$distotal)= @_;
 1267:     my $javascript;
 1268:     if ($env{'form.upfile_associate'} eq 'reverse') {
 1269: 	$javascript=&csvupload_javascript_reverse_associate();
 1270:     } else {
 1271: 	$javascript=&csvupload_javascript_forward_associate();
 1272:     }
 1273: 
 1274:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 1275:     my $ignore=&mt('Ignore First Line');
 1276:     $r->print(<<ENDPICK);
 1277: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1278: <h3>Identify fields</h3>
 1279: Total number of records found in file: $distotal <hr />
 1280: Enter as many fields as you can. The system will inform you and bring you back
 1281: to this page if the data selected is insufficient to create the slots.<hr />
 1282: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 1283: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 1284: <input type="hidden" name="associate"  value="" />
 1285: <input type="hidden" name="datatoken"  value="$datatoken" />
 1286: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 1287: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 1288: <input type="hidden" name="upfile_associate" 
 1289:                                        value="$env{'form.upfile_associate'}" />
 1290: <input type="hidden" name="command"    value="csvuploadassign" />
 1291: <hr />
 1292: <script type="text/javascript" language="Javascript">
 1293: $javascript
 1294: </script>
 1295: ENDPICK
 1296:     return '';
 1297: 
 1298: }
 1299: 
 1300: sub csvuploadmap_footer {
 1301:     my ($request,$i,$keyfields) =@_;
 1302:     $request->print(<<ENDPICK);
 1303: </table>
 1304: <input type="hidden" name="nfields" value="$i" />
 1305: <input type="hidden" name="keyfields" value="$keyfields" />
 1306: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
 1307: </form>
 1308: ENDPICK
 1309: }
 1310: 
 1311: sub csvupload_javascript_reverse_associate {
 1312:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1313:     return(<<ENDPICK);
 1314:   function verify(vf) {
 1315:     var foundstart=0;
 1316:     var foundend=0;
 1317:     var foundname=0;
 1318:     var foundtype=0;
 1319:     for (i=0;i<=vf.nfields.value;i++) {
 1320:       tw=eval('vf.f'+i+'.selectedIndex');
 1321:       if (i==0 && tw!=0) { foundname=1; }
 1322:       if (i==1 && tw!=0) { foundtype=1; }
 1323:       if (i==2 && tw!=0) { foundstat=1; }
 1324:       if (i==3 && tw!=0) { foundend=1; }
 1325:     }
 1326:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1327: 	alert('$error1');
 1328: 	return;
 1329:     }
 1330:     vf.submit();
 1331:   }
 1332:   function flip(vf,tf) {
 1333:   }
 1334: ENDPICK
 1335: }
 1336: 
 1337: sub csvupload_javascript_forward_associate {
 1338:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1339:   return(<<ENDPICK);
 1340:   function verify(vf) {
 1341:     var foundstart=0;
 1342:     var foundend=0;
 1343:     var foundname=0;
 1344:     var foundtype=0;
 1345:     for (i=0;i<=vf.nfields.value;i++) {
 1346:       tw=eval('vf.f'+i+'.selectedIndex');
 1347:       if (tw==1) { foundname=1; }
 1348:       if (tw==2) { foundtype=1; }
 1349:       if (tw==3) { foundstat=1; }
 1350:       if (tw==4) { foundend=1; }
 1351:     }
 1352:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1353: 	alert('$error1');
 1354: 	return;
 1355:     }
 1356:     vf.submit();
 1357:   }
 1358:   function flip(vf,tf) {
 1359:   }
 1360: ENDPICK
 1361: }
 1362: 
 1363: sub csv_upload_map {
 1364:     my ($r)= @_;
 1365: 
 1366:     my $datatoken;
 1367:     if (!$env{'form.datatoken'}) {
 1368: 	$datatoken=&Apache::loncommon::upfile_store($r);
 1369:     } else {
 1370: 	$datatoken=$env{'form.datatoken'};
 1371: 	&Apache::loncommon::load_tmp_file($r);
 1372:     }
 1373:     my @records=&Apache::loncommon::upfile_record_sep();
 1374:     if ($env{'form.noFirstLine'}) { shift(@records); }
 1375:     &csvuploadmap_header($r,$datatoken,$#records+1);
 1376:     my ($i,$keyfields);
 1377:     if (@records) {
 1378: 	my @fields=&csvupload_fields();
 1379: 
 1380: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 1381: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 1382: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 1383: 							  \@fields);
 1384: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 1385: 	    chop($keyfields);
 1386: 	} else {
 1387: 	    unshift(@fields,['none','']);
 1388: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1389: 							    \@fields);
 1390: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1391: 	    $keyfields=join(',',sort(keys(%sone)));
 1392: 	}
 1393:     }
 1394:     &csvuploadmap_footer($r,$i,$keyfields);
 1395: 
 1396:     return '';
 1397: }
 1398: 
 1399: sub csvupload_fields {
 1400:     return (['name','Slot name'],
 1401: 	    ['type','Type of slot'],
 1402: 	    ['starttime','Start Time of slot'],
 1403: 	    ['endtime','End Time of slot'],
 1404: 	    ['startreserve','Reservation Start Time'],
 1405: 	    ['ip','IP or DNS restriction'],
 1406: 	    ['proctor','List of proctor ids'],
 1407: 	    ['description','Slot Description'],
 1408: 	    ['maxspace','Maximum number of reservations'],
 1409: 	    ['symb','Resource Restriction'],
 1410: 	    ['uniqueperiod','Date range of slot exclusion'],
 1411: 	    ['secret','Secret word proctor uses to validate'],
 1412: 	    ['allowedsections','Sections slot is restricted to'],
 1413: 	    ['allowedusers','Users slot is restricted to'],
 1414: 	    );
 1415: }
 1416: 
 1417: sub csv_upload_assign {
 1418:     my ($r,$mgr)= @_;
 1419:     &Apache::loncommon::load_tmp_file($r);
 1420:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1421:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1422:     my %fields=&Apache::grades::get_fields();
 1423:     $r->print('<h3>Creating Slots</h3>');
 1424:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1425:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1426:     my $countdone=0;
 1427:     my @errors;
 1428:     foreach my $slot (@slotdata) {
 1429: 	my %slot;
 1430: 	my %entries=&Apache::loncommon::record_sep($slot);
 1431: 	my $domain;
 1432: 	my $name=$entries{$fields{'name'}};
 1433: 	if ($name=~/^\s*$/) {
 1434: 	    push(@errors,"Did not create slot with no name");
 1435: 	    next;
 1436: 	}
 1437: 	if ($name=~/\s/) { 
 1438: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1439: 	    next;
 1440: 	}
 1441: 	if ($name=~/\W/) { 
 1442: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1443: 	    next;
 1444: 	}
 1445: 	if ($entries{$fields{'type'}}) {
 1446: 	    $slot{'type'}=$entries{$fields{'type'}};
 1447: 	} else {
 1448: 	    $slot{'type'}='preassigned';
 1449: 	}
 1450: 	if ($slot{'type'} ne 'preassigned' &&
 1451: 	    $slot{'type'} ne 'schedulable_student') {
 1452: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1453: 	    next;
 1454: 	}
 1455: 	if ($entries{$fields{'starttime'}}) {
 1456: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1457: 	}
 1458: 	if ($entries{$fields{'endtime'}}) {
 1459: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1460: 	}
 1461: 
 1462: 	# start/endtime must be defined and greater than zero
 1463: 	if (!$slot{'starttime'}) {
 1464: 	    push(@errors,"$name not created -- Invalid start time");
 1465: 	    next;
 1466: 	}
 1467: 	if (!$slot{'endtime'}) {
 1468: 	    push(@errors,"$name not created -- Invalid end time");
 1469: 	    next;
 1470: 	}
 1471: 	if ($slot{'starttime'} > $slot{'endtime'}) {
 1472: 	    push(@errors,"$name not created -- Slot starts after it ends");
 1473: 	    next;
 1474: 	}
 1475: 
 1476: 	if ($entries{$fields{'startreserve'}}) {
 1477: 	    $slot{'startreserve'}=
 1478: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1479: 	}
 1480: 	if (defined($slot{'startreserve'})
 1481: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
 1482: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
 1483: 	    next;
 1484: 	}
 1485: 
 1486: 	foreach my $key ('ip','proctor','description','maxspace',
 1487: 			 'secret','symb') {
 1488: 	    if ($entries{$fields{$key}}) {
 1489: 		$slot{$key}=$entries{$fields{$key}};
 1490: 	    }
 1491: 	}
 1492: 
 1493: 	if ($entries{$fields{'uniqueperiod'}}) {
 1494: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1495: 	    my @times=(&UnixDate($start,"%s"),
 1496: 		       &UnixDate($end,"%s"));
 1497: 	    $slot{'uniqueperiod'}=\@times;
 1498: 	}
 1499: 	if (defined($slot{'uniqueperiod'})
 1500: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
 1501: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
 1502: 	    next;
 1503: 	}
 1504: 
 1505: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1506: 	$r->print('.');
 1507: 	$r->rflush();
 1508: 	$countdone++;
 1509:     }
 1510:     $r->print("<p>Created $countdone slots\n</p>");
 1511:     foreach my $error (@errors) {
 1512: 	$r->print("<p>$error\n</p>");
 1513:     }
 1514:     &show_table($r,$mgr);
 1515:     return '';
 1516: }
 1517: 
 1518: sub handler {
 1519:     my $r=shift;
 1520: 
 1521:     &Apache::loncommon::content_type($r,'text/html');
 1522:     &Apache::loncommon::no_cache($r);
 1523:     if ($r->header_only()) {
 1524: 	$r->send_http_header();
 1525: 	return OK;
 1526:     }
 1527: 
 1528:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1529:     
 1530:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1531:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1532:     my $title='Requesting Another Worktime';
 1533:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1534: 	$title = 'Managing Slots';
 1535:     }
 1536:     &start_page($r,$title);
 1537: 
 1538:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1539: 	&show_table($r,$mgr);
 1540:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1541: 	&remove_registration($r);
 1542:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1543: 	if ($env{'form.entry'} eq 'remove all') {
 1544: 	    &release_all_slot($r,$mgr);
 1545: 	} else {
 1546: 	    &release_slot($r,undef,undef,undef,$mgr);
 1547: 	}
 1548:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1549: 	&delete_slot($r);
 1550:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1551: 	&upload_start($r);
 1552:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1553: 	&csv_upload_map($r);
 1554:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1555: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1556: 	    &csv_upload_assign($r,$mgr);
 1557: 	} else {
 1558: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1559: 		$env{'form.upfile_associate'} = 'reverse';
 1560: 	    } else {
 1561: 		$env{'form.upfile_associate'} = 'forward';
 1562: 	    }
 1563: 	    &csv_upload_map($r);
 1564: 	}
 1565:     } else {
 1566: 	my $symb=&unescape($env{'form.symb'});
 1567: 	if (!defined($symb)) {
 1568: 	    &fail($r,'not_valid');
 1569: 	    return OK;
 1570: 	}
 1571: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1572: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1573: 	if ($useslots ne 'resource' 
 1574: 	    && $useslots ne 'map' 
 1575: 	    && $useslots ne 'map_map') {
 1576: 	    &fail($r,'not_available');
 1577: 	    return OK;
 1578: 	}
 1579: 	$env{'request.symb'}=$symb;
 1580: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1581: 	                               : 'problem';
 1582: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1583: 	if ($status eq 'CAN_ANSWER' ||
 1584: 	    $status eq 'NEEDS_CHECKIN' ||
 1585: 	    $status eq 'WAITING_FOR_GRADE') {
 1586: 	    &fail($r,'not_allowed');
 1587: 	    return OK;
 1588: 	}
 1589: 	if ($env{'form.requestattempt'}) {
 1590: 	    &show_choices($r,$symb);
 1591: 	} elsif ($env{'form.command'} eq 'release') {
 1592: 	    &release_slot($r,$symb);
 1593: 	} elsif ($env{'form.command'} eq 'get') {
 1594: 	    &get_slot($r,$symb);
 1595: 	} elsif ($env{'form.command'} eq 'change') {
 1596: 	    if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1597: 		&get_slot($r,$symb);
 1598: 	    }
 1599: 	} else {
 1600: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1601: 	}
 1602:     }
 1603:     &end_page($r);
 1604:     return OK;
 1605: }
 1606: 
 1607: 1;
 1608: __END__

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