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

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

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