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

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

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