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

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

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