File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.96: download - view: text, annotated - select for diffs
Mon May 4 16:45:58 2009 UTC (15 years, 1 month ago) by bisitz
Branches: MAIN
CVS tags: HEAD
XHTML:
- XHTML conform <img> tag closure
- Added some missing alt attributes to <img>

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

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