File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.125.2.6: download - view: text, annotated - select for diffs
Fri Sep 14 21:06:22 2018 UTC (5 years, 8 months ago) by raeburn
Branches: version_2_11_X
- For 2.11
  Backport 1.135

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

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