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

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

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