File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.127: download - view: text, annotated - select for diffs
Fri Sep 25 02:03:29 2015 UTC (8 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Reservations screen for students.
  - If all problems in a folder have useslots set to map or map_map and
    slotstatus is the same for each problem, slot actions menu shown for
    folder, and rows for individual resources are hidden.
  - Arrow to left of folder icon toggles display.

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

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