File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.128: download - view: text, annotated - select for diffs
Fri Sep 25 16:13:00 2015 UTC (8 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Values user selected in hour dropdown lists for "Open after" and
 "Closed before" when using "Search by date" display of slots were not used.

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

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