File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.115: download - view: text, annotated - select for diffs
Thu Aug 23 14:17:13 2012 UTC (11 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Subroutine name change in lonnet.pm. Update places where called.
  - old name: instructor_log
  - temporary name (lonnet.pm rev. 1.1184): write_rolelog
  - new name: write_log

    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.115 2012/08/23 14:17:13 raeburn 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,$brcrum)=@_;
   60:     my $args;
   61:     if (ref($brcrum) eq 'ARRAY') {
   62:         $args = {bread_crumbs => $brcrum};
   63:     }
   64:     $r->print(&Apache::loncommon::start_page($title,undef,$args));
   65: }
   66: 
   67: sub end_page {
   68:     my ($r)=@_;
   69:     $r->print(&Apache::loncommon::end_page());
   70: }
   71: 
   72: =pod
   73: 
   74:  slot_reservations db
   75:    - keys are 
   76:     - slotname\0id -> value is an hashref of
   77:                          name -> user@domain of holder
   78:                          timestamp -> timestamp of reservation
   79:                          symb -> symb of resource that it is reserved for
   80: 
   81: =cut
   82: 
   83: sub get_course {
   84:     (undef,my $courseid)=&Apache::lonnet::whichuser();
   85:     my $cdom=$env{'course.'.$courseid.'.domain'};
   86:     my $cnum=$env{'course.'.$courseid.'.num'};
   87:     return ($cnum,$cdom);
   88: }
   89: 
   90: sub get_reservation_ids {
   91:     my ($slot_name)=@_;
   92:     
   93:     my ($cnum,$cdom)=&get_course();
   94: 
   95:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
   96: 				       "^$slot_name\0");
   97:     if (&Apache::lonnet::error(%consumed)) { 
   98: 	return 'error: Unable to determine current status';
   99:     }
  100:     my ($tmp)=%consumed;
  101:     if ($tmp=~/^error: 2 / ) {
  102: 	return 0;
  103:     }
  104:     return keys(%consumed);
  105: }
  106: 
  107: sub space_available {
  108:     my ($slot_name,$slot)=@_;
  109:     my $max=$slot->{'maxspace'};
  110: 
  111:     if (!defined($max)) { return 1; }
  112: 
  113:     my $consumed=scalar(&get_reservation_ids($slot_name));
  114:     if ($consumed < $max) {
  115: 	return 1
  116:     }
  117:     return 0;
  118: }
  119: 
  120: sub check_for_reservation {
  121:     my ($symb,$mode)=@_;
  122:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  123: 				       $env{'user.domain'}, $env{'user.name'});
  124: 
  125:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  126: 				    $env{'user.domain'}, $env{'user.name'});
  127:     my @slots = (split(/:/,$student), split(/:/, $course));
  128: 
  129:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  130: 
  131:     my ($cnum,$cdom)=&get_course();
  132:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  133: 
  134:     if (&Apache::lonnet::error($student) 
  135: 	|| &Apache::lonnet::error($course)
  136: 	|| &Apache::lonnet::error(%slots)) {
  137: 	return 'error: Unable to determine current status';
  138:     }    
  139:     my @got;
  140:     my @sorted_slots = &Apache::loncommon::sorted_slots(\@slots,\%slots,'starttime');
  141:     foreach my $slot_name (@sorted_slots) {
  142: 	next if (!defined($slots{$slot_name}) ||
  143: 		 !ref($slots{$slot_name}));
  144: 	&Apache::lonxml::debug(time." $slot_name ".
  145: 			       $slots{$slot_name}->{'starttime'}." -- ".
  146: 			       $slots{$slot_name}->{'startreserve'}." -- ".
  147:                                $slots{$slot_name}->{'endreserve'});
  148: 	if (($slots{$slot_name}->{'endtime'} > time) &&
  149: 	    ($slots{$slot_name}->{'startreserve'} < time) &&
  150:             ((!$slots{$slot_name}->{'endreserve'}) || 
  151:              ($slots{$slot_name}->{'endreserve'} > time))) {
  152: 	    # between start of reservation time and end of reservation time
  153:             # and before end of slot
  154: 	    if ($mode eq 'allslots') {
  155: 		push(@got,$slot_name);
  156: 	    } else {
  157: 		return($slot_name, $slots{$slot_name});
  158: 	    }
  159: 	}
  160:     }
  161:     if ($mode eq 'allslots' && @got) {
  162: 	return @got;
  163:     }
  164:     return (undef,undef);
  165: }
  166: 
  167: sub get_consumed_uniqueperiods {
  168:     my ($slots) = @_;
  169:     my $navmap=Apache::lonnavmaps::navmap->new;
  170:     if (!defined($navmap)) {
  171:         return 'error: Unable to determine current status';
  172:     }
  173:     my @problems = $navmap->retrieveResources(undef,
  174: 					      sub { $_[0]->is_problem() },1,0);
  175:     my %used_slots;
  176:     foreach my $problem (@problems) {
  177: 	my $symb = $problem->symb();
  178: 	my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
  179: 					   $symb, $env{'user.domain'},
  180: 					   $env{'user.name'});
  181: 	my $course =  &Apache::lonnet::EXT("resource.0.available",
  182: 					   $symb, $env{'user.domain'},
  183: 					   $env{'user.name'});
  184: 	if (&Apache::lonnet::error($student) 
  185: 	    || &Apache::lonnet::error($course)) {
  186: 	    return 'error: Unable to determine current status';
  187: 	}
  188: 	foreach my $slot (split(/:/,$student), split(/:/, $course)) {
  189: 	    $used_slots{$slot}=1;
  190: 	}
  191:     }
  192: 
  193:     if (!ref($slots)) {
  194: 	my ($cnum,$cdom)=&get_course();
  195: 	my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
  196: 	if (&Apache::lonnet::error(%slots)) {
  197: 	    return 'error: Unable to determine current status';
  198: 	}
  199: 	$slots = \%slots;
  200:     }
  201: 
  202:     my %consumed_uniqueperiods;
  203:     foreach my $slot_name (keys(%used_slots)) {
  204: 	next if (!defined($slots->{$slot_name}) ||
  205: 		 !ref($slots->{$slot_name}));
  206: 	
  207:         next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
  208: 		 !ref($slots->{$slot_name}{'uniqueperiod'}));
  209: 	$consumed_uniqueperiods{$slot_name} = 
  210: 	    $slots->{$slot_name}{'uniqueperiod'};
  211:     }
  212:     return \%consumed_uniqueperiods;
  213: }
  214: 
  215: sub check_for_conflict {
  216:     my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
  217: 
  218:     if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
  219: 
  220:     if (!ref($consumed_uniqueperiods)) {
  221: 	$consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
  222:         if (ref($consumed_uniqueperiods) eq 'HASH') {
  223: 	    if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
  224: 	        return 'error: Unable to determine current status';
  225: 	    }
  226:         } else {
  227:             return 'error: Unable to determine current status';
  228:         }
  229:     }
  230:     
  231:     my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
  232:     foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
  233: 	my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
  234: 	if (!
  235: 	    ($start < $new_uniq_start &&  $end < $new_uniq_start) ||
  236: 	    ($start > $new_uniq_end   &&  $end > $new_uniq_end  )) {
  237: 	    return $slot_name;
  238: 	}
  239:     }
  240:     return undef;
  241: }
  242: 
  243: sub make_reservation {
  244:     my ($slot_name,$slot,$symb,$cnum,$cdom)=@_;
  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",$symb,
  251: 					 $env{'user.domain'},$env{'user.name'});
  252:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
  253: 
  254:     if (&Apache::lonnet::error($value) 
  255: 	|| &Apache::lonnet::error($use_slots)) { 
  256: 	return 'error: Unable to determine current status';
  257:     }
  258: 
  259:     my $parm_symb  = $symb;
  260:     my $parm_level = 1;
  261:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
  262: 	my ($map) = &Apache::lonnet::decode_symb($symb);
  263: 	$parm_symb = &Apache::lonnet::symbread($map);
  264: 	$parm_level = 2;
  265:     }
  266: 
  267:     foreach my $other_slot (split(/:/, $value)) {
  268: 	if ($other_slot eq $slot_name) {
  269: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  270: 					       $cnum, "^$slot_name\0");   
  271: 	    if (&Apache::lonnet::error($value)) { 
  272: 		return 'error: Unable to determine current status';
  273: 	    }
  274: 	    my $me=$env{'user.name'}.':'.$env{'user.domain'};
  275: 	    foreach my $key (keys(%consumed)) {
  276: 		if ($consumed{$key}->{'name'} eq $me) {
  277: 		    my $num=(split('\0',$key))[1];
  278: 		    return -$num;
  279: 		}
  280: 	    }
  281: 	}
  282:     }
  283: 
  284:     my $max=$slot->{'maxspace'};
  285:     if (!defined($max)) { $max=99999; }
  286: 
  287:     my (@ids)=&get_reservation_ids($slot_name);
  288:     if (&Apache::lonnet::error(@ids)) { 
  289: 	return 'error: Unable to determine current status';
  290:     }
  291:     my $last=0;
  292:     foreach my $id (@ids) {
  293: 	my $num=(split('\0',$id))[1];
  294: 	if ($num > $last) { $last=$num; }
  295:     }
  296:     
  297:     my $wanted=$last+1;
  298:     &Apache::lonxml::debug("wanted $wanted<br />");
  299:     if (scalar(@ids) >= $max) {
  300: 	# full up
  301: 	return undef;
  302:     }
  303:     
  304:     my %reservation=('name'      => $env{'user.name'}.':'.$env{'user.domain'},
  305: 		     'timestamp' => time,
  306: 		     'symb'      => $parm_symb);
  307: 
  308:     my $success=&Apache::lonnet::newput('slot_reservations',
  309: 					{"$slot_name\0$wanted" =>
  310: 					     \%reservation},
  311: 					$cdom, $cnum);
  312: 
  313:     if ($success eq 'ok') {
  314: 	my $new_value=$slot_name;
  315: 	if ($value) {
  316: 	    $new_value=$value.':'.$new_value;
  317: 	}
  318:         &store_slot_parm($symb,$slot_name,$parm_level,$new_value,$cnum,$cdom);
  319: 	return $wanted;
  320:     }
  321: 
  322:     # someone else got it
  323:     return undef;
  324: }
  325: 
  326: sub store_slot_parm {
  327:     my ($symb,$slot_name,$parm_level,$new_value,$cnum,$cdom) = @_;
  328:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  329:                                                   '0_availablestudent',
  330:                                                    $parm_level, $new_value,
  331:                                                    'string',
  332:                                                    $env{'user.name'},
  333:                                                    $env{'user.domain'});
  334:     &Apache::lonxml::debug("hrrm $result");
  335:     my %storehash = (
  336:                        symb    => $symb,
  337:                        slot    => $slot_name,
  338:                        action  => 'reserve',
  339:                        context => $env{'form.context'},
  340:                     );
  341: 
  342:     &Apache::lonnet::write_log('slotreservationslog',\%storehash,
  343:                                '',$env{'user.name'},$env{'user.domain'},
  344:                                $cnum,$cdom);
  345:     &Apache::lonnet::write_log($cdom.'_'.$cnum.'_slotlog',\%storehash,
  346:                                1,$env{'user.name'},$env{'user.domain'},
  347:                                $env{'user.name'},$env{'user.domain'});
  348: 
  349:     return;
  350: }
  351: 
  352: sub remove_registration {
  353:     my ($r) = @_;
  354:     if ($env{'form.entry'} ne 'remove all') {
  355: 	return &remove_registration_user($r);
  356:     }
  357:     my $slot_name = $env{'form.slotname'};
  358:     my %slot=&Apache::lonnet::get_slot($slot_name);
  359: 
  360:     my ($cnum,$cdom)=&get_course();
  361:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  362: 				       "^$slot_name\0");
  363:     if (&Apache::lonnet::error(%consumed)) {
  364: 	$r->print("<p><span class=\"LC_error\">".&mt('A network error has occurred.').'</span></p>');
  365: 	return;
  366:     }
  367:     if (!%consumed) {
  368: 	$r->print('<p>'.&mt('Slot [_1] has no reservations.',
  369: 			    '<tt>'.$slot_name.'</tt>').'</p>');
  370: 	return;
  371:     }
  372: 
  373:     my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed)));
  374:     my $names = join(' ',@names);
  375: 
  376:     my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name);
  377:     &remove_registration_confirmation($r,$msg,['entry','slotname','context']);
  378: }
  379: 
  380: sub remove_registration_user {
  381:     my ($r) = @_;
  382:     
  383:     my $slot_name = $env{'form.slotname'};
  384: 
  385:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
  386: 					     $env{'form.udom'});
  387: 
  388:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
  389: 
  390:     my $msg = &mt('Remove [_1] from slot [_2] for [_3]',
  391: 		  $name,$slot_name,$title);
  392:     
  393:     &remove_registration_confirmation($r,$msg,['uname','udom','slotname',
  394: 					       'entry','symb','context']);
  395: }
  396: 
  397: sub remove_registration_confirmation {
  398:     my ($r,$msg,$inputs) =@_;
  399: 
  400:     my $hidden_input;
  401:     foreach my $parm (@{$inputs}) {
  402: 	$hidden_input .=
  403: 	    '<input type="hidden" name="'.$parm.'" value="'
  404: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
  405:     }
  406:     my %lt = &Apache::lonlocal::texthash(
  407:         'yes' => 'Yes',
  408:         'no'  => 'No',
  409:     );
  410:     $r->print(<<"END_CONFIRM");
  411: <p> $msg </p>
  412: <form action="/adm/slotrequest" method="post">
  413:     <input type="hidden" name="command" value="release" />
  414:     <input type="hidden" name="button" value="yes" />
  415:     $hidden_input
  416:     <input type="submit" value="$lt{'yes'}" />
  417: </form>
  418: <form action="/adm/slotrequest" method="post">
  419:     <input type="hidden" name="command" value="showslots" />
  420:     <input type="submit" value="$lt{'no'}" />
  421: </form>
  422: END_CONFIRM
  423: 
  424: }
  425: 
  426: sub release_all_slot {
  427:     my ($r,$mgr)=@_;
  428:     
  429:     my $slot_name = $env{'form.slotname'};
  430: 
  431:     my ($cnum,$cdom)=&get_course();
  432: 
  433:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  434: 				       "^$slot_name\0");
  435:     
  436:     $r->print('<p>'.&mt('Releasing reservations').'</p>');
  437: 
  438:     foreach my $entry (sort { $consumed{$a}{'name'} cmp 
  439: 				  $consumed{$b}{'name'} } (keys(%consumed))) {
  440: 	my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
  441: 	my ($result,$msg) =
  442: 	    &release_reservation($slot_name,$uname,$udom,
  443: 				 $consumed{$entry}{'symb'},$mgr);
  444:         if (!$result) {
  445:             $r->print('<p><span class="LC_error">'.&mt($msg).'</span></p>');
  446:         } else {
  447: 	    $r->print("<p>$msg</p>");
  448:         }
  449: 	$r->rflush();
  450:     }
  451:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  452: 	      &mt('Return to slot list').'</a></p>');
  453:     &return_link($r);
  454: }
  455: 
  456: sub release_slot {
  457:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
  458: 
  459:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  460: 
  461:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
  462:     if ($mgr eq 'F' 
  463: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
  464: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
  465:     }
  466: 
  467:     if ($mgr eq 'F' 
  468: 	&& defined($env{'form.symb'})) {
  469: 	$symb = &unescape($env{'form.symb'});
  470:     }
  471: 
  472:     my ($result,$msg) =
  473: 	&release_reservation($slot_name,$uname,$udom,$symb,$mgr);
  474:     if (!$result) {
  475:         $r->print('<p><span class="LC_error">'.&mt($msg).'</span></p>');
  476:     } else {
  477:         $r->print("<p>$msg</p>");
  478:     }
  479:     
  480:     if ($mgr eq 'F') {
  481: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  482: 		  &mt('Return to slot list').'</a></p>');
  483:     }
  484: 
  485:     if (!$inhibit_return_link) { &return_link($r);  }
  486:     return $result;
  487: }
  488: 
  489: sub release_reservation {
  490:     my ($slot_name,$uname,$udom,$symb,$mgr) = @_;
  491:     my %slot=&Apache::lonnet::get_slot($slot_name);
  492:     my $description=&get_description($slot_name,\%slot);
  493: 
  494:     if ($mgr ne 'F') {
  495: 	if ($slot{'starttime'} < time) {
  496: 	    return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description));
  497: 	}
  498:     }
  499: 
  500:     # if the reservation symb is for a map get a resource in that map
  501:     # to check slot parameters on
  502:     my $navmap=Apache::lonnavmaps::navmap->new;
  503:     if (!defined($navmap)) {
  504:         return (0,'error: Unable to determine current status');
  505:     }
  506:     my $passed_resource = $navmap->getBySymb($symb);
  507:     if ($passed_resource->is_map()) {
  508: 	my ($a_resource) = 
  509: 	    $navmap->retrieveResources($passed_resource, 
  510: 				       sub {$_[0]->is_problem()},0,1);
  511: 	$symb = $a_resource->symb();
  512:     }
  513: 
  514:     # get parameter string, check for existance, rebuild string with the slot
  515:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
  516:                                        $symb,$udom,$uname);
  517:     my @slots = split(/:/,$student);
  518: 
  519:     my @new_slots;
  520:     foreach my $exist_slot (@slots) {
  521: 	if ($exist_slot eq $slot_name) { next; }
  522: 	push(@new_slots,$exist_slot);
  523:     }
  524:     my $new_param = join(':',@new_slots);
  525: 
  526:     my ($cnum,$cdom)=&get_course();
  527: 
  528:     # get slot reservations, check if user has one, if so remove reservation
  529:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  530: 				       "^$slot_name\0");
  531:     foreach my $entry (keys(%consumed)) {
  532: 	if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
  533: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  534: 				 $cdom,$cnum);
  535:             my %storehash = (
  536:                                symb    => $symb,
  537:                                slot    => $slot_name,
  538:                                action  => 'release',
  539:                                context => $env{'form.context'},
  540:                         );
  541:             &Apache::lonnet::write_log('slotreservationslog',\%storehash,
  542:                                        1,$uname,$udom,$cnum,$cdom);
  543:             &Apache::lonnet::write_log($cdom.'_'.$cnum.'_slotlog',\%storehash,
  544:                                        1,$uname,$udom,$uname,$udom);
  545: 	}
  546:     }
  547: 
  548:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",
  549: 					 $symb,$udom,$uname);
  550:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
  551: 
  552:     if (&Apache::lonnet::error($use_slots)) { 
  553: 	return (0,'error: Unable to determine current status');
  554:     }
  555: 
  556:     my $parm_level = 1;
  557:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
  558: 	$parm_level = 2;
  559:     }
  560:     # store new parameter string
  561:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  562: 						      '0_availablestudent',
  563: 						      $parm_level, $new_param,
  564: 						      'string', $uname, $udom);
  565:     my $msg;
  566:     if ($mgr eq 'F') {
  567: 	$msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
  568:     } else {
  569: 	$msg = '<span style="font-weight: bold;">'.&mt('Released reservation: [_1]',$description).'</span><br /><br />';
  570:         my $person = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
  571:         my $subject = &mt('Reservation change: [_1]',$description);
  572:         my $msgbody = &mt('Reservation released by [_1] for [_2].',$person,$description);
  573:         $msg .= &slot_change_messaging($slot{'reservationmsg'},$subject,$msgbody,'release');
  574:     }
  575:     return (1,$msg);
  576: }
  577: 
  578: sub delete_slot {
  579:     my ($r)=@_;
  580: 
  581:     my $slot_name = $env{'form.slotname'};
  582:     my %slot=&Apache::lonnet::get_slot($slot_name);
  583: 
  584:     my ($cnum,$cdom)=&get_course();
  585:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  586: 				       "^$slot_name\0");
  587:     my ($tmp) = %consumed;
  588:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  589: 
  590:     if (%slot && !%consumed) {
  591: 	$slot{'type'} = 'deleted';
  592: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  593: 					$cdom, $cnum);
  594: 	if ($ret eq 'ok') {
  595: 	    $r->print('<p>'.&mt('Slot [_1] marked as deleted.','<tt>'.$slot_name.'</tt>').'</p>');
  596: 	} else {
  597: 	    $r->print('<p><span class="LC_error">'.&mt('An error occurred when attempting to delete slot: [_1]','<tt>'.$slot_name.'</tt>')." ($ret)</span></p>");
  598: 	}
  599:     } else {
  600: 	if (%consumed) {
  601: 	    $r->print('<p>'.&mt('Slot [_1] has active reservations.','<tt>'.$slot_name.'</tt>').'</p>');
  602: 	} else {
  603: 	    $r->print('<p>'.&mt('Slot [_1] does not exist.','<tt>'.$slot_name.'</tt>').'</p>');
  604: 	}
  605:     }
  606:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  607: 	      &mt('Return to slot list').'</a></p>');
  608:     &return_link($r);
  609: }
  610: 
  611: sub return_link {
  612:     my ($r) = @_;
  613:     if (($env{'form.command'} eq 'manageresv') || ($env{'form.context'} eq 'usermanage')) {
  614: 	$r->print('<p><a href="/adm/slotrequest?command=manageresv">'.
  615:                   &mt('Return to reservations'));  
  616:     } else {
  617:         $r->print('<p><a href="/adm/flip?postdata=return:">'.
  618: 	          &mt('Return to last resource').'</a></p>');
  619:     }
  620: }
  621: 
  622: sub get_slot {
  623:     my ($r,$symb,$conflictable_slot,$inhibit_return_link)=@_;
  624: 
  625:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  626:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
  627: 
  628:     if ($slot_name =~ /^error: (.*)/) {
  629: 	$r->print('<p><span class="LC_error">'
  630:                  .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
  631:                  .'</span></p>');
  632: 	&return_link($r);
  633: 	return 0;
  634:     }
  635:     if ($slot_name && $slot_name ne $conflictable_slot) {
  636: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  637: 	my $description1=&get_description($slot_name,\%slot);
  638: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  639: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  640: 	if ($slot_name ne $env{'form.slotname'}) {
  641: 	    $r->print(<<STUFF);
  642: <form method="post" action="/adm/slotrequest">
  643:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  644:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  645:    <input type="hidden" name="releaseslot" value="$slot_name" />
  646:    <input type="hidden" name="command" value="change" />
  647: STUFF
  648:             $r->print('<p class="LC_error">'.&mt('Reservation currently unchanged').'</p>');
  649:             if ($slot_name ne '') {
  650:                 $r->print('<p>'.&mt('To complete the transaction you [_1]must confirm[_2] you want to [_3]process the change[_4] to [_5].'
  651:                          ,'<b>','</b>','<i>','</i>','<b>'.$description2.'</b>')
  652:                          .'<br />'
  653:                          .&mt('Or you can choose to [_1]make no change[_2] and continue[_2] with the reservation you already had: [_3].'
  654:                          ,'<i>','</i>','<b>'.$description1.'</b>')
  655:                          .'</p><p><span class="LC_nobreak">'
  656:                          .'<input type="submit" name="change" value="'.&mt('Process the change').'" />' 
  657:                          .('&nbsp;'x3)
  658:                          .'<input type="submit" name="nochange" value="'.&mt('Make no change').'" />'
  659:                          .'</span></p>');
  660:             }
  661: 	    $r->print(<<STUFF);
  662: </form>
  663: STUFF
  664:         } else {
  665:             $r->print('<p>'.&mt('Already have a reservation: [_1].',$description1).'</p>');
  666: 	    &return_link($r);
  667: 	}
  668: 	return 0;
  669:     }
  670: 
  671:     my ($cnum,$cdom)=&get_course();
  672:     my $reserved=&make_reservation($env{'form.slotname'},
  673: 				   \%slot,$symb,$cnum,$cdom);
  674:     my $description=&get_description($env{'form.slotname'},\%slot);
  675:     if (defined($reserved)) {
  676: 	my $retvalue = 0;
  677: 	if ($slot_name =~ /^error: (.*)/) {
  678: 	    $r->print('<p><span class="LC_error">'
  679:                      .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
  680:                      .'</span></p>');
  681: 	} elsif ($reserved > -1) {
  682: 	    $r->print('<p style="font-weight: bold;">'.&mt('Successfully signed up:  [_1]',$description).'</p>');
  683: 	    $retvalue = 1;
  684:             my $person = &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
  685:             my $subject = &mt('Reservation change: [_1]',$description);
  686:             my $msgbody = &mt('Successful reservation by [_1] for [_2].',$person,$description);
  687:             my $msg = &slot_change_messaging($slot{'reservationmsg'},$subject,$msgbody,'reserve');
  688:             if ($msg) {
  689:                 $r->print($msg);
  690:             }
  691: 	} elsif ($reserved < 0) {
  692: 	    $r->print('<p>'.&mt('Already reserved: [_1]',$description).'</p>');
  693: 	}
  694: 	if (!$inhibit_return_link) { &return_link($r); }
  695: 	return 1;
  696:     }
  697: 
  698:     my %lt = &Apache::lonlocal::texthash(
  699:         'request' => 'Availibility list',
  700:         'try'     => 'Try again?',
  701:         'or'      => 'or',
  702:     );
  703: 
  704:     my $extra_input;
  705:     if ($conflictable_slot) {
  706: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
  707:     }
  708: 
  709:     $r->print('<p>'.&mt('[_1]Failed[_2] to reserve a slot for [_3].','<span class="LC_warning">','</span>',$description).'</p>');
  710:     $r->print(<<STUFF);
  711: <p>
  712: <form method="post" action="/adm/slotrequest">
  713:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  714:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  715:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  716:    <input type="hidden" name="command" value="$env{'form.command'}" />
  717:    $extra_input
  718: </form>
  719: </p>
  720: <p>
  721: $lt{'or'}
  722: <form method="post" action="/adm/slotrequest">
  723:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  724:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  725: </form>
  726: STUFF
  727: 
  728:     if (!$inhibit_return_link) { 
  729:         $r->print(&mt('or').'</p>');
  730:         &return_link($r);
  731:     } else {
  732:         $r->print('</p>');
  733:     }
  734:     return 0;
  735: }
  736: 
  737: sub allowed_slot {
  738:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
  739: 
  740:     #already started
  741:     if ($slot->{'starttime'} < time) {
  742: 	return 0;
  743:     }
  744:     &Apache::lonxml::debug("$slot_name starttime good");
  745: 
  746:     #already ended
  747:     if ($slot->{'endtime'} < time) {
  748: 	return 0;
  749:     }
  750:     &Apache::lonxml::debug("$slot_name endtime good");
  751: 
  752:     # not allowed to pick this one
  753:     if (defined($slot->{'type'})
  754: 	&& $slot->{'type'} ne 'schedulable_student') {
  755: 	return 0;
  756:     }
  757:     &Apache::lonxml::debug("$slot_name type good");
  758: 
  759:     # reserve time not yet started
  760:     if ($slot->{'startreserve'} > time) {
  761: 	return 0;
  762:     }
  763:     # reserve time ended
  764:     if (($slot->{'endreserve'}) &&
  765:         ($slot->{'endreserve'} < time)) {
  766:         return 0;
  767:     }    
  768:     &Apache::lonxml::debug("$slot_name reserve good");
  769: 
  770:     my $userallowed=0;
  771:     # its for a different set of users
  772:     if (defined($slot->{'allowedsections'})) {
  773: 	if (!defined($env{'request.role.sec'})
  774: 	    && grep(/^No section assigned$/,
  775: 		    split(',',$slot->{'allowedsections'}))) {
  776: 	    $userallowed=1;
  777: 	}
  778: 	if (defined($env{'request.role.sec'})
  779: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
  780: 		    split(',',$slot->{'allowedsections'}))) {
  781: 	    $userallowed=1;
  782: 	}
  783: 	if (defined($env{'request.course.groups'})) {
  784: 	    my @groups = split(/:/,$env{'request.course.groups'});
  785: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
  786: 	    foreach my $group (@groups) {
  787: 		if (grep {$_ eq $group} (@allowed_sec)) {
  788: 		    $userallowed=1;
  789: 		    last;
  790: 		}
  791: 	    }
  792: 	}
  793:     }
  794:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
  795: 
  796:     # its for a different set of users
  797:     if (defined($slot->{'allowedusers'})
  798: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
  799: 		split(',',$slot->{'allowedusers'}))) {
  800: 	$userallowed=1;
  801:     }
  802: 
  803:     if (!defined($slot->{'allowedusers'})
  804: 	&& !defined($slot->{'allowedsections'})) {
  805: 	$userallowed=1;
  806:     }
  807: 
  808:     &Apache::lonxml::debug("$slot_name user is $userallowed");
  809:     return 0 if (!$userallowed);
  810: 
  811:     # not allowed for this resource
  812:     if (defined($slot->{'symb'})
  813: 	&& $slot->{'symb'} ne $symb) {
  814: 	return 0;
  815:     }
  816: 
  817:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
  818: 				       $consumed_uniqueperiods);
  819:     if ($conflict =~ /^error: /) {
  820:         return 0;
  821:     } elsif ($conflict ne '') {
  822: 	if ($slots->{$conflict}{'starttime'} < time) {
  823: 	    return 0;
  824: 	}
  825:     }
  826:     &Apache::lonxml::debug("$slot_name symb good");
  827:     return 1;
  828: }
  829: 
  830: sub get_description {
  831:     my ($slot_name,$slot)=@_;
  832:     my $description=$slot->{'description'};
  833:     if (!defined($description)) {
  834: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  835: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  836: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  837:     }
  838:     return $description;
  839: }
  840: 
  841: sub show_choices {
  842:     my ($r,$symb,$formname)=@_;
  843: 
  844:     my ($cnum,$cdom)=&get_course();
  845:     my %slots = &Apache::lonnet::get_course_slots($cnum,$cdom);
  846:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
  847:     if (ref($consumed_uniqueperiods) eq 'HASH') {
  848:         if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
  849:             $r->print('<span class="LC_error">'.
  850:                       &mt('An error occurred determining slot availability').
  851:                       '</span>');
  852:             return;
  853:         }
  854:     } elsif ($consumed_uniqueperiods =~ /^error: /) {
  855:         $r->print('<span class="LC_error">'.
  856:                   &mt('An error occurred determining slot availability').
  857:                   '</span>');
  858:         return;
  859:     }
  860:     my (@available,$output);
  861:     &Apache::lonxml::debug("Checking Slots");
  862:     my @got_slots=&check_for_reservation($symb,'allslots');
  863:     if ($got_slots[0] =~ /^error: /) {
  864:         $r->print('<span class="LC_error">'.
  865:                   &mt('An error occurred determining slot availability').
  866:                   '</span>');
  867:         return;
  868:     }
  869:     foreach my $slot (sort 
  870: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  871: 		      (keys(%slots)))  {
  872: 
  873: 	&Apache::lonxml::debug("Checking Slot $slot");
  874: 	next if (!&allowed_slot($slot,$slots{$slot},$symb,\%slots,
  875: 				$consumed_uniqueperiods));
  876: 
  877:         push(@available,$slot);
  878:     }
  879:     if (!@available) {
  880:         $output = '<div class="LC_info">'.&mt('No available times.');
  881:         if ($env{'form.command'} ne 'manageresv') {
  882:             $output .= ' <a href="/adm/flip?postdata=return:">'.
  883:                        &mt('Return to last resource').'</a>';
  884:         }
  885:         $output .= '</div>';
  886:         $r->print($output);
  887:         return;
  888:     }
  889:     if ($env{'form.command'} eq 'manageresv') {
  890:         $output = '<table border="0">';
  891:     } else {
  892:         $output = &Apache::loncommon::start_data_table();
  893:     }
  894:     foreach my $slot (@available) { 
  895: 	my $description=&get_description($slot,$slots{$slot});
  896: 	my $form;
  897: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
  898: 	    &space_available($slot,$slots{$slot},$symb)) {
  899: 	    my $text=&mt('Select');
  900: 	    my $command='get';
  901: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
  902: 		$text=&mt('Drop Reservation');
  903: 		$command='release';
  904: 	    } else {
  905: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
  906: 						   \%slots,
  907: 						   $consumed_uniqueperiods);
  908:                 if ($conflict) {
  909:                     if ($conflict =~ /^error: /) {
  910:                         $form = '<span class="LC_error">'.
  911:                                 &mt('Slot: [_1] has unknown status.',$description).
  912:                                 '</span>';
  913:                     } else {
  914: 		        $text=&mt('Change Reservation');
  915: 		        $command='get';
  916: 		    }
  917:                 }
  918: 	    }
  919: 	    my $escsymb=&escape($symb);
  920:             if (!$form) {
  921:                 if ($formname) {
  922:                     $formname = 'name="'.$formname.'" ';
  923:                 }
  924:                 my $context = 'user';
  925:                 if ($env{'form.command'} eq 'manageresv') {
  926:                     $context = 'usermanage';
  927:                 }
  928: 	        $form=<<STUFF;
  929:    <form method="post" action="/adm/slotrequest" $formname>
  930:      <input type="submit" name="Select" value="$text" />
  931:      <input type="hidden" name="symb" value="$escsymb" />
  932:      <input type="hidden" name="slotname" value="$slot" />
  933:      <input type="hidden" name="command" value="$command" />
  934:      <input type="hidden" name="context" value="$context" />
  935:    </form>
  936: STUFF
  937: 	    }
  938:         } else {
  939:             $form = &mt('Unavailable');
  940:         }
  941:         if ($env{'form.command'} eq 'manageresv') {
  942:             $output .= '<tr>';
  943:         } else {
  944: 	    $output .= &Apache::loncommon::start_data_table_row();
  945:         }
  946:         $output .= " 
  947:  <td>$form</td>
  948:  <td>$description</td>\n";
  949:         if ($env{'form.command'} eq 'manageresv') {
  950:             $output .= '</tr>';
  951:         } else {
  952:             $output .= &Apache::loncommon::end_data_table_row();
  953:         }
  954:     }
  955:     if ($env{'form.command'} eq 'manageresv') {
  956:         $output .= '</table>';
  957:     } else {
  958:        $output .= &Apache::loncommon::end_data_table();
  959:     }
  960:     $r->print($output);
  961: }
  962: 
  963: sub to_show {
  964:     my ($slotname,$slot,$when,$deleted,$name) = @_;
  965:     my $time=time;
  966:     my $week=60*60*24*7;
  967: 
  968:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  969: 	return 0;
  970:     }
  971: 
  972:     if ($name && $name->{'value'} =~ /\w/) {
  973: 	if ($name->{'type'} eq 'substring') {
  974: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
  975: 		return 0;
  976: 	    }
  977: 	}
  978: 	if ($name->{'type'} eq 'exact') {
  979: 	    if ($slotname eq $name->{'value'}) {
  980: 		return 0;
  981: 	    }
  982: 	}
  983:     }
  984: 
  985:     if ($when eq 'any') {
  986: 	return 1;
  987:     } elsif ($when eq 'now') {
  988: 	if ($time > $slot->{'starttime'} &&
  989: 	    $time < $slot->{'endtime'}) {
  990: 	    return 1;
  991: 	}
  992: 	return 0;
  993:     } elsif ($when eq 'nextweek') {
  994: 	if ( ($time        < $slot->{'starttime'} &&
  995: 	      ($time+$week) > $slot->{'starttime'})
  996: 	     ||
  997: 	     ($time        < $slot->{'endtime'} &&
  998: 	      ($time+$week) > $slot->{'endtime'}) ) {
  999: 	    return 1;
 1000: 	}
 1001: 	return 0;
 1002:     } elsif ($when eq 'lastweek') {
 1003: 	if ( ($time        > $slot->{'starttime'} &&
 1004: 	      ($time-$week) < $slot->{'starttime'})
 1005: 	     ||
 1006: 	     ($time        > $slot->{'endtime'} &&
 1007: 	      ($time-$week) < $slot->{'endtime'}) ) {
 1008: 	    return 1;
 1009: 	}
 1010: 	return 0;
 1011:     } elsif ($when eq 'willopen') {
 1012: 	if ($time < $slot->{'starttime'}) {
 1013: 	    return 1;
 1014: 	}
 1015: 	return 0;
 1016:     } elsif ($when eq 'wereopen') {
 1017: 	if ($time > $slot->{'endtime'}) {
 1018: 	    return 1;
 1019: 	}
 1020: 	return 0;
 1021:     }
 1022:     
 1023:     return 1;
 1024: }
 1025: 
 1026: sub remove_link {
 1027:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
 1028: 
 1029:     my $remove = &mt('Remove');
 1030: 
 1031:     if ($entry eq 'remove all') {
 1032: 	$remove = &mt('Remove All');
 1033: 	undef($uname);
 1034: 	undef($udom);
 1035:     }
 1036: 
 1037:     $slotname  = &escape($slotname);
 1038:     $entry     = &escape($entry);
 1039:     $uname     = &escape($uname);
 1040:     $udom      = &escape($udom);
 1041:     $symb      = &escape($symb);
 1042: 
 1043:     return <<"END_LINK";
 1044:  <a href="/adm/slotrequest?command=remove_registration&amp;slotname=$slotname&amp;entry=$entry&amp;uname=$uname&amp;udom=$udom&amp;symb=$symb&amp;context=manage"
 1045:    >($remove)</a>
 1046: END_LINK
 1047: 
 1048: }
 1049: 
 1050: sub show_table {
 1051:     my ($r,$mgr)=@_;
 1052: 
 1053:     my ($cnum,$cdom)=&get_course();
 1054:     my $crstype=&Apache::loncommon::course_type($cdom.'_'.$cnum);
 1055:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
 1056:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
 1057: 	undef(%slots);
 1058:     } 
 1059:     my $available;
 1060:     if ($mgr eq 'F') {
 1061:     # FIXME: This line should be deleted once Slots uses breadcrumbs
 1062:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
 1063: 
 1064: 	$r->print('<div>');
 1065: 	$r->print('<form method="post" action="/adm/slotrequest">
 1066: <input type="hidden" name="command" value="uploadstart" />
 1067: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
 1068: </form>');
 1069: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
 1070: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
 1071: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
 1072: </form>');
 1073: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
 1074: 	$r->print('</div>');
 1075:     }
 1076: 
 1077:     if (!keys(%slots)) {
 1078:         if ($crstype eq 'Community') {
 1079:             $r->print('<div>'.&mt('No slots have been created in this community.').'</div>');
 1080:         } else {
 1081:             $r->print('<div>'.&mt('No slots have been created in this course.').'</div>');
 1082:         }
 1083:         return;
 1084:     }
 1085:     
 1086:     my %Saveable_Parameters = ('show'              => 'array',
 1087: 			       'when'              => 'scalar',
 1088: 			       'order'             => 'scalar',
 1089: 			       'deleted'           => 'scalar',
 1090: 			       'name_filter_type'  => 'scalar',
 1091: 			       'name_filter_value' => 'scalar',
 1092: 			       );
 1093:     &Apache::loncommon::store_course_settings('slotrequest',
 1094: 					      \%Saveable_Parameters);
 1095:     &Apache::loncommon::restore_course_settings('slotrequest',
 1096: 						\%Saveable_Parameters);
 1097:     &Apache::grades::init_perm();
 1098:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
 1099:     &Apache::grades::reset_perm();
 1100: 
 1101:     # what to display filtering
 1102:     my %show_fields=&Apache::lonlocal::texthash(
 1103: 	     'name'            => 'Slot Name',
 1104: 	     'description'     => 'Description',
 1105: 	     'type'            => 'Type',
 1106: 	     'starttime'       => 'Start time',
 1107: 	     'endtime'         => 'End Time',
 1108:              'startreserve'    => 'Time students can start reserving',
 1109:              'endreserve'      => 'Time students can no longer reserve',
 1110:              'reservationmsg'  => 'Message triggered by reservation',
 1111: 	     'secret'          => 'Secret Word',
 1112: 	     'space'           => '# of students/max',
 1113: 	     'ip'              => 'IP or DNS restrictions',
 1114: 	     'symb'            => 'Resource slot is restricted to.',
 1115: 	     'allowedsections' => 'Sections slot is restricted to.',
 1116: 	     'allowedusers'    => 'Users slot is restricted to.',
 1117: 	     'uniqueperiod'    => 'Period of time slot is unique',
 1118: 	     'scheduled'       => 'Scheduled Students',
 1119: 	     'proctor'         => 'List of proctors');
 1120:     if ($crstype eq 'Community') {
 1121:         $show_fields{'startreserve'} = &mt('Time members can start reserving');
 1122:         $show_fields{'endreserve'} = &mt('Time members can no longer reserve');
 1123:         $show_fields{'scheduled'} = &mt('Scheduled Members');
 1124:     }
 1125:     my @show_order=('name','description','type','starttime','endtime',
 1126: 		    'startreserve','endreserve','reservationmsg','secret','space',
 1127: 		    'ip','symb','allowedsections','allowedusers','uniqueperiod',
 1128: 		    'scheduled','proctor');
 1129:     my @show = 
 1130: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
 1131: 	                            : keys(%show_fields);
 1132:     my %show =  map { $_ => 1 } (@show);
 1133: 
 1134:     #when filtering setup
 1135:     my %when_fields=&Apache::lonlocal::texthash(
 1136: 	     'now'      => 'Open now',
 1137: 	     'nextweek' => 'Open within the next week',
 1138: 	     'lastweek' => 'Were open last week',
 1139: 	     'willopen' => 'Will open later',
 1140: 	     'wereopen' => 'Were open',
 1141: 	     'any'      => 'Anytime',
 1142: 						);
 1143:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
 1144:     $when_fields{'select_form_order'} = \@when_order;
 1145:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
 1146:                                             : 'now';
 1147: 
 1148:     #display of students setup
 1149:     my %stu_display_fields=
 1150: 	&Apache::lonlocal::texthash('username' => 'User name',
 1151: 				    'fullname' => 'Full name',
 1152: 				    );
 1153:     my @stu_display_order=('fullname','username');
 1154:     my @stu_display = 
 1155: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
 1156: 	                                  : keys(%stu_display_fields);
 1157:     my %stu_display =  map { $_ => 1 } (@stu_display);
 1158: 
 1159:     #name filtering setup
 1160:     my %name_filter_type_fields=
 1161: 	&Apache::lonlocal::texthash('substring' => 'Substring',
 1162: 				    'exact'     => 'Exact',
 1163: 				    #'reg'       => 'Regular Expression',
 1164: 				    );
 1165:     my @name_filter_type_order=('substring','exact');
 1166: 
 1167:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
 1168:     my $name_filter_type = 
 1169: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
 1170:                                                 : 'substring';
 1171:     my $name_filter = {'type'  => $name_filter_type,
 1172: 		       'value' => $env{'form.name_filter_value'},};
 1173: 
 1174:     
 1175:     #deleted slot filtering
 1176:     #default to hide if no value
 1177:     $env{'form.deleted'} ||= 'hide';
 1178:     my $hide_radio = 
 1179: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
 1180:     my $show_radio = 
 1181: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
 1182: 	
 1183:     $r->print('<form method="post" action="/adm/slotrequest">
 1184: <input type="hidden" name="command" value="showslots" />');
 1185:     $r->print('<div>');
 1186:     $r->print('<table class="inline">
 1187:       <tr><th>'.&mt('Show').'</th>
 1188:           <th>'.&mt('Student Display').'</th>
 1189:           <th>'.&mt('Open').'</th>
 1190:           <th>'.&mt('Slot Name Filter').'</th>
 1191:           <th>'.&mt('Options').'</th>
 1192:       </tr>
 1193:       <tr><td valign="top">'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
 1194: 	      '</td>
 1195:            <td valign="top">
 1196:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
 1197: 						    6,\%stu_display_fields,
 1198: 						    \@stu_display_order).'
 1199:            </td>
 1200:            <td valign="top">'.&Apache::loncommon::select_form($when,'when',\%when_fields).
 1201:           '</td>
 1202:            <td valign="top">'.&Apache::loncommon::select_form($name_filter_type,
 1203: 						 'name_filter_type',
 1204: 						 \%name_filter_type_fields).
 1205: 	      '<br />'.
 1206: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
 1207: 					      $env{'form.name_filter_value'},
 1208: 					      15).
 1209:           '</td>
 1210:            <td valign="top">
 1211:             <table>
 1212:               <tr>
 1213:                 <td rowspan="2">Deleted slots:</td>
 1214:                 <td><label>'.$show_radio.'Show</label></td>
 1215:               </tr>
 1216:               <tr>
 1217:                 <td><label>'.$hide_radio.'Hide</label></td>
 1218:               </tr>
 1219:             </table>
 1220: 	  </td>
 1221:        </tr>
 1222:     </table>');
 1223:     $r->print('</div>');
 1224:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
 1225:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
 1226:     $r->print(&Apache::loncommon::start_data_table().
 1227: 	      &Apache::loncommon::start_data_table_header_row().'
 1228: 	       <th></th>');
 1229:     foreach my $which (@show_order) {
 1230: 	if ($which ne 'proctor' && exists($show{$which})) {
 1231: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
 1232: 	}
 1233:     }
 1234:     $r->print(&Apache::loncommon::end_data_table_header_row());
 1235: 
 1236:     my %name_cache;
 1237:     my $slotsort = sub {
 1238: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|endreserve|ip|symb|allowedsections|allowedusers|reservationmsg)$/) {
 1239: 	    if (lc($slots{$a}->{$env{'form.order'}})
 1240: 		ne lc($slots{$b}->{$env{'form.order'}})) {
 1241: 		return (lc($slots{$a}->{$env{'form.order'}}) 
 1242: 			cmp lc($slots{$b}->{$env{'form.order'}}));
 1243: 	    }
 1244: 	} elsif ($env{'form.order'} eq 'space') {
 1245: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
 1246: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
 1247: 	    }
 1248: 	} elsif ($env{'form.order'} eq 'name') {
 1249: 	    if (lc($a) cmp lc($b)) {
 1250: 		return lc($a) cmp lc($b);
 1251: 	    }
 1252: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
 1253: 	    
 1254: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
 1255: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
 1256: 		return ($slots{$a}->{'uniqueperiod'}[0]
 1257: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
 1258: 	    }
 1259: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
 1260: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
 1261: 		return ($slots{$a}->{'uniqueperiod'}[1]
 1262: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
 1263: 	    }
 1264: 	}
 1265: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
 1266:     };
 1267: 
 1268:     my %consumed;
 1269:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1270: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
 1271: 	my ($tmp)=%consumed;
 1272: 	if ($tmp =~ /^error: /) { undef(%consumed); }
 1273:     }
 1274: 
 1275:     my %msgops = &slot_reservationmsg_options();
 1276: 
 1277:     foreach my $slot (sort $slotsort (keys(%slots)))  {
 1278: 	if (!&to_show($slot,$slots{$slot},$when,
 1279: 		      $env{'form.deleted'},$name_filter)) { next; }
 1280:         my $reservemsg;
 1281: 	if (defined($slots{$slot}->{'type'})
 1282: 	    && $slots{$slot}->{'type'} eq 'schedulable_student') {
 1283: 	    $reservemsg = $msgops{$slots{$slot}->{'reservationmsg'}};
 1284: 	}
 1285: 	my $description=&get_description($slot,$slots{$slot});
 1286: 	my ($id_count,$ids);
 1287: 	    
 1288: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
 1289: 	    my $re_str = "$slot\0";
 1290: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
 1291: 	    $id_count = scalar(@this_slot);
 1292: 	    if (exists($show{'scheduled'})) {
 1293: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
 1294: 					      $consumed{$b}{name} }
 1295: 				   (@this_slot)) {
 1296: 		    my (undef,$id)=split("\0",$entry);
 1297: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
 1298: 		    $ids.= '<span class="LC_nobreak">';
 1299: 		    foreach my $item (@stu_display_order) {
 1300: 			if ($stu_display{$item}) {
 1301: 			    if ($item eq 'fullname') {
 1302: 				$ids.=$fullname->{"$uname:$udom"}.' ';
 1303: 			    } elsif ($item eq 'username') {
 1304: 				$ids.="<tt>$uname:$udom</tt> ";
 1305: 			    }
 1306: 			}
 1307: 		    }
 1308: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
 1309: 				       $consumed{$entry}{'symb'}).'</span><br />';
 1310: 		}
 1311: 	    }
 1312: 	}
 1313: 
 1314: 	my $start=($slots{$slot}->{'starttime'}?
 1315: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
 1316: 	my $end=($slots{$slot}->{'endtime'}?
 1317: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
 1318: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
 1319: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
 1320:         my $end_reserve=($slots{$slot}->{'endreserve'}?
 1321:                          &Apache::lonlocal::locallocaltime($slots{$slot}->{'endreserve'}):'');
 1322: 	
 1323: 	my $unique;
 1324: 	if (ref($slots{$slot}{'uniqueperiod'})) {
 1325: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
 1326: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
 1327: 	}
 1328: 
 1329: 	my $title;
 1330: 	if (exists($slots{$slot}{'symb'})) {
 1331: 	    my (undef,undef,$res)=
 1332: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
 1333: 	    $res =   &Apache::lonnet::clutter($res);
 1334: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
 1335: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
 1336: 	}
 1337: 
 1338: 	my $allowedsections;
 1339: 	if (exists($show{'allowedsections'})) {
 1340: 	    $allowedsections = 
 1341: 		join(', ',sort(split(/\s*,\s*/,
 1342: 				     $slots{$slot}->{'allowedsections'})));
 1343: 	}
 1344: 
 1345: 	my @allowedusers;
 1346: 	if (exists($show{'allowedusers'})) {
 1347: 	    @allowedusers= map {
 1348: 		my ($uname,$udom)=split(/:/,$_);
 1349: 		my $fullname=$name_cache{$_};
 1350: 		if (!defined($fullname)) {
 1351: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1352: 		    $fullname =~s/\s/&nbsp;/g;
 1353: 		    $name_cache{$_} = $fullname;
 1354: 		}
 1355: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1356: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
 1357: 	}
 1358: 	my $allowedusers=join(', ',@allowedusers);
 1359: 	
 1360: 	my @proctors;
 1361: 	my $rowspan=1;
 1362: 	my $colspan=1;
 1363: 	if (exists($show{'proctor'})) {
 1364: 	    $rowspan=2;
 1365: 	    @proctors= map {
 1366: 		my ($uname,$udom)=split(/:/,$_);
 1367: 		my $fullname=$name_cache{$_};
 1368: 		if (!defined($fullname)) {
 1369: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
 1370: 		    $fullname =~s/\s/&nbsp;/g;
 1371: 		    $name_cache{$_} = $fullname;
 1372: 		}
 1373: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
 1374: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
 1375: 	}
 1376: 	my $proctors=join(', ',@proctors);
 1377: 
 1378:         my %lt = &Apache::lonlocal::texthash (
 1379:                                                edit   => 'Edit',
 1380:                                                delete => 'Delete',
 1381:                                                slotlog => 'History',
 1382:         );
 1383: 	my $edit=(<<"EDITLINK");
 1384: <a href="/adm/helper/newslot.helper?name=$slot">$lt{'edit'}</a>
 1385: EDITLINK
 1386: 
 1387: 	my $delete=(<<"DELETELINK");
 1388: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">$lt{'delete'}</a>
 1389: DELETELINK
 1390: 
 1391:         my $showlog=(<<"LOGLINK");
 1392: <a href="/adm/slotrequest?command=slotlog&amp;slotname=$slot">$lt{'slotlog'}</a>
 1393: LOGLINK
 1394: 
 1395:         my $remove_all=&remove_link($slot,'remove all').'<br />';
 1396: 
 1397:         if ($ids eq '') {
 1398:             undef($remove_all);
 1399:         } else {
 1400:             undef($delete);
 1401:         }
 1402: 	if ($slots{$slot}{'type'} ne 'schedulable_student') {
 1403:             undef($showlog); 
 1404: 	    undef($remove_all);
 1405: 	}
 1406: 
 1407: 	my $row_start=&Apache::loncommon::start_data_table_row();
 1408: 	my $row_end=&Apache::loncommon::end_data_table_row();
 1409:         $r->print($row_start.
 1410: 		  "\n<td rowspan=\"$rowspan\">$edit $delete $showlog</td>\n");
 1411: 	if (exists($show{'name'})) {
 1412: 	    $colspan++;$r->print("<td>$slot</td>");
 1413: 	}
 1414: 	if (exists($show{'description'})) {
 1415: 	    $colspan++;$r->print("<td>$description</td>\n");
 1416: 	}
 1417: 	if (exists($show{'type'})) {
 1418: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
 1419: 	}
 1420: 	if (exists($show{'starttime'})) {
 1421: 	    $colspan++;$r->print("<td>$start</td>\n");
 1422: 	}
 1423: 	if (exists($show{'endtime'})) {
 1424: 	    $colspan++;$r->print("<td>$end</td>\n");
 1425: 	}
 1426: 	if (exists($show{'startreserve'})) {
 1427: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
 1428: 	}
 1429:         if (exists($show{'endreserve'})) {
 1430:             $colspan++;$r->print("<td>$end_reserve</td>\n");
 1431:         }
 1432:         if (exists($show{'reservationmsg'})) {
 1433:             $colspan++;$r->print("<td>$reservemsg</td>\n");
 1434:         }
 1435: 	if (exists($show{'secret'})) {
 1436: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
 1437: 	}
 1438: 	if (exists($show{'space'})) {
 1439: 	    my $display = $id_count;
 1440: 	    if ($slots{$slot}{'maxspace'}>0) {
 1441: 		$display.='/'.$slots{$slot}{'maxspace'};
 1442: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
 1443: 		    $display = '<strong>'.$display.' (full) </strong>';
 1444: 		}
 1445: 	    }
 1446: 	    $colspan++;$r->print("<td>$display</td>\n");
 1447: 	}
 1448: 	if (exists($show{'ip'})) {
 1449: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
 1450: 	}
 1451: 	if (exists($show{'symb'})) {
 1452: 	    $colspan++;$r->print("<td>$title</td>\n");
 1453: 	}
 1454: 	if (exists($show{'allowedsections'})) {
 1455: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
 1456: 	}
 1457: 	if (exists($show{'allowedusers'})) {
 1458: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
 1459: 	}
 1460: 	if (exists($show{'uniqueperiod'})) {
 1461: 	    $colspan++;$r->print("<td>$unique</td>\n");
 1462: 	}
 1463: 	if (exists($show{'scheduled'})) {
 1464: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
 1465: 	}
 1466: 	$r->print("$row_end\n");
 1467: 	if (exists($show{'proctor'})) {
 1468: 	    $r->print(<<STUFF);
 1469: $row_start
 1470:  <td colspan="$colspan">$proctors</td>
 1471: $row_end
 1472: STUFF
 1473:         }
 1474:     }
 1475:     $r->print(&Apache::loncommon::end_data_table().'</form>');
 1476:     return;
 1477: }
 1478: 
 1479: sub manage_reservations {
 1480:     my ($r,$crstype) = @_;
 1481:     my $navmap = Apache::lonnavmaps::navmap->new();
 1482:     $r->print('<p>'
 1483:              .&mt('Instructors may use a reservation system to place restrictions on when and where assignments can be worked on.')
 1484:              .'<br />'
 1485:              .&mt('One example is for management of laboratory space, which is only available at certain times, and has a limited number of seats.')
 1486:              .'</p>'
 1487:     );
 1488:     if (!defined($navmap)) {
 1489:         $r->print('<div class="LC_error">');
 1490:         if ($crstype eq 'Community') {
 1491:             $r->print(&mt('Unable to retrieve information about community contents'));
 1492:         } else {
 1493:             $r->print(&mt('Unable to retrieve information about course contents'));
 1494:         }
 1495:         $r->print('</div>');
 1496:         &Apache::lonnet::logthis('Manage Reservations - could not create navmap object in '.lc($crstype).':'.$env{'request.course.id'});
 1497:         return;
 1498:     }
 1499:     my (%parent,%shownparent,%container,%container_title,%contents);
 1500:     my ($depth,$count,$reservable,$lastcontainer,$rownum) = (0,0,0,0,0);
 1501:     my @backgrounds = ("LC_odd_row","LC_even_row");
 1502:     my $numcolors = scalar(@backgrounds);
 1503:     my $location=&Apache::loncommon::lonhttpdurl("/adm/lonIcons/whitespace_21.gif");
 1504:     my $slotheader = '<p>'.
 1505:                  &mt('Your reservation status for any such assignments is listed below:').
 1506:                  '</p>'.
 1507:                  '<table class="LC_data_table LC_tableOfContent">'."\n";
 1508:     my $shownheader = 0;
 1509:     my $it=$navmap->getIterator(undef,undef,undef,1,undef,undef);
 1510:     while (my $resource = $it->next()) {
 1511:         if ($resource == $it->BEGIN_MAP()) {
 1512:             $depth++;
 1513:             $parent{$depth} = $lastcontainer;
 1514:         }
 1515:         if ($resource == $it->END_MAP()) {
 1516:             $depth--;
 1517:             $lastcontainer = $parent{$depth};
 1518:         }
 1519:         if (ref($resource)) {
 1520:             my $symb = $resource->symb();
 1521:             my $ressymb = $symb;
 1522:             $contents{$lastcontainer} ++;
 1523:             next if (!$resource->is_problem() && !$resource->is_sequence() && 
 1524:                      !$resource->is_page());
 1525:             $count ++;
 1526:             if (($resource->is_sequence()) || ($resource->is_page())) {
 1527:                 $lastcontainer = $count;
 1528:                 $container{$lastcontainer} = $resource;
 1529:                 $container_title{$lastcontainer} = $resource->compTitle();
 1530:             }
 1531:             if ($resource->is_problem()) {
 1532:                 my ($useslots) = $resource->slot_control();
 1533:                 next if (($useslots eq '') || ($useslots =~ /^\s*no\s*$/i));
 1534:                 my ($msg,$get_choices,$slotdescription);
 1535:                 my $title = $resource->compTitle();
 1536:                 my $status = $resource->simpleStatus('0');
 1537:                 my ($slot_status,$date,$slot_name) = $resource->check_for_slot('0');
 1538:                 if ($slot_name ne '') {
 1539:                     my %slot=&Apache::lonnet::get_slot($slot_name);
 1540:                     $slotdescription=&get_description($slot_name,\%slot);
 1541:                 }
 1542:                 if ($slot_status == $resource->NOT_IN_A_SLOT) {
 1543:                     $msg=&mt('No current reservation.');
 1544:                     $get_choices = 1;
 1545:                 } elsif ($slot_status == $resource->NEEDS_CHECKIN) {
 1546:                     $msg='<span class="LC_nobreak">'.&mt('Reserved:').
 1547:                          '&nbsp;'.$slotdescription.'</span><br />'.
 1548:                          &mt('Access requires proctor validation.');
 1549:                 } elsif ($slot_status == $resource->WAITING_FOR_GRADE) {
 1550:                     $msg=&mt('Submitted and currently in grading queue.');
 1551:                 } elsif ($slot_status == $resource->CORRECT) {
 1552:                     $msg=&mt('Problem is unavailable.');
 1553:                 } elsif ($slot_status == $resource->RESERVED) {
 1554:                     $msg='<span class="LC_nobreak">'.&mt('Reserved:').
 1555:                          '&nbsp;'.$slotdescription.'</span><br />'.
 1556:                          &mt('Problem is currently available.');
 1557:                 } elsif ($slot_status == $resource->RESERVED_LOCATION) {
 1558:                     $msg='<span class="LC_nobreak">'.&mt('Reserved:').
 1559:                          '&nbsp;'.$slotdescription.'</span><br />'.
 1560:                          &mt('Problem is available at a different location.');
 1561:                     $get_choices = 1;
 1562:                 } elsif ($slot_status == $resource->RESERVED_LATER) {
 1563:                     $msg='<span class="LC_nobreak">'.&mt('Reserved:').
 1564:                          '&nbsp;'.$slotdescription.'</span><br />'.
 1565:                          &mt('Problem will be available later.');
 1566:                     $get_choices = 1;
 1567:                 } elsif ($slot_status == $resource->RESERVABLE) {
 1568:                     $msg=&mt('Reservation needed');
 1569:                     $get_choices = 1;
 1570:                 } elsif ($slot_status == $resource->RESERVABLE_LATER) {
 1571:                     $msg=&mt('Reservation needed: will be reservable later.');
 1572:                 } elsif ($slot_status == $resource->NOTRESERVABLE) {
 1573:                     $msg=&mt('Reservation needed: none available.');
 1574:                 } elsif ($slot_status == $resource->UNKNOWN) {
 1575:                     $msg=&mt('Unable to determine status due to network problems.');
 1576:                 } else {
 1577:                     if ($status != $resource->OPEN) {
 1578:                         $msg = &Apache::lonnavmaps::getDescription($resource,'0'); 
 1579:                     }
 1580:                 }
 1581:                 $reservable ++;
 1582:                 my $treelevel = $depth;
 1583:                 my $higherup = $lastcontainer;
 1584:                 if ($depth > 1) {
 1585:                     my @maprows;
 1586:                     while ($treelevel > 1) {
 1587:                         if (ref($container{$higherup})) {
 1588:                             my $res = $container{$higherup};
 1589:                             last if (defined($shownparent{$higherup}));
 1590:                             my $maptitle = $res->compTitle();
 1591:                             my $type = 'sequence';
 1592:                             if ($res->is_page()) {
 1593:                                 $type = 'page';
 1594:                             }
 1595:                             &show_map_row($treelevel,$location,$type,$maptitle,
 1596:                                           \@maprows);
 1597:                             $shownparent{$higherup} = 1;
 1598:                         }
 1599:                         $treelevel --;
 1600:                         $higherup = $parent{$treelevel};
 1601:                     }
 1602:                     foreach my $item (@maprows) {
 1603:                         $rownum ++;
 1604:                         my $bgcolor = $backgrounds[$rownum % $numcolors];
 1605:                         if (!$shownheader) {
 1606:                             $r->print($slotheader);
 1607:                             $shownheader = 1;
 1608:                         }
 1609:                         $r->print('<tr class="'.$bgcolor.'">'.$item.'</tr>'."\n");
 1610:                     }
 1611:                 }
 1612:                 $rownum ++;
 1613:                 my $bgcolor = $backgrounds[$rownum % $numcolors];
 1614:                 if (!$shownheader) {
 1615:                     $r->print($slotheader);
 1616:                     $shownheader = 1;
 1617:                 }
 1618:                 $r->print('<tr class="'.$bgcolor.'"><td>'."\n");
 1619:                 for (my $i=0; $i<$depth; $i++) {
 1620:                     $r->print('<img src="'.$location.'" alt="" />');
 1621:                 }
 1622:                 my $result = '<a href="'.$resource->src().'?symb='.$symb.'">'.
 1623:                              '<img class="LC_contentImage" src="/adm/lonIcons/';
 1624:                 if ($resource->is_task()) {
 1625:                     $result .= 'task.gif" alt="'.&mt('Task');
 1626:                 } else {
 1627:                     $result .= 'problem.gif" alt="'.&mt('Problem');
 1628:                 }
 1629:                 $result .= '" /><b>'.$title.'</b></a>'.('&nbsp;' x6).'</td>';
 1630:                 my $hasaction;
 1631:                 if ($status == $resource->OPEN) {
 1632:                     if ($get_choices) {
 1633:                         $hasaction = 1;
 1634:                     }
 1635:                 }
 1636:                 if ($hasaction) {
 1637:                     $result .= '<td valgn="middle">'.$msg.'</td>'.
 1638:                                '<td valign="middle">'.('&nbsp;' x6);
 1639:                 } else {
 1640:                     $result .= '<td colspan="2" valign="middle">'.$msg.'</td>';
 1641:                 }
 1642:                 $r->print($result);
 1643:                 if ($hasaction) {
 1644:                     my $formname = 'manageres_'.$reservable;
 1645:                     &show_choices($r,$symb,$formname);
 1646:                     $r->print('</td>');
 1647:                 }
 1648:                 $r->print('</tr>');
 1649:             }
 1650:         }
 1651:     }
 1652:     if ($shownheader) {
 1653:         $r->print('</table>');
 1654:     }
 1655:     if (!$reservable) {
 1656:         $r->print('<span class="LC_info">');
 1657:         if ($crstype eq 'Community') {
 1658:             $r->print(&mt('No community items currently require a reservation to gain access.'));
 1659:         } else {
 1660:             $r->print(&mt('No course items currently require a reservation to gain access.'));
 1661:         }
 1662:         $r->print('</span>');
 1663:     }
 1664:     $r->print('<p><a href="/adm/slotrequest?command=showresv">'.
 1665:               &mt('Reservation History').'</a></p>');
 1666: }
 1667: 
 1668: sub show_map_row {
 1669:     my ($depth,$location,$type,$title,$maprows) = @_;
 1670:     my $output = '<td>';
 1671:     for (my $i=0; $i<$depth-1; $i++) {
 1672:         $output .= '<img src="'.$location.'" alt="" />';
 1673:     }
 1674:     if ($type eq 'page') {
 1675:         $output .= '<img src="/adm/lonIcons/navmap.page.open.gif" alt="" />&nbsp;'."\n";
 1676:     } else {
 1677:         $output .= '<img src="/adm/lonIcons/navmap.folder.open.gif" alt="" />&nbsp;'."\n";
 1678:     }
 1679:     $output .= $title.'</td><td colspan="2">&nbsp;</td>'."\n";
 1680:     unshift (@{$maprows},$output);
 1681:     return;
 1682: }
 1683: 
 1684: sub show_reservations {
 1685:     my ($r,$uname,$udom) = @_;
 1686:     if (!defined($uname)) {
 1687:         $uname = $env{'user.name'};
 1688:     }
 1689:     if (!defined($udom)) {
 1690:         $udom = $env{'user.domain'};
 1691:     }
 1692:     my $formname = 'slotlog';
 1693:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1694:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1695:     my $crstype = &Apache::loncommon::course_type();
 1696:     my %log=&Apache::lonnet::dump('nohist_'.$cdom.'_'.$cnum.'_slotlog',$udom,$uname);
 1697:     if ($env{'form.origin'} eq 'aboutme') {
 1698:         $r->print('<div class="LC_fontsize_large">');
 1699:         my $name = &Apache::loncommon::plainname($env{'form.uname'},$env{'form.udom'},
 1700:                                                     'firstname');
 1701:         if ($crstype eq 'Community') {
 1702:             $r->print(&mt('History of member-reservable slots for: [_1]',
 1703:                           $name));
 1704:         } else {
 1705:             $r->print(&mt('History of student-reservable slots for: [_1]',
 1706:                           $name));
 1707: 
 1708:         }
 1709:         $r->print('</div>');
 1710:     }
 1711:     $r->print('<form action="/adm/slotrequest" method="post" name="'.$formname.'">');
 1712:     # set defaults
 1713:     my $now = time();
 1714:     my $defstart = $now - (7*24*3600); #7 days ago
 1715:     my %defaults = (
 1716:                      page           => '1',
 1717:                      show           => '10',
 1718:                      action         => 'any',
 1719:                      log_start_date => $defstart,
 1720:                      log_end_date   => $now,
 1721:                    );
 1722:     my $more_records = 0;
 1723: 
 1724:     # set current
 1725:     my %curr;
 1726:     foreach my $item ('show','page','action') {
 1727:         $curr{$item} = $env{'form.'.$item};
 1728:     }
 1729:     my ($startdate,$enddate) =
 1730:         &Apache::lonuserutils::get_dates_from_form('log_start_date',
 1731:                                                    'log_end_date');
 1732:     $curr{'log_start_date'} = $startdate;
 1733:     $curr{'log_end_date'} = $enddate;
 1734:     foreach my $key (keys(%defaults)) {
 1735:         if ($curr{$key} eq '') {
 1736:             $curr{$key} = $defaults{$key};
 1737:         }
 1738:     }
 1739:     my ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
 1740:     $r->print(&display_filter($formname,$cdom,$cnum,\%curr,$version));
 1741:     my $showntablehdr = 0;
 1742:     my $tablehdr = &Apache::loncommon::start_data_table().
 1743:                    &Apache::loncommon::start_data_table_header_row().
 1744:                    '<th>&nbsp;</th><th>'.&mt('When').'</th><th>'.&mt('Action').'</th>'.
 1745:                    '<th>'.&mt('Description').'</th><th>'.&mt('Start time').'</th>'.
 1746:                    '<th>'.&mt('End time').'</th><th>'.&mt('Resource').'</th>'.
 1747:                    &Apache::loncommon::end_data_table_header_row();
 1748:     my ($minshown,$maxshown);
 1749:     $minshown = 1;
 1750:     my $count = 0;
 1751:     if ($curr{'show'} ne &mt('all')) {
 1752:         $maxshown = $curr{'page'} * $curr{'show'};
 1753:         if ($curr{'page'} > 1) {
 1754:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
 1755:         }
 1756:     }
 1757:     my (%titles,%maptitles);
 1758:     my %lt = &reservationlog_contexts($crstype);
 1759:     foreach my $id (sort { $log{$b}{'exe_time'}<=>$log{$a}{'exe_time'} } (keys(%log))) {
 1760:         next if (($log{$id}{'exe_time'} < $curr{'log_start_date'}) ||
 1761:                  ($log{$id}{'exe_time'} > $curr{'log_end_date'}));
 1762:         if ($curr{'show'} ne &mt('all')) {
 1763:             if ($count >= $curr{'page'} * $curr{'show'}) {
 1764:                 $more_records = 1;
 1765:                 last;
 1766:             }
 1767:         }
 1768:         if ($curr{'action'} ne 'any') {
 1769:             next if ($log{$id}{'logentry'}{'action'} ne $curr{'action'});
 1770:         }
 1771:         $count ++;
 1772:         next if ($count < $minshown);
 1773:         if (!$showntablehdr) {
 1774:             $r->print($tablehdr);
 1775:             $showntablehdr = 1;
 1776:         }
 1777:         my $symb = $log{$id}{'logentry'}{'symb'};
 1778:         my $slot_name = $log{$id}{'logentry'}{'slot'};
 1779:         my %slot=&Apache::lonnet::get_slot($slot_name);
 1780:         my $description = $slot{'description'};
 1781:         my $start = ($slot{'starttime'}?
 1782:                      &Apache::lonlocal::locallocaltime($slot{'starttime'}):'');
 1783:         my $end = ($slot{'endtime'}?
 1784:                    &Apache::lonlocal::locallocaltime($slot{'endtime'}):'');
 1785:         my $title = &get_resource_title($symb,\%titles,\%maptitles);
 1786:         my $chgaction = $log{$id}{'logentry'}{'action'};
 1787:         if ($chgaction ne '' && $lt{$chgaction} ne '') {
 1788:             $chgaction = $lt{$chgaction};
 1789:         }
 1790:         $r->print(&Apache::loncommon::start_data_table_row().'<td>'.$count.'</td><td>'.&Apache::lonlocal::locallocaltime($log{$id}{'exe_time'}).'</td><td>'.$chgaction.'</td><td>'.$description.'</td><td>'.$start.'</td><td>'.$end.'</td><td>'.$title.'</td>'.&Apache::loncommon::end_data_table_row()."\n");
 1791:     }
 1792:     if ($showntablehdr) {
 1793:         $r->print(&Apache::loncommon::end_data_table().'<br />');
 1794:         if (($curr{'page'} > 1) || ($more_records)) {
 1795:             $r->print('<table><tr>');
 1796:             if ($curr{'page'} > 1) {
 1797:                 $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
 1798:             }
 1799:             if ($more_records) {
 1800:                 $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
 1801:             }
 1802:             $r->print('</tr></table>');
 1803:             $r->print(<<"ENDSCRIPT");
 1804: <script type="text/javascript">
 1805: function chgPage(caller) {
 1806:     if (caller == 'previous') {
 1807:         document.$formname.page.value --;
 1808:     }
 1809:     if (caller == 'next') {
 1810:         document.$formname.page.value ++;
 1811:     }
 1812:     document.$formname.submit();
 1813:     return;
 1814: }
 1815: </script>
 1816: ENDSCRIPT
 1817:         }
 1818:     } else {
 1819:         $r->print('<span class="LC_info">'
 1820:                  .&mt('There are no transactions to display.')
 1821:                  .'</span>'
 1822:         );
 1823:     }
 1824:     $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'."\n".
 1825:               '<input type="hidden" name="command" value="showresv" />'."\n");
 1826:     if ($env{'form.origin'} eq 'aboutme') {
 1827:         $r->print('<input type="hidden" name="origin" value="'.$env{'form.origin'}.'" />'."\n".
 1828:                   '<input type="hidden" name="uname" value="'.$env{'form.uname'}.'" />'."\n".
 1829:                   '<input type="hidden" name="udom" value="'.$env{'form.udom'}.'" />'."\n");
 1830:     }
 1831:     $r->print('</form>');
 1832:     return;
 1833: }
 1834: 
 1835: sub show_reservations_log {
 1836:     my ($r) = @_;
 1837:     my $badslot;
 1838:     my $crstype = &Apache::loncommon::course_type();
 1839:     if ($env{'form.slotname'} eq '') {
 1840:         $r->print('<div class="LC_warning">'.&mt('No slot name provided').'</div>');
 1841:         $badslot = 1;
 1842:     } else {
 1843:         my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
 1844:         if (keys(%slot) == 0) {
 1845:             $r->print('<div class="LC_warning">'.&mt('Invalid slot name: [_1]',$env{'form.slotname'}).'</div>');
 1846:             $badslot = 1;
 1847:         } elsif ($slot{type} ne 'schedulable_student') {
 1848:             my $description = &get_description($env{'form.slotname'},\%slot);
 1849:             $r->print('<div class="LC_warning">');
 1850:             if ($crstype eq 'Community') {
 1851:                 $r->print(&mt('Reservation history unavailable for non-member-reservable slot: [_1].',$description));
 1852:             } else {
 1853:                 $r->print(&mt('Reservation history unavailable for non-student-reservable slot: [_1].',$description));
 1854:             }
 1855:             $r->print('</div>');
 1856:             $badslot = 1;
 1857:         }
 1858:     }
 1859:     if ($badslot) {
 1860:         $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
 1861:                   &mt('Return to slot list').'</a></p>');
 1862:         return;
 1863:     }
 1864:     my $formname = 'reservationslog';
 1865:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1866:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1867:     my %slotlog=&Apache::lonnet::dump('nohist_slotreservationslog',$cdom,$cnum);
 1868:     if ((keys(%slotlog))[0]=~/^error\:/) { undef(%slotlog); }
 1869: 
 1870:     my (%log,@allsymbs);
 1871:     if (keys(%slotlog)) {
 1872:         foreach my $key (keys(%slotlog)) {
 1873:             if (ref($slotlog{$key}) eq 'HASH') {
 1874:                 if (ref($slotlog{$key}{'logentry'}) eq 'HASH') {
 1875:                     if ($slotlog{$key}{'logentry'}{'slot'} eq $env{'form.slotname'}) {
 1876:                         $log{$key} = $slotlog{$key};
 1877:                         if ($slotlog{$key}{'logentry'}{'symb'} ne '') {
 1878:                             push(@allsymbs,$slotlog{$key}{'logentry'}{'symb'});
 1879:                         }
 1880:                     }
 1881:                 }
 1882:             }
 1883:         }
 1884:     }
 1885: 
 1886:     $r->print('<form action="/adm/slotrequest" method="post" name="'.$formname.'">');
 1887:     my %saveable_parameters = ('show' => 'scalar',);
 1888:     &Apache::loncommon::store_course_settings('reservationslog',
 1889:                                               \%saveable_parameters);
 1890:     &Apache::loncommon::restore_course_settings('reservationslog',
 1891:                                                 \%saveable_parameters);
 1892:     # set defaults
 1893:     my $now = time();
 1894:     my $defstart = $now - (7*24*3600); #7 days ago
 1895:     my %defaults = (
 1896:                      page           => '1',
 1897:                      show           => '10',
 1898:                      chgcontext     => 'any',
 1899:                      action         => 'any',
 1900:                      symb           => 'any',
 1901:                      log_start_date => $defstart,
 1902:                      log_end_date   => $now,
 1903:                    );
 1904:     my $more_records = 0;
 1905: 
 1906:     # set current
 1907:     my %curr;
 1908:     foreach my $item ('show','page','chgcontext','action','symb') {
 1909:         $curr{$item} = $env{'form.'.$item};
 1910:     }
 1911:     my ($startdate,$enddate) =
 1912:         &Apache::lonuserutils::get_dates_from_form('log_start_date',
 1913:                                                    'log_end_date');
 1914:     $curr{'log_start_date'} = $startdate;
 1915:     $curr{'log_end_date'} = $enddate;
 1916:     foreach my $key (keys(%defaults)) {
 1917:         if ($curr{$key} eq '') {
 1918:             $curr{$key} = $defaults{$key};
 1919:         }
 1920:     }
 1921:     my (%whodunit,%changed,$version);
 1922:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
 1923: 
 1924:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
 1925:     my $description = $slot{'description'};
 1926:     $r->print('<span class="LC_fontsize_large">');
 1927:     if ($crstype eq 'Community') {
 1928:         $r->print(&mt('Reservation changes for member-reservable slot: [_1]',$description));
 1929:     } else {
 1930:         $r->print(&mt('Reservation changes for student-reservable slot: [_1]',$description));
 1931:     }
 1932:     $r->print('</span><br />');
 1933:     $r->print(&display_filter($formname,$cdom,$cnum,\%curr,$version,\@allsymbs));
 1934:     my $showntablehdr = 0;
 1935:     my $tablehdr = &Apache::loncommon::start_data_table().
 1936:                    &Apache::loncommon::start_data_table_header_row().
 1937:                    '<th>&nbsp;</th><th>'.&mt('When').'</th><th>'.&mt('Who made the change').
 1938:                    '</th><th>'.&mt('Affected User').'</th><th>'.&mt('Action').'</th>'.
 1939:                    '<th>'.&mt('Resource').'</th><th>'.&mt('Context').'</th>'.
 1940:                    &Apache::loncommon::end_data_table_header_row();
 1941:     my ($minshown,$maxshown);
 1942:     $minshown = 1;
 1943:     my $count = 0;
 1944:     if ($curr{'show'} ne &mt('all')) {
 1945:         $maxshown = $curr{'page'} * $curr{'show'};
 1946:         if ($curr{'page'} > 1) {
 1947:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
 1948:         }
 1949:     }
 1950:     my %lt = &reservationlog_contexts($crstype);
 1951:     my (%titles,%maptitles);
 1952:     foreach my $id (sort { $log{$b}{'exe_time'}<=>$log{$a}{'exe_time'} } (keys(%log))) {
 1953:         next if (($log{$id}{'exe_time'} < $curr{'log_start_date'}) ||
 1954:                  ($log{$id}{'exe_time'} > $curr{'log_end_date'}));
 1955:         if ($curr{'show'} ne &mt('all')) {
 1956:             if ($count >= $curr{'page'} * $curr{'show'}) {
 1957:                 $more_records = 1;
 1958:                 last;
 1959:             }
 1960:         }
 1961:         if ($curr{'chgcontext'} ne 'any') {
 1962:             if ($curr{'chgcontext'} eq 'user') {
 1963:                 next if (($log{$id}{'logentry'}{'context'} ne 'user') && 
 1964:                          ($log{$id}{'logentry'}{'context'} ne 'usermanage'));
 1965:             } else {
 1966:                 next if ($log{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
 1967:             }
 1968:         }
 1969:         if ($curr{'action'} ne 'any') {
 1970:             next if ($log{$id}{'logentry'}{'action'} ne $curr{'action'});
 1971:         }
 1972:         if ($curr{'symb'} ne 'any') {
 1973:             next if ($log{$id}{'logentry'}{'symb'} ne $curr{'symb'});
 1974:         }
 1975:         $count ++;
 1976:         next if ($count < $minshown);
 1977:         if (!$showntablehdr) {
 1978:             $r->print($tablehdr);
 1979:             $showntablehdr = 1;
 1980:         }
 1981:         if ($whodunit{$log{$id}{'exe_uname'}.':'.$log{$id}{'exe_udom'}} eq '') {
 1982:             $whodunit{$log{$id}{'exe_uname'}.':'.$log{$id}{'exe_udom'}} =
 1983:                 &Apache::loncommon::plainname($log{$id}{'exe_uname'},$log{$id}{'exe_udom'});
 1984:         }
 1985:         if ($changed{$log{$id}{'uname'}.':'.$log{$id}{'udom'}} eq '') {
 1986:             $changed{$log{$id}{'uname'}.':'.$log{$id}{'udom'}} =
 1987:                 &Apache::loncommon::plainname($log{$id}{'uname'},$log{$id}{'udom'});
 1988:         }
 1989:         my $symb = $log{$id}{'logentry'}{'symb'};
 1990:         my $title = &get_resource_title($symb,\%titles,\%maptitles); 
 1991:         my $chgcontext = $log{$id}{'logentry'}{'context'};
 1992:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
 1993:             $chgcontext = $lt{$chgcontext};
 1994:         }
 1995:         my $chgaction = $log{$id}{'logentry'}{'action'};
 1996:         if ($chgaction ne '' && $lt{$chgaction} ne '') {
 1997:             $chgaction = $lt{$chgaction}; 
 1998:         }
 1999:         $r->print(&Apache::loncommon::start_data_table_row().'<td>'.$count.'</td><td>'.&Apache::lonlocal::locallocaltime($log{$id}{'exe_time'}).'</td><td>'.$whodunit{$log{$id}{'exe_uname'}.':'.$log{$id}{'exe_udom'}}.'</td><td>'.$changed{$log{$id}{'uname'}.':'.$log{$id}{'udom'}}.'</td><td>'.$chgaction.'</td><td>'.$title.'</td><td>'.$chgcontext.'</td>'.&Apache::loncommon::end_data_table_row()."\n");
 2000:     }
 2001:     if ($showntablehdr) {
 2002:         $r->print(&Apache::loncommon::end_data_table().'<br />');
 2003:         if (($curr{'page'} > 1) || ($more_records)) {
 2004:             $r->print('<table><tr>');
 2005:             if ($curr{'page'} > 1) {
 2006:                 $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
 2007:             }
 2008:             if ($more_records) {
 2009:                 $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
 2010:             }
 2011:             $r->print('</tr></table>');
 2012:             $r->print(<<"ENDSCRIPT");
 2013: <script type="text/javascript">
 2014: function chgPage(caller) {
 2015:     if (caller == 'previous') {
 2016:         document.$formname.page.value --;
 2017:     }
 2018:     if (caller == 'next') {
 2019:         document.$formname.page.value ++;
 2020:     }
 2021:     document.$formname.submit();
 2022:     return;
 2023: }
 2024: </script>
 2025: ENDSCRIPT
 2026:         }
 2027:     } else {
 2028:         $r->print(&mt('There are no records to display.'));
 2029:     }
 2030:     $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
 2031:               '<input type="hidden" name="slotname" value="'.$env{'form.slotname'}.'" />'.
 2032:               '<input type="hidden" name="command" value="slotlog" /></form>'.
 2033:               '<p><a href="/adm/slotrequest?command=showslots">'.
 2034:               &mt('Return to slot list').'</a></p>');
 2035:     return;
 2036: }
 2037: 
 2038: sub get_resource_title {
 2039:     my ($symb,$titles,$maptitles) = @_;
 2040:     my $title;
 2041:     if ((ref($titles) eq 'HASH') && (ref($maptitles) eq 'HASH')) { 
 2042:         if (defined($titles->{$symb})) {
 2043:             $title = $titles->{$symb};
 2044:         } else {
 2045:             $title = &Apache::lonnet::gettitle($symb);
 2046:             my $maptitle;
 2047:             my ($mapurl) = &Apache::lonnet::decode_symb($symb);
 2048:             if (defined($maptitles->{$mapurl})) {
 2049:                 $maptitle = $maptitles->{$mapurl};
 2050:             } else {
 2051:                 if ($mapurl eq $env{'course.'.$env{'request.course.id'}.'.url'}) {
 2052:                     $maptitle=&mt('Main Course Documents');
 2053:                 } else {
 2054:                     $maptitle=&Apache::lonnet::gettitle($mapurl);
 2055:                 }
 2056:                 $maptitles->{$mapurl} = $maptitle;
 2057:             }
 2058:             if ($maptitle ne '') {
 2059:                 $title .= ' '.&mt('(in [_1])',$maptitle);
 2060:             }
 2061:             $titles->{$symb} = $title;
 2062:         }
 2063:     } else {
 2064:         $title = $symb;
 2065:     }
 2066:     return $title;
 2067: }
 2068: 
 2069: sub reservationlog_contexts {
 2070:     my ($crstype) = @_;
 2071:     my %lt = &Apache::lonlocal::texthash (
 2072:                                              any        => 'Any',
 2073:                                              user       => 'By student',
 2074:                                              manage     => 'Via Slot Manager',
 2075:                                              parameter  => 'Via Parameter Manager',
 2076:                                              reserve    => 'Made reservation',
 2077:                                              release    => 'Dropped reservation',
 2078:                                              usermanage => 'By student', 
 2079:                                          );
 2080:     if ($crstype eq 'Community') {
 2081:         $lt{'user'} = &mt('By member');
 2082:         $lt{'usermanage'} = $lt{'user'};
 2083:     }
 2084:     return %lt;
 2085: }
 2086: 
 2087: sub display_filter {
 2088:     my ($formname,$cdom,$cnum,$curr,$version,$allsymbs) = @_;
 2089:     my $nolink = 1;
 2090:     my (%titles,%maptitles);
 2091:     my $output = '<br /><table><tr><td valign="top">'.
 2092:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b><br />'.
 2093:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
 2094:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
 2095:                  '</td><td>&nbsp;&nbsp;</td>';
 2096:     my $startform =
 2097:         &Apache::lonhtmlcommon::date_setter($formname,'log_start_date',
 2098:                                             $curr->{'log_start_date'},undef,
 2099:                                             undef,undef,undef,undef,undef,undef,$nolink);
 2100:     my $endform =
 2101:         &Apache::lonhtmlcommon::date_setter($formname,'log_end_date',
 2102:                                             $curr->{'log_end_date'},undef,
 2103:                                             undef,undef,undef,undef,undef,undef,$nolink);
 2104:     my $crstype = &Apache::loncommon::course_type();
 2105:     my %lt = &reservationlog_contexts($crstype);
 2106:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').
 2107:                '</b><br /><table><tr><td>'.&mt('After:').
 2108:                '</td><td>'.$startform.'</td></tr><tr><td>'.&mt('Before:').'</td><td>'.
 2109:                $endform.'</td></tr></table></td><td>&nbsp;&nbsp;</td>';
 2110:     if (ref($allsymbs) eq 'ARRAY') {
 2111:         $output .= '<td valign="top"><b>'.&mt('Resource').'</b><br />'.
 2112:                    '<select name="resource"><option value="any"';
 2113:         if ($curr->{'resource'} eq 'any') {
 2114:             $output .= ' selected="selected"';
 2115:         }
 2116:         $output .=  '>'.&mt('Any').'</option>'."\n";
 2117:         foreach my $symb (@{$allsymbs}) {
 2118:             my $title = &get_resource_title($symb,\%titles,\%maptitles);
 2119:             my $selstr = '';
 2120:             if ($curr->{'resource'} eq $symb) {
 2121:                 $selstr = ' selected="selected"';
 2122:             }
 2123:             $output .= '  <option value="'.$symb.'"'.$selstr.'>'.$title.'</option>';
 2124:         }
 2125:         $output .= '</select></td><td>&nbsp;&nbsp;</td><td valign="top"><b>'.
 2126:                    &mt('Context:').'</b><br /><select name="chgcontext">';
 2127:         foreach my $chgtype ('any','user','manage','parameter') {
 2128:             my $selstr = '';
 2129:             if ($curr->{'chgcontext'} eq $chgtype) {
 2130:                 $output .= $selstr = ' selected="selected"';
 2131:             }
 2132:             $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
 2133:         }
 2134:         $output .= '</select></td>';
 2135:     } else {
 2136:         $output .= '<td valign="top"><b>'.&mt('Action').'</b><br />'.
 2137:                    '<select name="action"><option value="any"';
 2138:         if ($curr->{'action'} eq 'any') {
 2139:             $output .= ' selected="selected"';
 2140:         }
 2141:         $output .=  '>'.&mt('Any').'</option>'."\n";
 2142:         foreach my $actiontype ('reserve','release') {
 2143:             my $selstr = '';
 2144:             if ($curr->{'action'} eq $actiontype) {
 2145:                 $output .= $selstr = ' selected="selected"';
 2146:             }
 2147:             $output .= '<option value="'.$actiontype.'"'.$selstr.'>'.$lt{$actiontype}.'</option>'."\n";
 2148:         }
 2149:         $output .= '</select></td>';
 2150:     }
 2151:     $output .= '<td>&nbsp;&nbsp;</td><td valign="middle"><input type="submit" value="'.
 2152:                &mt('Update Display').'" /></tr></table>'.
 2153:                '<p class="LC_info">'.
 2154:                &mt('Only changes made from servers running LON-CAPA [_1] or later are displayed.'
 2155:                   ,'2.9.0');
 2156:     if ($version) {
 2157:         $output .= ' '.&mt('This LON-CAPA server is version [_1]',$version);
 2158:     }
 2159:     $output .= '</p><hr /><br />';
 2160:     return $output;
 2161: }
 2162: 
 2163: sub slot_change_messaging {
 2164:     my ($setting,$subject,$msg,$action) = @_;
 2165:     my $user = $env{'user.name'};
 2166:     my $domain = $env{'user.domain'};
 2167:     my ($message_status,$comment_status);
 2168:     if ($setting eq 'only_student'
 2169:         || $setting eq 'student_and_user_notes_screen') {
 2170:         $message_status =
 2171:             &Apache::lonmsg::user_normal_msg($user,$domain,$subject,$msg);
 2172:         $message_status = '<li>'.&mt('Sent to you: [_1]',
 2173:                                     $message_status).' </li>';
 2174:     }
 2175:     if ($setting eq 'student_and_user_notes_screen') {
 2176:         $comment_status =
 2177:             &Apache::lonmsg::store_instructor_comment($subject.'<br />'.
 2178:                                                       $msg,$user,$domain);
 2179:         $comment_status = '<li>'.&mt('Entry added to course record (viewable by instructor): [_1]',
 2180:                                     $comment_status).'</li>';
 2181:     }
 2182:     if ($message_status || $comment_status) {
 2183:         my $msgtitle;
 2184:         if ($action eq 'reserve') {
 2185:             $msgtitle = &mt('Status of messages about saved reservation');
 2186:         } elsif ($action eq 'release') {
 2187:             $msgtitle = &mt('Status of messages about dropped reservation');
 2188:         } elsif ($action eq 'nochange') {
 2189:             $msgtitle = &mt('Status of messages about unchanged existing reservation');
 2190:         }
 2191:         return '<span class="LC_info">'.$msgtitle.'</span>'
 2192:                .'<ul>'
 2193:                .$message_status
 2194:                .$comment_status
 2195:                .'</ul><hr />';
 2196:     }
 2197: }
 2198: 
 2199: sub upload_start {
 2200:     my ($r)=@_;    
 2201:     $r->print(
 2202:         &Apache::grades::checkforfile_js()
 2203:        .'<h3>'.&mt('Specify a file containing the slot definitions.').'</h3>'
 2204:        .'<form method="post" enctype="multipart/form-data"'
 2205:        .' action="/adm/slotrequest" name="slotupload">'
 2206:        .'<input type="hidden" name="command" value="csvuploadmap" />'
 2207:        .&Apache::lonhtmlcommon::start_pick_box()
 2208:        .&Apache::lonhtmlcommon::row_title(&mt('File'))
 2209:        .&Apache::loncommon::upfile_select_html()
 2210:        .&Apache::lonhtmlcommon::row_closure()
 2211:        .&Apache::lonhtmlcommon::row_title(
 2212:             '<label for="noFirstLine">'
 2213:            .&mt('Ignore First Line')
 2214:            .'</label>')
 2215:        .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
 2216:        .&Apache::lonhtmlcommon::row_closure(1)
 2217:        .&Apache::lonhtmlcommon::end_pick_box()
 2218:        .'<p>'
 2219:        .'<input type="button" onclick="javascript:checkUpload(this.form);"'
 2220:        .' value="'.&mt('Next').'" />'
 2221:        .'</p>'
 2222:       .'</form>'
 2223:     );
 2224: }
 2225: 
 2226: sub csvuploadmap_header {
 2227:     my ($r,$datatoken,$distotal)= @_;
 2228:     my $javascript;
 2229:     if ($env{'form.upfile_associate'} eq 'reverse') {
 2230: 	$javascript=&csvupload_javascript_reverse_associate();
 2231:     } else {
 2232: 	$javascript=&csvupload_javascript_forward_associate();
 2233:     }
 2234: 
 2235:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 2236:     my $ignore=&mt('Ignore First Line');
 2237: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
 2238: 
 2239:     $r->print(<<ENDPICK);
 2240: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 2241: <h3>Identify fields $help_field</h3>	
 2242: Total number of records found in file: $distotal <hr />
 2243: Enter as many fields as you can. The system will inform you and bring you back
 2244: to this page if the data selected is insufficient to create the slots.<hr />
 2245: <input type="button" value="Reverse Association" onclick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 2246: <label><input type="checkbox" name="noFirstLine"$checked />$ignore</label>
 2247: <input type="hidden" name="associate"  value="" />
 2248: <input type="hidden" name="datatoken"  value="$datatoken" />
 2249: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 2250: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 2251: <input type="hidden" name="upfile_associate" 
 2252:                                        value="$env{'form.upfile_associate'}" />
 2253: <input type="hidden" name="command"    value="csvuploadassign" />
 2254: <hr />
 2255: <script type="text/javascript" language="Javascript">
 2256: $javascript
 2257: </script>
 2258: ENDPICK
 2259:     return '';
 2260: 
 2261: }
 2262: 
 2263: sub csvuploadmap_footer {
 2264:     my ($request,$i,$keyfields) =@_;
 2265:     my $buttontext = &mt('Create Slots');
 2266:     $request->print(<<ENDPICK);
 2267: </table>
 2268: <input type="hidden" name="nfields" value="$i" />
 2269: <input type="hidden" name="keyfields" value="$keyfields" />
 2270: <input type="button" onclick="javascript:verify(this.form)" value="$buttontext" /><br />
 2271: </form>
 2272: ENDPICK
 2273: }
 2274: 
 2275: sub csvupload_javascript_reverse_associate {
 2276:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 2277:     return(<<ENDPICK);
 2278:   function verify(vf) {
 2279:     var foundstart=0;
 2280:     var foundend=0;
 2281:     var foundname=0;
 2282:     var foundtype=0;
 2283:     for (i=0;i<=vf.nfields.value;i++) {
 2284:       tw=eval('vf.f'+i+'.selectedIndex');
 2285:       if (i==0 && tw!=0) { foundname=1; }
 2286:       if (i==1 && tw!=0) { foundtype=1; }
 2287:       if (i==2 && tw!=0) { foundstat=1; }
 2288:       if (i==3 && tw!=0) { foundend=1; }
 2289:     }
 2290:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 2291: 	alert('$error1');
 2292: 	return;
 2293:     }
 2294:     vf.submit();
 2295:   }
 2296:   function flip(vf,tf) {
 2297:   }
 2298: ENDPICK
 2299: }
 2300: 
 2301: sub csvupload_javascript_forward_associate {
 2302:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 2303:   return(<<ENDPICK);
 2304:   function verify(vf) {
 2305:     var foundstart=0;
 2306:     var foundend=0;
 2307:     var foundname=0;
 2308:     var foundtype=0;
 2309:     for (i=0;i<=vf.nfields.value;i++) {
 2310:       tw=eval('vf.f'+i+'.selectedIndex');
 2311:       if (tw==1) { foundname=1; }
 2312:       if (tw==2) { foundtype=1; }
 2313:       if (tw==3) { foundstat=1; }
 2314:       if (tw==4) { foundend=1; }
 2315:     }
 2316:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 2317: 	alert('$error1');
 2318: 	return;
 2319:     }
 2320:     vf.submit();
 2321:   }
 2322:   function flip(vf,tf) {
 2323:   }
 2324: ENDPICK
 2325: }
 2326: 
 2327: sub csv_upload_map {
 2328:     my ($r)= @_;
 2329: 
 2330:     my $datatoken;
 2331:     if (!$env{'form.datatoken'}) {
 2332: 	$datatoken=&Apache::loncommon::upfile_store($r);
 2333:     } else {
 2334: 	$datatoken=$env{'form.datatoken'};
 2335: 	&Apache::loncommon::load_tmp_file($r);
 2336:     }
 2337:     my @records=&Apache::loncommon::upfile_record_sep();
 2338:     if ($env{'form.noFirstLine'}) { shift(@records); }
 2339:     &csvuploadmap_header($r,$datatoken,$#records+1);
 2340:     my ($i,$keyfields);
 2341:     if (@records) {
 2342: 	my @fields=&csvupload_fields();
 2343: 
 2344: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 2345: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 2346: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 2347: 							  \@fields);
 2348: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 2349: 	    chop($keyfields);
 2350: 	} else {
 2351: 	    unshift(@fields,['none','']);
 2352: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 2353: 							    \@fields);
 2354: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 2355: 	    $keyfields=join(',',sort(keys(%sone)));
 2356: 	}
 2357:     }
 2358:     &csvuploadmap_footer($r,$i,$keyfields);
 2359: 
 2360:     return '';
 2361: }
 2362: 
 2363: sub csvupload_fields {
 2364:     return (['name','Slot name'],
 2365: 	    ['type','Type of slot'],
 2366: 	    ['starttime','Start Time of slot'],
 2367: 	    ['endtime','End Time of slot'],
 2368: 	    ['startreserve','Reservation Start Time'],
 2369:             ['endreserve','Reservation End Time'],
 2370:             ['reservationmsg','Message when reservation changed'],
 2371: 	    ['ip','IP or DNS restriction'],
 2372: 	    ['proctor','List of proctor ids'],
 2373: 	    ['description','Slot Description'],
 2374: 	    ['maxspace','Maximum number of reservations'],
 2375: 	    ['symb','Resource Restriction'],
 2376: 	    ['uniqueperiod','Date range of slot exclusion'],
 2377: 	    ['secret','Secret word proctor uses to validate'],
 2378: 	    ['allowedsections','Sections slot is restricted to'],
 2379: 	    ['allowedusers','Users slot is restricted to'],
 2380: 	    );
 2381: }
 2382: 
 2383: sub csv_upload_assign {
 2384:     my ($r,$mgr)= @_;
 2385:     &Apache::loncommon::load_tmp_file($r);
 2386:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 2387:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 2388:     my %fields=&Apache::grades::get_fields();
 2389:     $r->print('<h3>'.&mt('Creating Slots').'</h3>');
 2390:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 2391:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 2392:     my $countdone=0;
 2393:     my @errors;
 2394:     foreach my $slot (@slotdata) {
 2395: 	my %slot;
 2396: 	my %entries=&Apache::loncommon::record_sep($slot);
 2397: 	my $domain;
 2398: 	my $name=$entries{$fields{'name'}};
 2399: 	if ($name=~/^\s*$/) {
 2400: 	    push(@errors,"Did not create slot with no name");
 2401: 	    next;
 2402: 	}
 2403: 	if ($name=~/\s/) { 
 2404: 	    push(@errors,"$name not created -- Name must not contain spaces");
 2405: 	    next;
 2406: 	}
 2407: 	if ($name=~/\W/) { 
 2408: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 2409: 	    next;
 2410: 	}
 2411: 	if ($entries{$fields{'type'}}) {
 2412: 	    $slot{'type'}=$entries{$fields{'type'}};
 2413: 	} else {
 2414: 	    $slot{'type'}='preassigned';
 2415: 	}
 2416: 	if ($slot{'type'} ne 'preassigned' &&
 2417: 	    $slot{'type'} ne 'schedulable_student') {
 2418: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 2419: 	    next;
 2420: 	}
 2421: 	if ($entries{$fields{'starttime'}}) {
 2422: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 2423: 	}
 2424: 	if ($entries{$fields{'endtime'}}) {
 2425: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 2426: 	}
 2427: 
 2428: 	# start/endtime must be defined and greater than zero
 2429: 	if (!$slot{'starttime'}) {
 2430: 	    push(@errors,"$name not created -- Invalid start time");
 2431: 	    next;
 2432: 	}
 2433: 	if (!$slot{'endtime'}) {
 2434: 	    push(@errors,"$name not created -- Invalid end time");
 2435: 	    next;
 2436: 	}
 2437: 	if ($slot{'starttime'} > $slot{'endtime'}) {
 2438: 	    push(@errors,"$name not created -- Slot starts after it ends");
 2439: 	    next;
 2440: 	}
 2441: 
 2442: 	if ($entries{$fields{'startreserve'}}) {
 2443: 	    $slot{'startreserve'}=
 2444: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 2445: 	}
 2446: 	if (defined($slot{'startreserve'})
 2447: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
 2448: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
 2449: 	    next;
 2450: 	}
 2451: 
 2452:         if ($entries{$fields{'endreserve'}}) {
 2453:             $slot{'endreserve'}=
 2454:                 &UnixDate($entries{$fields{'endreserve'}},"%s");
 2455:         }
 2456:         if (defined($slot{'endreserve'})
 2457:             && $slot{'endreserve'} > $slot{'starttime'}) {
 2458:             push(@errors,"$name not created -- Slot's reservation end time is after the slot's start time.");
 2459:             next;
 2460:         }
 2461: 
 2462:         if ($slot{'type'} eq 'schedulable_student') {
 2463:             if ($entries{$fields{'reservationmsg'}}) {
 2464:                  if (($entries{$fields{'reservationmsg'}} eq 'only_student') ||
 2465:                      ($entries{$fields{'reservationmsg'}} eq 'student_and_user_notes_screen')) {
 2466:                       $slot{'reservationmsg'}=$entries{$fields{'reservationmsg'}};
 2467:                  } else {
 2468:                       unless (($entries{$fields{'reservationmsg'}} eq 'none') ||
 2469:                               ($entries{$fields{'reservationmsg'}} eq '')) {
 2470:                           push(@errors,"$name -- Slot's reservationmsg setting ignored - not one of: 'only_student', 'student_and_user_notes_screen', 'none' or ''");
 2471:                       }
 2472:                  }
 2473:             }
 2474:         }
 2475: 
 2476: 	foreach my $key ('ip','proctor','description','maxspace',
 2477: 			 'secret','symb') {
 2478: 	    if ($entries{$fields{$key}}) {
 2479: 		$slot{$key}=$entries{$fields{$key}};
 2480: 	    }
 2481: 	}
 2482: 
 2483: 	if ($entries{$fields{'uniqueperiod'}}) {
 2484: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 2485: 	    my @times=(&UnixDate($start,"%s"),
 2486: 		       &UnixDate($end,"%s"));
 2487: 	    $slot{'uniqueperiod'}=\@times;
 2488: 	}
 2489: 	if (defined($slot{'uniqueperiod'})
 2490: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
 2491: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
 2492: 	    next;
 2493: 	}
 2494: 
 2495: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 2496: 	$r->print('.');
 2497: 	$r->rflush();
 2498: 	$countdone++;
 2499:     }
 2500:     if ($countdone) {
 2501:         &Apache::lonnet::devalidate_slots_cache($cname,$cdom); 
 2502:     }
 2503:     $r->print('<p>'.&mt('Created [quant,_1,slot]',$countdone)."\n".'</p>');
 2504:     foreach my $error (@errors) {
 2505: 	$r->print('<p><span class="LC_warning">'.$error.'</span></p>'."\n");
 2506:     }
 2507:     &show_table($r,$mgr);
 2508:     return '';
 2509: }
 2510: 
 2511: sub slot_command_titles {
 2512:     my %titles = (
 2513:                  slotlog            => 'Reservation Logs',
 2514:                  showslots          => 'Manage Slots',
 2515:                  showresv           => 'Reservation History',
 2516:                  manageresv         => 'Manage Reservations',
 2517:                  uploadstart        => 'Upload Slots File',
 2518:                  csvuploadmap       => 'Upload Slots File',
 2519:                  csvuploadassign    => 'Upload Slots File',
 2520:                  delete             => 'Slot Deletion',
 2521:                  release            => 'Reservation Result',
 2522:                  remove_reservation => 'Remove Registration',
 2523:                  get_reservation    => 'Request Reservation',
 2524:               );
 2525:     return %titles;
 2526: }
 2527: 
 2528: sub slot_reservationmsg_options {
 2529:     my %options = &Apache::lonlocal::texthash (
 2530:                         only_student  => 'Sent to student',
 2531:         student_and_user_notes_screen => 'Sent to student and added to user notes',
 2532:                                  none => 'None sent and no record in user notes',
 2533:     );
 2534:     return %options;
 2535: }
 2536: 
 2537: sub handler {
 2538:     my $r=shift;
 2539: 
 2540:     &Apache::loncommon::content_type($r,'text/html');
 2541:     &Apache::loncommon::no_cache($r);
 2542:     if ($r->header_only()) {
 2543: 	$r->send_http_header();
 2544: 	return OK;
 2545:     }
 2546: 
 2547:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 2548: 
 2549:     my %crumb_titles = &slot_command_titles();
 2550:     my $brcrum;
 2551: 
 2552:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 2553:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 2554:     if ($env{'form.command'} eq 'showslots') {
 2555:         if (($vgr ne 'F') && ($mgr ne 'F')) {
 2556:             $env{'form.command'} = 'manageresv'; 
 2557:         }
 2558:     } elsif ($env{'form.command'} eq 'manageresv') {
 2559:         if (($vgr eq 'F') || ($mgr eq 'F')) {
 2560:             $env{'form.command'} = 'showslots';
 2561:         }
 2562:     }
 2563:     my $title='Requesting Another Worktime';
 2564:     if ($env{'form.command'} eq 'showresv') {
 2565:         $title = 'Reservation History';
 2566:         if ($env{'form.origin'} eq 'aboutme') {
 2567:             $brcrum =[{href=>"/adm/$env{'form.udom'}/$env{'form.uname'}/aboutme",text=>'Personal Information Page'}];
 2568:         } else {
 2569:             $brcrum =[{href=>"/adm/slotrequest?command=manageresv",text=>'Manage Reservations'}];
 2570:         }
 2571:         if (ref($brcrum) eq 'ARRAY') {
 2572:             push(@{$brcrum},{href=>"/adm/slotrequest?command=showresv",text=>$title});
 2573:         }
 2574:     } elsif ($env{'form.command'} eq 'manageresv') {
 2575:         $title = 'Manage Reservations';
 2576:         $brcrum =[{href=>"/adm/slotrequest?command=manageresv",text=>$title}];
 2577:     } elsif ($vgr eq 'F') {
 2578:         if ($env{'form.command'} =~ /^(slotlog|showslots|uploadstart|csvuploadmap|csvuploadassign|delete|release|remove_registration)$/) {
 2579:             $brcrum =[{href=>"/adm/slotrequest?command=showslots",
 2580:                        text=>$crumb_titles{'showslots'}}];
 2581: 	    $title = 'Managing Slots';
 2582:             unless ($env{'form.command'} eq 'showslots') {
 2583:                 if (ref($brcrum) eq 'ARRAY') {
 2584:                     push(@{$brcrum},{href=>"/adm/slotrequest?command=$env{'form.command'}",text=>$crumb_titles{$env{'form.command'}}});
 2585:                 }
 2586:             }
 2587:         }
 2588:     } elsif ($env{'form.command'} eq 'release') {
 2589:         if ($env{'form.context'} eq 'usermanage') {
 2590:             $brcrum =[{href=>"/adm/slotrequest?command=manageresv",
 2591:                        text=>$crumb_titles{'showslots'}}];
 2592:             $title = 'Manage Reservations';
 2593:             if (ref($brcrum) eq 'ARRAY') {
 2594:                 push(@{$brcrum},{href=>"/adm/slotrequest?command=$env{'form.command'}",text=>$crumb_titles{$env{'form.command'}}});
 2595:             }
 2596:         }
 2597:     } else {
 2598:         $brcrum =[];
 2599:     }
 2600:     &start_page($r,$title,$brcrum);
 2601: 
 2602:     if ($env{'form.command'} eq 'manageresv') {
 2603:         my $crstype = &Apache::loncommon::course_type();
 2604:         &manage_reservations($r,$crstype);
 2605:     } elsif ($env{'form.command'} eq 'showresv') {
 2606:         &show_reservations($r,$env{'form.uname'},$env{'form.udom'});
 2607:     } elsif ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 2608: 	&show_table($r,$mgr);
 2609:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 2610: 	&remove_registration($r);
 2611:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 2612: 	if ($env{'form.entry'} eq 'remove all') {
 2613: 	    &release_all_slot($r,$mgr);
 2614: 	} else {
 2615: 	    &release_slot($r,undef,undef,undef,$mgr);
 2616: 	}
 2617:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 2618: 	&delete_slot($r);
 2619:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 2620: 	&upload_start($r);
 2621:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 2622: 	&csv_upload_map($r);
 2623:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 2624: 	if ($env{'form.associate'} ne 'Reverse Association') {
 2625: 	    &csv_upload_assign($r,$mgr);
 2626: 	} else {
 2627: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 2628: 		$env{'form.upfile_associate'} = 'reverse';
 2629: 	    } else {
 2630: 		$env{'form.upfile_associate'} = 'forward';
 2631: 	    }
 2632: 	    &csv_upload_map($r);
 2633: 	}
 2634:     } elsif ($env{'form.command'} eq 'slotlog' && $mgr eq 'F') {
 2635:         &show_reservations_log($r);
 2636:     } else {
 2637: 	my $symb=&unescape($env{'form.symb'});
 2638: 	if (!defined($symb)) {
 2639: 	    &fail($r,'not_valid');
 2640: 	    return OK;
 2641: 	}
 2642: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 2643: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 2644: 	if ($useslots ne 'resource' 
 2645: 	    && $useslots ne 'map' 
 2646: 	    && $useslots ne 'map_map') {
 2647: 	    &fail($r,'not_available');
 2648: 	    return OK;
 2649: 	}
 2650: 	$env{'request.symb'}=$symb;
 2651: 	my $type = ($res =~ /\.task$/) ? 'Task'
 2652: 	                               : 'problem';
 2653: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 2654: 	if ($status eq 'CAN_ANSWER' ||
 2655: 	    $status eq 'NEEDS_CHECKIN' ||
 2656: 	    $status eq 'WAITING_FOR_GRADE') {
 2657: 	    &fail($r,'not_allowed');
 2658: 	    return OK;
 2659: 	}
 2660: 	if ($env{'form.requestattempt'}) {
 2661: 	    &show_choices($r,$symb);
 2662: 	} elsif ($env{'form.command'} eq 'release') {
 2663: 	    &release_slot($r,$symb);
 2664: 	} elsif ($env{'form.command'} eq 'get') {
 2665: 	    &get_slot($r,$symb);
 2666: 	} elsif ($env{'form.command'} eq 'change') {
 2667:             if ($env{'form.nochange'}) {
 2668:                 my $slot_name = $env{'form.releaseslot'};
 2669:                 my @slots = &check_for_reservation($symb,'allslots');
 2670:                 my $msg;
 2671:                 if (($slot_name ne '') && (grep(/^\Q$slot_name\E/,@slots))) { 
 2672:                      my %slot=&Apache::lonnet::get_slot($env{'form.releaseslot'});
 2673:                      my $description=&get_description($slot_name,\%slot);
 2674:                      $msg = '<span style="font-weight: bold;">'.
 2675:                             &mt('Unchanged reservation: [_1]',$description).'</span><br /><br />';
 2676:                      my $person = 
 2677:                          &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'});
 2678:                      my $subject = &mt('Reservation unchanged: [_1]',$description);
 2679:                      my $msgbody = &mt('No change to existing registration by [_1] for [_2].',$person,$description);
 2680:                      $msg .= &slot_change_messaging($slot{'reservationmsg'},$subject,$msgbody,'nochange');
 2681:                 } else {
 2682:                     $msg = '<span class="LC_warning">'.&mt('Reservation no longer reported as available.').'</span>';
 2683:                 }
 2684:                 $r->print($msg);
 2685:                 &return_link($r);
 2686: 	    } elsif (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
 2687: 		&release_slot($r,$symb,$env{'form.releaseslot'});
 2688: 	    }
 2689: 	} else {
 2690: 	    $r->print('<p>'.&mt('Unknown command: [_1]',$env{'form.command'}).'</p>');
 2691: 	}
 2692:     }
 2693:     &end_page($r);
 2694:     return OK;
 2695: }
 2696: 
 2697: 1;
 2698: __END__

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