File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.125.2.3: download - view: text, annotated - select for diffs
Mon Oct 24 01:13:54 2016 UTC (7 years, 6 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
- For 2.11
  - Backport 1.133

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

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