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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for requesting to have slots added to a students record
                      3: #
1.90    ! bisitz      4: # $Id: slotrequest.pm,v 1.89 2009/03/01 03:23:18 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.28      albertel   59:     my ($r,$title)=@_;
1.52      albertel   60:     $r->print(&Apache::loncommon::start_page($title));
1.1       albertel   61: }
                     62: 
                     63: sub end_page {
                     64:     my ($r)=@_;
1.52      albertel   65:     $r->print(&Apache::loncommon::end_page());
1.1       albertel   66: }
                     67: 
1.2       albertel   68: =pod
                     69: 
                     70:  slot_reservations db
                     71:    - keys are 
                     72:     - slotname\0id -> value is an hashref of
                     73:                          name -> user@domain of holder
                     74:                          timestamp -> timestamp of reservation
                     75:                          symb -> symb of resource that it is reserved for
                     76: 
                     77: =cut
                     78: 
                     79: sub get_course {
1.69      albertel   80:     (undef,my $courseid)=&Apache::lonnet::whichuser();
1.2       albertel   81:     my $cdom=$env{'course.'.$courseid.'.domain'};
                     82:     my $cnum=$env{'course.'.$courseid.'.num'};
                     83:     return ($cnum,$cdom);
                     84: }
                     85: 
                     86: sub get_reservation_ids {
                     87:     my ($slot_name)=@_;
                     88:     
                     89:     my ($cnum,$cdom)=&get_course();
                     90: 
                     91:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                     92: 				       "^$slot_name\0");
1.67      albertel   93:     if (&Apache::lonnet::error(%consumed)) { 
1.40      albertel   94: 	return 'error: Unable to determine current status';
                     95:     }
1.2       albertel   96:     my ($tmp)=%consumed;
                     97:     if ($tmp=~/^error: 2 / ) {
                     98: 	return 0;
                     99:     }
                    100:     return keys(%consumed);
                    101: }
                    102: 
                    103: sub space_available {
                    104:     my ($slot_name,$slot)=@_;
                    105:     my $max=$slot->{'maxspace'};
                    106: 
                    107:     if (!defined($max)) { return 1; }
                    108: 
                    109:     my $consumed=scalar(&get_reservation_ids($slot_name));
                    110:     if ($consumed < $max) {
                    111: 	return 1
                    112:     }
                    113:     return 0;
                    114: }
1.3       albertel  115: 
1.4       albertel  116: sub check_for_reservation {
1.43      albertel  117:     my ($symb,$mode)=@_;
1.4       albertel  118:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
                    119: 				       $env{'user.domain'}, $env{'user.name'});
                    120: 
                    121:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
                    122: 				    $env{'user.domain'}, $env{'user.name'});
                    123:     my @slots = (split(/:/,$student), split(/:/, $course));
                    124: 
                    125:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
                    126: 
                    127:     my ($cnum,$cdom)=&get_course();
                    128:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
                    129: 
1.67      albertel  130:     if (&Apache::lonnet::error($student) 
                    131: 	|| &Apache::lonnet::error($course)
                    132: 	|| &Apache::lonnet::error(%slots)) {
1.41      albertel  133: 	return 'error: Unable to determine current status';
                    134:     }    
1.43      albertel  135:     my @got;
                    136:     foreach my $slot_name (sort {
                    137: 	if (ref($slots{$a}) && ref($slots{$b})) {
                    138: 	    return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
                    139: 	}
                    140: 	if (ref($slots{$a})) { return -1;}
                    141: 	if (ref($slots{$b})) { return 1;}
                    142: 	return 0;
                    143:     } @slots) {
1.4       albertel  144: 	next if (!defined($slots{$slot_name}) ||
                    145: 		 !ref($slots{$slot_name}));
                    146: 	&Apache::lonxml::debug(time." $slot_name ".
                    147: 			       $slots{$slot_name}->{'starttime'}." -- ".
                    148: 			       $slots{$slot_name}->{'startreserve'});
1.7       albertel  149: 	if ($slots{$slot_name}->{'endtime'} > time &&
1.4       albertel  150: 	    $slots{$slot_name}->{'startreserve'} < time) {
1.7       albertel  151: 	    # between start of reservation times and end of slot
1.43      albertel  152: 	    if ($mode eq 'allslots') {
                    153: 		push(@got,$slot_name);
                    154: 	    } else {
                    155: 		return($slot_name, $slots{$slot_name});
                    156: 	    }
1.4       albertel  157: 	}
                    158:     }
1.43      albertel  159:     if ($mode eq 'allslots' && @got) {
                    160: 	return @got;
                    161:     }
1.4       albertel  162:     return (undef,undef);
                    163: }
                    164: 
1.48      albertel  165: sub get_consumed_uniqueperiods {
                    166:     my ($slots) = @_;
                    167:     my $navmap=Apache::lonnavmaps::navmap->new;
1.85      raeburn   168:     if (!defined($navmap)) {
                    169:         return 'error: Unable to determine current status';
                    170:     }
1.48      albertel  171:     my @problems = $navmap->retrieveResources(undef,
                    172: 					      sub { $_[0]->is_problem() },1,0);
                    173:     my %used_slots;
                    174:     foreach my $problem (@problems) {
                    175: 	my $symb = $problem->symb();
                    176: 	my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
                    177: 					   $symb, $env{'user.domain'},
                    178: 					   $env{'user.name'});
                    179: 	my $course =  &Apache::lonnet::EXT("resource.0.available",
                    180: 					   $symb, $env{'user.domain'},
                    181: 					   $env{'user.name'});
1.67      albertel  182: 	if (&Apache::lonnet::error($student) 
                    183: 	    || &Apache::lonnet::error($course)) {
1.48      albertel  184: 	    return 'error: Unable to determine current status';
                    185: 	}
                    186: 	foreach my $slot (split(/:/,$student), split(/:/, $course)) {
                    187: 	    $used_slots{$slot}=1;
                    188: 	}
                    189:     }
1.43      albertel  190: 
                    191:     if (!ref($slots)) {
1.48      albertel  192: 	my ($cnum,$cdom)=&get_course();
                    193: 	my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
1.67      albertel  194: 	if (&Apache::lonnet::error(%slots)) {
1.48      albertel  195: 	    return 'error: Unable to determine current status';
                    196: 	}
1.43      albertel  197: 	$slots = \%slots;
                    198:     }
1.41      albertel  199: 
1.48      albertel  200:     my %consumed_uniqueperiods;
                    201:     foreach my $slot_name (keys(%used_slots)) {
1.43      albertel  202: 	next if (!defined($slots->{$slot_name}) ||
                    203: 		 !ref($slots->{$slot_name}));
1.48      albertel  204: 	
1.43      albertel  205:         next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
                    206: 		 !ref($slots->{$slot_name}{'uniqueperiod'}));
1.48      albertel  207: 	$consumed_uniqueperiods{$slot_name} = 
                    208: 	    $slots->{$slot_name}{'uniqueperiod'};
                    209:     }
                    210:     return \%consumed_uniqueperiods;
                    211: }
                    212: 
                    213: sub check_for_conflict {
                    214:     my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
                    215: 
                    216:     if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
                    217: 
                    218:     if (!ref($consumed_uniqueperiods)) {
                    219: 	$consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
1.85      raeburn   220:         if (ref($consumed_uniqueperiods) eq 'HASH') {
                    221: 	    if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
                    222: 	        return 'error: Unable to determine current status';
                    223: 	    }
                    224:         } else {
                    225:             return 'error: Unable to determine current status';
                    226:         }
1.48      albertel  227:     }
                    228:     
                    229:     my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
                    230:     foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
                    231: 	my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
1.43      albertel  232: 	if (!
                    233: 	    ($start < $new_uniq_start &&  $end < $new_uniq_start) ||
                    234: 	    ($start > $new_uniq_end   &&  $end > $new_uniq_end  )) {
1.5       albertel  235: 	    return $slot_name;
                    236: 	}
                    237:     }
                    238:     return undef;
                    239: }
                    240: 
1.2       albertel  241: sub make_reservation {
1.89      raeburn   242:     my ($slot_name,$slot,$symb,$cnum,$cdom)=@_;
1.3       albertel  243: 
                    244:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
                    245: 				   $env{'user.domain'},$env{'user.name'});
                    246:     &Apache::lonxml::debug("value is  $value<br />");
1.59      albertel  247: 
1.80      albertel  248:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",$symb,
                    249: 					 $env{'user.domain'},$env{'user.name'});
1.59      albertel  250:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
                    251: 
1.67      albertel  252:     if (&Apache::lonnet::error($value) 
                    253: 	|| &Apache::lonnet::error($use_slots)) { 
1.40      albertel  254: 	return 'error: Unable to determine current status';
                    255:     }
                    256: 
1.59      albertel  257:     my $parm_symb  = $symb;
                    258:     my $parm_level = 1;
1.66      albertel  259:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
1.59      albertel  260: 	my ($map) = &Apache::lonnet::decode_symb($symb);
                    261: 	$parm_symb = &Apache::lonnet::symbread($map);
                    262: 	$parm_level = 2;
                    263:     }
                    264: 
1.3       albertel  265:     foreach my $other_slot (split(/:/, $value)) {
                    266: 	if ($other_slot eq $slot_name) {
                    267: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
                    268: 					       $cnum, "^$slot_name\0");   
1.67      albertel  269: 	    if (&Apache::lonnet::error($value)) { 
1.40      albertel  270: 		return 'error: Unable to determine current status';
                    271: 	    }
1.57      albertel  272: 	    my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.3       albertel  273: 	    foreach my $key (keys(%consumed)) {
                    274: 		if ($consumed{$key}->{'name'} eq $me) {
                    275: 		    my $num=(split('\0',$key))[1];
                    276: 		    return -$num;
                    277: 		}
                    278: 	    }
                    279: 	}
                    280:     }
                    281: 
1.2       albertel  282:     my $max=$slot->{'maxspace'};
1.3       albertel  283:     if (!defined($max)) { $max=99999; }
1.2       albertel  284: 
                    285:     my (@ids)=&get_reservation_ids($slot_name);
1.67      albertel  286:     if (&Apache::lonnet::error(@ids)) { 
1.40      albertel  287: 	return 'error: Unable to determine current status';
                    288:     }
1.2       albertel  289:     my $last=0;
                    290:     foreach my $id (@ids) {
                    291: 	my $num=(split('\0',$id))[1];
                    292: 	if ($num > $last) { $last=$num; }
                    293:     }
                    294:     
                    295:     my $wanted=$last+1;
1.3       albertel  296:     &Apache::lonxml::debug("wanted $wanted<br />");
1.7       albertel  297:     if (scalar(@ids) >= $max) {
1.2       albertel  298: 	# full up
1.7       albertel  299: 	return undef;
1.2       albertel  300:     }
                    301:     
1.57      albertel  302:     my %reservation=('name'      => $env{'user.name'}.':'.$env{'user.domain'},
1.2       albertel  303: 		     'timestamp' => time,
1.59      albertel  304: 		     'symb'      => $parm_symb);
1.2       albertel  305: 
                    306:     my $success=&Apache::lonnet::newput('slot_reservations',
                    307: 					{"$slot_name\0$wanted" =>
                    308: 					     \%reservation},
1.3       albertel  309: 					$cdom, $cnum);
                    310: 
1.2       albertel  311:     if ($success eq 'ok') {
1.3       albertel  312: 	my $new_value=$slot_name;
                    313: 	if ($value) {
                    314: 	    $new_value=$value.':'.$new_value;
                    315: 	}
1.89      raeburn   316:         &store_slot_parm($symb,$slot_name,$parm_level,$new_value,$cnum,$cdom);
1.2       albertel  317: 	return $wanted;
                    318:     }
1.3       albertel  319: 
1.2       albertel  320:     # someone else got it
1.3       albertel  321:     return undef;
                    322: }
                    323: 
1.89      raeburn   324: sub store_slot_parm {
                    325:     my ($symb,$slot_name,$parm_level,$new_value,$cnum,$cdom) = @_;
                    326:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
                    327:                                                   '0_availablestudent',
                    328:                                                    $parm_level, $new_value,
                    329:                                                    'string',
                    330:                                                    $env{'user.name'},
                    331:                                                    $env{'user.domain'});
                    332:     &Apache::lonxml::debug("hrrm $result");
                    333:     my %storehash = (
                    334:                        symb    => $symb,
                    335:                        slot    => $slot_name,
                    336:                        action  => 'reserve',
                    337:                        context => $env{'form.context'},
                    338:                     );
                    339: 
                    340:     &Apache::lonnet::instructor_log('slotreservationslog',\%storehash,
                    341:                                     '',$env{'user.name'},$env{'user.domain'},
                    342:                                     $cnum,$cdom);
                    343:     return;
                    344: }
                    345: 
1.33      albertel  346: sub remove_registration {
                    347:     my ($r) = @_;
1.55      albertel  348:     if ($env{'form.entry'} ne 'remove all') {
                    349: 	return &remove_registration_user($r);
                    350:     }
                    351:     my $slot_name = $env{'form.slotname'};
                    352:     my %slot=&Apache::lonnet::get_slot($slot_name);
                    353: 
                    354:     my ($cnum,$cdom)=&get_course();
                    355:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    356: 				       "^$slot_name\0");
1.67      albertel  357:     if (&Apache::lonnet::error(%consumed)) {
1.83      raeburn   358: 	$r->print("<p><span class=\"LC_error\">".&mt('A network error has occurred.').'</span></p>');
1.55      albertel  359: 	return;
                    360:     }
                    361:     if (!%consumed) {
1.87      raeburn   362: 	$r->print('<p>'.&mt('Slot [_1] has no reservations.',
                    363: 			    '<tt>'.$slot_name.'</tt>').'</p>');
1.55      albertel  364: 	return;
                    365:     }
                    366: 
                    367:     my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed)));
                    368:     my $names = join(' ',@names);
                    369: 
                    370:     my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name);
1.89      raeburn   371:     &remove_registration_confirmation($r,$msg,['entry','slotname','context']);
1.55      albertel  372: }
                    373: 
                    374: sub remove_registration_user {
                    375:     my ($r) = @_;
                    376:     
                    377:     my $slot_name = $env{'form.slotname'};
                    378: 
1.33      albertel  379:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
                    380: 					     $env{'form.udom'});
                    381: 
                    382:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
                    383: 
1.55      albertel  384:     my $msg = &mt('Remove [_1] from slot [_2] for [_3]',
                    385: 		  $name,$slot_name,$title);
                    386:     
                    387:     &remove_registration_confirmation($r,$msg,['uname','udom','slotname',
1.89      raeburn   388: 					       'entry','symb','context']);
1.55      albertel  389: }
                    390: 
                    391: sub remove_registration_confirmation {
                    392:     my ($r,$msg,$inputs) =@_;
                    393: 
1.33      albertel  394:     my $hidden_input;
1.55      albertel  395:     foreach my $parm (@{$inputs}) {
1.33      albertel  396: 	$hidden_input .=
                    397: 	    '<input type="hidden" name="'.$parm.'" value="'
                    398: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
                    399:     }
1.90    ! bisitz    400:     my %lt = &Apache::lonlocal::texthash(
        !           401:         'yes' => 'Yes',
        !           402:         'no'  => 'No',
        !           403:     );
1.33      albertel  404:     $r->print(<<"END_CONFIRM");
1.55      albertel  405: <p> $msg </p>
1.64      albertel  406: <form action="/adm/slotrequest" method="post">
1.33      albertel  407:     <input type="hidden" name="command" value="release" />
1.55      albertel  408:     <input type="hidden" name="button" value="yes" />
1.33      albertel  409:     $hidden_input
1.55      albertel  410:     <input type="submit" value="$lt{'yes'}" />
1.33      albertel  411: </form>
1.64      albertel  412: <form action="/adm/slotrequest" method="post">
1.33      albertel  413:     <input type="hidden" name="command" value="showslots" />
1.55      albertel  414:     <input type="submit" value="$lt{'no'}" />
1.33      albertel  415: </form>
                    416: END_CONFIRM
                    417: 
                    418: }
                    419: 
1.55      albertel  420: sub release_all_slot {
                    421:     my ($r,$mgr)=@_;
                    422:     
                    423:     my $slot_name = $env{'form.slotname'};
                    424: 
                    425:     my ($cnum,$cdom)=&get_course();
                    426: 
                    427:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    428: 				       "^$slot_name\0");
                    429:     
                    430:     $r->print('<p>'.&mt('Releasing reservations').'</p>');
                    431: 
                    432:     foreach my $entry (sort { $consumed{$a}{'name'} cmp 
                    433: 				  $consumed{$b}{'name'} } (keys(%consumed))) {
1.57      albertel  434: 	my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.55      albertel  435: 	my ($result,$msg) =
                    436: 	    &release_reservation($slot_name,$uname,$udom,
                    437: 				 $consumed{$entry}{'symb'},$mgr);
1.85      raeburn   438:         if (!$result) {
                    439:             $r->print('<p><span class="LC_error">'.&mt($msg).'</span></p>');
                    440:         } else {
                    441: 	    $r->print("<p>$msg</p>");
                    442:         }
1.55      albertel  443: 	$r->rflush();
                    444:     }
                    445:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    446: 	      &mt('Return to slot list').'</a></p>');
                    447:     &return_link($r);
                    448: }
                    449: 
1.5       albertel  450: sub release_slot {
1.33      albertel  451:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
1.6       albertel  452: 
                    453:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
                    454: 
1.33      albertel  455:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
                    456:     if ($mgr eq 'F' 
                    457: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
                    458: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
                    459:     }
                    460: 
                    461:     if ($mgr eq 'F' 
                    462: 	&& defined($env{'form.symb'})) {
1.71      albertel  463: 	$symb = &unescape($env{'form.symb'});
1.33      albertel  464:     }
1.55      albertel  465: 
                    466:     my ($result,$msg) =
                    467: 	&release_reservation($slot_name,$uname,$udom,$symb,$mgr);
1.85      raeburn   468:     if (!$result) {
                    469:         $r->print('<p><span class="LC_error">'.&mt($msg).'</span></p>');
                    470:     } else {
                    471:         $r->print("<p>$msg</p>");
                    472:     }
1.55      albertel  473:     
                    474:     if ($mgr eq 'F') {
                    475: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    476: 		  &mt('Return to slot list').'</a></p>');
                    477:     }
                    478: 
                    479:     if (!$inhibit_return_link) { &return_link($r);  }
                    480:     return $result;
                    481: }
                    482: 
                    483: sub release_reservation {
                    484:     my ($slot_name,$uname,$udom,$symb,$mgr) = @_;
1.39      albertel  485:     my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  486:     my $description=&get_description($slot_name,\%slot);
1.33      albertel  487: 
1.39      albertel  488:     if ($mgr ne 'F') {
1.43      albertel  489: 	if ($slot{'starttime'} < time) {
1.55      albertel  490: 	    return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description));
1.39      albertel  491: 	}
                    492:     }
1.80      albertel  493: 
                    494:     # if the reservation symb is for a map get a resource in that map
                    495:     # to check slot parameters on
                    496:     my $navmap=Apache::lonnavmaps::navmap->new;
1.85      raeburn   497:     if (!defined($navmap)) {
                    498:         return (0,'error: Unable to determine current status');
                    499:     }
1.80      albertel  500:     my $passed_resource = $navmap->getBySymb($symb);
                    501:     if ($passed_resource->is_map()) {
                    502: 	my ($a_resource) = 
                    503: 	    $navmap->retrieveResources($passed_resource, 
                    504: 				       sub {$_[0]->is_problem()},0,1);
                    505: 	$symb = $a_resource->symb();
                    506:     }
                    507: 
1.5       albertel  508:     # get parameter string, check for existance, rebuild string with the slot
1.81      raeburn   509:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
                    510:                                        $symb,$udom,$uname);
                    511:     my @slots = split(/:/,$student);
1.33      albertel  512: 
1.6       albertel  513:     my @new_slots;
                    514:     foreach my $exist_slot (@slots) {
                    515: 	if ($exist_slot eq $slot_name) { next; }
                    516: 	push(@new_slots,$exist_slot);
                    517:     }
                    518:     my $new_param = join(':',@new_slots);
1.5       albertel  519: 
1.55      albertel  520:     my ($cnum,$cdom)=&get_course();
                    521: 
1.5       albertel  522:     # get slot reservations, check if user has one, if so remove reservation
1.6       albertel  523:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    524: 				       "^$slot_name\0");
                    525:     foreach my $entry (keys(%consumed)) {
1.57      albertel  526: 	if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
1.6       albertel  527: 	    &Apache::lonnet::del('slot_reservations',[$entry],
                    528: 				 $cdom,$cnum);
1.89      raeburn   529:             my %storehash = (
                    530:                                symb    => $symb,
                    531:                                slot    => $slot_name,
                    532:                                action  => 'release',
                    533:                                context => $env{'form.context'},
                    534:                         );
                    535:             &Apache::lonnet::instructor_log('slotreservationslog',\%storehash,
                    536:                                         1,$uname,$udom,$cnum,$cdom);
1.6       albertel  537: 	}
                    538:     }
1.33      albertel  539: 
1.80      albertel  540:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",
                    541: 					 $symb,$udom,$uname);
1.59      albertel  542:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
                    543: 
1.67      albertel  544:     if (&Apache::lonnet::error($use_slots)) { 
1.59      albertel  545: 	return (0,'error: Unable to determine current status');
                    546:     }
                    547: 
                    548:     my $parm_level = 1;
1.66      albertel  549:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
1.59      albertel  550: 	$parm_level = 2;
                    551:     }
1.5       albertel  552:     # store new parameter string
1.6       albertel  553:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
                    554: 						      '0_availablestudent',
1.59      albertel  555: 						      $parm_level, $new_param,
                    556: 						      'string', $uname, $udom);
1.55      albertel  557:     my $msg;
1.33      albertel  558:     if ($mgr eq 'F') {
1.55      albertel  559: 	$msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
                    560:     } else {
                    561: 	$msg = &mt('Released Reservation: [_1]',$description);
1.33      albertel  562:     }
1.55      albertel  563:     return (1,$msg);
1.5       albertel  564: }
                    565: 
1.34      albertel  566: sub delete_slot {
                    567:     my ($r)=@_;
                    568: 
                    569:     my $slot_name = $env{'form.slotname'};
                    570:     my %slot=&Apache::lonnet::get_slot($slot_name);
                    571: 
                    572:     my ($cnum,$cdom)=&get_course();
                    573:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    574: 				       "^$slot_name\0");
1.38      albertel  575:     my ($tmp) = %consumed;
                    576:     if ($tmp =~ /error: 2/) { undef(%consumed); }
1.34      albertel  577: 
                    578:     if (%slot && !%consumed) {
                    579: 	$slot{'type'} = 'deleted';
                    580: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
                    581: 					$cdom, $cnum);
                    582: 	if ($ret eq 'ok') {
1.87      raeburn   583: 	    $r->print('<p>'.&mt('Slot [_1] marked as deleted.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  584: 	} else {
1.87      raeburn   585: 	    $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  586: 	}
                    587:     } else {
                    588: 	if (%consumed) {
1.87      raeburn   589: 	    $r->print('<p>'.&mt('Slot [_1] has active reservations.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  590: 	} else {
1.87      raeburn   591: 	    $r->print('<p>'.&mt('Slot [_1] does not exist.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  592: 	}
                    593:     }
                    594:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    595: 	      &mt('Return to slot list').'</a></p>');
1.42      albertel  596:     &return_link($r);
1.34      albertel  597: }
                    598: 
1.40      albertel  599: sub return_link {
                    600:     my ($r) = @_;
                    601:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
                    602: 	      &mt('Return to last resource').'</a></p>');
                    603: }
                    604: 
1.3       albertel  605: sub get_slot {
1.75      albertel  606:     my ($r,$symb,$conflictable_slot,$inhibit_return_link)=@_;
1.3       albertel  607: 
1.43      albertel  608:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
                    609:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
1.40      albertel  610: 
                    611:     if ($slot_name =~ /^error: (.*)/) {
1.82      bisitz    612: 	$r->print('<p><span class="LC_error">'
                    613:                  .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
                    614:                  .'</span></p>');
1.40      albertel  615: 	&return_link($r);
1.75      albertel  616: 	return 0;
1.40      albertel  617:     }
1.75      albertel  618:     if ($slot_name && $slot_name ne $conflictable_slot) {
1.5       albertel  619: 	my %slot=&Apache::lonnet::get_slot($slot_name);
1.6       albertel  620: 	my $description1=&get_description($slot_name,\%slot);
                    621: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
                    622: 	my $description2=&get_description($env{'form.slotname'},\%slot);
1.87      raeburn   623: 	$r->print('<p>'.&mt('Already have a reservation: [_1].',$description1).'</p>');
1.7       albertel  624: 	if ($slot_name ne $env{'form.slotname'}) {
                    625: 	    $r->print(<<STUFF);
1.64      albertel  626: <form method="post" action="/adm/slotrequest">
1.6       albertel  627:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    628:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
                    629:    <input type="hidden" name="releaseslot" value="$slot_name" />
                    630:    <input type="hidden" name="command" value="change" />
                    631: STUFF
1.88      bisitz    632:             $r->print('<p>'
                    633:                      .&mt('You can either [_1]Change[_2] your reservation from [_3] to [_4] or'
                    634:                          ,'<input type="submit" name="change" value="'
                    635:                          ,'" />'
                    636:                          ,'<b>'.$description1.'</b>'
                    637:                          ,'<b>'.$description2.'</b>')
                    638:                      .'<br /></p>'
                    639:             );
1.40      albertel  640: 	    &return_link($r);
1.7       albertel  641: 	    $r->print(<<STUFF);
1.6       albertel  642: </form>
                    643: STUFF
1.7       albertel  644:         } else {
1.40      albertel  645: 	    &return_link($r);
1.7       albertel  646: 	}
1.75      albertel  647: 	return 0;
1.5       albertel  648:     }
1.45      albertel  649: 
1.89      raeburn   650:     my ($cnum,$cdom)=&get_course();
1.3       albertel  651:     my $reserved=&make_reservation($env{'form.slotname'},
1.89      raeburn   652: 				   \%slot,$symb,$cnum,$cdom);
1.3       albertel  653:     my $description=&get_description($env{'form.slotname'},\%slot);
1.7       albertel  654:     if (defined($reserved)) {
1.75      albertel  655: 	my $retvalue = 0;
1.40      albertel  656: 	if ($slot_name =~ /^error: (.*)/) {
1.82      bisitz    657: 	    $r->print('<p><span class="LC_error">'
                    658:                      .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
                    659:                      .'</span></p>');
1.40      albertel  660: 	} elsif ($reserved > -1) {
1.87      raeburn   661: 	    $r->print('<p>'.&mt('Success: [_1]',$description).'</p>');
1.75      albertel  662: 	    $retvalue = 1;
1.7       albertel  663: 	} elsif ($reserved < 0) {
1.87      raeburn   664: 	    $r->print('<p>'.&mt('Already reserved: [_1]',$description).'</p>');
1.7       albertel  665: 	}
1.75      albertel  666: 	if (!$inhibit_return_link) { &return_link($r); }
                    667: 	return 1;
1.3       albertel  668:     }
                    669: 
1.90    ! bisitz    670:     my %lt = &Apache::lonlocal::texthash(
        !           671:         'request' => 'Availibility list',
        !           672:         'try'     => 'Try again?',
        !           673:         'or'      => 'or',
        !           674:     );
1.3       albertel  675: 
1.75      albertel  676:     my $extra_input;
                    677:     if ($conflictable_slot) {
                    678: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
                    679:     }
                    680: 
1.87      raeburn   681:     $r->print('<p>'.&mt('[_1]Failed[_2] to reserve a slot for [_3].','<span class="LC_warning">','</span>',$description).'</p>');
1.3       albertel  682:     $r->print(<<STUFF);
                    683: <p>
1.64      albertel  684: <form method="post" action="/adm/slotrequest">
1.3       albertel  685:    <input type="submit" name="Try Again" value="$lt{'try'}" />
                    686:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    687:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
1.75      albertel  688:    <input type="hidden" name="command" value="$env{'form.command'}" />
                    689:    $extra_input
1.3       albertel  690: </form>
                    691: </p>
                    692: <p>
1.87      raeburn   693: $lt{'or'}
1.64      albertel  694: <form method="post" action="/adm/slotrequest">
1.3       albertel  695:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    696:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
                    697: </form>
                    698: STUFF
1.42      albertel  699: 
1.87      raeburn   700:     if (!$inhibit_return_link) { 
                    701:         $r->print(&mt('or').'</p>').&return_link($r);
                    702:     } else {
                    703:         $r->print('</p>');
                    704:     }
1.75      albertel  705:     return 0;
1.3       albertel  706: }
                    707: 
                    708: sub allowed_slot {
1.48      albertel  709:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
1.49      albertel  710: 
1.3       albertel  711:     #already started
                    712:     if ($slot->{'starttime'} < time) {
1.76      albertel  713: 	return 0;
1.3       albertel  714:     }
1.5       albertel  715:     &Apache::lonxml::debug("$slot_name starttime good");
1.49      albertel  716: 
1.3       albertel  717:     #already ended
                    718:     if ($slot->{'endtime'} < time) {
                    719: 	return 0;
                    720:     }
1.5       albertel  721:     &Apache::lonxml::debug("$slot_name endtime good");
1.49      albertel  722: 
1.3       albertel  723:     # not allowed to pick this one
                    724:     if (defined($slot->{'type'})
                    725: 	&& $slot->{'type'} ne 'schedulable_student') {
                    726: 	return 0;
                    727:     }
1.5       albertel  728:     &Apache::lonxml::debug("$slot_name type good");
1.49      albertel  729: 
1.53      albertel  730:     # reserve time not yet started
                    731:     if ($slot->{'startreserve'} > time) {
                    732: 	return 0;
                    733:     }
                    734:     &Apache::lonxml::debug("$slot_name reserve good");
                    735: 
1.50      albertel  736:     my $userallowed=0;
1.49      albertel  737:     # its for a different set of users
1.50      albertel  738:     if (defined($slot->{'allowedsections'})) {
                    739: 	if (!defined($env{'request.role.sec'})
                    740: 	    && grep(/^No section assigned$/,
                    741: 		    split(',',$slot->{'allowedsections'}))) {
                    742: 	    $userallowed=1;
                    743: 	}
                    744: 	if (defined($env{'request.role.sec'})
                    745: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
                    746: 		    split(',',$slot->{'allowedsections'}))) {
                    747: 	    $userallowed=1;
                    748: 	}
1.68      albertel  749: 	if (defined($env{'request.course.groups'})) {
                    750: 	    my @groups = split(/:/,$env{'request.course.groups'});
                    751: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
                    752: 	    foreach my $group (@groups) {
                    753: 		if (grep {$_ eq $group} (@allowed_sec)) {
                    754: 		    $userallowed=1;
                    755: 		    last;
                    756: 		}
                    757: 	    }
                    758: 	}
1.49      albertel  759:     }
1.50      albertel  760:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
1.49      albertel  761: 
                    762:     # its for a different set of users
1.50      albertel  763:     if (defined($slot->{'allowedusers'})
                    764: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
                    765: 		split(',',$slot->{'allowedusers'}))) {
                    766: 	$userallowed=1;
1.49      albertel  767:     }
1.51      albertel  768: 
                    769:     if (!defined($slot->{'allowedusers'})
                    770: 	&& !defined($slot->{'allowedsections'})) {
                    771: 	$userallowed=1;
                    772:     }
                    773: 
1.50      albertel  774:     &Apache::lonxml::debug("$slot_name user is $userallowed");
                    775:     return 0 if (!$userallowed);
1.49      albertel  776: 
1.3       albertel  777:     # not allowed for this resource
                    778:     if (defined($slot->{'symb'})
                    779: 	&& $slot->{'symb'} ne $symb) {
                    780: 	return 0;
                    781:     }
1.50      albertel  782: 
1.48      albertel  783:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
                    784: 				       $consumed_uniqueperiods);
1.85      raeburn   785:     if ($conflict =~ /^error: /) {
                    786:         return 0;
1.86      raeburn   787:     } elsif ($conflict ne '') {
1.44      albertel  788: 	if ($slots->{$conflict}{'starttime'} < time) {
                    789: 	    return 0;
                    790: 	}
                    791:     }
1.5       albertel  792:     &Apache::lonxml::debug("$slot_name symb good");
1.3       albertel  793:     return 1;
1.2       albertel  794: }
                    795: 
1.3       albertel  796: sub get_description {
                    797:     my ($slot_name,$slot)=@_;
                    798:     my $description=$slot->{'description'};
                    799:     if (!defined($description)) {
1.4       albertel  800: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3       albertel  801: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
                    802: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
                    803:     }
                    804:     return $description;
                    805: }
1.2       albertel  806: 
                    807: sub show_choices {
                    808:     my ($r,$symb)=@_;
                    809: 
                    810:     my ($cnum,$cdom)=&get_course();
                    811:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.48      albertel  812:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
1.85      raeburn   813:     if (ref($consumed_uniqueperiods) eq 'HASH') {
                    814:         if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
                    815:             $r->print('<span class="LC_error">'.
                    816:                       &mt('An error occurred determining slot availability').
                    817:                       '</span>');
                    818:             return;
                    819:         }
                    820:     } elsif ($consumed_uniqueperiods =~ /^error: /) {
                    821:         $r->print('<span class="LC_error">'.
                    822:                   &mt('An error occurred determining slot availability').
                    823:                   '</span>');
                    824:         return;
                    825:     }
1.3       albertel  826:     my $available;
1.5       albertel  827:     &Apache::lonxml::debug("Checking Slots");
1.43      albertel  828:     my @got_slots=&check_for_reservation($symb,'allslots');
1.85      raeburn   829:     if ($got_slots[0] =~ /^error: /) {
                    830:         $r->print('<span class="LC_error">'.
                    831:                   &mt('An error occurred determining slot availability').
                    832:                   '</span>');
                    833:         return;
                    834:     }
                    835:     $r->print('<table border="1">');
1.2       albertel  836:     foreach my $slot (sort 
                    837: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
                    838: 		      (keys(%slots)))  {
1.5       albertel  839: 
                    840: 	&Apache::lonxml::debug("Checking Slot $slot");
1.48      albertel  841: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
                    842: 				$consumed_uniqueperiods));
1.3       albertel  843: 
                    844: 	$available++;
                    845: 
                    846: 	my $description=&get_description($slot,$slots{$slot});
1.2       albertel  847: 
                    848: 	my $form=&mt('Unavailable');
1.43      albertel  849: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
1.7       albertel  850: 	    &space_available($slot,$slots{$slot},$symb)) {
1.5       albertel  851: 	    my $text=&mt('Select');
                    852: 	    my $command='get';
1.43      albertel  853: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
1.70      albertel  854: 		$text=&mt('Drop Reservation');
1.5       albertel  855: 		$command='release';
1.43      albertel  856: 	    } else {
                    857: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
1.48      albertel  858: 						   \%slots,
                    859: 						   $consumed_uniqueperiods);
1.85      raeburn   860:                 if ($conflict) {
                    861:                     if ($conflict =~ /^error: /) {
                    862:                         $r->print('<tr><td><span class="LC_error" colspan="2">'
                    863:                                   .&mt('Slot: [_1] has unknown status.',$description)
                    864:                                   .'</span></td></tr>');
                    865:                     } else {
                    866: 		        $text=&mt('Change Reservation');
                    867: 		        $command='get';
                    868: 		    }
                    869:                 }
1.5       albertel  870: 	    }
1.63      www       871: 	    my $escsymb=&escape($symb);
1.2       albertel  872: 	    $form=<<STUFF;
1.64      albertel  873:    <form method="post" action="/adm/slotrequest">
1.5       albertel  874:      <input type="submit" name="Select" value="$text" />
1.3       albertel  875:      <input type="hidden" name="symb" value="$escsymb" />
                    876:      <input type="hidden" name="slotname" value="$slot" />
1.5       albertel  877:      <input type="hidden" name="command" value="$command" />
1.89      raeburn   878:      <input type="hidden" name="context" value="user" />
1.2       albertel  879:    </form>
                    880: STUFF
                    881: 	}
                    882: 	$r->print(<<STUFF);
                    883: <tr>
                    884:  <td>$form</td>
                    885:  <td>$description</td>
                    886: </tr>
                    887: STUFF
                    888:     }
1.3       albertel  889: 
                    890:     if (!$available) {
1.87      raeburn   891: 	$r->print('<tr><td>'.&mt('No available times.').
                    892:                   ' <a href="/adm/flip?postdata=return:">'.
1.3       albertel  893: 		  &mt('Return to last resource').'</a></td></tr>');
                    894:     }
1.2       albertel  895:     $r->print('</table>');
                    896: }
                    897: 
1.30      albertel  898: sub to_show {
1.54      albertel  899:     my ($slotname,$slot,$when,$deleted,$name) = @_;
1.30      albertel  900:     my $time=time;
                    901:     my $week=60*60*24*7;
1.54      albertel  902: 
1.35      albertel  903:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
                    904: 	return 0;
                    905:     }
1.54      albertel  906: 
                    907:     if ($name && $name->{'value'} =~ /\w/) {
                    908: 	if ($name->{'type'} eq 'substring') {
                    909: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
                    910: 		return 0;
                    911: 	    }
                    912: 	}
                    913: 	if ($name->{'type'} eq 'exact') {
                    914: 	    if ($slotname eq $name->{'value'}) {
                    915: 		return 0;
                    916: 	    }
                    917: 	}
                    918:     }
                    919: 
1.35      albertel  920:     if ($when eq 'any') {
                    921: 	return 1;
                    922:     } elsif ($when eq 'now') {
1.30      albertel  923: 	if ($time > $slot->{'starttime'} &&
                    924: 	    $time < $slot->{'endtime'}) {
                    925: 	    return 1;
                    926: 	}
                    927: 	return 0;
                    928:     } elsif ($when eq 'nextweek') {
                    929: 	if ( ($time        < $slot->{'starttime'} &&
                    930: 	      ($time+$week) > $slot->{'starttime'})
                    931: 	     ||
                    932: 	     ($time        < $slot->{'endtime'} &&
                    933: 	      ($time+$week) > $slot->{'endtime'}) ) {
                    934: 	    return 1;
                    935: 	}
                    936: 	return 0;
                    937:     } elsif ($when eq 'lastweek') {
                    938: 	if ( ($time        > $slot->{'starttime'} &&
                    939: 	      ($time-$week) < $slot->{'starttime'})
                    940: 	     ||
                    941: 	     ($time        > $slot->{'endtime'} &&
                    942: 	      ($time-$week) < $slot->{'endtime'}) ) {
                    943: 	    return 1;
                    944: 	}
                    945: 	return 0;
                    946:     } elsif ($when eq 'willopen') {
                    947: 	if ($time < $slot->{'starttime'}) {
                    948: 	    return 1;
                    949: 	}
                    950: 	return 0;
                    951:     } elsif ($when eq 'wereopen') {
                    952: 	if ($time > $slot->{'endtime'}) {
                    953: 	    return 1;
                    954: 	}
                    955: 	return 0;
                    956:     }
                    957:     
                    958:     return 1;
                    959: }
                    960: 
1.33      albertel  961: sub remove_link {
                    962:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
                    963: 
1.55      albertel  964:     my $remove = &mt('Remove');
                    965: 
                    966:     if ($entry eq 'remove all') {
                    967: 	$remove = &mt('Remove All');
                    968: 	undef($uname);
                    969: 	undef($udom);
                    970:     }
                    971: 
1.63      www       972:     $slotname  = &escape($slotname);
                    973:     $entry     = &escape($entry);
                    974:     $uname     = &escape($uname);
                    975:     $udom      = &escape($udom);
                    976:     $symb      = &escape($symb);
1.33      albertel  977: 
                    978:     return <<"END_LINK";
1.89      raeburn   979:  <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  980:    >($remove)</a>
                    981: END_LINK
                    982: 
                    983: }
                    984: 
1.5       albertel  985: sub show_table {
1.19      albertel  986:     my ($r,$mgr)=@_;
1.5       albertel  987: 
                    988:     my ($cnum,$cdom)=&get_course();
                    989:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19      albertel  990:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
                    991: 	undef(%slots);
                    992:     } 
1.5       albertel  993:     my $available;
1.14      albertel  994:     if ($mgr eq 'F') {
1.72      rezaferr  995:     # FIXME: This line should be deleted once Slots uses breadcrumbs
1.73      albertel  996:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
1.72      rezaferr  997: 
1.30      albertel  998: 	$r->print('<div>');
1.64      albertel  999: 	$r->print('<form method="post" action="/adm/slotrequest">
1.14      albertel 1000: <input type="hidden" name="command" value="uploadstart" />
                   1001: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
                   1002: </form>');
1.72      rezaferr 1003: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
1.64      albertel 1004: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
1.28      albertel 1005: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
                   1006: </form>');
1.72      rezaferr 1007: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
1.30      albertel 1008: 	$r->print('</div>');
1.14      albertel 1009:     }
1.29      albertel 1010:     
1.54      albertel 1011:     my %Saveable_Parameters = ('show'              => 'array',
                   1012: 			       'when'              => 'scalar',
                   1013: 			       'order'             => 'scalar',
                   1014: 			       'deleted'           => 'scalar',
                   1015: 			       'name_filter_type'  => 'scalar',
                   1016: 			       'name_filter_value' => 'scalar',
1.35      albertel 1017: 			       );
1.46      albertel 1018:     &Apache::loncommon::store_course_settings('slotrequest',
                   1019: 					      \%Saveable_Parameters);
                   1020:     &Apache::loncommon::restore_course_settings('slotrequest',
                   1021: 						\%Saveable_Parameters);
                   1022:     &Apache::grades::init_perm();
                   1023:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
                   1024:     &Apache::grades::reset_perm();
1.29      albertel 1025: 
1.54      albertel 1026:     # what to display filtering
1.30      albertel 1027:     my %show_fields=&Apache::lonlocal::texthash(
1.49      albertel 1028: 	     'name'            => 'Slot Name',
                   1029: 	     'description'     => 'Description',
                   1030: 	     'type'            => 'Type',
                   1031: 	     'starttime'       => 'Start time',
                   1032: 	     'endtime'         => 'End Time',
                   1033:              'startreserve'    => 'Time students can start reserving',
                   1034: 	     'secret'          => 'Secret Word',
1.74      albertel 1035: 	     'space'           => '# of students/max',
1.49      albertel 1036: 	     'ip'              => 'IP or DNS restrictions',
                   1037: 	     'symb'            => 'Resource slot is restricted to.',
                   1038: 	     'allowedsections' => 'Sections slot is restricted to.',
                   1039: 	     'allowedusers'    => 'Users slot is restricted to.',
                   1040: 	     'uniqueperiod'    => 'Period of time slot is unique',
                   1041: 	     'scheduled'       => 'Scheduled Students',
                   1042: 	     'proctor'         => 'List of proctors');
1.30      albertel 1043:     my @show_order=('name','description','type','starttime','endtime',
1.74      albertel 1044: 		    'startreserve','secret','space','ip','symb',
1.49      albertel 1045: 		    'allowedsections','allowedusers','uniqueperiod',
                   1046: 		    'scheduled','proctor');
1.30      albertel 1047:     my @show = 
1.29      albertel 1048: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30      albertel 1049: 	                            : keys(%show_fields);
                   1050:     my %show =  map { $_ => 1 } (@show);
                   1051: 
1.54      albertel 1052:     #when filtering setup
1.30      albertel 1053:     my %when_fields=&Apache::lonlocal::texthash(
1.35      albertel 1054: 	     'now'      => 'Open now',
1.30      albertel 1055: 	     'nextweek' => 'Open within the next week',
                   1056: 	     'lastweek' => 'Were open last week',
                   1057: 	     'willopen' => 'Will open later',
1.35      albertel 1058: 	     'wereopen' => 'Were open',
                   1059: 	     'any'      => 'Anytime',
                   1060: 						);
                   1061:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1.30      albertel 1062:     $when_fields{'select_form_order'} = \@when_order;
                   1063:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
                   1064:                                             : 'now';
1.29      albertel 1065: 
1.54      albertel 1066:     #display of students setup
1.46      albertel 1067:     my %stu_display_fields=
                   1068: 	&Apache::lonlocal::texthash('username' => 'User name',
                   1069: 				    'fullname' => 'Full name',
                   1070: 				    );
                   1071:     my @stu_display_order=('fullname','username');
                   1072:     my @stu_display = 
                   1073: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
                   1074: 	                                  : keys(%stu_display_fields);
                   1075:     my %stu_display =  map { $_ => 1 } (@stu_display);
                   1076: 
1.54      albertel 1077:     #name filtering setup
                   1078:     my %name_filter_type_fields=
                   1079: 	&Apache::lonlocal::texthash('substring' => 'Substring',
                   1080: 				    'exact'     => 'Exact',
                   1081: 				    #'reg'       => 'Regular Expression',
                   1082: 				    );
                   1083:     my @name_filter_type_order=('substring','exact');
                   1084: 
                   1085:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
                   1086:     my $name_filter_type = 
                   1087: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
                   1088:                                                 : 'substring';
                   1089:     my $name_filter = {'type'  => $name_filter_type,
                   1090: 		       'value' => $env{'form.name_filter_value'},};
                   1091: 
1.64      albertel 1092:     
1.54      albertel 1093:     #deleted slot filtering
1.64      albertel 1094:     #default to hide if no value
                   1095:     $env{'form.deleted'} ||= 'hide';
1.35      albertel 1096:     my $hide_radio = 
                   1097: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
                   1098:     my $show_radio = 
                   1099: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
                   1100: 	
1.64      albertel 1101:     $r->print('<form method="post" action="/adm/slotrequest">
1.30      albertel 1102: <input type="hidden" name="command" value="showslots" />');
                   1103:     $r->print('<div>');
1.35      albertel 1104:     $r->print('<table class="inline">
                   1105:       <tr><th>'.&mt('Show').'</th>
1.46      albertel 1106:           <th>'.&mt('Student Display').'</th>
1.35      albertel 1107:           <th>'.&mt('Open').'</th>
1.54      albertel 1108:           <th>'.&mt('Slot Name Filter').'</th>
1.35      albertel 1109:           <th>'.&mt('Options').'</th>
                   1110:       </tr>
                   1111:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
                   1112: 	      '</td>
1.46      albertel 1113:            <td>
                   1114:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
                   1115: 						    6,\%stu_display_fields,
                   1116: 						    \@stu_display_order).'
                   1117:            </td>
1.35      albertel 1118:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
                   1119:           '</td>
1.54      albertel 1120:            <td>'.&Apache::loncommon::select_form($name_filter_type,
                   1121: 						 'name_filter_type',
                   1122: 						 %name_filter_type_fields).
                   1123: 	      '<br />'.
                   1124: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
                   1125: 					      $env{'form.name_filter_value'},
                   1126: 					      15).
                   1127:           '</td>
1.35      albertel 1128:            <td>
                   1129:             <table>
                   1130:               <tr>
                   1131:                 <td rowspan="2">Deleted slots:</td>
                   1132:                 <td><label>'.$show_radio.'Show</label></td>
                   1133:               </tr>
                   1134:               <tr>
                   1135:                 <td><label>'.$hide_radio.'Hide</label></td>
                   1136:               </tr>
                   1137:             </table>
                   1138: 	  </td>
                   1139:        </tr>
                   1140:     </table>');
1.30      albertel 1141:     $r->print('</div>');
                   1142:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21      albertel 1143:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
1.65      albertel 1144:     $r->print(&Apache::loncommon::start_data_table().
                   1145: 	      &Apache::loncommon::start_data_table_header_row().'
                   1146: 	       <th></th>');
1.30      albertel 1147:     foreach my $which (@show_order) {
                   1148: 	if ($which ne 'proctor' && exists($show{$which})) {
                   1149: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1.29      albertel 1150: 	}
                   1151:     }
1.65      albertel 1152:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.29      albertel 1153: 
1.21      albertel 1154:     my %name_cache;
                   1155:     my $slotsort = sub {
1.74      albertel 1156: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
1.21      albertel 1157: 	    if (lc($slots{$a}->{$env{'form.order'}})
                   1158: 		ne lc($slots{$b}->{$env{'form.order'}})) {
                   1159: 		return (lc($slots{$a}->{$env{'form.order'}}) 
                   1160: 			cmp lc($slots{$b}->{$env{'form.order'}}));
                   1161: 	    }
1.74      albertel 1162: 	} elsif ($env{'form.order'} eq 'space') {
                   1163: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
                   1164: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
                   1165: 	    }
1.23      albertel 1166: 	} elsif ($env{'form.order'} eq 'name') {
                   1167: 	    if (lc($a) cmp lc($b)) {
                   1168: 		return lc($a) cmp lc($b);
                   1169: 	    }
1.29      albertel 1170: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21      albertel 1171: 	    
                   1172: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
                   1173: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
                   1174: 		return ($slots{$a}->{'uniqueperiod'}[0]
                   1175: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
                   1176: 	    }
                   1177: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
                   1178: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
                   1179: 		return ($slots{$a}->{'uniqueperiod'}[1]
                   1180: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
                   1181: 	    }
                   1182: 	}
                   1183: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
                   1184:     };
1.74      albertel 1185: 
                   1186:     my %consumed;
                   1187:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
                   1188: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
                   1189: 	my ($tmp)=%consumed;
                   1190: 	if ($tmp =~ /^error: /) { undef(%consumed); }
                   1191:     }
                   1192: 
1.21      albertel 1193:     foreach my $slot (sort $slotsort (keys(%slots)))  {
1.54      albertel 1194: 	if (!&to_show($slot,$slots{$slot},$when,
                   1195: 		      $env{'form.deleted'},$name_filter)) { next; }
1.5       albertel 1196: 	if (defined($slots{$slot}->{'type'})
                   1197: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13      albertel 1198: 	    #next;
1.5       albertel 1199: 	}
                   1200: 	my $description=&get_description($slot,$slots{$slot});
1.74      albertel 1201: 	my ($id_count,$ids);
                   1202: 	    
                   1203: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
1.79      albertel 1204: 	    my $re_str = "$slot\0";
                   1205: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
1.74      albertel 1206: 	    $id_count = scalar(@this_slot);
                   1207: 	    if (exists($show{'scheduled'})) {
1.54      albertel 1208: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
                   1209: 					      $consumed{$b}{name} }
1.79      albertel 1210: 				   (@this_slot)) {
1.47      albertel 1211: 		    my (undef,$id)=split("\0",$entry);
1.57      albertel 1212: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.84      bisitz   1213: 		    $ids.= '<span class="LC_nobreak">';
1.47      albertel 1214: 		    foreach my $item (@stu_display_order) {
                   1215: 			if ($stu_display{$item}) {
                   1216: 			    if ($item eq 'fullname') {
                   1217: 				$ids.=$fullname->{"$uname:$udom"}.' ';
                   1218: 			    } elsif ($item eq 'username') {
1.57      albertel 1219: 				$ids.="<tt>$uname:$udom</tt> ";
1.47      albertel 1220: 			    }
1.46      albertel 1221: 			}
                   1222: 		    }
1.47      albertel 1223: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
1.84      bisitz   1224: 				       $consumed{$entry}{'symb'}).'</span><br />';
1.46      albertel 1225: 		}
1.38      albertel 1226: 	    }
1.5       albertel 1227: 	}
1.33      albertel 1228: 
1.24      albertel 1229: 	my $start=($slots{$slot}->{'starttime'}?
                   1230: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
                   1231: 	my $end=($slots{$slot}->{'endtime'}?
                   1232: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1.28      albertel 1233: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24      albertel 1234: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
                   1235: 	
1.14      albertel 1236: 	my $unique;
                   1237: 	if (ref($slots{$slot}{'uniqueperiod'})) {
1.64      albertel 1238: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
1.14      albertel 1239: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
                   1240: 	}
1.33      albertel 1241: 
1.29      albertel 1242: 	my $title;
                   1243: 	if (exists($slots{$slot}{'symb'})) {
                   1244: 	    my (undef,undef,$res)=
                   1245: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
                   1246: 	    $res =   &Apache::lonnet::clutter($res);
                   1247: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
                   1248: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
                   1249: 	}
1.33      albertel 1250: 
1.49      albertel 1251: 	my $allowedsections;
                   1252: 	if (exists($show{'allowedsections'})) {
                   1253: 	    $allowedsections = 
                   1254: 		join(', ',sort(split(/\s*,\s*/,
                   1255: 				     $slots{$slot}->{'allowedsections'})));
                   1256: 	}
                   1257: 
                   1258: 	my @allowedusers;
                   1259: 	if (exists($show{'allowedusers'})) {
                   1260: 	    @allowedusers= map {
                   1261: 		my ($uname,$udom)=split(/:/,$_);
                   1262: 		my $fullname=$name_cache{$_};
                   1263: 		if (!defined($fullname)) {
                   1264: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1265: 		    $fullname =~s/\s/&nbsp;/g;
                   1266: 		    $name_cache{$_} = $fullname;
                   1267: 		}
                   1268: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1269: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
                   1270: 	}
                   1271: 	my $allowedusers=join(', ',@allowedusers);
                   1272: 	
1.29      albertel 1273: 	my @proctors;
                   1274: 	my $rowspan=1;
                   1275: 	my $colspan=1;
1.30      albertel 1276: 	if (exists($show{'proctor'})) {
1.29      albertel 1277: 	    $rowspan=2;
                   1278: 	    @proctors= map {
1.62      albertel 1279: 		my ($uname,$udom)=split(/:/,$_);
1.29      albertel 1280: 		my $fullname=$name_cache{$_};
                   1281: 		if (!defined($fullname)) {
                   1282: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1283: 		    $fullname =~s/\s/&nbsp;/g;
                   1284: 		    $name_cache{$_} = $fullname;
                   1285: 		}
                   1286: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1287: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
                   1288: 	}
1.20      albertel 1289: 	my $proctors=join(', ',@proctors);
1.14      albertel 1290: 
1.34      albertel 1291: 	my $edit=(<<"EDITLINK");
1.31      albertel 1292: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
                   1293: EDITLINK
1.34      albertel 1294: 
                   1295: 	my $delete=(<<"DELETELINK");
1.64      albertel 1296: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
1.34      albertel 1297: DELETELINK
1.55      albertel 1298: 
1.56      albertel 1299:         my $remove_all=&remove_link($slot,'remove all').'<br />';
1.55      albertel 1300: 
1.34      albertel 1301:         if ($ids ne '') { undef($delete); }
1.56      albertel 1302: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
                   1303: 	    || $ids eq '') { 
1.55      albertel 1304: 	    undef($remove_all);
                   1305: 	}
1.34      albertel 1306: 
1.65      albertel 1307: 	my $row_start=&Apache::loncommon::start_data_table_row();
                   1308: 	my $row_end=&Apache::loncommon::end_data_table_row();
                   1309:         $r->print($row_start.
                   1310: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1.30      albertel 1311: 	if (exists($show{'name'})) {
1.29      albertel 1312: 	    $colspan++;$r->print("<td>$slot</td>");
                   1313: 	}
1.33      albertel 1314: 	if (exists($show{'description'})) {
                   1315: 	    $colspan++;$r->print("<td>$description</td>\n");
                   1316: 	}
1.30      albertel 1317: 	if (exists($show{'type'})) {
1.29      albertel 1318: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
                   1319: 	}
1.30      albertel 1320: 	if (exists($show{'starttime'})) {
1.29      albertel 1321: 	    $colspan++;$r->print("<td>$start</td>\n");
                   1322: 	}
1.30      albertel 1323: 	if (exists($show{'endtime'})) {
1.29      albertel 1324: 	    $colspan++;$r->print("<td>$end</td>\n");
                   1325: 	}
1.30      albertel 1326: 	if (exists($show{'startreserve'})) {
1.29      albertel 1327: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
                   1328: 	}
1.30      albertel 1329: 	if (exists($show{'secret'})) {
1.29      albertel 1330: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
                   1331: 	}
1.74      albertel 1332: 	if (exists($show{'space'})) {
                   1333: 	    my $display = $id_count;
                   1334: 	    if ($slots{$slot}{'maxspace'}>0) {
                   1335: 		$display.='/'.$slots{$slot}{'maxspace'};
                   1336: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
                   1337: 		    $display = '<strong>'.$display.' (full) </strong>';
                   1338: 		}
                   1339: 	    }
                   1340: 	    $colspan++;$r->print("<td>$display</td>\n");
1.29      albertel 1341: 	}
1.30      albertel 1342: 	if (exists($show{'ip'})) {
1.29      albertel 1343: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
                   1344: 	}
1.30      albertel 1345: 	if (exists($show{'symb'})) {
1.29      albertel 1346: 	    $colspan++;$r->print("<td>$title</td>\n");
                   1347: 	}
1.49      albertel 1348: 	if (exists($show{'allowedsections'})) {
                   1349: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
                   1350: 	}
                   1351: 	if (exists($show{'allowedusers'})) {
                   1352: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
1.29      albertel 1353: 	}
1.64      albertel 1354: 	if (exists($show{'uniqueperiod'})) {
                   1355: 	    $colspan++;$r->print("<td>$unique</td>\n");
                   1356: 	}
1.47      albertel 1357: 	if (exists($show{'scheduled'})) {
1.64      albertel 1358: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
1.47      albertel 1359: 	}
1.65      albertel 1360: 	$r->print("$row_end\n");
1.30      albertel 1361: 	if (exists($show{'proctor'})) {
1.29      albertel 1362: 	    $r->print(<<STUFF);
1.65      albertel 1363: $row_start
1.29      albertel 1364:  <td colspan="$colspan">$proctors</td>
1.65      albertel 1365: $row_end
1.5       albertel 1366: STUFF
1.29      albertel 1367:         }
1.5       albertel 1368:     }
1.64      albertel 1369:     $r->print('</table></form>');
1.5       albertel 1370: }
                   1371: 
1.14      albertel 1372: sub upload_start {
1.19      albertel 1373:     my ($r)=@_;    
1.14      albertel 1374:     $r->print(&Apache::grades::checkforfile_js());
                   1375:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1376:     $result.='&nbsp;<b>'.
                   1377: 	&mt('Specify a file containing the slot definitions.').
                   1378: 	'</b></td></tr>'."\n";
1.87      raeburn  1379:     $result.='<tr bgcolor="#ffffe6"><td>'."\n";
1.14      albertel 1380:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1381:     my $ignore=&mt('Ignore First Line');
                   1382:     $result.=<<ENDUPFORM;
                   1383: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
                   1384: <input type="hidden" name="command" value="csvuploadmap" />
                   1385: $upfile_select
                   1386: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
                   1387: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
                   1388: </form>
                   1389: ENDUPFORM
                   1390:     $result.='</td></tr></table>'."\n";
                   1391:     $result.='</td></tr></table>'."\n";
                   1392:     $r->print($result);
                   1393: }
                   1394: 
                   1395: sub csvuploadmap_header {
1.19      albertel 1396:     my ($r,$datatoken,$distotal)= @_;
1.14      albertel 1397:     my $javascript;
                   1398:     if ($env{'form.upfile_associate'} eq 'reverse') {
                   1399: 	$javascript=&csvupload_javascript_reverse_associate();
                   1400:     } else {
                   1401: 	$javascript=&csvupload_javascript_forward_associate();
                   1402:     }
                   1403: 
                   1404:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
                   1405:     my $ignore=&mt('Ignore First Line');
1.72      rezaferr 1406: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
                   1407: 
1.14      albertel 1408:     $r->print(<<ENDPICK);
                   1409: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1.72      rezaferr 1410: <h3>Identify fields $help_field</h3>	
1.14      albertel 1411: Total number of records found in file: $distotal <hr />
                   1412: Enter as many fields as you can. The system will inform you and bring you back
                   1413: to this page if the data selected is insufficient to create the slots.<hr />
                   1414: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1415: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
                   1416: <input type="hidden" name="associate"  value="" />
                   1417: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1418: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
                   1419: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
                   1420: <input type="hidden" name="upfile_associate" 
                   1421:                                        value="$env{'form.upfile_associate'}" />
                   1422: <input type="hidden" name="command"    value="csvuploadassign" />
                   1423: <hr />
                   1424: <script type="text/javascript" language="Javascript">
                   1425: $javascript
                   1426: </script>
                   1427: ENDPICK
                   1428:     return '';
                   1429: 
                   1430: }
                   1431: 
                   1432: sub csvuploadmap_footer {
                   1433:     my ($request,$i,$keyfields) =@_;
1.87      raeburn  1434:     my $buttontext = &mt('Create Slots');
1.14      albertel 1435:     $request->print(<<ENDPICK);
                   1436: </table>
                   1437: <input type="hidden" name="nfields" value="$i" />
                   1438: <input type="hidden" name="keyfields" value="$keyfields" />
1.87      raeburn  1439: <input type="button" onClick="javascript:verify(this.form)" value="$buttontext" /><br />
1.14      albertel 1440: </form>
                   1441: ENDPICK
                   1442: }
                   1443: 
                   1444: sub csvupload_javascript_reverse_associate {
                   1445:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1446:     return(<<ENDPICK);
                   1447:   function verify(vf) {
                   1448:     var foundstart=0;
                   1449:     var foundend=0;
                   1450:     var foundname=0;
                   1451:     var foundtype=0;
                   1452:     for (i=0;i<=vf.nfields.value;i++) {
                   1453:       tw=eval('vf.f'+i+'.selectedIndex');
                   1454:       if (i==0 && tw!=0) { foundname=1; }
                   1455:       if (i==1 && tw!=0) { foundtype=1; }
                   1456:       if (i==2 && tw!=0) { foundstat=1; }
                   1457:       if (i==3 && tw!=0) { foundend=1; }
                   1458:     }
                   1459:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1460: 	alert('$error1');
                   1461: 	return;
                   1462:     }
                   1463:     vf.submit();
                   1464:   }
                   1465:   function flip(vf,tf) {
                   1466:   }
                   1467: ENDPICK
                   1468: }
                   1469: 
                   1470: sub csvupload_javascript_forward_associate {
                   1471:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1472:   return(<<ENDPICK);
                   1473:   function verify(vf) {
                   1474:     var foundstart=0;
                   1475:     var foundend=0;
                   1476:     var foundname=0;
                   1477:     var foundtype=0;
                   1478:     for (i=0;i<=vf.nfields.value;i++) {
                   1479:       tw=eval('vf.f'+i+'.selectedIndex');
                   1480:       if (tw==1) { foundname=1; }
                   1481:       if (tw==2) { foundtype=1; }
                   1482:       if (tw==3) { foundstat=1; }
                   1483:       if (tw==4) { foundend=1; }
                   1484:     }
                   1485:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1486: 	alert('$error1');
                   1487: 	return;
                   1488:     }
                   1489:     vf.submit();
                   1490:   }
                   1491:   function flip(vf,tf) {
                   1492:   }
                   1493: ENDPICK
                   1494: }
                   1495: 
                   1496: sub csv_upload_map {
1.19      albertel 1497:     my ($r)= @_;
1.14      albertel 1498: 
                   1499:     my $datatoken;
                   1500:     if (!$env{'form.datatoken'}) {
                   1501: 	$datatoken=&Apache::loncommon::upfile_store($r);
                   1502:     } else {
                   1503: 	$datatoken=$env{'form.datatoken'};
                   1504: 	&Apache::loncommon::load_tmp_file($r);
                   1505:     }
                   1506:     my @records=&Apache::loncommon::upfile_record_sep();
                   1507:     if ($env{'form.noFirstLine'}) { shift(@records); }
1.19      albertel 1508:     &csvuploadmap_header($r,$datatoken,$#records+1);
1.14      albertel 1509:     my ($i,$keyfields);
                   1510:     if (@records) {
                   1511: 	my @fields=&csvupload_fields();
                   1512: 
                   1513: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
                   1514: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                   1515: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1516: 							  \@fields);
                   1517: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   1518: 	    chop($keyfields);
                   1519: 	} else {
                   1520: 	    unshift(@fields,['none','']);
                   1521: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1522: 							    \@fields);
                   1523: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   1524: 	    $keyfields=join(',',sort(keys(%sone)));
                   1525: 	}
                   1526:     }
                   1527:     &csvuploadmap_footer($r,$i,$keyfields);
                   1528: 
                   1529:     return '';
                   1530: }
                   1531: 
                   1532: sub csvupload_fields {
                   1533:     return (['name','Slot name'],
                   1534: 	    ['type','Type of slot'],
                   1535: 	    ['starttime','Start Time of slot'],
                   1536: 	    ['endtime','End Time of slot'],
1.15      albertel 1537: 	    ['startreserve','Reservation Start Time'],
1.14      albertel 1538: 	    ['ip','IP or DNS restriction'],
                   1539: 	    ['proctor','List of proctor ids'],
                   1540: 	    ['description','Slot Description'],
                   1541: 	    ['maxspace','Maximum number of reservations'],
                   1542: 	    ['symb','Resource Restriction'],
                   1543: 	    ['uniqueperiod','Date range of slot exclusion'],
1.49      albertel 1544: 	    ['secret','Secret word proctor uses to validate'],
                   1545: 	    ['allowedsections','Sections slot is restricted to'],
                   1546: 	    ['allowedusers','Users slot is restricted to'],
                   1547: 	    );
1.14      albertel 1548: }
                   1549: 
                   1550: sub csv_upload_assign {
1.19      albertel 1551:     my ($r,$mgr)= @_;
1.14      albertel 1552:     &Apache::loncommon::load_tmp_file($r);
                   1553:     my @slotdata = &Apache::loncommon::upfile_record_sep();
                   1554:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
                   1555:     my %fields=&Apache::grades::get_fields();
1.87      raeburn  1556:     $r->print('<h3>'.&mt('Creating Slots').'</h3>');
1.14      albertel 1557:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1558:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1559:     my $countdone=0;
1.31      albertel 1560:     my @errors;
1.14      albertel 1561:     foreach my $slot (@slotdata) {
                   1562: 	my %slot;
                   1563: 	my %entries=&Apache::loncommon::record_sep($slot);
                   1564: 	my $domain;
                   1565: 	my $name=$entries{$fields{'name'}};
1.31      albertel 1566: 	if ($name=~/^\s*$/) {
                   1567: 	    push(@errors,"Did not create slot with no name");
                   1568: 	    next;
                   1569: 	}
                   1570: 	if ($name=~/\s/) { 
                   1571: 	    push(@errors,"$name not created -- Name must not contain spaces");
                   1572: 	    next;
                   1573: 	}
                   1574: 	if ($name=~/\W/) { 
                   1575: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
                   1576: 	    next;
                   1577: 	}
1.14      albertel 1578: 	if ($entries{$fields{'type'}}) {
                   1579: 	    $slot{'type'}=$entries{$fields{'type'}};
                   1580: 	} else {
                   1581: 	    $slot{'type'}='preassigned';
                   1582: 	}
1.31      albertel 1583: 	if ($slot{'type'} ne 'preassigned' &&
                   1584: 	    $slot{'type'} ne 'schedulable_student') {
                   1585: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
                   1586: 	    next;
                   1587: 	}
1.14      albertel 1588: 	if ($entries{$fields{'starttime'}}) {
                   1589: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
                   1590: 	}
                   1591: 	if ($entries{$fields{'endtime'}}) {
1.16      albertel 1592: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14      albertel 1593: 	}
1.58      albertel 1594: 
                   1595: 	# start/endtime must be defined and greater than zero
                   1596: 	if (!$slot{'starttime'}) {
                   1597: 	    push(@errors,"$name not created -- Invalid start time");
                   1598: 	    next;
                   1599: 	}
                   1600: 	if (!$slot{'endtime'}) {
                   1601: 	    push(@errors,"$name not created -- Invalid end time");
                   1602: 	    next;
                   1603: 	}
                   1604: 	if ($slot{'starttime'} > $slot{'endtime'}) {
                   1605: 	    push(@errors,"$name not created -- Slot starts after it ends");
                   1606: 	    next;
                   1607: 	}
                   1608: 
1.23      albertel 1609: 	if ($entries{$fields{'startreserve'}}) {
                   1610: 	    $slot{'startreserve'}=
                   1611: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
                   1612: 	}
1.58      albertel 1613: 	if (defined($slot{'startreserve'})
                   1614: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
                   1615: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
                   1616: 	    next;
                   1617: 	}
                   1618: 
1.14      albertel 1619: 	foreach my $key ('ip','proctor','description','maxspace',
                   1620: 			 'secret','symb') {
                   1621: 	    if ($entries{$fields{$key}}) {
                   1622: 		$slot{$key}=$entries{$fields{$key}};
                   1623: 	    }
                   1624: 	}
1.58      albertel 1625: 
1.14      albertel 1626: 	if ($entries{$fields{'uniqueperiod'}}) {
                   1627: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
                   1628: 	    my @times=(&UnixDate($start,"%s"),
                   1629: 		       &UnixDate($end,"%s"));
                   1630: 	    $slot{'uniqueperiod'}=\@times;
                   1631: 	}
1.58      albertel 1632: 	if (defined($slot{'uniqueperiod'})
                   1633: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
                   1634: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
                   1635: 	    next;
                   1636: 	}
1.14      albertel 1637: 
                   1638: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
                   1639: 	$r->print('.');
                   1640: 	$r->rflush();
                   1641: 	$countdone++;
                   1642:     }
1.87      raeburn  1643:     $r->print('<p>'.&mt('Created [quant,_1,slot]',$countdone)."\n".'</p>');
1.31      albertel 1644:     foreach my $error (@errors) {
1.87      raeburn  1645: 	$r->print('<p><span class="LC_warning">'.$error.'</span></p>'."\n");
1.31      albertel 1646:     }
1.19      albertel 1647:     &show_table($r,$mgr);
1.14      albertel 1648:     return '';
                   1649: }
                   1650: 
1.1       albertel 1651: sub handler {
                   1652:     my $r=shift;
                   1653: 
1.30      albertel 1654:     &Apache::loncommon::content_type($r,'text/html');
                   1655:     &Apache::loncommon::no_cache($r);
                   1656:     if ($r->header_only()) {
                   1657: 	$r->send_http_header();
                   1658: 	return OK;
                   1659:     }
                   1660: 
1.8       albertel 1661:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28      albertel 1662:     
1.12      albertel 1663:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14      albertel 1664:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28      albertel 1665:     my $title='Requesting Another Worktime';
                   1666:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
                   1667: 	$title = 'Managing Slots';
                   1668:     }
                   1669:     &start_page($r,$title);
                   1670: 
1.8       albertel 1671:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19      albertel 1672: 	&show_table($r,$mgr);
1.33      albertel 1673:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
                   1674: 	&remove_registration($r);
                   1675:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1.55      albertel 1676: 	if ($env{'form.entry'} eq 'remove all') {
                   1677: 	    &release_all_slot($r,$mgr);
                   1678: 	} else {
                   1679: 	    &release_slot($r,undef,undef,undef,$mgr);
                   1680: 	}
1.34      albertel 1681:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
                   1682: 	&delete_slot($r);
1.14      albertel 1683:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19      albertel 1684: 	&upload_start($r);
1.14      albertel 1685:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19      albertel 1686: 	&csv_upload_map($r);
1.14      albertel 1687:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
                   1688: 	if ($env{'form.associate'} ne 'Reverse Association') {
1.19      albertel 1689: 	    &csv_upload_assign($r,$mgr);
1.14      albertel 1690: 	} else {
                   1691: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                   1692: 		$env{'form.upfile_associate'} = 'reverse';
                   1693: 	    } else {
                   1694: 		$env{'form.upfile_associate'} = 'forward';
                   1695: 	    }
1.19      albertel 1696: 	    &csv_upload_map($r);
1.14      albertel 1697: 	}
1.8       albertel 1698:     } else {
1.63      www      1699: 	my $symb=&unescape($env{'form.symb'});
1.61      albertel 1700: 	if (!defined($symb)) {
                   1701: 	    &fail($r,'not_valid');
                   1702: 	    return OK;
                   1703: 	}
1.19      albertel 1704: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.36      albertel 1705: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1.66      albertel 1706: 	if ($useslots ne 'resource' 
                   1707: 	    && $useslots ne 'map' 
                   1708: 	    && $useslots ne 'map_map') {
1.61      albertel 1709: 	    &fail($r,'not_available');
1.19      albertel 1710: 	    return OK;
                   1711: 	}
                   1712: 	$env{'request.symb'}=$symb;
1.36      albertel 1713: 	my $type = ($res =~ /\.task$/) ? 'Task'
                   1714: 	                               : 'problem';
                   1715: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1.11      albertel 1716: 	if ($status eq 'CAN_ANSWER' ||
                   1717: 	    $status eq 'NEEDS_CHECKIN' ||
                   1718: 	    $status eq 'WAITING_FOR_GRADE') {
                   1719: 	    &fail($r,'not_allowed');
                   1720: 	    return OK;
                   1721: 	}
                   1722: 	if ($env{'form.requestattempt'}) {
                   1723: 	    &show_choices($r,$symb);
                   1724: 	} elsif ($env{'form.command'} eq 'release') {
                   1725: 	    &release_slot($r,$symb);
                   1726: 	} elsif ($env{'form.command'} eq 'get') {
                   1727: 	    &get_slot($r,$symb);
                   1728: 	} elsif ($env{'form.command'} eq 'change') {
1.75      albertel 1729: 	    if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
                   1730: 		&release_slot($r,$symb,$env{'form.releaseslot'});
1.39      albertel 1731: 	    }
1.11      albertel 1732: 	} else {
1.87      raeburn  1733: 	    $r->print('<p>'.&mt('Unknown command: [_1]',$env{'form.command'}).'</p>');
1.11      albertel 1734: 	}
1.2       albertel 1735:     }
1.1       albertel 1736:     &end_page($r);
                   1737:     return OK;
                   1738: }
1.3       albertel 1739: 
                   1740: 1;
                   1741: __END__

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