File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.125.2.3.4.2: download - view: text, annotated - select for diffs
Wed Apr 8 20:06:59 2020 UTC (4 years, 1 month ago) by raeburn
Branches: version_2_11_2_uiuc
Diff to branchpoint 1.125.2.3: preferred, unified
- For 2.11.2 (modified).
  Include changes in 1.141 (modified) and 1.142.

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

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