File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.140: download - view: text, annotated - select for diffs
Sat Jun 22 19:07:58 2019 UTC (4 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6911

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

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