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

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

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