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

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

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