File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.68: download - view: text, annotated - select for diffs
Fri Jun 30 04:28:18 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- BUG#4892 slots restricted ato a group weren't working

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

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