File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.147: download - view: text, annotated - select for diffs
Wed Jul 12 15:48:23 2023 UTC (10 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Remove trailing whitespace.  No code changes.

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

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