Annotation of loncom/interface/slotrequest.pm, revision 1.126

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

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