File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.142: download - view: text, annotated - select for diffs
Sun Jul 7 22:06:04 2019 UTC (4 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Remove span tags and assign LC_error class to enclosing p tags.
- Remove some training white space.
- Standard indent is 4 spaces.

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

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