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

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

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