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

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

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