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

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

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