File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.122: download - view: text, annotated - select for diffs
Sun May 10 03:58:12 2015 UTC (9 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, HEAD
"Reservations" screen for students.
  - Efficiency improvements
     - Information about all slots and consumed_uniqueperiods rerieved once,
       even if there are multiple resources which employ slots to control
       access.
  - User interface improvements in the case where there are many possible
    slots to choose from.  If more than a single possible slot, display
    menu to show current reservation, or search for reservations in a
    date/time range, or show all possible.

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

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